From b1b3da5393c75ced1afa1467be69d60af5403878 Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期四, 27 八月 2026 14:25:16 +0800
Subject: [PATCH] feat:合同审核、出库及履约阶段代码优化

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/OutBoundServiceImpl.java |  484 +++++++++++++++++++++++-----------------------------
 1 files changed, 215 insertions(+), 269 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 2a7809e..7443d48 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
@@ -12,6 +12,7 @@
 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.ContractPaymentScheduleProcessService;
 import com.by4cloud.platformx.business.service.OutBoundService;
 import com.by4cloud.platformx.common.core.util.R;
 import com.by4cloud.platformx.common.security.util.SecurityUtils;
@@ -46,9 +47,16 @@
 	private final ContractOutBoundMapper contractOutBoundMapper;
 	private final DeliveryOverdueMapper deliveryOverdueMapper;
 	private final StringRedisTemplate redisTemplate;
+	private final ContractPaymentScheduleProcessService processService;
 
 	@Value("${dept.smj}")
 	private String smj;
+
+	@Value("${dept.smjsc}")
+	private String smjsc;
+
+	@Value("${dept.smjzt}")
+	private String smjzt;
 
 	@Value("${dept.sgb}")
 	private String sgb;
@@ -102,19 +110,20 @@
 		//鍑哄簱
 		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)){
+			if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
 				outBound.setBatchNumber(generateGYBContractCKNo());
 			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",ymj)){
+			if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", ymj)) {
 				outBound.setBatchNumber(generateYMJContractCKNo());
 			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",tfgs)){
+			if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", tfgs)) {
 				outBound.setBatchNumber(generateTFContractCKNo());
 			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",jxc)){
+			if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", jxc)) {
 				outBound.setBatchNumber(generateJXCContractCKNo());
 			}
 			outBound.setBusGuestName(addDTO.getBusGuestName());
@@ -203,16 +212,16 @@
 					//鍚堝悓閲戦
 					Contract contract = contractMapper.selectById(contractId);
 					//鍑哄簱鏄惁瀹屾垚
-					if (outPirce.compareTo(contract.getBillingAmout())>0) {
+					if (outPirce.compareTo(contract.getBillingAmout()) > 0) {
 						contract.setBillingStatus("2");
 						contractMapper.updateById(contract);
 					}
-					if (outPirce.compareTo(contract.getBillingAmout())==0) {
+					if (outPirce.compareTo(contract.getBillingAmout()) == 0) {
 						contract.setBillingStatus("1");
 						contractMapper.updateById(contract);
 					}
-					if (outPirce.compareTo(contract.getAmount()) == 0&&
-							outPirce.compareTo(contract.getBillingAmout())==0) {
+					if (outPirce.compareTo(contract.getAmount()) == 0 &&
+							outPirce.compareTo(contract.getBillingAmout()) == 0) {
 						contract.setBillingStatus("3");
 						contractMapper.updateById(contract);
 					}
@@ -398,7 +407,8 @@
 						//褰撳墠涓哄悎鍚屾渶鍚庨樁娈�
 						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) {
+						if (lastNewConfirm.getTotalAmount().compareTo(new BigDecimal("0")) >= 0
+								&& contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
 							//鏈夐浠樹笖瓒呰繃搴旀敹 鍏抽棴鍚堝悓鐘舵��
 							contract.setContractStatus(3);
 							contractMapper.updateById(contract);
@@ -419,7 +429,7 @@
 		contractOutBound.setBusGuestName(contract.getPartyA());
 		contractOutBound.setSubjectMatterCode(subjectMatter.getMaterialCode());
 		contractOutBound.setSubjectMatterName(subjectMatter.getMaterialInternalName());
-		contractOutBound.setOutBoundTime(ObjUtil.isNotNull(addDTO.getOutBoundTime())?addDTO.getOutBoundTime():new Date());
+		contractOutBound.setOutBoundTime(ObjUtil.isNotNull(addDTO.getOutBoundTime()) ? addDTO.getOutBoundTime() : new Date());
 		contractOutBound.setOutBoundNum(subjectMatter.getLastDeliveredQuantity());
 		contractOutBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
 		contractOutBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
@@ -428,7 +438,7 @@
 		contractOutBound.setInvoiceStatus("0");
 		contractOutBound.setInvoiceNum(new BigDecimal("0"));
 		contractOutBoundMapper.insert(contractOutBound);
-		if (!StrUtil.equals(contract.getContractCategory(),"water_house")) {
+		if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
 			// 1. 纭畾鐢ㄤ簬姣旇緝鐨勫熀鍑嗘棩鏈燂紙浼樺厛绾э細璁″垝浜よ揣鏃ユ湡 > 鍚堝悓鍒版湡鏃ユ湡锛�
 			Date deadline = subjectMatter.getPlannedDeliveryDate();
 			if (ObjUtil.isNull(deadline)) {
@@ -461,6 +471,162 @@
 
 	@Override
 	public R addContractOut(OutBoundAddDTO addDTO) {
+		//鏍¢獙鍙傛暟
+		R<Contract> r = vailParam(addDTO);
+		if (!r.isOk()){
+			return r;
+		}
+		Contract contract = r.getData();
+		//鍑哄簱
+		handleOutBound(contract,addDTO);
+		//淇濆瓨璧勯噾鏄庣粏
+		BigDecimal outBoundAmount = savePaymentConfirm(contract,addDTO);
+		//鏇存柊鍚堝悓鐘舵�佸強闃舵
+		contract.setOutBoundAmout(outBoundAmount);
+		updateContractSchedule(contract);
+
+		return R.ok();
+	}
+
+	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)){
+			contract.setNextScheduleName("璐у埌绛炬敹");
+		}
+		contract.setBillingStatus("2");
+		contractMapper.updateById(contract);
+	}
+
+	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())){
+			schedule.setEffectiveDate(addDTO.getOutBoundTime());
+			schedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getOutBoundTime(),schedule.getAgreedDays()));
+			scheduleMapper.updateById(schedule);
+		}
+
+		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())
+					.last("limit 1"));
+			outAmount = outAmount.add(outSubjectMatterAddDTO.getOutBoundNum().multiply(subjectMatter.getUnitPrice()));
+		}
+		//瀛樺湪鍙戣揣鍓嶆敹娆鹃樁娈�
+		if (ObjUtil.isNotNull(schedule)) {
+			//鏂板褰撳墠闃舵搴旀敹
+			PaymentConfirm currentConfim = new PaymentConfirm();
+			currentConfim.setBusinessType(schedule.getStageName() + "搴旀敹");
+			currentConfim.setBusGuestId(contract.getPartyAId());
+			currentConfim.setBusGuestName(contract.getPartyA());
+			currentConfim.setContractId(contract.getId());
+			currentConfim.setContractName(contract.getContractName());
+			currentConfim.setContractNo(contract.getContractNo());
+			currentConfim.setScheduleId(schedule.getId());
+			currentConfim.setScheduleName(schedule.getStageName());
+			currentConfim.setConfirmTime(addDTO.getOutBoundTime());
+			//鎸夊嚭搴撴瘮渚嬭绠楀簲鏀�
+			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"));
+			if (ObjUtil.isNotNull(lastConfirm)) {
+				currentConfim.setTotalAmount(lastConfirm.getTotalAmount().subtract(currentConfim.getTransationAmount()));
+				if (contract.getContractNo().startsWith("ZB-")) {
+					paymentConfirmMapper.insert(currentConfim);
+				}
+				return outAmount;
+			}
+			currentConfim.setTotalAmount(currentConfim.getTransationAmount().multiply(new BigDecimal("-1")));
+			paymentConfirmMapper.insert(currentConfim);
+			//鍘嗗彶璧勯噾浣欓
+			processService.handleBeforeRemain(currentConfim,contract);
+		}
+		return outAmount;
+	}
+
+	private void handleOutBound(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())
+					.ne(ContractSubjectMatter::getDeliveryStatus, 2)
+					.eq(StrUtil.isNotEmpty(outSubjectMatterAddDTO.getLineNo()), ContractSubjectMatter::getLineNo, outSubjectMatterAddDTO.getLineNo())
+					.last("limit 1"));
+			if (ObjUtil.isNull(subjectMatter)) {
+				return;
+			}
+			subjectMatter.setLastDeliveredQuantity(outSubjectMatterAddDTO.getOutBoundNum());
+			outSubjectMatterAddDTO.setUnitPrice(subjectMatter.getUnitPrice());
+			if (subjectMatter.getDeliveryStatus() == 0) {
+				subjectMatter.setDeliveredQuantity(outSubjectMatterAddDTO.getOutBoundNum());
+				subjectMatter.setRemainingQuantity(subjectMatter.getQuantity().subtract(outSubjectMatterAddDTO.getOutBoundNum()));
+			}
+			if (subjectMatter.getDeliveryStatus() == 1) {
+				subjectMatter.setDeliveredQuantity(subjectMatter.getDeliveredQuantity().add(outSubjectMatterAddDTO.getOutBoundNum()));
+				subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(outSubjectMatterAddDTO.getOutBoundNum()));
+			}
+			subjectMatter.setDeliveryStatus(1);
+			if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0")) == 0) {
+				subjectMatter.setDeliveryStatus(2);
+			}
+			subjectMatter.setActualDeliveryDate(addDTO.getOutBoundTime());
+			contractSubjectMatterMapper.updateById(subjectMatter);
+			//淇濆瓨erp鍑哄簱
+			String batchNumber = genBatchNumber();
+			Long outBoundId = saveErpOutBound(contract,addDTO,outSubjectMatterAddDTO,batchNumber);
+			//淇濆瓨鍚堝悓鍑哄簱璁板綍
+			saveContractOutBound(contract, addDTO, subjectMatter, outBoundId);
+		});
+	}
+
+	private String genBatchNumber() {
+		if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
+				StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)) {
+			return generateSMJContractCKNo();
+		}
+		if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
+			return generateGYBContractCKNo();
+		}
+		if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", ymj)) {
+			return generateYMJContractCKNo();
+		}
+		if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", tfgs)) {
+			return generateTFContractCKNo();
+		}
+		if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", jxc)) {
+			return generateJXCContractCKNo();
+		}
+		return "";
+	}
+
+	private Long saveErpOutBound(Contract contract, OutBoundAddDTO addDTO,OutSubjectMatterAddDTO outSubjectMatterAddDTO,String batchNumber) {
+		OutBound outBound = new OutBound();
+		outBound.setBatchNumber(batchNumber);
+		outBound.setOutBoundNum(outSubjectMatterAddDTO.getOutBoundNum());
+		outBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
+		outBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
+		outBound.setBusGuestName(contract.getPartyA());
+		outBound.setBusGuestId(contract.getPartyAId());
+		outBound.setOutBoundTime(ObjUtil.isNotNull(addDTO.getOutBoundTime()) ? addDTO.getOutBoundTime() : new Date());
+		outBound.setSubjectMatterCode(outSubjectMatterAddDTO.getSubjectMatterCode());
+		outBound.setSubjectMatterName(outSubjectMatterAddDTO.getSubjectMatterName());
+		outBound.setOutBoundType("1");
+		if (StrUtil.isEmpty(addDTO.getOutBoundType())) {
+			outBound.setOutBoundType("0");
+		}
+		baseMapper.insert(outBound);
+		return outBound.getId();
+	}
+
+	private R<Contract> vailParam(OutBoundAddDTO addDTO) {
 		if (CollUtil.isEmpty(addDTO.getSubjectMatterList())) {
 			return R.failed("鍑哄簱鏍囩殑鐗╀笉鑳戒负绌�");
 		}
@@ -487,261 +653,41 @@
 		if (ObjUtil.isNull(contract)) {
 			return R.failed("鍚堝悓淇℃伅鏌ヨ澶辫触");
 		}
-		if (addDTO.getSubjectMatterList().stream().allMatch(item->item.getOutBoundNum().compareTo(new BigDecimal("0"))==0)){
+		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()));
+		addDTO.setSubjectMatterList(addDTO.getSubjectMatterList().stream().filter(item -> item.getOutBoundNum().compareTo(new BigDecimal("0")) > 0).collect(Collectors.toList()));
 		//鍑哄簱鏁伴噺鏍¢獙
-		for (OutSubjectMatterAddDTO outSubjectMatterAddDTO:	addDTO.getSubjectMatterList()) {
+		for (OutSubjectMatterAddDTO outSubjectMatterAddDTO : addDTO.getSubjectMatterList()) {
 			ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
-					.eq(ContractSubjectMatter::getContractId,contract.getId())
-					.eq(ContractSubjectMatter::getMaterialCode,outSubjectMatterAddDTO.getSubjectMatterCode())
-					.eq(StrUtil.isNotEmpty(outSubjectMatterAddDTO.getLineNo()),ContractSubjectMatter::getLineNo,outSubjectMatterAddDTO.getLineNo())
+					.eq(ContractSubjectMatter::getContractId, contract.getId())
+					.eq(ContractSubjectMatter::getMaterialCode, outSubjectMatterAddDTO.getSubjectMatterCode())
+					.eq(StrUtil.isNotEmpty(outSubjectMatterAddDTO.getLineNo()), ContractSubjectMatter::getLineNo, outSubjectMatterAddDTO.getLineNo())
 					.last("limit 1"));
-			if (ObjUtil.isNull(subjectMatter)){
+			if (ObjUtil.isNull(subjectMatter)) {
 				continue;
 			}
-			if (subjectMatter.getDeliveryStatus()==0){
-				if (outSubjectMatterAddDTO.getOutBoundNum().compareTo(subjectMatter.getQuantity())>0){
-					return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode()+"鍑哄簱鏁颁笉鑳借秴杩囧悎鍚屼腑鏁伴噺");
-				}
-			}
-			if (subjectMatter.getDeliveryStatus()==1){
-				if (outSubjectMatterAddDTO.getOutBoundNum().compareTo(subjectMatter.getRemainingQuantity())>0){
-					return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode()+"鍑哄簱鏁颁笉鑳借秴杩囨湭鍑哄簱鏁伴噺");
-				}
-			}
-			if (subjectMatter.getDeliveryStatus()==2){
-				return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode()+"宸插嚭搴撳畬鎴愶紝涓嶈兘瓒呴鍑哄簱");
-			}
-		}
-
-
-		addDTO.getSubjectMatterList().stream().forEach(outSubjectMatterAddDTO -> {
-				ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
-					.eq(ContractSubjectMatter::getContractId,contract.getId())
-					.eq(ContractSubjectMatter::getMaterialCode,outSubjectMatterAddDTO.getSubjectMatterCode())
-					.ne(ContractSubjectMatter::getDeliveryStatus,2)
-					.eq(StrUtil.isNotEmpty(outSubjectMatterAddDTO.getLineNo()),ContractSubjectMatter::getLineNo,outSubjectMatterAddDTO.getLineNo())
-					.last("limit 1"));
-			if (ObjUtil.isNull(subjectMatter)){
-				return;
-			}
-			subjectMatter.setLastDeliveredQuantity(outSubjectMatterAddDTO.getOutBoundNum());
-			outSubjectMatterAddDTO.setUnitPrice(subjectMatter.getUnitPrice());
 			if (subjectMatter.getDeliveryStatus() == 0) {
-				subjectMatter.setDeliveredQuantity(outSubjectMatterAddDTO.getOutBoundNum());
-				subjectMatter.setRemainingQuantity(subjectMatter.getQuantity().subtract(outSubjectMatterAddDTO.getOutBoundNum()));
+				if (outSubjectMatterAddDTO.getOutBoundNum().compareTo(subjectMatter.getQuantity()) > 0) {
+					return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode() + "鍑哄簱鏁颁笉鑳借秴杩囧悎鍚屼腑鏁伴噺");
+				}
 			}
 			if (subjectMatter.getDeliveryStatus() == 1) {
-				subjectMatter.setDeliveredQuantity(subjectMatter.getDeliveredQuantity().add(outSubjectMatterAddDTO.getOutBoundNum()));
-				subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().subtract(outSubjectMatterAddDTO.getOutBoundNum()));
-			}
-			subjectMatter.setDeliveryStatus(1);
-			if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0")) == 0) {
-				subjectMatter.setDeliveryStatus(2);
-			}
-			subjectMatter.setActualDeliveryDate(addDTO.getOutBoundTime());
-			contractSubjectMatterMapper.updateById(subjectMatter);
-			//
-			OutBound outBound = new OutBound();
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",smj)){
-				outBound.setBatchNumber(generateSMJContractCKNo());
-			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",sgb)){
-				outBound.setBatchNumber(generateGYBContractCKNo());
-			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",ymj)){
-				outBound.setBatchNumber(generateYMJContractCKNo());
-			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",tfgs)){
-				outBound.setBatchNumber(generateTFContractCKNo());
-			}
-			if (StrUtil.equals(SecurityUtils.getUser().getCompId()+"",jxc)){
-				outBound.setBatchNumber(generateJXCContractCKNo());
-			}
-			outBound.setOutBoundNum(outSubjectMatterAddDTO.getOutBoundNum());
-			outBound.setOutBoundAttNames(addDTO.getOutBoundAttNames());
-			outBound.setOutBoundAttPaths(addDTO.getOutBoundAttPaths());
-			outBound.setBusGuestName(contract.getPartyA());
-			outBound.setBusGuestId(contract.getPartyAId());
-			outBound.setOutBoundTime(ObjUtil.isNotNull(addDTO.getOutBoundTime())?addDTO.getOutBoundTime():new Date());
-			outBound.setSubjectMatterCode(outSubjectMatterAddDTO.getSubjectMatterCode());
-			outBound.setSubjectMatterName(outSubjectMatterAddDTO.getSubjectMatterName());
-			outBound.setOutBoundType("1");
-			if (StrUtil.isEmpty(addDTO.getOutBoundType())){
-				outBound.setOutBoundType("0");
-			}
-			baseMapper.insert(outBound);
-			//
-			saveContractOutBound(contract, addDTO, subjectMatter, outBound.getId());
-		});
-		//褰撳墠鍑哄簱鍚堝悓鍑哄簱鏍囩殑鐗�
-		List<ContractSubjectMatter> contractSubjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
-				.eq(ContractSubjectMatter::getContractId, contract.getId()).ne(ContractSubjectMatter::getDeliveryStatus, 0));
-		//鏌ヨ鍚堝悓鏄惁鏈夊嚭搴撻樁娈�
-		ContractPaymentSchedule schedule = scheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId())
-				.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);
-			//鍑哄簱鏄惁瀹屾垚
-			if (outPirce.compareTo(contract.getBillingAmout())>0) {
-				contract.setBillingStatus("2");
-				contractMapper.updateById(contract);
-			}
-			if (outPirce.compareTo(contract.getBillingAmout())==0) {
-				contract.setBillingStatus("1");
-				contractMapper.updateById(contract);
-			}
-			if (outPirce.compareTo(contract.getAmount()) == 0&&
-					outPirce.compareTo(contract.getBillingAmout())==0) {
-				contract.setBillingStatus("3");
-				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());
-			//璁$畻鍑哄簱閲戦
-			BigDecimal outAmount = addDTO.getSubjectMatterList().stream().map(item->item.getUnitPrice().multiply(item.getOutBoundNum())).reduce(BigDecimal.ZERO, BigDecimal::add);
-			paymentConfirm.setTransationAmount(outAmount.divide(new BigDecimal("100")).multiply(schedule.getPaymentRatio()));
-			if (ObjUtil.isNotNull(contract.getOutBoundAmout())){
-				contract.setOutBoundAmout(outAmount.add(contract.getOutBoundAmout()));
-			}else {
-				contract.setOutBoundAmout(outAmount);
-			}
-			contractMapper.updateById(contract);
-			//鏈�杩戞槑缁�
-			PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
-					.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
-			if (ObjUtil.isNotNull(lastConfirm)) {
-					//涓嶅瓨鍦ㄥ彂璐у簲鏀� 鍑哄簱鎬婚
-					paymentConfirm.setTotalAmount(lastConfirm.getTotalAmount().subtract(paymentConfirm.getTransationAmount()));
-					if (paymentConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
-						paymentConfirm.setAdvanceAmount(paymentConfirm.getTotalAmount());
-					}
-					if (paymentConfirm.getTotalAmount().compareTo(new BigDecimal("0"))<0){
-						paymentConfirm.setReceivableAmount(paymentConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
-					}
-					if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
-						paymentConfirmMapper.insert(paymentConfirm);
-					}
-//				}
-			} else {
-				//鏃犱箣鍓嶉樁娈靛簲鏀�
-				paymentConfirm.setTotalAmount(paymentConfirm.getTransationAmount().multiply(new BigDecimal("-1")));
-				if (schedule.getPaymentRatio().compareTo(new BigDecimal("0")) > 0) {
-					paymentConfirmMapper.insert(paymentConfirm);
-					//瀹㈡埛浠樻瀹屾垚鍚堝悓
-					List<Contract> customerCompleteContractList = contractMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
-							.eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
-					if (CollUtil.isNotEmpty(customerCompleteContractList)) {
-						List<BigDecimal> outAmountList = new ArrayList<>();
-						for (Contract completeContract : customerCompleteContractList
-						) {
-							PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, completeContract.getId())
-									.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
-							//鏌ヨ瀹㈡埛浠樻瀹屾垚鍚堝悓涓渶鍚庢槑缁嗛噾棰� 鏈夐鏀惰浆鍏ュ綋鍓嶅悎鍚�
-							if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
-								//鏂板瀹屾垚浠樻鍚堝悓 璧勯噾杞嚭
-								PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm, PaymentConfirm.class, "id", "transationAmount",
-										"advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
-								outConfirm.setBusinessType("璧勯噾杞嚭");
-								outConfirm.setConfirmTime(new Date());
-								outConfirm.setTransationAmount(contractLastConfirm.getTotalAmount());
-								outConfirm.setTotalAmount(new BigDecimal("0"));
-								paymentConfirmMapper.insert(outConfirm);
-								outAmountList.add(outConfirm.getTransationAmount());
-							}
-						}
-						if (CollUtil.isNotEmpty(outAmountList)) {
-							BigDecimal currentInSum = outAmountList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
-							//鏂板褰撳墠鍚堝悓 璧勯噾杞叆
-							PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm, PaymentConfirm.class, "id", "transationAmount",
-									"advanceAmount", "receivableAmount", "overdueAmount", "totalAmount");
-							inConfirm.setBusinessType("璧勯噾杞叆");
-							inConfirm.setConfirmTime(new Date());
-							inConfirm.setTransationAmount(currentInSum);
-							inConfirm.setTotalAmount(paymentConfirm.getTotalAmount().add(currentInSum));
-							//鏇存柊褰撳墠闃舵瀹為檯鏀舵 鍜� 鏀舵鐘舵��
-							schedule.setActualAmount(inConfirm.getTransationAmount());
-							schedule.setPaymentStatus(2);
-							if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
-								inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
-							} else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0")) < 0) {
-								inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
-								schedule.setActualAmount(paymentConfirm.getTotalAmount().subtract(currentInSum));
-								schedule.setPaymentStatus(1);
-							}
-							paymentConfirmMapper.insert(inConfirm);
-							//鏇存柊褰撳墠闃舵瀹為檯鏀舵 鍜� 鏀舵鐘舵��
-							scheduleMapper.updateById(schedule);
-						}
-					}
+				if (outSubjectMatterAddDTO.getOutBoundNum().compareTo(subjectMatter.getRemainingQuantity()) > 0) {
+					return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode() + "鍑哄簱鏁颁笉鑳借秴杩囨湭鍑哄簱鏁伴噺");
 				}
 			}
-
-			//鏇存柊鍙戣揣鍓嶆敹娆剧敓鏁堟椂闂�
-			if (ObjUtil.isNull(schedule.getEffectiveDate())) {
-
-				//鏇存柊灞ョ害
-				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);
-				}
-			}
-
-			//鏌ヨ鏄惁鏈夊悗缁樁娈�
-			List<ContractPaymentSchedule> afterSchedule = scheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-					.eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
-					.gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())
-					.orderByAsc(ContractPaymentSchedule::getCreateTime));
-			if (CollUtil.isNotEmpty(afterSchedule)&&contract.getOutBoundAmout().compareTo(contract.getAmount())==0) {
-				contract.setNextScheduleName(afterSchedule.get(0).getStageName());
-				contractMapper.updateById(contract);
-			}
-
-			if (CollUtil.isEmpty(afterSchedule)) {
-				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);
-				}
+			if (subjectMatter.getDeliveryStatus() == 2) {
+				return R.failed(outSubjectMatterAddDTO.getSubjectMatterCode() + "宸插嚭搴撳畬鎴愶紝涓嶈兘瓒呴鍑哄簱");
 			}
 		}
-		return R.ok();
+		return R.ok(contract);
 	}
 
 	/**
 	 * 鐢熸垚鏍煎紡涓�: yyyyMMdd + 4浣嶅簭鍒楀彿 (渚嬪: 20260629000001)
 	 */
-	public  String generateSMJContractCKNo() {
+	public String generateSMJContractCKNo() {
 		// 1. 鑾峰彇褰撳墠鏃ユ湡瀛楃涓�
 		String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
 
@@ -756,10 +702,10 @@
 		String seqStr = String.format("%04d", sequence);
 
 		// 5. 鎷兼帴杩斿洖
-		return "ZB-SMJ-CK-"+dateStr + seqStr;
+		return "ZB-SMJ-CK-" + dateStr + seqStr;
 	}
 
-	public  String generateYMJContractCKNo() {
+	public String generateYMJContractCKNo() {
 		// 1. 鑾峰彇褰撳墠鏃ユ湡瀛楃涓�
 		String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
 
@@ -774,10 +720,10 @@
 		String seqStr = String.format("%04d", sequence);
 
 		// 5. 鎷兼帴杩斿洖
-		return "ZB-YMJ-CK-"+dateStr + seqStr;
+		return "ZB-YMJ-CK-" + dateStr + seqStr;
 	}
 
-	public  String generateJXCContractCKNo() {
+	public String generateJXCContractCKNo() {
 		// 1. 鑾峰彇褰撳墠鏃ユ湡瀛楃涓�
 		String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
 
@@ -792,10 +738,10 @@
 		String seqStr = String.format("%04d", sequence);
 
 		// 5. 鎷兼帴杩斿洖
-		return "ZB-JXC-CK-"+dateStr + seqStr;
+		return "ZB-JXC-CK-" + dateStr + seqStr;
 	}
 
-	public  String generateTFContractCKNo() {
+	public String generateTFContractCKNo() {
 		// 1. 鑾峰彇褰撳墠鏃ユ湡瀛楃涓�
 		String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
 
@@ -810,10 +756,10 @@
 		String seqStr = String.format("%04d", sequence);
 
 		// 5. 鎷兼帴杩斿洖
-		return "ZB-TF-CK-"+dateStr + seqStr;
+		return "ZB-TF-CK-" + dateStr + seqStr;
 	}
 
-	public  String generateGYBContractCKNo() {
+	public String generateGYBContractCKNo() {
 		// 1. 鑾峰彇褰撳墠鏃ユ湡瀛楃涓�
 		String dateStr = DateUtil.format(new Date(), "yyyyMMdd");
 
@@ -828,42 +774,42 @@
 		String seqStr = String.format("%04d", sequence);
 
 		// 5. 鎷兼帴杩斿洖
-		return "ZB-GYB-CK-"+dateStr + seqStr;
+		return "ZB-GYB-CK-" + dateStr + seqStr;
 	}
 
 	@Override
 	public R delOutBound(String batchNumber) {
-		List<OutBound> outBoundList = baseMapper.selectList(Wrappers.<OutBound>lambdaQuery().eq(OutBound::getBatchNumber,batchNumber));
-		if (CollUtil.isEmpty(outBoundList)){
+		List<OutBound> outBoundList = baseMapper.selectList(Wrappers.<OutBound>lambdaQuery().eq(OutBound::getBatchNumber, batchNumber));
+		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 (CollUtil.isEmpty(contractOutBoundList)){
+				.in(ContractOutBound::getOutBoundId, outBoundList.stream().map(OutBound::getId).collect(Collectors.toList())));
+		if (CollUtil.isEmpty(contractOutBoundList)) {
 			return R.failed("鍑哄簱淇℃伅寮傚父锛岃鑱旂郴钀ラ攢绯荤粺绠$悊浜哄憳");
 		}
 		boolean allZero = true;
 		for (ContractOutBound item : contractOutBoundList) {
 			// 鍋囪 invoiceNum 鏄� Integer 瀵硅薄
-			if (item.getInvoiceNum() == null || item.getInvoiceNum().compareTo(new BigDecimal("0"))!=0) {
+			if (item.getInvoiceNum() == null || item.getInvoiceNum().compareTo(new BigDecimal("0")) != 0) {
 				allZero = false;
 				break; // 鍙戠幇涓�涓笉绗﹀悎鍗抽��鍑�
 			}
 		}
-		if (!allZero){
+		if (!allZero) {
 			return R.failed("璇峰厛鍐插噺鎵�鏈夎摑鐗屽悗鍦ㄦ搷浣�");
 		}
 		for (ContractOutBound item : contractOutBoundList) {
 			ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
-					.eq(ContractSubjectMatter::getContractId,item.getContractId()).eq(ContractSubjectMatter::getMaterialCode,item.getSubjectMatterCode())
-					.eq(ContractSubjectMatter::getLineNo,item.getLineNo()));
-			if (ObjUtil.isNotNull(subjectMatter)){
+					.eq(ContractSubjectMatter::getContractId, item.getContractId()).eq(ContractSubjectMatter::getMaterialCode, item.getSubjectMatterCode())
+					.eq(ContractSubjectMatter::getLineNo, item.getLineNo()));
+			if (ObjUtil.isNotNull(subjectMatter)) {
 				subjectMatter.setDeliveredQuantity(subjectMatter.getDeliveredQuantity().subtract(item.getOutBoundNum()));
 				subjectMatter.setRemainingQuantity(subjectMatter.getRemainingQuantity().add(item.getOutBoundNum()));
-				if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0"))==0){
+				if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0")) == 0) {
 					subjectMatter.setDeliveryStatus(0);
 				}
-				if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0"))>0){
+				if (subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0")) > 0) {
 					subjectMatter.setDeliveryStatus(1);
 				}
 				//鏍囩殑鐗╂洿鏂�
@@ -888,7 +834,7 @@
 			//鍑哄簱璁板綍鍒犻櫎
 			contractOutBoundMapper.deleteById(item);
 		}
-		baseMapper.delete(Wrappers.<OutBound>lambdaQuery().eq(OutBound::getBatchNumber,batchNumber));
+		baseMapper.delete(Wrappers.<OutBound>lambdaQuery().eq(OutBound::getBatchNumber, batchNumber));
 		return R.ok();
 	}
 }
\ No newline at end of file

--
Gitblit v1.9.1