shiyunteng
2026-08-27 b1b3da5393c75ced1afa1467be69d60af5403878
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractPaymentScheduleProcessServiceImpl.java
@@ -19,6 +19,7 @@
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
@@ -44,28 +45,16 @@
      if (ObjUtil.isNull(schedule)) {
         return R.failed("履约阶段与合同不一致,请联系技术人员");
      }
      if (schedule.getStageName().equals("货到签收") && CollUtil.isEmpty(addDTO.getContractOutBoundIds())) {
         return R.failed("请选择到货的出库信息");
      }
      //合同
      Contract contract = contractMapper.selectById(schedule.getContractId());
      List<ContractSubjectMatter> subjectMatterList = subjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery().eq(ContractSubjectMatter::getContractId, contract.getId()));
      if (CollUtil.isEmpty(subjectMatterList)) {
         return R.failed("该合同标的物异常,请联系技术人员");
      if (ObjUtil.isNull(contract)) {
         return R.failed("合同信息异常");
      }
//      if (!subjectMatterList.stream().allMatch(item -> item.getDeliveryStatus() == 2)) {
//         return R.failed("出库数量小于当前合同标的物数量,无法进行当前操作");
//      }
      if (schedule.getStageName().equals("货到签收")) {
         //更新合同出库到货时间
         addDTO.getContractOutBoundIds().forEach(contractOutBoundId -> {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundId);
            contractOutBound.setArrivalTime(addDTO.getProcessDate());
            contractOutBound.setArrivalAttNames(addDTO.getOutBoundAttNames());
            contractOutBound.setArrivalAttPaths(addDTO.getOutBoundAttPaths());
            contractOutBoundMapper.updateById(contractOutBound);
         });
      }
      return handleSchedule(addDTO, schedule, contract);
   }
   private void saveScheduleProcess(ContractPaymentScheduleProcessAddDTO addDTO, ContractPaymentSchedule schedule) {
      //履约
      ContractPaymentScheduleProcess contractPaymentScheduleProcess = BeanUtil.copyProperties(addDTO, ContractPaymentScheduleProcess.class);
      contractPaymentScheduleProcess.setContractId(schedule.getContractId());
@@ -77,386 +66,151 @@
      if (ObjUtil.isNull(lastProcess)) {
         baseMapper.insert(contractPaymentScheduleProcess);
      }
   }
      //更新当前阶段
      if (ObjUtil.isNull(schedule.getEffectiveDate()) && ObjUtil.isNull(schedule.getEffectiveEndDate())) {
         schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getProcessDate(), schedule.getAgreedDays()));
         contractPaymentScheduleMapper.updateById(schedule);
      }
//      //查询是否有之前阶段
//      ContractPaymentSchedule beforeSchedule = contractPaymentScheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
//            .lt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()).orderByDesc(ContractPaymentSchedule::getCreateTime).last("limit 1"));
//      if (ObjUtil.isNotNull(beforeSchedule) && ObjUtil.isNull(beforeSchedule.getEffectiveEndDate())) {
//         beforeSchedule.setEffectiveEndDate(schedule.getEffectiveDate());
//         contractPaymentScheduleMapper.updateById(beforeSchedule);
//      }
      List<ContractOutBound> contractOutBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery().eq(ContractOutBound::getContractId, contract.getId())
            .isNotNull(ContractOutBound::getArrivalTime));
   private R handleSchedule(ContractPaymentScheduleProcessAddDTO addDTO, ContractPaymentSchedule schedule, Contract contract) {
      if (schedule.getStageName().equals("货到签收")) {
         if (CollUtil.isNotEmpty(contractOutBoundList)
               && CollUtil.isNotEmpty(subjectMatterList)) {
            Map<String, BigDecimal> sumMap = contractOutBoundList.stream()
                  .collect(Collectors.groupingBy(
                        ContractOutBound::getSubjectMatterCode,
                        Collectors.reducing(
                              BigDecimal.ZERO,
                              ContractOutBound::getOutBoundNum,
                              BigDecimal::add
                        )
                  ));
            Boolean outFlag = checkQuantityEquality(subjectMatterList, sumMap);
            //出库到货与合同标的物种类数量一致
            if (outFlag) {
               //新增当前阶段应收
               PaymentConfirm currentConfim = new PaymentConfirm();
               currentConfim.setBusinessType(schedule.getStageName() + "应收");
               currentConfim.setBusGuestId(contract.getPartyAId());
               currentConfim.setBusGuestName(contract.getPartyA());
               currentConfim.setContractId(contract.getId());
               currentConfim.setContractName(contract.getContractName());
               currentConfim.setContractNo(contract.getContractNo());
               currentConfim.setScheduleId(schedule.getId());
               currentConfim.setScheduleName(schedule.getStageName());
               currentConfim.setConfirmTime(addDTO.getProcessDate());
               currentConfim.setTransationAmount(schedule.getPlannedAmount());
               currentConfim.setReceivableAmount(schedule.getPlannedAmount());
               PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
                     .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
               BigDecimal lastTotal = new BigDecimal("0");
               if (ObjUtil.isNotNull(lastConfirm)) {
                  lastTotal = lastConfirm.getTotalAmount();
               }else {
                  //客户付款完成合同
                  List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
                        .eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
                  if (CollUtil.isNotEmpty(customerCompleteContractList)){
                     List<BigDecimal> outAmountList = new ArrayList<>();
                     for (Contract completeContract:customerCompleteContractList
                     ) {
                        PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,completeContract.getId())
                              .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
                        //查询客户付款完成合同中最后明细金额 有预收转入当前合同
                        if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
                           //新增完成付款合同 资金转出
                           PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm,PaymentConfirm.class,"id","transationAmount",
                                 "advanceAmount","receivableAmount","overdueAmount","totalAmount");
                           outConfirm.setBusinessType("资金转出");
                           outConfirm.setConfirmTime(new Date());
                           outConfirm.setTransationAmount(contractLastConfirm.getTotalAmount());
                           outConfirm.setTotalAmount(new BigDecimal("0"));
                           paymentConfirmMapper.insert(outConfirm);
                           outAmountList.add(outConfirm.getTransationAmount());
                        }
                     }
                     if (CollUtil.isNotEmpty(outAmountList)){
                        BigDecimal currentSum = new BigDecimal("0");
                        for (BigDecimal currentAmount:outAmountList
                        ) {
                           currentSum=currentSum.add(currentAmount);
                           //新增当前合同 资金转入
                           PaymentConfirm inConfirm = BeanUtil.copyProperties(lastConfirm,PaymentConfirm.class,"id","transationAmount",
                                 "advanceAmount","receivableAmount","overdueAmount","totalAmount");
                           inConfirm.setBusinessType("资金转入");
                           inConfirm.setConfirmTime(new Date());
                           inConfirm.setTransationAmount(currentSum.subtract(currentAmount));
                           inConfirm.setTotalAmount(lastConfirm.getTotalAmount().add(currentSum));
                           if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
                              inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
                           }else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0"))<0){
                              inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
                           }
                           paymentConfirmMapper.insert(inConfirm);
                        }
                     }
                  }
               }
               currentConfim.setTotalAmount(lastTotal.subtract(currentConfim.getReceivableAmount()));
               if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
                  paymentConfirmMapper.insert(currentConfim);
                  //更新当前阶段付款情况
                  if (lastTotal.compareTo(new BigDecimal("0"))>0){
                     if (currentConfim.getTotalAmount().compareTo(new BigDecimal("0"))>=0) {
                        schedule.setPaymentStatus(2);
                        schedule.setPlannedAmount(schedule.getPlannedAmount());
                        contractPaymentScheduleMapper.updateById(schedule);
                     }else {
                        schedule.setPaymentStatus(1);
                        schedule.setPlannedAmount(lastTotal);
                     }
                  }
               }
               //查询是否有后续阶段
               List<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                     .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
                     .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
               if (CollUtil.isNotEmpty(afterSchedule) && afterSchedule.size() == 1) {
                  //最后阶段生效时间
                  ContractPaymentSchedule endSchedule = afterSchedule.get(0);
                  if (StrUtil.equals(endSchedule.getStageName(), "质保金")) {
                     endSchedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getProcessDate(), endSchedule.getAgreedDays()));
                     contractPaymentScheduleMapper.updateById(endSchedule);
                     //当前阶段生效时间
//                     schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getProcessDate(), schedule.getAgreedDays()));
//                     contractPaymentScheduleMapper.updateById(schedule);
                     //最后阶段应收
                     PaymentConfirm newConfim = new PaymentConfirm();
                     newConfim.setBusinessType(endSchedule.getStageName() + "应收");
                     newConfim.setBusGuestId(contract.getPartyAId());
                     newConfim.setBusGuestName(contract.getPartyA());
                     newConfim.setContractId(contract.getId());
                     newConfim.setContractName(contract.getContractName());
                     newConfim.setContractNo(contract.getContractNo());
                     newConfim.setScheduleId(endSchedule.getId());
                     newConfim.setScheduleName(schedule.getStageName());
                     newConfim.setConfirmTime(addDTO.getProcessDate());
                     newConfim.setTransationAmount(endSchedule.getPlannedAmount());
                     newConfim.setReceivableAmount(endSchedule.getPlannedAmount());
                     PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
                           .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
                     BigDecimal lastNewTotal = new BigDecimal("0");
                     if (ObjUtil.isNotNull(lastNewConfirm)) {
                        lastNewTotal = lastNewConfirm.getTotalAmount();
                     }
                     newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount()));
                     if (endSchedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
                        paymentConfirmMapper.insert(newConfim);
                     }
                  }
                  //更新合同下个阶段
                  contract.setNextScheduleName(endSchedule.getStageName());
                  contractMapper.updateById(contract);
               }
               if (CollUtil.isNotEmpty(afterSchedule) && afterSchedule.size() > 1) {
                  //更新合同下个阶段
                  contract.setNextScheduleName(afterSchedule.get(0).getStageName());
                  contractMapper.updateById(contract);
               }
               if (CollUtil.isEmpty(afterSchedule)) {
//                  schedule.setEffectiveEndDate(contract.getExpirationDate());
//                  contractPaymentScheduleMapper.updateById(schedule);
                  //更新合同下个阶段
                  contract.setNextScheduleName("无");
                  contractMapper.updateById(contract);
                  //当前为合同最后阶段
                  PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
                        .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
                  if (lastNewConfirm.getTotalAmount().compareTo(new BigDecimal("0")) >= 0) {
                     //有预付且超过应收 关闭合同状态
                     contract.setContractStatus(3);
                     contractMapper.updateById(contract);
                  }
               }
            }
         if (CollUtil.isEmpty(addDTO.getContractOutBoundIds())) {
            return R.failed("签收信息不能为空");
         }
      } else if (schedule.getStageName().equals("调试完成或验收")){
         //新增当前阶段应收
         PaymentConfirm currentConfim = new PaymentConfirm();
         currentConfim.setBusinessType(schedule.getStageName() + "应收");
         currentConfim.setBusGuestId(contract.getPartyAId());
         currentConfim.setBusGuestName(contract.getPartyA());
         currentConfim.setContractId(contract.getId());
         currentConfim.setContractName(contract.getContractName());
         currentConfim.setContractNo(contract.getContractNo());
         currentConfim.setScheduleId(schedule.getId());
         currentConfim.setScheduleName(schedule.getStageName());
         currentConfim.setConfirmTime(addDTO.getProcessDate());
         currentConfim.setTransationAmount(schedule.getPlannedAmount());
         currentConfim.setReceivableAmount(schedule.getPlannedAmount());
         PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
         BigDecimal lastTotal = new BigDecimal("0");
         if (ObjUtil.isNotNull(lastConfirm)) {
            lastTotal = lastConfirm.getTotalAmount();
         }
         currentConfim.setTotalAmount(lastTotal.subtract(currentConfim.getReceivableAmount()));
         if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
            paymentConfirmMapper.insert(currentConfim);
            //更新当前阶段付款情况
            if (lastTotal.compareTo(new BigDecimal("0"))>0){
               if (currentConfim.getTotalAmount().compareTo(new BigDecimal("0"))>=0) {
                  schedule.setPaymentStatus(2);
                  schedule.setPlannedAmount(schedule.getPlannedAmount());
                  contractPaymentScheduleMapper.updateById(schedule);
               }else {
                  schedule.setPaymentStatus(1);
                  schedule.setPlannedAmount(lastTotal);
               }
            }
         }
         //查询是否有后续阶段
         List<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
               .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
               .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
               .gt(ContractPaymentSchedule::getPaymentRatio,"0"));
         if (CollUtil.isNotEmpty(afterSchedule) && afterSchedule.size() == 1) {
            //最后阶段生效时间
            ContractPaymentSchedule endSchedule = afterSchedule.get(0);
//            if (StrUtil.equals(endSchedule.getStageName(), "质保金")) {
//               endSchedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getProcessDate(), endSchedule.getAgreedDays()));
//               contractPaymentScheduleMapper.updateById(endSchedule);
//               //当前阶段生效时间
//               schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getProcessDate(), schedule.getAgreedDays()));
//               contractPaymentScheduleMapper.updateById(schedule);
//               //最后阶段应收
//               PaymentConfirm newConfim = new PaymentConfirm();
//               newConfim.setBusinessType(endSchedule.getStageName() + "应收");
//               newConfim.setBusGuestId(contract.getPartyAId());
//               newConfim.setBusGuestName(contract.getPartyA());
//               newConfim.setContractId(contract.getId());
//               newConfim.setContractName(contract.getContractName());
//               newConfim.setContractNo(contract.getContractNo());
//               newConfim.setScheduleId(endSchedule.getId());
//               newConfim.setScheduleName(schedule.getStageName());
//               newConfim.setConfirmTime(addDTO.getProcessDate());
//               newConfim.setTransationAmount(endSchedule.getPlannedAmount());
//               newConfim.setReceivableAmount(endSchedule.getPlannedAmount());
//               PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
//                     .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
//               BigDecimal lastNewTotal = new BigDecimal("0");
//               if (ObjUtil.isNotNull(lastNewConfirm)) {
//                  lastNewTotal = lastNewConfirm.getTotalAmount();
//               }
//               newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount()));
//               if (endSchedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
//                  paymentConfirmMapper.insert(newConfim);
//                  //更新当前阶段付款情况
//                  if (lastNewTotal.compareTo(new BigDecimal("0"))>0){
//                     if (newConfim.getTotalAmount().compareTo(new BigDecimal("0"))>=0) {
//                        schedule.setPaymentStatus(2);
//                        schedule.setPlannedAmount(schedule.getPlannedAmount());
//                        contractPaymentScheduleMapper.updateById(schedule);
//                     }else {
//                        schedule.setPaymentStatus(1);
//                        schedule.setPlannedAmount(lastNewTotal);
//                     }
//                  }
//               }
//
//            }
            //更新合同下个阶段
            contract.setNextScheduleName(endSchedule.getStageName());
            contractMapper.updateById(contract);
         }
         if (CollUtil.isNotEmpty(afterSchedule) && afterSchedule.size() > 1) {
            //更新合同下个阶段
            contract.setNextScheduleName(afterSchedule.get(0).getStageName());
            contractMapper.updateById(contract);
         }
         if (CollUtil.isEmpty(afterSchedule)) {
//            schedule.setEffectiveEndDate(contract.getExpirationDate());
//            contractPaymentScheduleMapper.updateById(schedule);
            //更新合同下个阶段
            contract.setNextScheduleName("无");
            contractMapper.updateById(contract);
            //当前为合同最后阶段
            PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
                  .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
            if (lastNewConfirm.getTotalAmount().compareTo(new BigDecimal("0")) >= 0) {
               //有预付且超过应收 关闭合同状态
               contract.setContractStatus(3);
               contractMapper.updateById(contract);
            }
         }
      }else {
         //最后阶段应收
         PaymentConfirm newConfim = new PaymentConfirm();
         newConfim.setBusinessType(schedule.getStageName() + "应收");
         newConfim.setBusGuestId(contract.getPartyAId());
         newConfim.setBusGuestName(contract.getPartyA());
         newConfim.setContractId(contract.getId());
         newConfim.setContractName(contract.getContractName());
         newConfim.setContractNo(contract.getContractNo());
         newConfim.setScheduleId(schedule.getId());
         newConfim.setScheduleName(schedule.getStageName());
         newConfim.setConfirmTime(addDTO.getProcessDate());
         newConfim.setTransationAmount(schedule.getPlannedAmount());
         newConfim.setReceivableAmount(schedule.getPlannedAmount());
         PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
         BigDecimal lastNewTotal = new BigDecimal("0");
         if (ObjUtil.isNotNull(lastNewConfirm)) {
            lastNewTotal = lastNewConfirm.getTotalAmount();
         }
         newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount()));
         if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
            paymentConfirmMapper.insert(newConfim);
            //更新当前阶段付款情况
            if (lastNewTotal.compareTo(new BigDecimal("0"))>0){
               if (newConfim.getTotalAmount().compareTo(new BigDecimal("0"))>=0) {
                  schedule.setPaymentStatus(2);
                  schedule.setPlannedAmount(schedule.getPlannedAmount());
               }else {
                  schedule.setPaymentStatus(1);
                  schedule.setPlannedAmount(lastNewTotal);
               }
               contractPaymentScheduleMapper.updateById(schedule);
            }
         }
         //更新合同下个阶段
         contract.setNextScheduleName("");
         contractMapper.updateById(contract);
         //更新合同出库到货时间
         addDTO.getContractOutBoundIds().forEach(contractOutBoundId -> {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundId);
            contractOutBound.setArrivalTime(addDTO.getProcessDate());
            contractOutBound.setArrivalAttNames(addDTO.getOutBoundAttNames());
            contractOutBound.setArrivalAttPaths(addDTO.getOutBoundAttPaths());
            contractOutBoundMapper.updateById(contractOutBound);
         });
      }
      //付款比例不为0 保存收款明细
      if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
         savePaymentConfirm(addDTO, contract, schedule);
      }
      updateCurrentSchedule(addDTO.getProcessDate(), schedule, contract);
      //保存履约记录
      saveScheduleProcess(addDTO, schedule);
      return R.ok();
   }
   public boolean checkQuantityEquality(List<ContractSubjectMatter> subjectMatterList, Map<String, BigDecimal> materialCodeMap) {
      // 1. 基本校验
      if (subjectMatterList == null || materialCodeMap == null) {
         return false;
   private void savePaymentConfirm(ContractPaymentScheduleProcessAddDTO addDTO, Contract contract, ContractPaymentSchedule schedule) {
      BigDecimal amount = schedule.getPlannedAmount();
      if (schedule.getStageName().equals("货到签收")) {
         amount = new BigDecimal("0");
         //计算签收价格
         for (Long contractOutBoundId : addDTO.getContractOutBoundIds()) {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundId);
            ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                  .eq(ContractSubjectMatter::getContractId, contract.getId())
                  .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
                  .eq(ContractSubjectMatter::getLineNo, contractOutBound.getLineNo())
                  .last("limit 1"));
            amount = amount.add(contractOutBound.getOutBoundNum().multiply(subjectMatter.getUnitPrice()));
         }
         //实际到货金额按比例计算
         amount = schedule.getPlannedAmount().multiply(amount.divide(contract.getAmount(), 2, RoundingMode.HALF_UP));
      }
      // 如果要求两者包含的物料代码完全一致且数量一致,先比较 Key 的数量
      // 提取 List 中非空的 materialCode
      List<String> listCodes = subjectMatterList.stream()
            .map(ContractSubjectMatter::getMaterialCode)
            .filter(Objects::nonNull)
            .distinct()
            .sorted()
            .collect(Collectors.toList());
      List<String> mapKeys = materialCodeMap.keySet().stream()
            .sorted()
            .collect(Collectors.toList());
      // 如果物料编码集合不一致,直接返回 false
      if (!listCodes.equals(mapKeys)) {
         return false;
      //新增当前阶段应收
      PaymentConfirm currentConfim = new PaymentConfirm();
      currentConfim.setBusinessType(schedule.getStageName() + "应收");
      currentConfim.setBusGuestId(contract.getPartyAId());
      currentConfim.setBusGuestName(contract.getPartyA());
      currentConfim.setContractId(contract.getId());
      currentConfim.setContractName(contract.getContractName());
      currentConfim.setContractNo(contract.getContractNo());
      currentConfim.setScheduleId(schedule.getId());
      currentConfim.setScheduleName(schedule.getStageName());
      currentConfim.setConfirmTime(addDTO.getProcessDate());
      currentConfim.setTransationAmount(amount);
      currentConfim.setReceivableAmount(amount);
      PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
            .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
      if (ObjUtil.isNotNull(lastConfirm)) {
         currentConfim.setTotalAmount(lastConfirm.getTotalAmount().subtract(currentConfim.getTransationAmount()));
         if (contract.getContractNo().startsWith("ZB-")) {
            paymentConfirmMapper.insert(currentConfim);
         }
         return;
      }
      currentConfim.setTotalAmount(currentConfim.getTransationAmount().multiply(new BigDecimal("-1")));
      paymentConfirmMapper.insert(currentConfim);
      handleBeforeRemain(currentConfim, contract);
   }
      // 2. 逐个比较数量
      // 假设 ContractSubjectMatter 中有 getQuantity() 方法返回 BigDecimal 或 Number
      for (ContractSubjectMatter item : subjectMatterList) {
         String code = item.getMaterialCode();
         if (code == null) continue;
         BigDecimal mapValue = materialCodeMap.get(code);
         BigDecimal listValue = item.getQuantity(); // 假设这是 List 中代表数量的字段
         // 处理 null 情况
         if (mapValue == null && listValue == null) {
            continue; // 都为空视为相等,或根据业务定为不等
   public void handleBeforeRemain(PaymentConfirm currentConfim, Contract contract) {
      //客户付款完成合同
      List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
            .eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).eq(Contract::getContractStatus, 3));
      if (CollUtil.isNotEmpty(customerCompleteContractList)) {
         List<BigDecimal> outAmountList = new ArrayList<>();
         for (Contract completeContract : customerCompleteContractList) {
            PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, completeContract.getId())
                  .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
            //查询客户付款完成合同中最后明细金额 有预收转入当前合同
            if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0 &&
                  contract.getContractNo().startsWith("ZB-")) {
               //新增完成付款合同 资金转出
               PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm, PaymentConfirm.class, "id", "transationAmount",
                     "advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
               outConfirm.setBusinessType("资金转出");
               outConfirm.setConfirmTime(new Date());
               outConfirm.setTransationAmount(contractLastConfirm.getTotalAmount());
               outConfirm.setTotalAmount(new BigDecimal("0"));
               if (contract.getContractNo().startsWith("ZB-")) {
                  paymentConfirmMapper.insert(outConfirm);
               }
               outAmountList.add(contractLastConfirm.getTotalAmount());
            }
         }
         if (mapValue == null || listValue == null) {
            return false; // 一个为空一个不为空,不等
         }
         // 使用 compareTo 比较 BigDecimal 值,0 表示相等
         if (listValue.compareTo(mapValue) != 0) {
            return false; // 发现不相等,立即返回
         if (CollUtil.isNotEmpty(outAmountList)) {
            BigDecimal currentSum = outAmountList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
            //新增当前合同 资金转入
            PaymentConfirm inConfirm = BeanUtil.copyProperties(currentConfim, PaymentConfirm.class, "id", "transationAmount",
                  "advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
            inConfirm.setBusinessType("资金转入");
            inConfirm.setConfirmTime(new Date());
            inConfirm.setTransationAmount(currentSum);
            inConfirm.setTotalAmount(currentConfim.getTotalAmount().add(currentSum));
            if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
               inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
            } else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) < 0) {
               inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
            }
            if (contract.getContractNo().startsWith("ZB-")) {
               paymentConfirmMapper.insert(inConfirm);
            }
            contract.setPaidAmount(currentSum);
            contractMapper.updateById(contract);
         }
      }
   }
      return true; // 所有匹配项数量均相等
   private void updateCurrentSchedule(Date date, ContractPaymentSchedule schedule, Contract contract) {
      //查询是否有后续阶段
      ContractPaymentSchedule afterOneSchedule = contractPaymentScheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery()
            .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
            .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
            .orderByAsc(ContractPaymentSchedule::getCreateTime)
            .last("limit 1"));
      if (ObjUtil.isNotNull(afterOneSchedule)) {
         //更新下阶段名称
         List<ContractOutBound> contractOutBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery().eq(ContractOutBound::getContractId, contract.getId())
               .isNull(ContractOutBound::getArrivalTime));
         if (CollUtil.isEmpty(contractOutBoundList)) {
            contract.setNextScheduleName(afterOneSchedule.getStageName());
         }
      }
      if (ObjUtil.isNull(afterOneSchedule)) {
         contract.setNextScheduleName("");
      }
      contractMapper.updateById(contract);
      //更新履约阶段时间
      if (ObjUtil.isNull(schedule.getEffectiveDate()) && ObjUtil.isNull(schedule.getEffectiveEndDate())) {
         schedule.setEffectiveEndDate(DateUtil.offsetDay(date, schedule.getAgreedDays()));
      }
      contractPaymentScheduleMapper.updateById(schedule);
   }
   @Override
@@ -466,7 +220,7 @@
                  ContractPaymentSchedule::getPlannedAmount, ContractPaymentSchedule::getPlannedAmount,
                  ContractPaymentSchedule::getEffectiveEndDate, ContractPaymentSchedule::getPaymentDate,
                  ContractPaymentSchedule::getPaymentStatus, ContractPaymentSchedule::getActualAmount,
                  ContractPaymentSchedule::getAgreedDays,ContractPaymentSchedule::getPaymentRatio)
                  ContractPaymentSchedule::getAgreedDays, ContractPaymentSchedule::getPaymentRatio)
            .select(ContractPaymentScheduleProcess::getProcessDate)
            .leftJoin(ContractPaymentScheduleProcess.class, ContractPaymentScheduleProcess::getScheduleId, ContractPaymentSchedule::getId)
            .eq(ContractPaymentSchedule::getContractId, id)