/**
 * @filename:ContractTermsServiceImpl 2024-09-03
 * @project KJT v1.0.0 Copyright(c) 2024 wanglang Co. Ltd. All right reserved.
 */
package com.trade.service.trade.salescontract.service.impl;

import com.trade.service.trade.salescontract.domain.ContractTerms;
import com.trade.service.trade.salescontract.domain.ContractTermsExtend;
import com.trade.service.trade.salescontract.dto.ContractTermsDto;
import com.trade.service.trade.salescontract.mapper.ContractTermsMapper;
import com.trade.service.trade.salescontract.service.ContractTermsExtendService;
import com.trade.service.trade.salescontract.service.ContractTermsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:合同条款服务实现C
 * @version: v1.0.0
 * @author: wanglang
 */
@Service
public class ContractTermsServiceImpl extends ServiceImpl<ContractTermsMapper, ContractTerms> implements ContractTermsService {

    @Resource
    private SalesDataConverter salesDataConverter;
    @Resource
    private ContractTermsExtendService contractTermsExtendService;

    @Override
    public ContractTerms getByContractIdAndType(Long contractId, String contractType) {
        return this.lambdaQuery().eq(ContractTerms::getContractId, contractId)
                .eq(ContractTerms::getContractType, contractType).one();
    }

    @Override
    public ContractTerms getByContractNo(String contractNo) {
        return this.lambdaQuery().eq(ContractTerms::getContractNo, contractNo).one();
    }

    @Override
    public ContractTermsDto getDetailByContractIdAndType(Long contractId, String contractType) {
        ContractTerms contractTerms = getByContractIdAndType(contractId, contractType);
        if (contractTerms == null) {
            return null;
        }
        ContractTermsDto contractTermsDto = salesDataConverter.convertContractTermsDto(contractTerms);
        List<ContractTermsExtend> contractTermsExtends = contractTermsExtendService.lambdaQuery()
                .eq(ContractTermsExtend::getContractTermsId, contractTerms.getId()).list();
        contractTermsDto.setPaymentInfo(
                contractTermsExtends.stream().filter(c -> "1".equals(c.getType())).collect(Collectors.toList()));
        contractTermsDto.setTitleInfo(
                contractTermsExtends.stream().filter(c -> "2".equals(c.getType())).collect(Collectors.toList()));
        return contractTermsDto;
    }
}
