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

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.QueryService;
import com.ecep.contract.ds.company.repository.CompanyExtendInfoRepository;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyExtendInfo;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 公司文件服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "company-extend-info")
public class CompanyExtendInfoService implements QueryService<CompanyExtendInfo> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyExtendInfoService.class);
    @Autowired
    private CompanyExtendInfoRepository repository;

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

    public List<CompanyExtendInfo> findAll(Specification<CompanyExtendInfo> spec, Sort sort) {
        return repository.findAll(spec, sort);
    }

    public Specification<CompanyExtendInfo> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("businessScope"), "%" + searchText + "%"),
                    builder.like(root.get("taxRegistrationNumber"), "%" + searchText + "%"),
                    builder.like(root.get("legalPerson"), "%" + searchText + "%"));
        };
    }

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

    @Override
    public Page<CompanyExtendInfo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<CompanyExtendInfo> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "company");
        return findAll(spec, pageable);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'byCompany-'+#p0.company.id")
    })
    public void delete(CompanyExtendInfo extendInfo) {
        repository.delete(extendInfo);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'byCompany-'+#p0.company.id")
    })
    public CompanyExtendInfo save(CompanyExtendInfo extendInfo) {
        return repository.save(extendInfo);
    }

    @Cacheable(key = "'byCompany-'+#p0")
    public CompanyExtendInfo findByCompany(Company company) {
        List<CompanyExtendInfo> list = repository.findByCompany(company);
        if (list.isEmpty()) {
            CompanyExtendInfo extendInfo = new CompanyExtendInfo();
            extendInfo.setCompany(company);
            extendInfo.setDisableVerify(false);
            return repository.save(extendInfo);
        }
        return list.getFirst();
    }

}
