shiyunteng
2026-10-30 145580fd820bb31334f2e71643413e846b7ce904
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
@@ -18,6 +18,7 @@
import com.by4cloud.platformx.common.security.util.SecurityUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@@ -35,6 +36,7 @@
 * @author syt
 * @date 2026-04-29 11:32:05
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OutBoundServiceImpl extends ServiceImpl<OutBoundMapper, OutBound> implements OutBoundService {
@@ -43,7 +45,7 @@
   private final ContractSubjectMatterMapper contractSubjectMatterMapper;
   private final PaymentConfirmMapper paymentConfirmMapper;
   private final ContractMapper contractMapper;
   private final ContractPaymentScheduleProcessMapper scheduleProcessMapper;
   private final ProductMapper productMapper;
   private final ContractOutBoundMapper contractOutBoundMapper;
   private final DeliveryOverdueMapper deliveryOverdueMapper;
   private final StringRedisTemplate redisTemplate;
@@ -57,6 +59,9 @@
   @Value("${dept.smjzt}")
   private String smjzt;
   @Value("${dept.smjhw}")
   private String smjhw;
   @Value("${dept.sgb}")
   private String sgb;
@@ -111,7 +116,8 @@
      addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
         OutBound outBound = BeanUtil.copyProperties(outSubjectMatterAddDTO, OutBound.class);
         if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
               StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)) {
               StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)||
               StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
            outBound.setBatchNumber(generateSMJContractCKNo());
         }
         if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
@@ -337,18 +343,6 @@
//                     beforeSchedule.setEffectiveEndDate(schedule.getEffectiveDate());
//                     scheduleMapper.updateById(beforeSchedule);
//                  }
                  //更新履约
                  ContractPaymentScheduleProcess contractPaymentScheduleProcess = new ContractPaymentScheduleProcess();
                  contractPaymentScheduleProcess.setContractId(schedule.getContractId());
                  contractPaymentScheduleProcess.setContractName(schedule.getContractName());
                  contractPaymentScheduleProcess.setScheduleId(schedule.getId());
                  contractPaymentScheduleProcess.setScheduleName(schedule.getStageName());
                  contractPaymentScheduleProcess.setProcessDate(addDTO.getOutBoundTime());
                  ContractPaymentScheduleProcess lastProcess = scheduleProcessMapper.selectOne(Wrappers.<ContractPaymentScheduleProcess>lambdaQuery().eq(ContractPaymentScheduleProcess::getContractId, schedule.getContractId())
                        .eq(ContractPaymentScheduleProcess::getScheduleId, schedule.getId()).last("limit 1"));
                  if (ObjUtil.isNull(lastProcess)) {
                     scheduleProcessMapper.insert(contractPaymentScheduleProcess);
                  }
               }
               //查询是否有后续阶段
@@ -437,6 +431,7 @@
      contractOutBound.setLineNo(subjectMatter.getLineNo());
      contractOutBound.setInvoiceStatus("0");
      contractOutBound.setInvoiceNum(new BigDecimal("0"));
      contractOutBound.setUnitPrice(subjectMatter.getUnitPrice());
      contractOutBoundMapper.insert(contractOutBound);
      if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
         // 1. 确定用于比较的基准日期(优先级:计划交货日期 > 合同到期日期)
@@ -473,54 +468,59 @@
   public R addContractOut(OutBoundAddDTO addDTO) {
      //校验参数
      R<Contract> r = vailParam(addDTO);
      if (!r.isOk()){
      if (!r.isOk()) {
         return r;
      }
      Contract contract = r.getData();
      String batchNumber = genBatchNumber();
      //出库
      handleOutBound(contract,addDTO);
      handleOutBound(batchNumber,contract, addDTO);
      //保存资金明细
      BigDecimal outBoundAmount = savePaymentConfirm(contract,addDTO);
      BigDecimal outBoundAmount = savePaymentConfirm(contract, addDTO);
      //更新合同状态及阶段
      contract.setOutBoundAmout(outBoundAmount);
      updateContractSchedule(contract);
      return R.ok();
      return R.ok(batchNumber);
   }
   private void updateContractSchedule(Contract contract) {
      List<ContractSubjectMatter> unComOutList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
            .eq(ContractSubjectMatter::getContractId, contract.getId())
            .ne(ContractSubjectMatter::getDeliveryStatus, 2));
      if (CollUtil.isEmpty(unComOutList)){
      if (CollUtil.isEmpty(unComOutList)) {
         contract.setNextScheduleName("货到签收");
      }
      contract.setBillingStatus("2");
      contractMapper.updateById(contract);
   }
   private BigDecimal savePaymentConfirm(Contract contract,OutBoundAddDTO addDTO) {
   private BigDecimal savePaymentConfirm(Contract contract, OutBoundAddDTO addDTO) {
      //查询合同是否有出库阶段
      ContractPaymentSchedule schedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId())
            .eq(ContractPaymentSchedule::getStageName, "发货前").gt(ContractPaymentSchedule::getPaymentRatio,0).last("limit 1"));
      if (ObjUtil.isNull(schedule.getEffectiveDate())&&ObjUtil.isNull(schedule.getEffectiveEndDate())){
            .eq(ContractPaymentSchedule::getStageName, "发货前").last("limit 1"));
      if (ObjUtil.isNull(schedule.getEffectiveDate()) && ObjUtil.isNull(schedule.getEffectiveEndDate())) {
         schedule.setEffectiveDate(addDTO.getOutBoundTime());
         schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(),schedule.getAgreedDays()));
         schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
         scheduleMapper.updateById(schedule);
      }
      //履约几率
      processService.saveScheduleProcess(addDTO.getOutBoundTime(),schedule);
      BigDecimal outAmount = new BigDecimal("0");
      //计算出库价格
      BigDecimal outAmount = new BigDecimal("0");
      for (OutSubjectMatterAddDTO outSubjectMatterAddDTO : addDTO.getSubjectMatterList()) {
         ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
               .eq(ContractSubjectMatter::getContractId, contract.getId())
               .eq(ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getLineNo()),ContractSubjectMatter::getLineNo, outSubjectMatterAddDTO.getLineNo())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getDataId()),ContractSubjectMatter::getDataId, outSubjectMatterAddDTO.getDataId())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getSubjectMatterCode()),ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getLineNo()), ContractSubjectMatter::getLineNo, outSubjectMatterAddDTO.getLineNo())
               .last("limit 1"));
         outAmount = outAmount.add(outSubjectMatterAddDTO.getOutBoundNum().multiply(subjectMatter.getUnitPrice()));
      }
      //存在发货前收款阶段
      if (ObjUtil.isNotNull(schedule)) {
      if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
         //新增当前阶段应收
         PaymentConfirm currentConfim = new PaymentConfirm();
         currentConfim.setBusinessType(schedule.getStageName() + "应收");
@@ -533,7 +533,7 @@
         currentConfim.setScheduleName(schedule.getStageName());
         currentConfim.setConfirmTime(addDTO.getOutBoundTime());
         //按出库比例计算应收
         currentConfim.setTransationAmount(schedule.getPlannedAmount().multiply(outAmount.divide(contract.getAmount(),2,RoundingMode.HALF_UP)));
         currentConfim.setTransationAmount(schedule.getPlannedAmount().multiply(outAmount.divide(contract.getAmount(), 2, RoundingMode.HALF_UP)));
         currentConfim.setReceivableAmount(currentConfim.getTransationAmount());
         PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
@@ -547,16 +547,17 @@
         currentConfim.setTotalAmount(currentConfim.getTransationAmount().multiply(new BigDecimal("-1")));
         paymentConfirmMapper.insert(currentConfim);
         //历史资金余额
         processService.handleBeforeRemain(currentConfim,contract);
         processService.handleBeforeRemain(currentConfim, contract);
      }
      return outAmount;
   }
   private void handleOutBound(Contract contract, OutBoundAddDTO addDTO) {
   private void handleOutBound(String batchNumber,Contract contract, OutBoundAddDTO addDTO) {
      addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
         ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
               .eq(ContractSubjectMatter::getContractId, contract.getId())
               .eq(ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getDataId()),ContractSubjectMatter::getDataId, outSubjectMatterAddDTO.getDataId())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getSubjectMatterCode()),ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .ne(ContractSubjectMatter::getDeliveryStatus, 2)
               .eq(StrUtil.isNotEmpty(outSubjectMatterAddDTO.getLineNo()), ContractSubjectMatter::getLineNo, outSubjectMatterAddDTO.getLineNo())
               .last("limit 1"));
@@ -580,8 +581,7 @@
         subjectMatter.setActualDeliveryDate(addDTO.getOutBoundTime());
         contractSubjectMatterMapper.updateById(subjectMatter);
         //保存erp出库
         String batchNumber = genBatchNumber();
         Long outBoundId = saveErpOutBound(contract,addDTO,outSubjectMatterAddDTO,batchNumber);
         Long outBoundId = saveErpOutBound(contract, addDTO, outSubjectMatterAddDTO, batchNumber);
         //保存合同出库记录
         saveContractOutBound(contract, addDTO, subjectMatter, outBoundId);
      });
@@ -589,7 +589,8 @@
   private String genBatchNumber() {
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)) {
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
         return generateSMJContractCKNo();
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
@@ -607,7 +608,7 @@
      return "";
   }
   private Long saveErpOutBound(Contract contract, OutBoundAddDTO addDTO,OutSubjectMatterAddDTO outSubjectMatterAddDTO,String batchNumber) {
   private Long saveErpOutBound(Contract contract, OutBoundAddDTO addDTO, OutSubjectMatterAddDTO outSubjectMatterAddDTO, String batchNumber) {
      OutBound outBound = new OutBound();
      outBound.setBatchNumber(batchNumber);
      outBound.setOutBoundNum(outSubjectMatterAddDTO.getOutBoundNum());
@@ -618,6 +619,22 @@
      outBound.setOutBoundTime(ObjUtil.isNotNull(addDTO.getOutBoundTime()) ? addDTO.getOutBoundTime() : new Date());
      outBound.setSubjectMatterCode(outSubjectMatterAddDTO.getSubjectMatterCode());
      outBound.setSubjectMatterName(outSubjectMatterAddDTO.getSubjectMatterName());
      List<String> smjList = new ArrayList<>();
      smjList.add(smj);
      smjList.add(smjhw);
      if (("" + SecurityUtils.getUser().getCompId()).equals(smj)||("" + SecurityUtils.getUser().getCompId()).equals(smjhw)) {
         Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .in(Product::getCompId, smjList).last("limit 1"));
         if (ObjUtil.isNotNull(product) ) {
            outBound.setSubjectMatterName(outSubjectMatterAddDTO.getSubjectMatterName());
         }
      } else {
         Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
         if (ObjUtil.isNotNull(product) ) {
            outBound.setSubjectMatterName(outSubjectMatterAddDTO.getSubjectMatterName());
         }
      }
      outBound.setOutBoundType("1");
      if (StrUtil.isEmpty(addDTO.getOutBoundType())) {
         outBound.setOutBoundType("0");
@@ -629,6 +646,10 @@
   private R<Contract> vailParam(OutBoundAddDTO addDTO) {
      if (CollUtil.isEmpty(addDTO.getSubjectMatterList())) {
         return R.failed("出库标的物不能为空");
      }
      if (addDTO.getSubjectMatterList().stream().anyMatch(contractSubjectMatterAddDTO -> StrUtil.isBlank(contractSubjectMatterAddDTO.getDataId())&&
            StrUtil.isBlank(contractSubjectMatterAddDTO.getSubjectMatterCode()))){
         return R.failed("标的物的dataId和materialCode不能同时为空");
      }
      // 1. 确定查询用的合同编号字段值
      String targetContractNo = addDTO.getContractNo();
@@ -661,7 +682,8 @@
      for (OutSubjectMatterAddDTO outSubjectMatterAddDTO : addDTO.getSubjectMatterList()) {
         ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
               .eq(ContractSubjectMatter::getContractId, contract.getId())
               .eq(ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getDataId()),ContractSubjectMatter::getDataId, outSubjectMatterAddDTO.getDataId())
               .eq(StrUtil.isNotBlank(outSubjectMatterAddDTO.getSubjectMatterCode()),ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
               .eq(StrUtil.isNotEmpty(outSubjectMatterAddDTO.getLineNo()), ContractSubjectMatter::getLineNo, outSubjectMatterAddDTO.getLineNo())
               .last("limit 1"));
         if (ObjUtil.isNull(subjectMatter)) {
@@ -837,4 +859,33 @@
      baseMapper.delete(Wrappers.<OutBound>lambdaQuery().eq(OutBound::getBatchNumber, batchNumber));
      return R.ok();
   }
   @Override
   public R addContractOutSgb(OutBoundAddDTO addDTO) {
      String batchNumber = "";
      if (StrUtil.isNotBlank(addDTO.getOutBoundCode())){
         batchNumber = addDTO.getOutBoundCode();
      }else {
         batchNumber = genBatchNumber();
      }
      log.info("出库单号:{}",batchNumber);
      //校验参数
      R<Contract> r = vailParam(addDTO);
      if (!r.isOk()) {
         return r;
      }
      Contract contract = r.getData();
      //出库
      handleOutBound(batchNumber,contract, addDTO);
      //保存资金明细
      BigDecimal outBoundAmount = savePaymentConfirm(contract, addDTO);
      //更新合同状态及阶段
      contract.setOutBoundAmout(outBoundAmount);
      updateContractSchedule(contract);
      return R.ok(batchNumber);
   }
}