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/ContractServiceImpl.java |  115 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 103 insertions(+), 12 deletions(-)

diff --git a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
index 9f9e4bb..82307fb 100644
--- a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
+++ b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -7,6 +7,8 @@
 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.api.feign.RemoteFlowProcessService;
+import com.by4cloud.platformx.business.constant.FlowNameEnum;
 import com.by4cloud.platformx.business.dto.ContractAddDTO;
 import com.by4cloud.platformx.business.dto.ContractUpdateDTO;
 import com.by4cloud.platformx.business.entity.*;
@@ -16,6 +18,8 @@
 import com.by4cloud.platformx.business.vo.ContractDetailVo;
 import com.by4cloud.platformx.common.core.util.R;
 import com.by4cloud.platformx.common.security.util.SecurityUtils;
+import com.by4cloud.platformx.flow.task.api.feign.RemoteBusinessService;
+import com.by4cloud.platformx.flow.task.dto.ProcessInstanceParamDto;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
@@ -23,6 +27,7 @@
 import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -39,6 +44,7 @@
 	private final ContractPaymentScheduleProcessMapper contractPaymentScheduleProcessMapper;
 	private final PaymentConfirmMapper paymentConfirmMapper;
 	private final CurrentOverdueMapper currentOverdueMapper;
+	private final RemoteFlowProcessService remoteFlowProcessService;
 
 	@Override
 	public R add(ContractAddDTO addDTO) {
@@ -114,6 +120,14 @@
 		}
 		Contract contract = BeanUtil.copyProperties(updateDTO,Contract.class);
 		contract.setContractNo(ContractNumberGenerator.generateContractNumber());
+		contract.setBillingStatus("0");
+		contract.setErpPushFlag("0");
+		List<Contract> contracts;
+		contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,contract.getContractNo()));
+		while (ArrayUtil.isNotEmpty(contracts.toArray())){
+			contract.setContractNo(ContractNumberGenerator.generateContractNumber());
+			contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,contract.getContractNo()));
+		}
 		baseMapper.updateById(contract);
 
 		if (ArrayUtil.isNotEmpty(updateDTO.getContractSubjectMatter())){
@@ -135,12 +149,20 @@
 			AtomicInteger index = new AtomicInteger(1);
 			updateDTO.getContractPaymentSchedule().stream().forEach(contractPaymentScheduleAddDTO -> {
 				int currentIndex = index.getAndIncrement();
-				ContractPaymentSchedule subjectMatter = BeanUtil.copyProperties(contractPaymentScheduleAddDTO, ContractPaymentSchedule.class);
-				subjectMatter.setContractId(contract.getId());
-				subjectMatter.setContractName(contract.getContractName());
-				subjectMatter.setPlannedAmount(contract.getAmount().multiply(subjectMatter.getPaymentRatio().divide(new BigDecimal("100"))));
-				subjectMatter.setStageOrder(currentIndex);
-				contractPaymentScheduleMapper.insert(subjectMatter);
+				ContractPaymentSchedule schedule = BeanUtil.copyProperties(contractPaymentScheduleAddDTO, ContractPaymentSchedule.class);
+				schedule.setContractId(contract.getId());
+				schedule.setContractName(contract.getContractName());
+				schedule.setPlannedAmount(contract.getAmount().multiply(schedule.getPaymentRatio().divide(new BigDecimal("100"))));
+				schedule.setStageOrder(currentIndex);
+				contractPaymentScheduleMapper.insert(schedule);
+				if (contractPaymentScheduleAddDTO.getStageName().equals("璐у埌绛炬敹")){
+					contract.setArrivalScheduleId(schedule.getId());
+					baseMapper.updateById(contract);
+				}
+				if (contractPaymentScheduleAddDTO.getStageName().equals("璋冭瘯瀹屾垚鎴栭獙鏀�")){
+					contract.setAcceptScheduleId(schedule.getId());
+					baseMapper.updateById(contract);
+				}
 			});
 
 		}
@@ -160,7 +182,23 @@
 	}
 
 	@Override
-	public Boolean takeEffect(Long id) {
+	public R startApproval(Long id) {
+		Contract contract = baseMapper.selectById(id);
+		//鍚姩娴佺▼
+		ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
+		Map<String, Object> map = BeanUtil.beanToMap(contract);
+		dto.setParamMap(map);
+		dto.setFlowName(FlowNameEnum.鍚堝悓瀹℃壒.name());
+		R r1 = remoteFlowProcessService.startProcessInstance(dto);
+		if (r1.getCode() == 1) {
+			return R.failed("娴佺▼鍚姩澶辫触");
+		}
+		contract.setContractStatus(2);
+		baseMapper.updateById(contract);
+		return R.ok();
+	}
+
+	public void takeEffect(Long id) {
 		Contract contract = baseMapper.selectById(id);
 		contract.setContractStatus(1);
 		baseMapper.updateById(contract);
@@ -181,6 +219,13 @@
 			paymentConfirm.setTransationAmount(fitstSchedule.getPlannedAmount());
 			paymentConfirm.setReceivableAmount(fitstSchedule.getPlannedAmount());
 			paymentConfirm.setTotalAmount(fitstSchedule.getPlannedAmount().multiply(new BigDecimal("-1")));
+			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();
+			}
+			paymentConfirm.setTotalAmount(lastNewTotal.subtract(paymentConfirm.getReceivableAmount()));
 			paymentConfirmMapper.insert(paymentConfirm);
 			//鏂板鍚堝悓灞ョ害璁板綍
 			ContractPaymentScheduleProcess process = new ContractPaymentScheduleProcess();
@@ -190,6 +235,49 @@
 			process.setScheduleName(fitstSchedule.getStageName());
 			process.setProcessDate(contract.getSignDate());
 			contractPaymentScheduleProcessMapper.insert(process);
+			//褰撳墠涓哄悎鍚屾渶鍚庨樁娈�
+			PaymentConfirm newConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
+					.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
+			if (newConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>=0){
+				//鏈夐浠樹笖瓒呰繃搴旀敹 鍏抽棴鍚堝悓鐘舵��
+				contract.setContractStatus(3);
+				baseMapper.updateById(contract);
+			}
+		}else if (fitstSchedule.getStageName().equals("鍙戣揣鍓�")) {
+			//鏇存柊鍚堝悓涓嬩釜闃舵
+			contract.setNextScheduleName("鏃�");
+			baseMapper.updateById(contract);
+		}else if (fitstSchedule.getStageName().equals("璐у埌绛炬敹")) {
+			//鏇存柊鍚堝悓涓嬩釜闃舵
+			contract.setNextScheduleName("璐у埌绛炬敹");
+			baseMapper.updateById(contract);
+		}else if (fitstSchedule.getStageName().equals("璋冭瘯瀹屾垚鎴栭獙鏀�")) {
+			//鏇存柊鍚堝悓涓嬩釜闃舵
+			contract.setNextScheduleName("璋冭瘯瀹屾垚鎴栭獙鏀�");
+			baseMapper.updateById(contract);
+		}else {
+			//鏂板搴旀敹
+			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(fitstSchedule.getId());
+			paymentConfirm.setScheduleName(fitstSchedule.getStageName());
+			paymentConfirm.setConfirmTime(new Date());
+			paymentConfirm.setTransationAmount(fitstSchedule.getPlannedAmount());
+			paymentConfirm.setReceivableAmount(fitstSchedule.getPlannedAmount());
+			paymentConfirm.setTotalAmount(fitstSchedule.getPlannedAmount().multiply(new BigDecimal("-1")));
+			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();
+			}
+			paymentConfirm.setTotalAmount(lastNewTotal.subtract(paymentConfirm.getReceivableAmount()));
+			paymentConfirmMapper.insert(paymentConfirm);
 		}
 		//鏌ヨ鏄惁鏈夊悗缁樁娈�
 		List<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
@@ -204,7 +292,7 @@
 				endSchedule.setEffectiveEndDate(contract.getExpirationDate());
 				contractPaymentScheduleMapper.updateById(endSchedule);
 				//绗竴闃舵鐢熸晥鏃堕棿
-				fitstSchedule.setEffectiveDate(DateUtil.offsetDay(new Date(),fitstSchedule.getAgreedDays()));
+				fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
 				fitstSchedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
 				contractPaymentScheduleMapper.updateById(fitstSchedule);
 
@@ -235,18 +323,19 @@
 			baseMapper.updateById(contract);
 		}
 		if(ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()>1){
+			//绗竴闃舵鐢熸晥鏃堕棿
+			fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
+			contractPaymentScheduleMapper.updateById(fitstSchedule);
 			//鏇存柊鍚堝悓涓嬩釜闃舵
 			contract.setNextScheduleName(afterSchedule.get(0).getStageName());
 			baseMapper.updateById(contract);
 		}
+		//鍙湁褰撳墠鍚堝悓绛惧瓧闃舵
 		if(ArrayUtil.isEmpty(afterSchedule.toArray())){
+			fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
 			fitstSchedule.setEffectiveEndDate(contract.getExpirationDate());
 			contractPaymentScheduleMapper.updateById(fitstSchedule);
-			//鏇存柊鍚堝悓涓嬩釜闃舵
-			contract.setNextScheduleName("鏃�");
-			baseMapper.updateById(contract);
 		}
-		return Boolean.TRUE;
 	}
 
 	@Override
@@ -281,6 +370,7 @@
 				PaymentConfirm oiverdueConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getScheduleId, contractPaymentSchedule.getId())
 						.eq(PaymentConfirm::getBusinessType, "搴旀敹瓒呮湡"));
 				if (ObjUtil.isNull(oiverdueConfirm)) {
+					overdueConfirm.setCompId(contractPaymentSchedule.getCompId());
 					paymentConfirmMapper.insert(overdueConfirm);
 				}
 				//褰撳墠閫炬湡
@@ -299,6 +389,7 @@
 				CurrentOverdue overdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getContractId,currentOverdue.getContractId())
 						.eq(CurrentOverdue::getScheduleId,currentOverdue.getScheduleId()).last("limit 1"));
 				if (ObjUtil.isNull(overdue)){
+					currentOverdue.setCompId(contractPaymentSchedule.getCompId());
 					currentOverdueMapper.insert(currentOverdue);
 				}else {
 					overdue.setOverdueDuration(BigDecimal.valueOf(DateUtil.betweenDay(contractPaymentSchedule.getEffectiveEndDate(),new Date(),true)));

--
Gitblit v1.9.1