package com.inkasso.factoring.salesManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.adapter.util.CollectionUtil;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.enums.FileTypeEnum;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.salesManagement.dao.CSalesContractManagementMapper;
import com.inkasso.factoring.salesManagement.dao.CSalesContractSupplementInfoMapper;
import com.inkasso.factoring.salesManagement.entity.*;
import com.inkasso.factoring.salesManagement.entity.vo.SalesContractBasicVo;
import com.inkasso.factoring.salesManagement.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 销售合同变更信息表 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-11-28
 */
@Service
public class CSalesContractSupplementInfoServiceImpl extends ServiceImpl<CSalesContractSupplementInfoMapper, CSalesContractSupplementInfo>
        implements ICSalesContractSupplementInfoService {


    @Autowired
    private ICSalesContractManagementService salesContractManagementService;
    @Autowired
    private CSalesContractManagementMapper salesContractManagementMapper;
    @Autowired
    private ICSalesContractSupplementPersonInfoService supplementPersonInfoService;
    @Autowired
    private FileService fileService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${trade.salesContractChangeAddUrl}")
    private String salesContractChangeAddUrl;
    @Autowired
    private ICSalesContractPersonInfoService salesContractPersonInfoService;
    @Autowired
    private ICSalesContractBasicInfoService salesContractBasicInfoService;

//    @Override
//    public CSalesContractSupplementInfo getView(Long id) {
//
//        CSalesContractSupplementInfo supplementInfo = getById(id);
//        SalesContractBasicVo contractBasicVo = salesContractManagementMapper
//                .getBasicInfoById(supplementInfo.getcSalesContractManagementId());
//        autoDetails(supplementInfo);
//        supplementInfo.setSalesContractBasicVo(contractBasicVo);
//        return supplementInfo;
//    }

    @Override
    public CSalesContractSupplementInfo add(Long managementId) {


        CSalesContractSupplementInfo contractSupplementInfo = lambdaQuery().eq(CSalesContractSupplementInfo::getcSalesContractManagementId, managementId)
                .orderByDesc(CSalesContractSupplementInfo::getGmtCreate).last("limit 1").one();
        //销售合同信息
        CSalesContractManagement contractManagement = salesContractManagementService.getById(managementId);
        if (contractManagement == null) {
            throw new BusinessException(CodeUtil.FAIL, "找不到销售合同基础信息！");
        }

        if (contractSupplementInfo != null) {
            //退回
            autoDetails(contractSupplementInfo);
        } else {
            contractSupplementInfo = new CSalesContractSupplementInfo();
            //基础信息
            CSalesContractBasicInfo basicInfo = salesContractBasicInfoService.lambdaQuery()
                    .eq(CSalesContractBasicInfo::getcSalesContractManagementId, managementId).one();
            if (basicInfo != null) {
                //人员信息
                List<CSalesContractPersonInfo> personInfos = salesContractPersonInfoService.lambdaQuery()
                        .eq(CSalesContractPersonInfo::getcSalesContractBasicInfoId, basicInfo.getId()).list();
                if (CollectionUtil.isNotEmpty(personInfos)) {
                    List<CSalesContractSupplementPersonInfo> supplementPersonInfoList = new ArrayList<>();
                    personInfos.forEach(person -> {
                        CSalesContractSupplementPersonInfo personInfo = new CSalesContractSupplementPersonInfo();
                        BeanUtils.copyProperties(person, personInfo, "id");
                        supplementPersonInfoList.add(personInfo);
                    });
                    contractSupplementInfo.setSupplementPersonInfos(supplementPersonInfoList);
                }
            }

        }
        //填充基础信息
        SalesContractBasicVo contractBasicVo = salesContractManagementService.getView(managementId);
        contractSupplementInfo.setSalesContractBasicVo(contractBasicVo);

        return contractSupplementInfo;
    }

    @Override
    public void doChange(CSalesContractSupplementInfo salesContractSupplementInfo) {

        try {
            restTemplate.postForObject(salesContractChangeAddUrl, salesContractSupplementInfo, String.class);
        } catch (Exception e) {
            throw new BusinessException(CodeUtil.FAIL, e.getMessage());
        }
    }

    @Override
    public CSalesContractSupplementInfo getView(Long id) {
        CSalesContractSupplementInfo contractSupplementInfo = lambdaQuery().eq(CSalesContractSupplementInfo::getId, id).one();
        //销售合同信息
        if (contractSupplementInfo != null) {
            autoDetails(contractSupplementInfo);
            //填充基础信息
            SalesContractBasicVo contractBasicVo = salesContractManagementService.getView(contractSupplementInfo.getcSalesContractManagementId());
            contractSupplementInfo.setSalesContractBasicVo(contractBasicVo);
        }


        return contractSupplementInfo;
    }

    void autoDetails(CSalesContractSupplementInfo salesContractSupplementInfo) {

        List<CSalesContractSupplementPersonInfo> supplementPersonInfoList = supplementPersonInfoService
                .list(new QueryWrapper<CSalesContractSupplementPersonInfo>().eq("c_sales_contract_supplement_info_id", salesContractSupplementInfo.getId()));
        salesContractSupplementInfo.setSupplementPersonInfos(supplementPersonInfoList);
        //合同模板
        List<FileStorageDO> fileStorageDOList = fileService.findByBusinessId(salesContractSupplementInfo.getId(),
                FileTypeEnum.SALES_SUPPLEMENT_CONTRACT_TEMPLATE_FILE.getCode());
        salesContractSupplementInfo.setFiles(fileStorageDOList);
    }
}
