package com.ecep.contract.ds.contract.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.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.ds.contract.repository.ContractKindRepository;
import com.ecep.contract.model.ContractKind;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 合同种类服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "contract-kind")
public class ContractKindService implements IEntityService<ContractKind>, QueryService<ContractKind> {
    private static final Logger logger = LoggerFactory.getLogger(ContractKindService.class);

    @Lazy
    @Autowired
    private ContractKindRepository repository;

    @Override
    @Cacheable(key = "'kind-'+#p0")
    public ContractKind findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

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

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

    @Override
    public Specification<ContractKind> getSpecification(String searchText) {
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("title"), "%" + searchText + "%"));
        };
    }

    @Override
    public void delete(ContractKind entity) {
        repository.delete(entity);
    }

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

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

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

    @Caching(evict = {
            @CacheEvict(key = "'kinds'"),
            @CacheEvict(key = "'kind-'+#p0.id"),
            @CacheEvict(key = "'code-'+#p0.name"),
    })
    public ContractKind save(ContractKind kind) {
        return repository.save(kind);
    }

    @Caching(evict = {
            @CacheEvict(key = "'kinds'"),
            @CacheEvict(key = "'kind-'+#p0"),
    })
    public void delete(Integer id) {
        repository.deleteById(id);
    }
}