package com.sunxung.factoring.service.invoicemanage.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.*;
import com.sunxung.factoring.dict.impl.*;
import com.sunxung.factoring.entity.acceptance.AcceptanceBasicInfo;
import com.sunxung.factoring.entity.customerinformation.Customer;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.invoicemanage.*;
import com.sunxung.factoring.entity.project.BusinessAssignee;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractBasicInfo;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractManagement;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSignConclusion;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSupplementInfo;
import com.sunxung.factoring.entity.salescontractmanagement.*;
import com.sunxung.factoring.entity.settlement.SettlementBasicInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.entity.sys.FileStorageDO;
import com.sunxung.factoring.entity.sys.SysAttachmentRef;
import com.sunxung.factoring.mapper.invoicemanage.InvoiceApprovalMapper;
import com.sunxung.factoring.service.customerinformation.CustomerService;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.invoicemanage.*;
import com.sunxung.factoring.service.project.BusinessAssigneeService;
import com.sunxung.factoring.service.purchcontractmanagement.IPurchContractSignConclusionService;
import com.sunxung.factoring.service.purchcontractmanagement.IPurchContractSupplementInfoService;
import com.sunxung.factoring.service.purchcontractmanagement.PurchContractBasicInfoService;
import com.sunxung.factoring.service.purchcontractmanagement.PurchContractService;
import com.sunxung.factoring.service.salescontractmanagement.*;
import com.sunxung.factoring.service.settlement.SettlementBasicInfoService;
import com.sunxung.factoring.service.sys.*;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @Description : 销售合同审批service实现类
 * @Author : JiaYf
 * @Date: 2021-10-23 14:10
 */
@Service("sale_invoice_approvalService")
public class InvoiceApprovalServiceImpl extends ServiceImpl<InvoiceApprovalMapper, InvoiceApproval>
        implements InvoiceApprovalService {

    @Autowired
    private InvoiceBasicInfoService invoiceBasicInfoService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private DictionaryService dictService;

    @Autowired
    private ICSalesContractBasicInfoService salesContractBasicInfoService;

    @Autowired
    private SalesContractService salesContractService;

    @Autowired
    private PurchContractBasicInfoService purchContractBasicInfoService;

    @Autowired
    private PurchContractService purchContractService;

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

    @Autowired
    private InvoiceGoodsInfoService invoiceGoodsInfoService;

    @Autowired
    private SettlementBasicInfoService settlementBasicInfoService;


    @Autowired
    private ISalesContractSupplementInfoService salesContractSupplementInfoService;

    @Autowired
    private IPurchContractSupplementInfoService purchContractSupplementInfoService;

    @Autowired
    private InvoiceSupplierInfoService invoiceSupplierInfoService;

    @Autowired
    private IPurchContractSignConclusionService purchContractSignConclusionService;

    @Autowired
    private SalesInvoiceInfoService salesInvoiceInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private BusinessAssigneeService assigneeService;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private FileService fileService;

    @Autowired
    private PurchaseInvoiceInfoService purchaseInvoiceInfoService;
    @Autowired
    private IEnterpriseService iEnterpriseService;

    @Autowired
    private ICSalesSignConfirmationService salesSignConfirmationService;

    @Autowired
    private ICSalesSignConfirmationElectronicInfoService signConfirmationElectronicInfoService;

    @Autowired
    private ICSalesContractSupplementBasicInfoService salesContractSupplementBasicInfoService;

    @Autowired
    private ICSalesSupplementSignConfirmationService salesSupplementSignConfirmationService;

    @Autowired
    private ICSalesSupplementSignConfirmationElectronicInfoService salesSupplementSignConfirmationElectronicInfoService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(InvoiceApproval invoiceApproval, String businessKey, String taskId, HttpServletRequest request) {
        InvoiceBasicInfo invoiceBasicInfo = invoiceBasicInfoService.getById(businessKey);
        //保存销售发票信息
        SalesInvoiceInfo salesInvoiceInfo = invoiceApproval.getSalesInvoiceInfo();
        //添加审批结论
        invoiceApproval.setAuditorId(ThreadLocalUtil.getUserId());
        String result;
        switch (invoiceApproval.getApprovalConclusion()) {
            case ConstantConversionUtil.APPROVAL_RESULT_PASS:
                result = ConstantUtil.APPROVAL;
                if(salesInvoiceInfo != null){
                    invoiceBasicInfo.setInvoiceStatus(dictService.getByCode(InvoiceDict.ChildEnum.PASS.getCode()).getId());
                }
                break;
            case ConstantConversionUtil.APPROVAL_RESULT_BACK:
                result = ConstantUtil.OVERRULE;
                invoiceBasicInfo.setInvoiceStatus(dictService.getByCode(InvoiceDict.ChildEnum.OVERRULE.getCode()).getId());
                break;
            default:
                result = "";
        }
        //修改验收单信息
        //保存审核结果
        invoiceApproval.setAuditorId(ThreadLocalUtil.getUserId());
        invoiceApproval.setInvoiceBasicInfoId(invoiceBasicInfo.getId());
        save(invoiceApproval);
        invoiceBasicInfoService.updateById(invoiceBasicInfo);
        //流程处理
        Enterprise customer = iEnterpriseService.getById(invoiceBasicInfo.getEnterpriseCustomerInfoId());
        if(salesInvoiceInfo != null){
            salesInvoiceInfo.setInvoiceBasicInfoId(invoiceBasicInfo.getId());
            salesInvoiceInfoService.save(salesInvoiceInfo);
            List<Long> salesInvoicFile = salesInvoiceInfo.getSalesInvoicFileIds();
            if(CollectionUtil.isNotEmpty(salesInvoicFile)){
                for (Long id:salesInvoicFile ) {
                    FileStorage  fileStorage = fileStorageService.get(id);
                    FileStorage.Belong belong = new FileStorage.Belong();
                    belong.setBelongId(salesInvoiceInfo.getId()+"");
                    belong.setBelongType(FileModuleDict.ChildEnum.INVOICE_SALES_FILE.getCode());
                    fileStorageService.updateBelongById(fileStorage.getId(),belong);
                }
            }
            String processKey = ConstantUtil.INVOICE;
            Map<String, Object> msgVariables = new HashMap<>(2);
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + invoiceBasicInfo.getBusinessNumber() + "核心企业为：" + customer.getCorporateName());
            if (result.equals(ConstantUtil.OVERRULE)) {
                msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                        "业务编号为：" + invoiceBasicInfo.getBusinessNumber() + "核心企业为：" + customer.getCorporateName() + "”的发票已被退回进入“"
                                + ConstantConversionUtil.flowBusinessKeyNames.get(processKey) + "—修改发票”阶段，"
                                + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
            } else if (result.equals(ConstantUtil.APPROVAL)) {
                msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                        "业务编号为：" + invoiceBasicInfo.getBusinessNumber() + "核心企业为：" + customer.getCorporateName() + "”的发票已审核通过");
            }
            flowableService.completeMyTask(taskId, invoiceBasicInfo, null, result, invoiceApproval.getId(), msgVariables, processKey);
        }else {
            // 业务审核通过，新增处理人-财务专员
            List<Long> financeStaffLists = userService.findUserIdByRole(ConstantUtil.CASHIER);
            if (financeStaffLists == null || financeStaffLists.size() == 0) {
                throw new ValidatorException(CodeUtil.FAIL, ConstantUtil.FINANCE_STAFF + "角色内无任何用户！");
            }
//            BusinessAssignee financeStaffList = new BusinessAssignee(invoiceBasicInfo.getId(), ConstantUtil.INVOICE);
//            financeStaffList.setCandidateId(StringUtils.join(financeStaffLists, ","));
//            financeStaffList.setAssigneeType(ConstantUtil.CASHIER);
//            assigneeService.add(financeStaffList);
            //3.完成流程第一步提交发票，开启下一步财务审核
            Map<String, Object> procVariables = new HashMap<>();
            Map<String, Object> msgVariables = new HashMap<>(2);
            procVariables.put(ConstantUtil.SALE_INVOICE_FINANCIAL_APPROVAL_CANDIDATE,
                    assigneeService.getCandidateByBusiness(invoiceBasicInfo.getId(), ConstantUtil.INVOICE, ConstantUtil.CASHIER));
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "业务编号为：" + invoiceBasicInfo.getBusinessNumber() + "核心企业为：" + customer.getCorporateName());
            msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                    "业务编号为：" + invoiceBasicInfo.getBusinessNumber() + "核心企业为：" + customer.getCorporateName() + "”的发票已进入“"
                            + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.INVOICE) + "—财务审核阶段，"
                            + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
//            Long historyId = invoiceBasicInfoService.addHistorySaleInvoice(invoiceBasicInfo, request);
            flowableService.completeMyTask(taskId, invoiceBasicInfo, procVariables, result,invoiceApproval.getId(), msgVariables, ConstantUtil.INVOICE, financeStaffLists);
        }
    }

    @Override
    public InvoiceApproval getView(Long id) {
        InvoiceApproval invoiceApproval = baseMapper.selectById(id);
        InvoiceApproval fileList = getFileList(invoiceApproval.getInvoiceBasicInfoId());
        invoiceApproval.setPurchContract(fileList.getPurchContract());
        invoiceApproval.setSalesContract(fileList.getSalesContract());
        invoiceApproval.setSettlementFile(fileList.getSettlementFile());
        invoiceApproval.setSettlementUpstreamSignedFile(fileList.getSettlementUpstreamSignedFile());
        invoiceApproval.setPendingInvoice(fileList.getPendingInvoice());
        invoiceApproval.setAmountKind(fileList.getAmountKind());
        invoiceApproval.setSalesInvoiceInfo(fileList.getSalesInvoiceInfo());
        return invoiceApproval;
    }


    @Override
    public InvoiceApproval getFileList(Long businessKey) {
        InvoiceApproval invoiceApproval = new InvoiceApproval();
        InvoiceBasicInfo invoiceBasicInfo = invoiceBasicInfoService.getById(businessKey);
        List<InvoiceGoodsInfo> invoiceGoodsInfoList = invoiceGoodsInfoService.list(new QueryWrapper<InvoiceGoodsInfo>().eq("c_invoice_basic_info_id", businessKey));
        Set<Long> settlementInfoIds = new HashSet<>();
        BigDecimal nvoiceNum = BigDecimal.ZERO, amount = BigDecimal.ZERO, taxAmount = BigDecimal.ZERO,priceTaxTotal = BigDecimal.ZERO;
        if (invoiceGoodsInfoList != null && invoiceGoodsInfoList.size() > 0) {
            for (InvoiceGoodsInfo invoiceGoodsInfo : invoiceGoodsInfoList) {
                settlementInfoIds.add(invoiceGoodsInfo.getSettlementBasicInfoId());
                nvoiceNum = nvoiceNum.add(invoiceGoodsInfo.getOpenQuantity());
                amount = amount.add(invoiceGoodsInfo.getOpenAmount());
            }
        }

        //销售合同文件
        List<FileStorage> salesContract = autoSalesFiles(invoiceBasicInfo);
        invoiceApproval.setSalesContract(salesContract);
        //采购合同文件
        List<FileStorage> purchFilelist = autoPurchFiles(invoiceBasicInfo);
        invoiceApproval.setPurchContract(purchFilelist);

        //下游结算单文件
        List<FileStorage> settlementFiles = new ArrayList<>();
        for (Long settlementInfoId : settlementInfoIds) {
            List<FileStorage> files = sysAttachmentRefService.getCfiles(AttachmentDto.builder().setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_FILE).setEntityId(settlementInfoId).build());
            settlementFiles.addAll(files);
        }
        invoiceApproval.setSettlementFile(settlementFiles);

        //上游结算单文件
        List<FileStorage> settlementUpstreamSignedFile = new ArrayList<>();
        for (Long settlementInfoId : settlementInfoIds) {
            SettlementBasicInfo settlementBasicInfo = settlementBasicInfoService.getById(settlementInfoId);
            if (settlementBasicInfo != null) {
                List<FileStorage> files = new ArrayList<>();
                if (ProjectInitiationStatusDict.ChildEnum.PROCESSING.getCode().equals(settlementBasicInfo.getSettlementStatus())) {
                    files = sysAttachmentRefService.getCfiles(AttachmentDto.builder()
                            .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE)
                            .setEntityId(settlementInfoId).build());
//                    if (files != null && files.size() > 0) {
//                        files.stream().forEach(fileStorage -> {
//                            fileStorageService.add(fileStorage);
//
//                        });
//                    }
                } else {
                    files.add(sysAttachmentRefService.getCfiles(AttachmentDto.builder()
                            .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_SIGNED_FILE)
                            .setEntityId(settlementInfoId).build()).get(0));
//                    if (files != null && files.size() > 0) {
//                        files.stream().forEach(fileStorage -> {
//                            fileStorageService.add(fileStorage);
//
//                        });
//                    }
                }
                settlementUpstreamSignedFile.addAll(files);
            }
        }

        invoiceApproval.setSettlementUpstreamSignedFile(settlementUpstreamSignedFile);
        //参考待开发票
        invoiceApproval.setPendingInvoice(sysAttachmentRefService.getCfiles(AttachmentDto.builder().setChildEnum(FileModuleDict.ChildEnum.INVOICE_SALES_FILE).setEntityId(invoiceBasicInfo.getId()).build()));
        //金额种类是货款前端显示结算单文件和发票文件
        boolean amountKind = true;
        if (invoiceBasicInfo.getAmountKind().equals(dictService.getByCode(AmountKindDict.ChildEnum.PAYMENT.getCode()).getId())) {
            amountKind = false;
        }
        invoiceApproval.setAmountKind(amountKind);
        //销售发票信息
        QueryWrapper<SalesInvoiceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_invoice_basic_info_id", businessKey);
        queryWrapper.orderByDesc("gmt_modified").last("limit 1");
        SalesInvoiceInfo salesInvoiceInfo = salesInvoiceInfoService.getOne(queryWrapper);
        BigDecimal bigDecimal = BigDecimal.valueOf(1 + 0.13);
        BigDecimal bigDecimal1 = BigDecimal.valueOf(0.13);
        if(salesInvoiceInfo != null && salesInvoiceInfo.getId() != null){
            List<FileStorage> fileStorages = fileStorageService.findByBelongIdAndBelongType(salesInvoiceInfo.getId() + "", FileModuleDict.ChildEnum.INVOICE_SALES_FILE.getCode());
            salesInvoiceInfo.setSalesInvoicFile(CollectionUtil.isNotEmpty(fileStorages)?fileStorages:new ArrayList<>());
            salesInvoiceInfo.setNvoiceNum(nvoiceNum);
            salesInvoiceInfo.setAmount(amount.divide(bigDecimal,2, RoundingMode.HALF_UP));
            salesInvoiceInfo.setTaxAmount(amount.multiply(bigDecimal1).divide(bigDecimal,2, RoundingMode.HALF_UP));
            salesInvoiceInfo.setPriceTaxTotal(amount);
            invoiceApproval.setSalesInvoiceInfo(salesInvoiceInfo);
        }else {
            salesInvoiceInfo = new SalesInvoiceInfo();
            salesInvoiceInfo.setInvoiceBasicInfoId(businessKey);
            salesInvoiceInfo.setNvoiceNum(nvoiceNum);
            salesInvoiceInfo.setAmount(amount.divide(bigDecimal,2, RoundingMode.HALF_UP));
            salesInvoiceInfo.setTaxAmount(amount.multiply(bigDecimal1).divide(bigDecimal,2, RoundingMode.HALF_UP));
            salesInvoiceInfo.setPriceTaxTotal(amount);
            invoiceApproval.setSalesInvoiceInfo(salesInvoiceInfo);
        }
        return invoiceApproval;
    }

    /**
     * 填充销售合同附件
     *
     * @param invoiceBasicInfo
     */
    List<FileStorage> autoSalesFiles(InvoiceBasicInfo invoiceBasicInfo) {
        //销售合同 文件
        List<FileStorage> salesContract = new ArrayList<>();
        CSalesContract salesContractManagement = salesContractService.getByBusinessNumber(invoiceBasicInfo.getBusinessNumber());
        CSalesContractBasicInfo salesContractBasicInfo = salesContractBasicInfoService
                .getOne(new QueryWrapper<CSalesContractBasicInfo>().eq("c_sales_contract_management_id", salesContractManagement.getId()));
        //销售合同原件
        //合同文件
        List<FileStorageDO> fileStorageDOS = fileService.findByBusinessId(salesContractBasicInfo.getId(), FileModuleDict.ChildEnum.SALES_CONTRACT_FILE.getCode());
        if (CollectionUtil.isNotEmpty(fileStorageDOS)) {
            fileStorageDOS.forEach(fileStorageDO -> {
                FileStorage fileStorage = new FileStorage();
                fileStorage.setAbsolutePath(fileStorageDO.getAbsolutePath());
                fileStorage.setName(fileStorageDO.getOriginalName());
                fileStorage.setPath(fileStorageDO.getPath());
                fileStorage.setId(fileStorageDO.getId());
                fileStorage.setOriginalName(fileStorageDO.getOriginalName());
                salesContract.add(fileStorage);
            });
        }
        //已签署的文件
        CSalesSignConfirmation salesSignConfirmation = salesSignConfirmationService
                .lambdaQuery().eq(CSalesSignConfirmation::getcSalesContractManagementId, salesContractManagement.getId()).one();
        if (salesSignConfirmation != null) {
            List<CSalesSignConfirmationElectronicInfo> signElectronicInfos = signConfirmationElectronicInfoService
                    .list(new QueryWrapper<CSalesSignConfirmationElectronicInfo>().eq("c_sales_sign_confirmation_id", salesSignConfirmation.getId()));
            if (CollectionUtil.isNotEmpty(signElectronicInfos)) {
                signElectronicInfos.forEach(signElectronicInfo -> {
                    salesContract.addAll(sysAttachmentRefService.getFiles(AttachmentDto.builder()
                            .setChildEnum(FileModuleDict.ChildEnum.SIGN_CONFIRM_BACK_FILE).setEntityId(signElectronicInfo.getId()).build()));
                });
            }
        }
        //销售合同补充文件
        List<CSalesContractSupplementInfo> salesContractSupplementInfos = salesContractSupplementInfoService
                .list(new QueryWrapper<CSalesContractSupplementInfo>().eq("c_sales_contract_management_id", salesContractManagement.getId()));
        if (CollectionUtil.isNotEmpty(salesContractSupplementInfos)) {
            salesContractSupplementInfos.stream().forEach(s -> {
                if (salesContractManagement.getContractStatus().equals(ProjectInitiationStatusDict.ChildEnum.COMPLETED.getCode())) {

                    //补充合同原件
                    CSalesContractSupplementBasicInfo salesContractSupplementBasicInfo = salesContractSupplementBasicInfoService.lambdaQuery()
                            .eq(CSalesContractSupplementBasicInfo::getcSalesContractSupplementInfoId, s.getId())
                            .one();
                    List<FileStorageDO> SupplementFileStorageDOS = fileService.findByBusinessId(salesContractSupplementBasicInfo.getId(),
                            FileModuleDict.ChildEnum.SALES_CONTRACT_SUPPLEMENT_CONTRACT_FILE.getCode());
                    if (CollectionUtil.isNotEmpty(SupplementFileStorageDOS)) {
                        SupplementFileStorageDOS.forEach(fileStorageDO -> {
                            FileStorage fileStorage = new FileStorage();
                            fileStorage.setAbsolutePath(fileStorageDO.getAbsolutePath());
                            fileStorage.setName(fileStorageDO.getOriginalName());
                            fileStorage.setPath(fileStorageDO.getPath());
                            fileStorage.setId(fileStorageDO.getId());
                            fileStorage.setOriginalName(fileStorageDO.getOriginalName());
                            salesContract.add(fileStorage);
                        });
                    }

                    CSalesSupplementSignConfirmation salesSupplementSignConfirmation = salesSupplementSignConfirmationService
                            .lambdaQuery().eq(CSalesSupplementSignConfirmation::getcSalesContractSupplementInfoId, s.getId()).one();
                    if (salesSupplementSignConfirmation != null) {
                        List<CSalesSupplementSignConfirmationElectronicInfo> supplementSignConfirmationElectronicInfos = salesSupplementSignConfirmationElectronicInfoService
                                .lambdaQuery().eq(CSalesSupplementSignConfirmationElectronicInfo::getcSalesSupplementSignConfirmationId, salesSupplementSignConfirmation.getId()).list();
                        if (CollectionUtil.isNotEmpty(supplementSignConfirmationElectronicInfos)) {
                            //获取销售合同补充签署的附件
                            supplementSignConfirmationElectronicInfos.forEach(supplementSignConfirmationElectronicInfo -> {
                                List<FileStorage> salesContractSupplementFiles = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                                        .setChildEnum(FileModuleDict.ChildEnum.SALES_SUPPLEMENT_SIGN_CONFIRM_BACK_FILE)
                                        .setEntityId(supplementSignConfirmationElectronicInfo.getId()).build());
                                salesContract.addAll(salesContractSupplementFiles);
                            });
                        }

                    }
                }
            });
        }
        return salesContract;
    }

    /**
     * 填充采购合同附件
     *
     * @param invoiceBasicInfo
     */
    List<FileStorage> autoPurchFiles(InvoiceBasicInfo invoiceBasicInfo) {
        //采购合同 文件
        PurchContractManagement purchContractManagement = purchContractService
                .getOne(new QueryWrapper<PurchContractManagement>().eq("business_number", invoiceBasicInfo.getBusinessNumber())
                );  //.eq("c_supplier_info_id", acceptanceBasicInfo.getcSupplierInfoId())
        PurchContractBasicInfo purchContractBasicInfo = purchContractBasicInfoService.lambdaQuery().eq(PurchContractBasicInfo::getPurchContractManagementId, purchContractManagement.getId()).one();

        List<FileStorage> purchaseContract = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                .setChildEnum(FileModuleDict.ChildEnum.PURCHASE_CONTRACT_SIGN_FILE)
                .setEntityId(purchContractBasicInfo.getId()).build());

        //采购合同补充合同
        List<PurchContractSupplementInfo> contractSupplementInfos = purchContractSupplementInfoService
                .list(new QueryWrapper<PurchContractSupplementInfo>().eq("c_purch_contract_management_id", purchContractManagement.getId()));
        //采购合同状态
        if (purchContractManagement.getContractStatus().equals(ProjectInitiationStatusDict.ChildEnum.COMPLETED.getCode())) {
            if (CollectionUtil.isNotEmpty(contractSupplementInfos)) {
                contractSupplementInfos.stream().forEach(contractSupplementInfo -> {
                    //采购合同变更已签署文件
                    AttachmentDto attachmentDto = AttachmentDto.builder()
                            .setEntityId(contractSupplementInfo.getId())
                            .setChildEnum(FileModuleDict.ChildEnum.PURCHASE_CONTRACT_SUPPLEMENT_SIGN_FILE).build();
                    purchaseContract.addAll(sysAttachmentRefService.getFiles(attachmentDto));

                });
            }
        }
        return purchaseContract;
    }

}
