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.ContractTypeRepository;
import com.ecep.contract.model.ContractType;
import com.fasterxml.jackson.databind.JsonNode;

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

    @Lazy
    @Autowired
    private ContractTypeRepository repository;

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

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

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

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

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

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

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

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

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

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