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

import java.util.List;

import com.ecep.contract.vo.ContractVo;
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.SpringApp;
import com.ecep.contract.ds.contract.repository.ContractPayPlanRepository;
import com.ecep.contract.ds.contract.model.Contract;
import com.ecep.contract.ds.contract.model.ContractPayPlan;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.ContractPayPlanVo;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 合同支付计划服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "contract-pay-plan")
public class ContractPayPlanService implements IEntityService<ContractPayPlan>, QueryService<ContractPayPlanVo>,
        VoableService<ContractPayPlan, ContractPayPlanVo> {
    @Lazy
    @Autowired
    private ContractPayPlanRepository repository;

    @Override
    public ContractPayPlan getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Cacheable(key = "#p0")
    @Override
    public ContractPayPlanVo findById(Integer id) {
        return repository.findById(id).map(ContractPayPlan::toVo).orElse(null);
    }

    @Override
    public Specification<ContractPayPlan> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, cb) -> {
            return cb.or(cb.like(root.get("payTerm"), "%" + searchText + "%"));
        };
    }

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

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

    public List<ContractPayPlan> findAllByContract(ContractVo contract) {
        return repository.findAllByContractId(contract.getId());
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id")
    })
    @Override
    public void delete(ContractPayPlan entity) {
        repository.delete(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id")
    })
    @Override
    public ContractPayPlan save(ContractPayPlan entity) {
        return repository.save(entity);
    }

    @Override
    public void updateByVo(ContractPayPlan model, ContractPayPlanVo vo) {
        if (model == null || vo == null) {
            return;
        }
        model.setRefId(vo.getRefId());
        model.setPayRatio(vo.getPayRatio());
        model.setPayCurrency(vo.getPayCurrency() != null ? vo.getPayCurrency() : 0);
        model.setPayDate(vo.getPayDate());
        model.setPayTerm(vo.getPayTerm());

        // 处理关联对象
        if (vo.getContractId() == null) {
            model.setContract(null);
        } else {
            model.setContract(SpringApp.getBean(ContractService.class).getById(vo.getContractId()));
        }

        if (vo.getUpdateDate() != null) {
            model.setUpdateDate(vo.getUpdateDate());
        }
    }
}
