shiyunteng
2026-05-27 0527f90adf2aea086af681fb8f3dbf49c0a5ed31
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java
@@ -120,7 +120,7 @@
            }
            //更新付款阶段付款
            List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                  .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
                  .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2).ne(ContractPaymentSchedule::getPaymentStatus, 3)
                  .orderByAsc(ContractPaymentSchedule::getCreateTime));
            if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
               BigDecimal remain = addDTO.getTransationAmount();
@@ -217,6 +217,7 @@
            ;
         }
      }else {
         List<Contract> customerContractList = contractService.list(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, addDTO.getBusGuestId())
               .eq(Contract::getPartyBId,SecurityUtils.getUser().getCompId()).orderByAsc(Contract::getCreateTime).apply(" amount != paid_amount"));
         if (ArrayUtil.isEmpty(customerContractList.toArray())) {
@@ -229,6 +230,15 @@
         if (ObjUtil.isNull(customer)) {
            return  R.failed("客商查询失败");
         }
         //新增付款单
         PaymentSlip slip = new PaymentSlip();
         slip.setBusGuestId(customer.getId());
         slip.setBusGuestName(customer.getCompanyName());
         slip.setPayeeCompId(SecurityUtils.getUser().getCompId());
         slip.setPaymentTime(addDTO.getConfirmTime());
         slip.setPaymentAmount(addDTO.getTransationAmount());
         slip.setPayType(addDTO.getPayType());
         paymentSlipMapper.insert(slip);
         BigDecimal transtionAmount = addDTO.getTransationAmount();
         //剩余付款
@@ -243,19 +253,25 @@
         List<Contract> advanceContractList = new ArrayList<>();
         //付款时间
         Date payDate = addDTO.getConfirmTime();
         for (Contract contract : customerContractList) {
         Iterator<Contract> iterator = customerContractList.iterator();
         while (iterator.hasNext()) {
            Contract contract = iterator.next();
            PaymentConfirm lastConfirm = baseMapper.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 {
            }
            //合同最近明细总额为0 或 > 0 情况下
            if (lastTotal.compareTo(new BigDecimal("0")) >= 0) {
               log.info("当前合同号:{},明细总额无应收款", contract.getContractNo());
               advanceContractList.add(contract);
               continue;
               if (iterator.hasNext()) {
                  continue;
               }
            }
            transtionRemainAmount = transtionRemainAmount.add(lastTotal);
@@ -271,9 +287,16 @@
            entity.setBusGuestName(customer.getCompanyName());
            entity.setBusinessType("客户付款");
            if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
               entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
               entity.setTotalAmount(new BigDecimal("0"));
               entity.setConfirmTime(payDate);
               // 判断是否是最后一个
               if (!iterator.hasNext()) {
                  entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
                  entity.setAdvanceAmount(transtionRemainAmount);
                  entity.setTotalAmount(transtionRemainAmount);
               }else {
                  entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
                  entity.setTotalAmount(new BigDecimal("0"));
               }
               baseMapper.insert(entity);
               payContractList.add(contract);
            } else if (transtionRemainAmount.compareTo(new BigDecimal("0")) == 0) {
@@ -285,7 +308,8 @@
               break;
            } else {
               entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
               entity.setTotalAmount(transtionAmount);
               entity.setReceivableAmount(transtionRemainAmount.multiply(new BigDecimal("-1")));
               entity.setTotalAmount(transtionRemainAmount);
               entity.setConfirmTime(payDate);
               baseMapper.insert(entity);
               payContractList.add(contract);
@@ -297,6 +321,7 @@
            //更新付款阶段付款
            List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                  .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
                  .ne(ContractPaymentSchedule::getPaymentStatus, 3)
                  .orderByAsc(ContractPaymentSchedule::getCreateTime));
            if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
               BigDecimal remain = transtionAmount;
@@ -351,48 +376,50 @@
            //合同所有付款阶段
            List<ContractPaymentSchedule> payComScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                  .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0)
                  );
                  .ne(ContractPaymentSchedule::getPaymentStatus, 3));
            if (ArrayUtil.isNotEmpty(payComScheduleList.toArray())){
               BigDecimal payTotal = payComScheduleList.stream().map(item->StrUtil.equals(item.getPaymentStatus() + "", "0") ? item.getPlannedAmount() :
               BigDecimal payTotal = payComScheduleList.stream().map(item->StrUtil.equals(item.getPaymentStatus() + "", "2") ? item.getPlannedAmount() :
                     item.getActualAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
               contract.setPaidAmount(payTotal);
               if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
                  contract.setContractStatus(3);
               }
               contractService.updateById(contract);
//               if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
//                  contract.setContractStatus(3);
//               }
               if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
                  contract.setBillingStatus("2");
                  contract.setContractStatus(3);
                  payCompleteContractList.add(contract);
               }
               //付款 且 付款未全付
               if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
                  payNoCompleteContractList.add(contract);
               }
               contractService.updateById(contract);
            }
         });
         //客户预付有剩余 且 有预付款的合同
         if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
            if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
               //余额 放到付款未完成合同 预收
               Contract lastContract = payNoCompleteContractList.get(0);
               //保存明细
               saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
            }
            if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
               //客户预付有剩余 没有预付款的合同
               Contract lastContract = advanceContractList.get(0);
               //保存明细
               saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
            }
            if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
                  &&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
               //客户预付有剩余 没有预付款的合同
               Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
               //保存明细
               saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
            }
         }
//         if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
//            if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
//               //余额 放到付款未完成合同 预收
//               Contract lastContract = payNoCompleteContractList.get(0);
//               //保存明细
//               saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
//            }
//            if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
//               //客户预付有剩余 没有预付款的合同
//               Contract lastContract = advanceContractList.get(0);
//               //保存明细
//               saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
//            }
//            if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
//                  &&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
//               //客户预付有剩余 没有预付款的合同
//               Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
//               //保存明细
//               saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
//
//            }
//         }
      }
      return R.ok();
   }
@@ -537,7 +564,7 @@
         return;
      }
      //保存付款单
      Boolean codeFlag = savePaymentSlip(customer,record);
      Boolean codeFlag = savePaymentSlip(customer,record,dept);
      if (codeFlag){
         log.error("付款单id:{},付款单已存在");
         return;
@@ -555,7 +582,9 @@
      List<Contract> advanceContractList = new ArrayList<>();
      //付款时间
      Date payDate = DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT);
      for (Contract contract : contractList) {
      Iterator<Contract> iterator = contractList.iterator();
      while (iterator.hasNext()) {
         Contract contract = iterator.next();
         PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
         BigDecimal lastTotal = new BigDecimal("0");
@@ -587,9 +616,16 @@
         entity.setPayType(record.get("bodyItemSettleModeName") != null ? record.getString("bodyItemSettleModeName").contains("承兑汇票") ? 1 : 0 : null);
         entity.setBipId(record.getString("id"));
         if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
            entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
            entity.setTotalAmount(new BigDecimal("0"));
            entity.setConfirmTime(payDate);
            // 判断是否是最后一个
            if (!iterator.hasNext()) {
               entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
               entity.setAdvanceAmount(transtionRemainAmount);
               entity.setTotalAmount(transtionRemainAmount);
            }else {
               entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
               entity.setTotalAmount(new BigDecimal("0"));
            }
            baseMapper.insert(entity);
         } else if (transtionRemainAmount.compareTo(new BigDecimal("0")) == 0) {
            entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
@@ -609,7 +645,7 @@
      payContractList.stream().forEach(contract -> {
         //更新付款阶段付款
         List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
               .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
               .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2).ne(ContractPaymentSchedule::getPaymentStatus, 3)
               .orderByAsc(ContractPaymentSchedule::getCreateTime));
         if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
            BigDecimal remain = transtionAmount;
@@ -663,55 +699,63 @@
         }
         //合同所有付款阶段
         List<ContractPaymentSchedule> payComScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
               .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0)
               .eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0).ne(ContractPaymentSchedule::getPaymentStatus,3)
         );
         if (ArrayUtil.isNotEmpty(payComScheduleList.toArray())){
            BigDecimal payTotal = payComScheduleList.stream().map(item->StrUtil.equals(item.getPaymentStatus() + "", "0") ? item.getPlannedAmount() :
                  item.getActualAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
            contract.setPaidAmount(payTotal);
            if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
               contract.setContractStatus(3);
            }
            contractService.updateById(contract);
//            if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
//               contract.setContractStatus(3);
//            }
//            contractService.updateById(contract);
//            if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
//               payCompleteContractList.add(contract);
//            }
            if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
               contract.setBillingStatus("2");
               contract.setContractStatus(3);
               payCompleteContractList.add(contract);
            }
            //付款 且 付款未全付
            if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
               payNoCompleteContractList.add(contract);
            }
            contractService.updateById(contract);
         }
      });
      //客户预付有剩余 且 有预付款的合同
      if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
         if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
            //余额 放到付款未完成合同 预收
            Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
            //保存明细
            saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
         }
         if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
            //客户预付有剩余 没有预付款的合同
            Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
            //保存明细
            saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
         }
         if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
               &&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
            //客户预付有剩余 没有预付款的合同
            Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
            //保存明细
            saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
         }
      }
//      if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
//         if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
//            //余额 放到付款未完成合同 预收
//            Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
//            //保存明细
//            saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
//         }
//         if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
//            //客户预付有剩余 没有预付款的合同
//            Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
//            //保存明细
//            saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
//         }
//         if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
//               &&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
//            //客户预付有剩余 没有预付款的合同
//            Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
//            //保存明细
//            saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
//
//         }
//      }
   }
   private Boolean savePaymentSlip(BusinessCustomer customer, JSONObject record) {
   private Boolean savePaymentSlip(BusinessCustomer customer, JSONObject record,SysDept dept) {
      PaymentSlip slip = new PaymentSlip();
      slip.setBusGuestId(customer.getId());
      slip.setBusGuestName(customer.getCompanyName());
      slip.setPayee(dept.getName());
      slip.setPayeeCompId(dept.getDeptId());
      slip.setPaymentTime(ObjUtil.isNotNull(record.get("billDate"))?
            DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT):new Date());
      slip.setPaymentAmount(ObjUtil.isNotNull(record.get("bodyItemOriTaxExcludedAmount"))?record.getBigDecimal("bodyItemOriTaxExcludedAmount"):new BigDecimal("0"));