package com.ecep.contract.ds.vendor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.vendor.repository.CompanyVendorEntityRepository;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.CompanyVendorEntity;
import com.ecep.contract.util.SpecificationUtils;

@Lazy
@Service
@CacheConfig(cacheNames = "company-vendor-entity")
public class CompanyVendorEntityService implements IEntityService<CompanyVendorEntity> {
    @Lazy
    @Autowired
    private CompanyVendorEntityRepository repository;

    @Cacheable(key = "#p0")
    @Override
    public CompanyVendorEntity findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    /**
     * 因为 code 具有唯一性，所以不需要增加 CompanyVendor 加以区分
     *
     * @param code 供应商编号
     * @return CompanyVendorEntity
     */
    @Cacheable(key = "'code-'+#p0")
    public CompanyVendorEntity findByCode(String code) {
        return repository.findByCode(code).orElse(null);
    }

    @Override
    public Specification<CompanyVendorEntity> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return SpecificationUtils.andWith(searchText, this::buildSearchSpecification);
    }

    protected Specification<CompanyVendorEntity> buildSearchSpecification(String searchText) {
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("abbName"), "%" + searchText + "%"),
                    builder.like(root.get("code"), "%" + searchText + "%")
            );
        };
    }

    @Override
    public List<CompanyVendorEntity> search(String searchText) {
        Specification<CompanyVendorEntity> spec = getSpecification(searchText);
        return repository.findAll(spec, Pageable.ofSize(10)).getContent();
    }

    @Override
    public Page<CompanyVendorEntity> findAll(Specification<CompanyVendorEntity> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    @Caching(
            evict = {
                    @CacheEvict(key = "#p0.id"),
                    @CacheEvict(key = "'code-'+#p0.code")
            }
    )
    @Override
    public void delete(CompanyVendorEntity entity) {
        repository.delete(entity);
    }

    @Caching(
            evict = {
                    @CacheEvict(key = "#p0.id"),
                    @CacheEvict(key = "'code-'+#p0.code")
            }
    )
    @Override
    public CompanyVendorEntity save(CompanyVendorEntity entity) {
        return repository.save(entity);
    }

    public List<CompanyVendorEntity> findAllByVendor(CompanyVendor vendor) {
        return repository.findByVendor(vendor);
    }
}
