platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
@@ -1,9 +1,8 @@
package com.by4cloud.platformx.business.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -27,6 +26,8 @@
import java.util.*;
import java.util.stream.Collectors;
;
/**
 * EPR出库记录
 *
@@ -49,6 +50,12 @@
   @Value("${dept.smj}")
   private String smj;
   @Value("${dept.smjsc}")
   private String smjsc;
   @Value("${dept.smjzt}")
   private String smjzt;
   @Value("${dept.sgb}")
   private String sgb;
@@ -63,7 +70,7 @@
   @Override
   public R add(OutBoundAddDTO addDTO) {
      if (ArrayUtil.isEmpty(addDTO.getSubjectMatterList().toArray())) {
      if (CollUtil.isEmpty(addDTO.getSubjectMatterList())) {
         return R.failed("出库标的物不能为空");
      }
      MPJLambdaWrapper<ContractSubjectMatter> wrapper = new MPJLambdaWrapper<ContractSubjectMatter>()
@@ -79,7 +86,7 @@
            )
            .orderByAsc(ContractSubjectMatter::getCreateTime);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(wrapper);
      if (ArrayUtil.isEmpty(subjectMatterList.toArray())) {
      if (CollUtil.isEmpty(subjectMatterList)) {
         return R.failed("没有查询到相关合同订单");
      }
      List<String> erpCodeList = subjectMatterList.stream().map(ContractSubjectMatter::getMaterialCode).collect(Collectors.toList());
@@ -101,7 +108,8 @@
      //出库
      addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
         OutBound outBound = BeanUtil.copyProperties(outSubjectMatterAddDTO, OutBound.class);
         if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)){
         if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)||StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smjsc)||
               StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smjzt)){
            outBound.setBatchNumber(generateSMJContractCKNo());
         }
         if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",sgb)){
@@ -185,7 +193,7 @@
      });
      //出库应收款明细
      if (ArrayUtil.isNotEmpty(currentOutMap.keySet())) {
      if (CollUtil.isNotEmpty(currentOutMap.keySet())) {
         for (Long contractId : currentOutMap.keySet()) {
            //当前出库合同出库标的物
            List<ContractSubjectMatter> contractSubjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
@@ -264,7 +272,7 @@
                     //客户付款完成合同
                     List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
                           .eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
                     if (ArrayUtil.isNotEmpty(customerCompleteContractList)) {
                     if (CollUtil.isNotEmpty(customerCompleteContractList)) {
                        List<BigDecimal> outAmountList = new ArrayList<>();
                        for (Contract completeContract : customerCompleteContractList
                        ) {
@@ -283,7 +291,7 @@
                              outAmountList.add(outConfirm.getTransationAmount());
                           }
                        }
                        if (ArrayUtil.isNotEmpty(outAmountList.toArray())) {
                        if (CollUtil.isNotEmpty(outAmountList)) {
                           BigDecimal currentInSum = outAmountList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
                           //新增当前合同 资金转入
                           PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm, PaymentConfirm.class, "id", "transationAmount",
@@ -316,7 +324,7 @@
                  //查询是否有后续阶段
//                  List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId)
//                        .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
//                  if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
//                  if (.isEmpty(afterSchedule)) {
//                     schedule.setEffectiveEndDate(contract.getExpirationDate());
//                  }
                  scheduleMapper.updateById(schedule);
@@ -346,7 +354,7 @@
                     .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
                     .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
                     .orderByAsc(ContractPaymentSchedule::getCreateTime));
               if (ArrayUtil.isNotEmpty(afterSchedule.toArray()) && afterSchedule.size() == 1) {
               if (CollUtil.isNotEmpty(afterSchedule) && afterSchedule.size() == 1) {
                  //最后阶段生效时间
                  ContractPaymentSchedule endSchedule = afterSchedule.get(0);
                  if (StrUtil.equals(endSchedule.getStageName(), "质保金")) {
@@ -383,12 +391,12 @@
                  contract.setNextScheduleName(endSchedule.getStageName());
                  contractMapper.updateById(contract);
               }
               if (ArrayUtil.isNotEmpty(afterSchedule.toArray()) && afterSchedule.size() > 1) {
               if (CollUtil.isNotEmpty(afterSchedule) && afterSchedule.size() > 1) {
                  //更新合同下个阶段
                  contract.setNextScheduleName(afterSchedule.get(0).getStageName());
                  contractMapper.updateById(contract);
               }
               if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
               if (CollUtil.isEmpty(afterSchedule)) {
//                  schedule.setEffectiveEndDate(contract.getExpirationDate());
//                  scheduleMapper.updateById(schedule);
                  //更新合同下个阶段
@@ -460,7 +468,7 @@
   @Override
   public R addContractOut(OutBoundAddDTO addDTO) {
      if (ArrayUtil.isEmpty(addDTO.getSubjectMatterList().toArray())) {
      if (CollUtil.isEmpty(addDTO.getSubjectMatterList())) {
         return R.failed("出库标的物不能为空");
      }
      // 1. 确定查询用的合同编号字段值
@@ -486,6 +494,10 @@
      if (ObjUtil.isNull(contract)) {
         return R.failed("合同信息查询失败");
      }
      if (addDTO.getSubjectMatterList().stream().allMatch(item->item.getOutBoundNum().compareTo(new BigDecimal("0"))==0)){
         return R.failed("出库数量不能都为为0");
      }
      addDTO.setSubjectMatterList(addDTO.getSubjectMatterList().stream().filter(item->item.getOutBoundNum().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList()));
      //出库数量校验
      for (OutSubjectMatterAddDTO outSubjectMatterAddDTO:   addDTO.getSubjectMatterList()) {
         ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
@@ -495,9 +507,6 @@
               .last("limit 1"));
         if (ObjUtil.isNull(subjectMatter)){
            continue;
         }
         if (outSubjectMatterAddDTO.getOutBoundNum().compareTo(new BigDecimal("0"))==0){
            return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode()+"出库数量不能为0");
         }
         if (subjectMatter.getDeliveryStatus()==0){
            if (outSubjectMatterAddDTO.getOutBoundNum().compareTo(subjectMatter.getQuantity())>0){
@@ -543,7 +552,8 @@
         contractSubjectMatterMapper.updateById(subjectMatter);
         //
         OutBound outBound = new OutBound();
         if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)){
         if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)||StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smjsc)||
               StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smjzt)){
            outBound.setBatchNumber(generateSMJContractCKNo());
         }
         if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",sgb)){
@@ -644,7 +654,7 @@
               //客户付款完成合同
               List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
                     .eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
               if (ArrayUtil.isNotEmpty(customerCompleteContractList)) {
               if (CollUtil.isNotEmpty(customerCompleteContractList)) {
                  List<BigDecimal> outAmountList = new ArrayList<>();
                  for (Contract completeContract : customerCompleteContractList
                  ) {
@@ -663,7 +673,7 @@
                        outAmountList.add(outConfirm.getTransationAmount());
                     }
                  }
                  if (ArrayUtil.isNotEmpty(outAmountList.toArray())) {
                  if (CollUtil.isNotEmpty(outAmountList)) {
                     BigDecimal currentInSum = outAmountList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
                     //新增当前合同 资金转入
                     PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm, PaymentConfirm.class, "id", "transationAmount",
@@ -712,12 +722,12 @@
               .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
               .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
               .orderByAsc(ContractPaymentSchedule::getCreateTime));
         if (ArrayUtil.isNotEmpty(afterSchedule.toArray())&&contract.getOutBoundAmout().compareTo(contract.getAmount())==0) {
         if (CollUtil.isNotEmpty(afterSchedule)&&contract.getOutBoundAmout().compareTo(contract.getAmount())==0) {
            contract.setNextScheduleName(afterSchedule.get(0).getStageName());
            contractMapper.updateById(contract);
         }
         if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
         if (CollUtil.isEmpty(afterSchedule)) {
            schedule.setEffectiveEndDate(contract.getExpirationDate());
            scheduleMapper.updateById(schedule);
            //更新合同下个阶段
@@ -832,12 +842,12 @@
   @Override
   public R delOutBound(String batchNumber) {
      List<OutBound> outBoundList = baseMapper.selectList(Wrappers.<OutBound>lambdaQuery().eq(OutBound::getBatchNumber,batchNumber));
      if (ArrayUtil.isEmpty(outBoundList)){
      if (CollUtil.isEmpty(outBoundList)){
         return R.failed("出库信息异常,请联系营销系统管理人员");
      }
      List<ContractOutBound> contractOutBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
            .in(ContractOutBound::getOutBoundId,outBoundList.stream().map(OutBound::getId).collect(Collectors.toList())));
      if (ArrayUtil.isEmpty(contractOutBoundList)){
      if (CollUtil.isEmpty(contractOutBoundList)){
         return R.failed("出库信息异常,请联系营销系统管理人员");
      }
      boolean allZero = true;