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

import java.util.List;

import com.ecep.contract.QueryService;
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.VendorEntityRepository;
import com.ecep.contract.model.Vendor;
import com.ecep.contract.model.VendorEntity;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "company-vendor-entity")
public class VendorEntityService implements IEntityService<VendorEntity>, QueryService<VendorEntity> {
    @Lazy
    @Autowired
    private VendorEntityRepository repository;

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

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

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

    protected Specification<VendorEntity> 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<VendorEntity> search(String searchText) {
        Specification<VendorEntity> spec = getSpecification(searchText);
        return repository.findAll(spec, Pageable.ofSize(10)).getContent();
    }

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

    @Override
    public Page<VendorEntity> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<VendorEntity> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // 添加额外的参数过滤
        spec = SpecificationUtils.andParam(spec, paramsNode, "vendor");
        return findAll(spec, pageable);
    }

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

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

    public List<VendorEntity> findAllByVendor(Vendor vendor) {
        return repository.findByVendor(vendor);
    }
}