package com.ecep.contract.ds.contract.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.QueryService;
import com.ecep.contract.ds.contract.repository.ContractCatalogRepository;
import com.ecep.contract.model.ContractCatalog;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 合同分类服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "contract-catalog")
public class ContractCatalogService implements IEntityService<ContractCatalog>, QueryService<ContractCatalog> {
    @Lazy
    @Autowired
    private ContractCatalogRepository repository;

    /**
     * 根据 id 查找 ContractCatalog
     */
    @Cacheable(key = "#p0")
    @Override
    public ContractCatalog findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    public ContractCatalog findByName(String name) {
        return repository.findByName(name).orElse(null);
    }

    @Cacheable(key = "'code-'+#p0")
    public ContractCatalog findByCode(String code) {
        return repository.findByCode(code).orElse(null);
    }

    @Cacheable(key = "'catalogs'")
    public List<ContractCatalog> findAll() {
        return repository.findAll();
    }

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

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

    @Override
    public Specification<ContractCatalog> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("memo"), "%" + searchText + "%"));
        };
    }

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

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