From c427840445fefb679a287dbeaba81bd417388aeb Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期五, 04 九月 2026 17:10:44 +0800
Subject: [PATCH] feat:开票和bip推送分离 新增bip报账单  新增合同标的物变更

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java |  570 ++++++++++++++++++--------------------------------------
 1 files changed, 185 insertions(+), 385 deletions(-)

diff --git a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java
index f15f836..9ddc4f3 100644
--- a/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java
+++ b/platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java
@@ -3,7 +3,6 @@
 import cn.hutool.core.bean.BeanUtil;
 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 cn.hutool.http.HttpUtil;
@@ -94,114 +93,76 @@
 				return R.failed("瀹㈠晢鏌ヨ澶辫触锛岃妫�鏌ュ鍟�");
 			}
 
-			PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
-					.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
-			BigDecimal lastTotal = new BigDecimal("0");
-			if (ObjUtil.isNotNull(lastConfirm)) {
-				lastTotal = lastConfirm.getTotalAmount();
-			}
-
-
 			//杞叆
 			if (StrUtil.equals(addDTO.getInOrOut(), "1")) {
+				//淇濆瓨鏀舵鍗�
+				savePaymentSlip(addDTO,customer,contract);
+				//淇濆瓨鏀舵鍗�
+				savePaymentConfirm(addDTO,contract,customer);
+				//澶勭悊鏀舵
+				handleSlipToSchedule(addDTO,contract);
+				//鏇存柊鐘舵��
+				updateContractStatus(addDTO,contract);
+			}
+			//杞嚭
+			if (StrUtil.equals(addDTO.getInOrOut(), "2")) {
+				return handleBackSlip(addDTO,contract,customer);
+			}
 
+		}
 
-				//鏂板浠樻鍗�
-				PaymentSlip slip = new PaymentSlip();
-				slip.setBusGuestId(customer.getId());
-				slip.setBusGuestName(customer.getCompanyName());
-				slip.setPayeeCompId(SecurityUtils.getUser().getCompId());
-				slip.setPaymentTime(addDTO.getConfirmTime());
-				slip.setPaymentAmount(addDTO.getTransationAmount());
-				slip.setPayType(addDTO.getPayType());
-				slip.setContractId(contract.getId());
-				paymentSlipMapper.insert(slip);
+		return R.ok();
+	}
 
-				BigDecimal newtotal = addDTO.getTransationAmount().add(lastTotal);
-				PaymentConfirm entity = new PaymentConfirm();
-				entity.setContractId(contract.getId());
-				entity.setContractName(contract.getContractName());
-				entity.setContractNo(addDTO.getContractNo());
-				if (ObjUtil.isNotNull(lastConfirm)) {
-					entity.setScheduleId(lastConfirm.getScheduleId());
-					entity.setScheduleName(lastConfirm.getScheduleName());
-				}
-				entity.setBusGuestId(customer.getId());
-				entity.setBusGuestName(customer.getCompanyName());
-				entity.setBusinessType("瀹㈡埛浠樻");
-				if (newtotal.compareTo(new BigDecimal("0")) > 0) {
-					entity.setTransationAmount(addDTO.getTransationAmount());
-					entity.setAdvanceAmount(newtotal);
-					entity.setTotalAmount(newtotal);
-					entity.setConfirmTime(new Date());
-					baseMapper.insert(entity);
-				} else if (newtotal.compareTo(new BigDecimal("0")) == 0) {
-					entity.setTransationAmount(addDTO.getTransationAmount());
-					entity.setTotalAmount(newtotal);
-					entity.setConfirmTime(new Date());
-					baseMapper.insert(entity);
-				} else {
-					entity.setTransationAmount(addDTO.getTransationAmount());
-					entity.setReceivableAmount(newtotal.multiply(new BigDecimal("-1")));
-					entity.setTotalAmount(newtotal);
-					entity.setConfirmTime(new Date());
-					baseMapper.insert(entity);
-				}
+	private R handleBackSlip(PaymentConfirmAddDTO addDTO,Contract contract,BusinessCustomer customer) {
+		PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
+				.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
+		PaymentConfirm entity = new PaymentConfirm();
+		entity.setContractId(contract.getId());
+		entity.setContractName(contract.getContractName());
+		entity.setContractNo(contract.getContractNo());
 
-					//鏇存柊浠樻闃舵浠樻
-					List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-							.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2).ne(ContractPaymentSchedule::getPaymentStatus, 3)
-							.orderByAsc(ContractPaymentSchedule::getCreateTime));
-					if (CollUtil.isNotEmpty(scheduleList)) {
-						BigDecimal remain = addDTO.getTransationAmount();
-						for (ContractPaymentSchedule schedule : scheduleList) {
-							BigDecimal lastRemain = remain;
-							remain = remain.subtract(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount() :
-									schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
-							if (remain.compareTo(new BigDecimal("0")) >= 0) {
-								schedule.setActualAmount(schedule.getPlannedAmount());
-								schedule.setPaymentDate(addDTO.getConfirmTime());
-								schedule.setPaymentStatus(2);
-								contractPaymentScheduleMapper.updateById(schedule);
-								//鏌ヨ鏄惁鏈夊綋鍓嶉�炬湡
-								CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
-										.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
-								if (ObjUtil.isNotNull(currentOverdue)) {
-									//鏂板閫炬湡鍘嗗彶
-									HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
-									historyOverdue.setPaymentTime(addDTO.getConfirmTime());
-									historyOverdue.setCompId(schedule.getCompId());
-									historyOverdueMapper.insert(historyOverdue);
-									//鍒犻櫎褰撳墠閫炬湡
-									currentOverdueMapper.deleteById(currentOverdue);
-								}
-							} else {
-								schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount().add(remain) :
-										schedule.getActualAmount().add(lastRemain));
-								schedule.setPaymentDate(addDTO.getConfirmTime());
-								schedule.setPaymentStatus(1);
-								contractPaymentScheduleMapper.updateById(schedule);
-								//鏌ヨ鏄惁鏈夊綋鍓嶉�炬湡
-								CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
-										.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
-								if (ObjUtil.isNotNull(currentOverdue)) {
-									//鏂板閫炬湡鍘嗗彶
-									HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
-									historyOverdue.setReceivableAmount(lastRemain);
-									historyOverdue.setPaymentTime(addDTO.getConfirmTime());
-									historyOverdue.setCompId(schedule.getCompId());
-									historyOverdueMapper.insert(historyOverdue);
-									//鏇存柊褰撳墠閫炬湡
-									currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
-									currentOverdueMapper.updateById(currentOverdue);
-								}
-								break;
-							}
-						}
-					}
-				}
-			//闈炴埧灞嬬璧� 姘寸數
-			if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
+		entity.setBusGuestId(customer.getId());
+		entity.setBusGuestName(customer.getCompanyName());
+		entity.setBusinessType("閫�娆�");
+		entity.setTransationAmount(addDTO.getTransationAmount());
+		BigDecimal newtotal = addDTO.getTransationAmount().multiply(new BigDecimal("-1")).add(lastConfirm.getTotalAmount());
+		if (newtotal.compareTo(new BigDecimal("0")) > 0) {
+			entity.setAdvanceAmount(newtotal);
+			entity.setTotalAmount(newtotal);
+		} else if (newtotal.compareTo(new BigDecimal("0")) == 0) {
+			entity.setTotalAmount(newtotal);
+		} else {
+			return R.failed("褰撳墠瀹㈡埛浣欓涓�" + lastConfirm.getTotalAmount() + "涓嶈冻浠ユ弧瓒冲綋鍓嶅彂鐢熼噾棰濓紝鏃犳硶鎿嶄綔");
+		}
+		entity.setConfirmTime(new Date());
+		baseMapper.insert(entity);
+		contract.setPaidAmount(contract.getPaidAmount().subtract(addDTO.getTransationAmount()));
+		contract.setContractStatus(2);
+		contractService.updateById(contract);
+		return R.ok();
+	}
+
+	private void updateContractStatus(PaymentConfirmAddDTO addDTO,Contract contract) {
+
+		//闈炴埧灞嬬璧� 姘寸數
+		if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
+			//鏇存柊鍚堝悓宸蹭粯娆�
+			BigDecimal paid = new BigDecimal("0");
+			if (ObjUtil.isNotNull(contract.getPaidAmount())) {
+				paid = contract.getPaidAmount();
+			}
+			paid = paid.add(addDTO.getTransationAmount());
+			contract.setPaidAmount(paid);
+			if (paid.compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0&&contract.getBillingAmout().compareTo(contract.getAmount())==0) {
+				contract.setContractStatus(3);
+				contract.setPaidAmount(contract.getAmount().subtract(contract.getDropAmount()));
+			}
+			contractService.updateById(contract);
+		}else {
+			List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId,contract.getId())
+					.eq(ContractExecDate::getGenFlag,"0"));
+			if (CollUtil.isEmpty(execDateList)){
 				//鏇存柊鍚堝悓宸蹭粯娆�
 				BigDecimal paid = new BigDecimal("0");
 				if (ObjUtil.isNotNull(contract.getPaidAmount())) {
@@ -209,275 +170,120 @@
 				}
 				paid = paid.add(addDTO.getTransationAmount());
 				contract.setPaidAmount(paid);
-				if (paid.compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0&&contract.getBillingAmout().compareTo(contract.getAmount())==0) {
+				if (contract.getBillingAmout().compareTo(contract.getAmount())==0&&paid.compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0) {
 					contract.setContractStatus(3);
-					contract.setPaidAmount(contract.getAmount().subtract(contract.getDropAmount()));
+					contract.setPaidAmount(contract.getAmount());
 				}
 				contractService.updateById(contract);
-			}else {
-				List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId,contract.getId())
-						.eq(ContractExecDate::getGenFlag,"0"));
-				if (CollUtil.isEmpty(execDateList)){
-					//鏇存柊鍚堝悓宸蹭粯娆�
-					BigDecimal paid = new BigDecimal("0");
-					if (ObjUtil.isNotNull(contract.getPaidAmount())) {
-						paid = contract.getPaidAmount();
-					}
-					paid = paid.add(addDTO.getTransationAmount());
-					contract.setPaidAmount(paid);
-					if (contract.getBillingAmout().compareTo(contract.getAmount())==0&&paid.compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0) {
-						contract.setContractStatus(3);
-						contract.setPaidAmount(contract.getAmount());
-					}
-					contractService.updateById(contract);
-				}
 			}
-			//杞嚭
-			if (StrUtil.equals(addDTO.getInOrOut(), "2")) {
-				PaymentConfirm entity = new PaymentConfirm();
-				entity.setContractId(contract.getId());
-				entity.setContractName(contract.getContractName());
-				entity.setContractNo(addDTO.getContractNo());
-
-				entity.setBusGuestId(customer.getId());
-				entity.setBusGuestName(customer.getCompanyName());
-				entity.setBusinessType("閫�娆�");
-				entity.setTransationAmount(addDTO.getTransationAmount());
-				BigDecimal newtotal = addDTO.getTransationAmount().multiply(new BigDecimal("-1")).add(lastConfirm.getTotalAmount());
-				if (newtotal.compareTo(new BigDecimal("0")) > 0) {
-					entity.setAdvanceAmount(newtotal);
-					entity.setTotalAmount(newtotal);
-				} else if (newtotal.compareTo(new BigDecimal("0")) == 0) {
-					entity.setTotalAmount(newtotal);
-				} else {
-					return R.failed("褰撳墠瀹㈡埛浣欓涓�" + lastConfirm.getTotalAmount() + "涓嶈冻浠ユ弧瓒冲綋鍓嶅彂鐢熼噾棰濓紝鏃犳硶鎿嶄綔");
-				}
-				entity.setConfirmTime(new Date());
-				baseMapper.insert(entity);
-			}
-
 		}
-//		else {
-//
-//			List<Contract> customerContractList = contractService.list(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, addDTO.getBusGuestId())
-//					.eq(Contract::getPartyBId, SecurityUtils.getUser().getCompId()).orderByAsc(Contract::getCreateTime)
-//					.apply(" ((contract_category != 'water_house' and amount != paid_amount) or contract_category = 'water_house')"));
-//			if (ArrayUtil.isEmpty(customerContractList)) {
-//				return R.failed("涓烘煡璇㈠埌鏈粯娆惧悎鍚�");
-//			}
-//			BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
-//					.eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getRegisterName, addDTO.getBusGuestName())
-//					.eq(ObjUtil.isNotNull(addDTO.getBusGuestId()), BusinessCustomer::getId, addDTO.getBusGuestId())
-//					.last("limit 1"));
-//			if (ObjUtil.isNull(customer)) {
-//				return R.failed("瀹㈠晢鏌ヨ澶辫触");
-//			}
-//			//鏂板浠樻鍗�
-//			PaymentSlip slip = new PaymentSlip();
-//			slip.setBusGuestId(customer.getId());
-//			slip.setBusGuestName(customer.getRegisterName());
-//			slip.setPayeeCompId(SecurityUtils.getUser().getCompId());
-//			slip.setPaymentTime(addDTO.getConfirmTime());
-//			slip.setPaymentAmount(addDTO.getTransationAmount());
-//			slip.setPayType(addDTO.getPayType());
-//			paymentSlipMapper.insert(slip);
-//
-//			BigDecimal transtionAmount = addDTO.getTransationAmount();
-//			//鍓╀綑浠樻
-//			BigDecimal transtionRemainAmount = transtionAmount;
-//			//鏀跺埌浠樻鐨勫悎鍚�
-//			List<Contract> payContractList = new ArrayList<>();
-//			//浠樻瀹屾垚鐨勫悎鍚�
-//			List<Contract> payCompleteContractList = new ArrayList<>();
-//			//浠樻鏈畬鎴愮殑鍚堝悓
-//			List<Contract> payNoCompleteContractList = new ArrayList<>();
-//			//鏈夐浠樻鐨勫悎鍚�
-//			List<Contract> advanceContractList = new ArrayList<>();
-//			//浠樻鏃堕棿
-//			Date payDate = addDTO.getConfirmTime();
-//			Iterator<Contract> iterator = customerContractList.iterator();
-//			while (iterator.hasNext()) {
-//				Contract contract = iterator.next();
-//				PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
-//						.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
-//				BigDecimal lastTotal = new BigDecimal("0");
-//				if (ObjUtil.isNotNull(lastConfirm)) {
-//					lastTotal = lastConfirm.getTotalAmount();
-//				} else {
-//
-//				}
-//
-//				//鍚堝悓鏈�杩戞槑缁嗘�婚涓�0 鎴� > 0 鎯呭喌涓�
-//				if (lastTotal.compareTo(new BigDecimal("0")) >= 0) {
-//					log.info("褰撳墠鍚堝悓鍙凤細{},鏄庣粏鎬婚鏃犲簲鏀舵", contract.getContractNo());
-//					advanceContractList.add(contract);
-//					if (iterator.hasNext()) {
-//						continue;
-//					}
-//				}
-//
-//				transtionRemainAmount = transtionRemainAmount.add(lastTotal);
-//				PaymentConfirm entity = new PaymentConfirm();
-//				entity.setContractId(contract.getId());
-//				entity.setContractName(contract.getContractName());
-//				entity.setContractNo(contract.getContractNo());
-//				if (ObjUtil.isNotNull(lastConfirm)) {
-//					entity.setScheduleId(lastConfirm.getScheduleId());
-//					entity.setScheduleName(lastConfirm.getScheduleName());
-//				}
-//				entity.setBusGuestId(customer.getId());
-//				entity.setBusGuestName(customer.getRegisterName());
-//				entity.setBusinessType("瀹㈡埛浠樻");
-//				if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
-//					entity.setConfirmTime(payDate);
-//					// 鍒ゆ柇鏄惁鏄渶鍚庝竴涓�
-//					if (!iterator.hasNext()) {
-//						entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
-//						entity.setAdvanceAmount(transtionRemainAmount);
-//						entity.setTotalAmount(transtionRemainAmount);
-//					} else {
-//						entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
-//						entity.setTotalAmount(new BigDecimal("0"));
-//					}
-//					baseMapper.insert(entity);
-//					payContractList.add(contract);
-//				} else if (transtionRemainAmount.compareTo(new BigDecimal("0")) == 0) {
-//					entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
-//					entity.setTotalAmount(new BigDecimal("0"));
-//					entity.setConfirmTime(payDate);
-//					baseMapper.insert(entity);
-//					payContractList.add(contract);
-//					break;
-//				} else {
-//					entity.setTransationAmount(transtionRemainAmount.subtract(lastTotal));
-//					entity.setReceivableAmount(transtionRemainAmount.multiply(new BigDecimal("-1")));
-//					entity.setTotalAmount(transtionRemainAmount);
-//					entity.setConfirmTime(payDate);
-//					baseMapper.insert(entity);
-//					payContractList.add(contract);
-//					break;
-//				}
-//			}
-//
-//			payContractList.stream().forEach(contract -> {
-//				//鏇存柊浠樻闃舵浠樻
-//				List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-//						.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
-//						.ne(ContractPaymentSchedule::getPaymentStatus, 3)
-//						.orderByAsc(ContractPaymentSchedule::getCreateTime));
-//				if (ArrayUtil.isNotEmpty(scheduleList)) {
-//					BigDecimal remain = transtionAmount;
-//					for (ContractPaymentSchedule schedule : scheduleList) {
-//						BigDecimal lastRemain = remain;
-//						remain = lastRemain.subtract(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount() :
-//								schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
-//						if (remain.compareTo(new BigDecimal("0")) >= 0) {
-//							schedule.setActualAmount(schedule.getPlannedAmount());
-//							schedule.setPaymentDate(payDate);
-//							schedule.setPaymentStatus(2);
-//							contractPaymentScheduleMapper.updateById(schedule);
-//							//鏌ヨ鏄惁鏈夊綋鍓嶉�炬湡
-//							CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
-//									.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
-//							if (ObjUtil.isNotNull(currentOverdue)) {
-//								//鏂板閫炬湡鍘嗗彶
-//								HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
-//								historyOverdue.setPaymentTime(payDate);
-//								historyOverdue.setCompId(schedule.getCompId());
-//								historyOverdueMapper.insert(historyOverdue);
-//								//鍒犻櫎褰撳墠閫炬湡
-//								currentOverdueMapper.deleteById(currentOverdue);
-//							}
-//						} else {
-//							schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount().add(remain) :
-//									schedule.getActualAmount().add(lastRemain));
-//							schedule.setPaymentDate(payDate);
-//							schedule.setPaymentStatus(1);
-//							contractPaymentScheduleMapper.updateById(schedule);
-//							//鏌ヨ鏄惁鏈夊綋鍓嶉�炬湡
-//							CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
-//									.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
-//							if (ObjUtil.isNotNull(currentOverdue)) {
-//								//鏂板閫炬湡鍘嗗彶
-//								HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
-//								historyOverdue.setReceivableAmount(lastRemain);
-//								historyOverdue.setPaymentTime(payDate);
-//								historyOverdue.setCompId(schedule.getCompId());
-//								historyOverdueMapper.insert(historyOverdue);
-//								//鏌ヨ鍘嗗彶宸蹭粯閫炬湡閲戦
-//								List<HistoryOverdue> historyOverdueList = historyOverdueMapper.selectList(Wrappers.<HistoryOverdue>lambdaQuery().eq(HistoryOverdue::getScheduleId, currentOverdue.getScheduleId()));
-//								BigDecimal hisTotal = historyOverdueList.stream().map(item -> item.getReceivableAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
-//								//鏇存柊褰撳墠閫炬湡
-//								currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(hisTotal));
-//								currentOverdueMapper.updateById(currentOverdue);
-//							}
-//							break;
-//						}
-//					}
-//				}
-//				//鍚堝悓鎵�鏈変粯娆鹃樁娈�
-//				List<ContractPaymentSchedule> payComScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-//						.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0)
-//						.ne(ContractPaymentSchedule::getPaymentStatus, 3));
-//				if (ArrayUtil.isNotEmpty(payComScheduleList)) {
-//					BigDecimal payTotal = payComScheduleList.stream().map(item -> StrUtil.equals(item.getPaymentStatus() + "", "2") ? item.getPlannedAmount() :
-//							item.getActualAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
-//					contract.setPaidAmount(payTotal);
-////					if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
-////						contract.setContractStatus(3);
-////					}
-//					if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
-//
-//						contract.setContractStatus(3);
-//						payCompleteContractList.add(contract);
-//					}
-//					//浠樻 涓� 浠樻鏈叏浠�
-//					if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
-//						payNoCompleteContractList.add(contract);
-//					}
-//					if (StrUtil.isNotEmpty(contract.getContractCategory()) && StrUtil.equals(contract.getContractCategory(), "water_house")) {
-//						List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery()
-//								.eq(ContractExecDate::getContractId, contract.getId()).eq(ContractExecDate::getGenFlag, "0"));
-//						if (ArrayUtil.isEmpty(execDateList)) {
-//							List<ContractPaymentSchedule> contractPaymentScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-//									.eq(ContractPaymentSchedule::getContractId, contract.getId()));
-//							if (ArrayUtil.isNotEmpty(contractPaymentScheduleList) && contractPaymentScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
-//								contract.setContractStatus(3);
-//								contractService.updateById(contract);
-//							}
-//						}
-//					} else {
-//						contractService.updateById(contract);
-//					}
-//
-//				}
-//			});
+	}
 
-			//瀹㈡埛棰勪粯鏈夊墿浣� 涓� 鏈夐浠樻鐨勫悎鍚�
-//			if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
-//				if (ArrayUtil.isNotEmpty(payNoCompleteContractList)){
-//					//浣欓 鏀惧埌浠樻鏈畬鎴愬悎鍚� 棰勬敹
-//					Contract lastContract = payNoCompleteContractList.get(0);
-//					//淇濆瓨鏄庣粏
-//					saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
-//				}
-//				if (ArrayUtil.isEmpty(payNoCompleteContractList)&&ArrayUtil.isNotEmpty(advanceContractList)){
-//					//瀹㈡埛棰勪粯鏈夊墿浣� 娌℃湁棰勪粯娆剧殑鍚堝悓
-//					Contract lastContract = advanceContractList.get(0);
-//					//淇濆瓨鏄庣粏
-//					saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
-//				}
-//				if (ArrayUtil.isEmpty(payNoCompleteContractList)&&ArrayUtil.isEmpty(advanceContractList)
-//						&&ArrayUtil.isNotEmpty(payCompleteContractList)) {
-//					//瀹㈡埛棰勪粯鏈夊墿浣� 娌℃湁棰勪粯娆剧殑鍚堝悓
-//					Contract lastContract = payCompleteContractList.get(payCompleteContractList.size() - 1);
-//					//淇濆瓨鏄庣粏
-//					saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
-//
-//				}
-//			}
-//		}
-		return R.ok();
+	private void handleSlipToSchedule(PaymentConfirmAddDTO addDTO,Contract contract) {
+		//鏇存柊浠樻闃舵浠樻
+		List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
+				.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2).ne(ContractPaymentSchedule::getPaymentStatus, 3)
+				.orderByAsc(ContractPaymentSchedule::getCreateTime));
+		if (CollUtil.isNotEmpty(scheduleList)) {
+			BigDecimal remain = addDTO.getTransationAmount();
+			for (ContractPaymentSchedule schedule : scheduleList) {
+				BigDecimal lastRemain = remain;
+				remain = remain.subtract(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount() :
+						schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
+				if (remain.compareTo(new BigDecimal("0")) >= 0) {
+					schedule.setActualAmount(schedule.getPlannedAmount());
+					schedule.setPaymentDate(addDTO.getConfirmTime());
+					schedule.setPaymentStatus(2);
+					contractPaymentScheduleMapper.updateById(schedule);
+					//鏌ヨ鏄惁鏈夊綋鍓嶉�炬湡
+					CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
+							.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
+					if (ObjUtil.isNotNull(currentOverdue)) {
+						//鏂板閫炬湡鍘嗗彶
+						HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
+						historyOverdue.setPaymentTime(addDTO.getConfirmTime());
+						historyOverdue.setCompId(schedule.getCompId());
+						historyOverdueMapper.insert(historyOverdue);
+						//鍒犻櫎褰撳墠閫炬湡
+						currentOverdueMapper.deleteById(currentOverdue);
+					}
+				} else {
+					schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus() + "", "0") ? schedule.getPlannedAmount().add(remain) :
+							schedule.getActualAmount().add(lastRemain));
+					schedule.setPaymentDate(addDTO.getConfirmTime());
+					schedule.setPaymentStatus(1);
+					contractPaymentScheduleMapper.updateById(schedule);
+					//鏌ヨ鏄惁鏈夊綋鍓嶉�炬湡
+					CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId, schedule.getId())
+							.eq(CurrentOverdue::getContractId, schedule.getContractId()).last("limit 1"));
+					if (ObjUtil.isNotNull(currentOverdue)) {
+						//鏂板閫炬湡鍘嗗彶
+						HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue, HistoryOverdue.class, "id");
+						historyOverdue.setReceivableAmount(lastRemain);
+						historyOverdue.setPaymentTime(addDTO.getConfirmTime());
+						historyOverdue.setCompId(schedule.getCompId());
+						historyOverdueMapper.insert(historyOverdue);
+						//鏇存柊褰撳墠閫炬湡
+						currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
+						currentOverdueMapper.updateById(currentOverdue);
+					}
+					break;
+				}
+			}
+		}
+	}
+
+	private void savePaymentConfirm(PaymentConfirmAddDTO addDTO,Contract contract,BusinessCustomer customer) {
+
+		PaymentConfirm lastConfirm = baseMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId())
+				.orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
+		BigDecimal lastTotal = new BigDecimal("0");
+		if (ObjUtil.isNotNull(lastConfirm)) {
+			lastTotal = lastConfirm.getTotalAmount();
+		}
+		BigDecimal newtotal = addDTO.getTransationAmount().add(lastTotal);
+		PaymentConfirm entity = new PaymentConfirm();
+		entity.setContractId(contract.getId());
+		entity.setContractName(contract.getContractName());
+		entity.setContractNo(addDTO.getContractNo());
+		if (ObjUtil.isNotNull(lastConfirm)) {
+			entity.setScheduleId(lastConfirm.getScheduleId());
+			entity.setScheduleName(lastConfirm.getScheduleName());
+		}
+		entity.setBusGuestId(customer.getId());
+		entity.setBusGuestName(customer.getCompanyName());
+		entity.setBusinessType("瀹㈡埛浠樻");
+		if (newtotal.compareTo(new BigDecimal("0")) > 0) {
+			entity.setTransationAmount(addDTO.getTransationAmount());
+			entity.setAdvanceAmount(newtotal);
+			entity.setTotalAmount(newtotal);
+			entity.setConfirmTime(new Date());
+			baseMapper.insert(entity);
+		} else if (newtotal.compareTo(new BigDecimal("0")) == 0) {
+			entity.setTransationAmount(addDTO.getTransationAmount());
+			entity.setTotalAmount(newtotal);
+			entity.setConfirmTime(new Date());
+			baseMapper.insert(entity);
+		} else {
+			entity.setTransationAmount(addDTO.getTransationAmount());
+			entity.setReceivableAmount(newtotal.multiply(new BigDecimal("-1")));
+			entity.setTotalAmount(newtotal);
+			entity.setConfirmTime(new Date());
+			baseMapper.insert(entity);
+		}
+	}
+
+	private void savePaymentSlip(PaymentConfirmAddDTO addDTO,BusinessCustomer customer,Contract contract) {
+		//鏂板浠樻鍗�
+		PaymentSlip slip = new PaymentSlip();
+		slip.setBusGuestId(customer.getId());
+		slip.setBusGuestName(customer.getCompanyName());
+		slip.setPayeeCompId(SecurityUtils.getUser().getCompId());
+		slip.setPaymentTime(addDTO.getConfirmTime());
+		slip.setPaymentAmount(addDTO.getTransationAmount());
+		slip.setPayType(addDTO.getPayType());
+		slip.setContractId(contract.getId());
+		paymentSlipMapper.insert(slip);
 	}
 
 	@Override
@@ -515,6 +321,7 @@
 			}else {
 				params.put("enddate", DateUtil.today());
 			}
+			params.put("dc_flag","1");//1 鍊� 2 璐� 鍙煡鏀舵
 			//鍗曟嵁鐘舵�佸凡瀹℃壒
 			params.put("accentity_code", bipCode);
 			String finalAccessToken = accessToken;
@@ -601,27 +408,19 @@
 			return;
 		}
 		log.info("==================寮�濮嬪鐞嗛摱琛屾祦姘翠俊鎭�===================");
-		recordList.stream().forEach(record -> {
-
-			savePaymentConfirm((JSONObject) record, dept);
+		recordList.stream().forEach(obj -> {
+			JSONObject record = (JSONObject) obj;
+			BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
+					.eq(BusinessCustomer::getCompanyName, record.getString("to_acct_name"))
+					.eq(BusinessCustomer::getCompId,dept.getDeptId())
+			);
+			//淇濆瓨浠樻鍗�
+			Boolean codeFlag = saveBankStatement(customer, record,dept);
+			if (!codeFlag) {
+				log.error("浠樻鍗昳d锛歿},浠樻鍗曞凡瀛樺湪");
+			}
 		});
 		log.info("==================缁撴潫澶勭悊閾惰娴佹按淇℃伅===================");
-	}
-
-	private void savePaymentConfirm(JSONObject record, SysDept dept) {
-
-
-		BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
-				.eq(BusinessCustomer::getCompanyName, record.getString("to_acct_name"))
-				.eq(BusinessCustomer::getCompId,dept.getDeptId())
-		);
-				//淇濆瓨浠樻鍗�
-		Boolean codeFlag = saveBankStatement(customer, record,dept);
-		if (codeFlag) {
-			log.error("浠樻鍗昳d锛歿},浠樻鍗曞凡瀛樺湪");
-			return;
-		}
-
 	}
 
 	private Boolean saveBankStatement(BusinessCustomer customer, JSONObject record,SysDept dept) {
@@ -634,6 +433,7 @@
 		bankStatement.setPayBankAccName(record.getString("to_acct_name"));
 		bankStatement.setTranDate(DateUtil.parseDateTime(record.getString("tran_date")));
 		bankStatement.setTranAmount(record.getBigDecimal("tran_amt"));
+		bankStatement.setDcFlag(record.getString("dc_flag"));
 		bankStatement.setDataId(record.getLong("id"));
 		bankStatement.setDataJson(record.toJSONString());
 		if (ObjUtil.isNotNull(customer)) {

--
Gitblit v1.9.1