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

/**
 * 合同分组服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "contract-group")
public class ContractGroupService implements IEntityService<ContractGroup>, QueryService<ContractGroup> {
    private static final Logger logger = LoggerFactory.getLogger(ContractGroupService.class);

    @Lazy
    @Autowired
    private ContractGroupRepository repository;

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

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

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

    @Override
    public Specification<ContractGroup> 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 + "%"));
        };
    }

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

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

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

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

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

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

    public ContractGroup newContractGroup() {
        ContractGroup group = new ContractGroup();
        group.setCode("");
        group.setName("");
        group.setTitle("");
        return group;
    }

}