From fca21683e1b5b906d2514082ddfbae8eb820c9ea Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期五, 15 五月 2026 17:12:55 +0800
Subject: [PATCH] feat:合同模版、出库标的物、合同管理拆分
---
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java | 315 +++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 309 insertions(+), 6 deletions(-)
diff --git a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
index e8e8581..252dee9 100644
--- a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
+++ b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java
@@ -1,12 +1,26 @@
package com.by4cloud.platformx.business.service.impl;
+import cn.hutool.core.bean.BeanUtil;
+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.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.by4cloud.platformx.business.entity.OutBound;
-import com.by4cloud.platformx.business.mapper.OutBoundMapper;
+import com.by4cloud.platformx.business.dto.OutBoundAddDTO;
+import com.by4cloud.platformx.business.dto.OutSubjectMatterAddDTO;
+import com.by4cloud.platformx.business.entity.*;
+import com.by4cloud.platformx.business.mapper.*;
import com.by4cloud.platformx.business.service.OutBoundService;
+import com.by4cloud.platformx.common.core.util.R;
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
+import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
-import java.util.Date;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* EPR鍑哄簱璁板綍
@@ -15,11 +29,300 @@
* @date 2026-04-29 11:32:05
*/
@Service
+@RequiredArgsConstructor
public class OutBoundServiceImpl extends ServiceImpl<OutBoundMapper, OutBound> implements OutBoundService {
+ private final ContractPaymentScheduleMapper scheduleMapper;
+ private final ContractSubjectMatterMapper contractSubjectMatterMapper;
+ private final PaymentConfirmMapper paymentConfirmMapper;
+ private final ContractMapper contractMapper;
+ private final ContractPaymentScheduleProcessMapper scheduleProcessMapper;
+ private final ContractOutBoundMapper contractOutBoundMapper;
+
@Override
- public boolean save(OutBound entity) {
- entity.setOutBoundTime(new Date());
- return super.save(entity);
+ public R add(OutBoundAddDTO addDTO) {
+ if (ArrayUtil.isEmpty(addDTO.getSubjectMatterList().toArray())) {
+ return R.failed("鍑哄簱鏍囩殑鐗╀笉鑳戒负绌�");
+ }
+ MPJLambdaWrapper<ContractSubjectMatter> wrapper = new MPJLambdaWrapper<ContractSubjectMatter>()
+ .selectAll(ContractSubjectMatter.class)
+ .ne(ContractSubjectMatter::getDeliveryStatus, 2)
+ .in(ContractSubjectMatter::getMaterialCode, addDTO.getSubjectMatterList().stream().map(item -> item.getSubjectMatterCode()).collect(Collectors.toList()))
+ .exists(Contract.class, contractQuery ->
+ contractQuery.select(ContractSubjectMatter::getId)
+ .eq(Contract::getId, ContractSubjectMatter::getContractId)
+ .eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), Contract::getPartyA, addDTO.getBusGuestName())
+ .eq(ObjUtil.isNotNull(addDTO.getBusGuestId()), Contract::getPartyAId, addDTO.getBusGuestId())
+ )
+ .orderByAsc(ContractSubjectMatter::getCreateTime);
+ List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(wrapper);
+ if (ArrayUtil.isEmpty(subjectMatterList.toArray())) {
+ return R.failed("娌℃湁鏌ヨ鍒扮浉鍏冲悎鍚岃鍗�");
+ }
+ Double outTotal = subjectMatterList.stream().mapToDouble(item -> {
+ // 浼樺厛鍒ゆ柇 remainNum
+ if (item.getRemainingQuantity() != null && item.getRemainingQuantity().compareTo(new BigDecimal("0")) > 0) {
+ return item.getRemainingQuantity().doubleValue();
+ } else {
+ // remainNum 涓� null 鎴� 0 鏃讹紝浣跨敤 quantity
+ return item.getQuantity() == null ? 0.0 : item.getQuantity().doubleValue();
+ }
+ }).sum();
+ BigDecimal orderNum = addDTO.getSubjectMatterList().stream().map(OutSubjectMatterAddDTO::getOutBoundNum)
+ .filter(num -> num != null) // 杩囨护鎺� null 鍊硷紝闃叉 NullPointerException
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+ if (new BigDecimal(outTotal).compareTo(orderNum) < 0) {
+ return R.failed("鍑哄簱鏁伴噺瓒呭嚭鍚堝悓璁㈠崟鏁伴噺");
+ }
+ //褰撳墠鍏ュ簱鏍囩殑鐗�
+ Map<Long, Object> currentOutMap = new HashMap<>();
+ //鍑哄簱
+ addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
+ BigDecimal remainNum = outSubjectMatterAddDTO.getOutBoundNum();
+ for (ContractSubjectMatter subjectMatter : subjectMatterList) {
+ if (outSubjectMatterAddDTO.getSubjectMatterCode().equals(subjectMatter.getMaterialCode())) {
+ Contract contract = contractMapper.selectById(subjectMatter.getContractId());
+ if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0")) > 0) {
+ remainNum = remainNum.subtract(subjectMatter.getRemainingQuantity());
+ //閮ㄥ垎浜や粯鐘舵�佷笅 鍏ㄩ儴鍑哄簱
+ if (remainNum.compareTo(new BigDecimal("0")) >= 0) {
+ BigDecimal currentOut = subjectMatter.getRemainingQuantity();
+ subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity());
+ subjectMatter.setRemainingQuantity(new BigDecimal("0"));
+ subjectMatter.setActualDeliveryDate(DateUtil.today());
+ subjectMatter.setDeliveryStatus(2);
+ subjectMatter.setLastDeliveredQuantity(currentOut);
+ contractSubjectMatterMapper.updateById(subjectMatter);
+ currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
+ //鏂板鍚堝悓鍑哄簱鍘嗗彶
+ saveContractOutBound(contract, addDTO, subjectMatter);
+ } else {
+ BigDecimal currentOut = remainNum.add(subjectMatter.getRemainingQuantity());
+ subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum));
+ subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(currentOut));
+ subjectMatter.setActualDeliveryDate(DateUtil.today());
+ subjectMatter.setDeliveryStatus(1);
+ subjectMatter.setLastDeliveredQuantity(currentOut);
+ contractSubjectMatterMapper.updateById(subjectMatter);
+ currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
+ //鏂板鍚堝悓鍑哄簱鍘嗗彶
+ saveContractOutBound(contract, addDTO, subjectMatter);
+ break;
+ }
+
+ } else {
+ //鏈氦浠樼姸鎬佷笅
+ remainNum = remainNum.subtract(subjectMatter.getQuantity());
+ if (remainNum.compareTo(new BigDecimal("0")) >= 0) {
+ subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity());
+ subjectMatter.setRemainingQuantity(new BigDecimal("0"));
+ subjectMatter.setActualDeliveryDate(DateUtil.today());
+ subjectMatter.setDeliveryStatus(2);
+ subjectMatter.setLastDeliveredQuantity(subjectMatter.getQuantity());
+ contractSubjectMatterMapper.updateById(subjectMatter);
+ currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
+ //鏂板鍚堝悓鍑哄簱鍘嗗彶
+ saveContractOutBound(contract, addDTO, subjectMatter);
+ } else {
+ BigDecimal currentOut = remainNum.add(subjectMatter.getQuantity());
+ subjectMatter.setDeliveredQuantity(subjectMatter.getQuantity().add(remainNum));
+ subjectMatter.setRemainingQuantity(remainNum.multiply(new BigDecimal("-1")));
+ subjectMatter.setActualDeliveryDate(DateUtil.today());
+ subjectMatter.setDeliveryStatus(1);
+ subjectMatter.setLastDeliveredQuantity(currentOut);
+ contractSubjectMatterMapper.updateById(subjectMatter);
+ currentOutMap.put(subjectMatter.getContractId(), subjectMatter);
+ //鏂板鍚堝悓鍑哄簱鍘嗗彶
+ saveContractOutBound(contract, addDTO, subjectMatter);
+ break;
+ }
+ }
+ }
+ }
+ OutBound outBound = BeanUtil.copyProperties(outSubjectMatterAddDTO, OutBound.class);
+ outBound.setBusGuestName(addDTO.getBusGuestName());
+ outBound.setBusGuestId(addDTO.getBusGuestId());
+ outBound.setOutBoundTime(addDTO.getOutBoundTime());
+ baseMapper.insert(outBound);
+ });
+
+ //鍑哄簱搴旀敹娆炬槑缁�
+ if (ArrayUtil.isNotEmpty(currentOutMap.keySet())) {
+ for (Long contractId : currentOutMap.keySet()) {
+ //褰撳墠鍑哄簱鍚堝悓鍑哄簱鏍囩殑鐗�
+ List<ContractSubjectMatter> contractSubjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
+ .eq(ContractSubjectMatter::getContractId, contractId).ne(ContractSubjectMatter::getDeliveryStatus, 0));
+ //鏌ヨ鍚堝悓鏄惁鏈夊嚭搴撻樁娈�
+ ContractPaymentSchedule schedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId)
+ .eq(ContractPaymentSchedule::getStageName, "鍙戣揣鍓�"));
+ //瀛樺湪鍑哄簱闃舵
+ if (ObjUtil.isNotNull(schedule)) {
+ //褰撳墠鍑哄簱閲戦
+ BigDecimal outPirce = contractSubjectMatterList.stream()
+ .map(item -> (item.getDeliveryStatus() == 1 ? item.getDeliveredQuantity().multiply(item.getUnitPrice()) : item.getQuantity().multiply(item.getUnitPrice())))
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+ //鍚堝悓閲戦
+ Contract contract = contractMapper.selectById(contractId);
+ //鍑哄簱鏄惁瀹屾垚
+ if (outPirce.compareTo(contract.getAmount()) < 0) {
+ contract.setBillingStatus("1");
+ contractMapper.updateById(contract);
+ }
+ if (outPirce.compareTo(contract.getAmount()) == 0) {
+ contract.setBillingStatus("2");
+ contractMapper.updateById(contract);
+ }
+ //璁$畻鍙戣揣鍓嶅簲鏀舵洿鏂拌祫閲戞槑缁�
+ PaymentConfirm paymentConfirm = new PaymentConfirm();
+ paymentConfirm.setBusinessType("鍙戣揣鍓嶅簲鏀�");
+ paymentConfirm.setBusGuestId(contract.getPartyAId());
+ paymentConfirm.setBusGuestName(contract.getPartyA());
+ paymentConfirm.setContractId(contract.getId());
+ paymentConfirm.setContractName(contract.getContractName());
+ paymentConfirm.setContractNo(contract.getContractNo());
+ paymentConfirm.setScheduleId(schedule.getId());
+ paymentConfirm.setScheduleName(schedule.getStageName());
+ paymentConfirm.setConfirmTime(addDTO.getOutBoundTime());
+ //鎸夊嚭搴撹祫閲戞瘮渚� 璁$畻鍙戣揣鍓嶅簲鏀�
+ paymentConfirm.setTransationAmount(outPirce.divide(contract.getAmount(), 10, RoundingMode.HALF_UP).multiply(schedule.getPlannedAmount()));
+ paymentConfirm.setReceivableAmount(paymentConfirm.getTransationAmount());
+
+ //鏈�杩戝簲鏀�
+ PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
+ .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
+
+ if (ObjUtil.isNotNull(lastConfirm)) {
+ //宸插瓨鍦ㄥ彂璐у簲鏀�
+ if (StrUtil.equals(lastConfirm.getBusinessType(), paymentConfirm.getBusinessType())) {
+
+ //鏌ヨ涔嬪墠鎵�鏈夊彂璐у墠搴旀敹
+ List<PaymentConfirm> fhConfirmList = paymentConfirmMapper.selectList(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
+ .eq(PaymentConfirm::getScheduleId,schedule.getId()).orderByDesc(PaymentConfirm::getCreateTime));
+ BigDecimal fhTotalAmount = fhConfirmList.stream().map(item->item.getTransationAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
+ //鏈鍙戣揣鍓嶅簲鏀� 鍑哄簱鎬婚 - 鎵�鏈夊彂璐у墠搴旀敹
+ paymentConfirm.setTransationAmount(paymentConfirm.getTransationAmount().subtract(fhTotalAmount));
+ paymentConfirm.setReceivableAmount(paymentConfirm.getTransationAmount());
+ paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount()));
+ paymentConfirmMapper.insert(paymentConfirm);
+ } else {
+ //涓嶅瓨鍦ㄥ彂璐у簲鏀� 鍑哄簱鎬婚
+ paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount()));
+ paymentConfirmMapper.insert(paymentConfirm);
+ }
+ } else {
+ //鏃犱箣鍓嶉樁娈靛簲鏀�
+ paymentConfirm.setTotalAmount(paymentConfirm.getTransationAmount().multiply(new BigDecimal("-1")));
+ paymentConfirmMapper.insert(paymentConfirm);
+ }
+
+ //鏇存柊鍙戣揣鍓嶆敹娆剧敓鏁堟椂闂�
+ if (ObjUtil.isNull(schedule.getEffectiveDate())) {
+ schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
+ //鏌ヨ鏄惁鏈夊悗缁樁娈�
+ List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contractId)
+ .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()));
+ if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
+ schedule.setEffectiveEndDate(contract.getExpirationDate());
+ }
+ scheduleMapper.updateById(schedule);
+ //鏌ヨ鏄惁鏈変箣鍓嶉樁娈�
+ ContractPaymentSchedule beforeSchedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
+ .lt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()).orderByDesc(ContractPaymentSchedule::getCreateTime).last("limit 1"));
+ if (ObjUtil.isNotNull(beforeSchedule)) {
+ 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());
+ scheduleProcessMapper.insert(contractPaymentScheduleProcess);
+ }
+
+ //鏌ヨ鏄惁鏈夊悗缁樁娈�
+ List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
+ .eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
+ .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
+ .orderByAsc(ContractPaymentSchedule::getCreateTime));
+ if (ArrayUtil.isNotEmpty(afterSchedule.toArray()) && afterSchedule.size() == 1) {
+ //鏈�鍚庨樁娈电敓鏁堟椂闂�
+ ContractPaymentSchedule endSchedule = afterSchedule.get(0);
+ if (StrUtil.equals(endSchedule.getStageName(), "璐ㄤ繚閲�")) {
+ endSchedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), endSchedule.getAgreedDays()));
+ endSchedule.setEffectiveEndDate(contract.getExpirationDate());
+ scheduleMapper.updateById(endSchedule);
+ //褰撳墠闃舵鐢熸晥鏃堕棿
+ schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getOutBoundTime(), schedule.getAgreedDays()));
+ schedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
+ scheduleMapper.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.getOutBoundTime());
+ 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()));
+ paymentConfirmMapper.insert(newConfim);
+ }
+ //鏇存柊鍚堝悓涓嬩釜闃舵
+ contract.setNextScheduleName(endSchedule.getStageName());
+ contractMapper.updateById(contract);
+ }
+ if (ArrayUtil.isNotEmpty(afterSchedule.toArray()) && afterSchedule.size() > 1) {
+ //鏇存柊鍚堝悓涓嬩釜闃舵
+ contract.setNextScheduleName(afterSchedule.get(0).getStageName());
+ contractMapper.updateById(contract);
+ }
+ if (ArrayUtil.isEmpty(afterSchedule.toArray())) {
+ schedule.setEffectiveEndDate(contract.getExpirationDate());
+ scheduleMapper.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);
+ }
+ }
+ }
+ }
+ }
+
+ return R.ok();
+ }
+
+ private void saveContractOutBound(Contract contract, OutBoundAddDTO addDTO, ContractSubjectMatter subjectMatter) {
+ ContractOutBound contractOutBound = new ContractOutBound();
+ contractOutBound.setContractId(contract.getId());
+ contractOutBound.setContractName(contract.getContractName());
+ contractOutBound.setBusGuestId(contract.getPartyAId());
+ contractOutBound.setBusGuestName(contract.getPartyA());
+ contractOutBound.setSubjectMatterCode(subjectMatter.getMaterialCode());
+ contractOutBound.setSubjectMatterName(subjectMatter.getMaterialInternalName());
+ contractOutBound.setOutBoundTime(addDTO.getOutBoundTime());
+ contractOutBound.setOutBoundNum(subjectMatter.getLastDeliveredQuantity());
+ contractOutBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
+ contractOutBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
+ contractOutBoundMapper.insert(contractOutBound);
}
}
\ No newline at end of file
--
Gitblit v1.9.1