From c5c284896ce6428cffc5092e9cdef4fefc23409f Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期五, 18 九月 2026 17:12:49 +0800
Subject: [PATCH] feat: 石煤机客商产品查询处理 合同推送新增选商方式和是否关联交易

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java |  636 +++++++++++++++++++-------------------------------------
 1 files changed, 219 insertions(+), 417 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 fb66342..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
@@ -1,9 +1,8 @@
 package com.by4cloud.platformx.business.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.date.DatePattern;
+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;
@@ -31,7 +30,7 @@
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
 
 import java.io.UnsupportedEncodingException;
@@ -43,6 +42,8 @@
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
+
+;
 
 /**
  * 鏀舵纭
@@ -66,7 +67,7 @@
 	private final MeterReadRecordMapper meterReadRecordMapper;
 	private final BankStatementMapper bankStatementMapper;
 	private final RemoteDeptService remoteDeptService;
-	private final RedisTemplate redisTemplate;
+	private final StringRedisTemplate redisTemplate;
 
 	@Value("${bip.url}")
 	private String url;
@@ -92,113 +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());
-				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 (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
-						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())) {
@@ -206,278 +170,120 @@
 				}
 				paid = paid.add(addDTO.getTransationAmount());
 				contract.setPaidAmount(paid);
-				if (paid.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());
 				}
 				contractService.updateById(contract);
-			}else {
-				List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId,contract.getId())
-						.eq(ContractExecDate::getGenFlag,"0"));
-				List<MeterReadRecord> recordList = meterReadRecordMapper.selectList(Wrappers.<MeterReadRecord>lambdaQuery()
-						.eq(MeterReadRecord::getContractId,contract.getId())
-						.isNull(MeterReadRecord::getMeterReadNum));
-				if (ArrayUtil.isEmpty(execDateList.toArray())&&ArrayUtil.isEmpty(recordList.toArray())){
-					//鏇存柊鍚堝悓宸蹭粯娆�
-					BigDecimal paid = new BigDecimal("0");
-					if (ObjUtil.isNotNull(contract.getPaidAmount())) {
-						paid = contract.getPaidAmount();
-					}
-					paid = paid.add(addDTO.getTransationAmount());
-					contract.setPaidAmount(paid);
-					if (contract.getAmount().compareTo(new BigDecimal("0"))==0&&paid.compareTo(contract.getAmount()) >= 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.toArray())) {
-//				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.toArray())) {
-//					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.toArray())) {
-//					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.toArray())) {
-//							List<ContractPaymentSchedule> contractPaymentScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-//									.eq(ContractPaymentSchedule::getContractId, contract.getId()));
-//							if (ArrayUtil.isNotEmpty(contractPaymentScheduleList.toArray()) && 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.toArray())){
-//					//浣欓 鏀惧埌浠樻鏈畬鎴愬悎鍚� 棰勬敹
-//					Contract lastContract = payNoCompleteContractList.get(0);
-//					//淇濆瓨鏄庣粏
-//					saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
-//				}
-//				if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
-//					//瀹㈡埛棰勪粯鏈夊墿浣� 娌℃湁棰勪粯娆剧殑鍚堝悓
-//					Contract lastContract = advanceContractList.get(0);
-//					//淇濆瓨鏄庣粏
-//					saveNewPaymentConfirm(customer,null,lastContract,transtionRemainAmount,payDate);
-//				}
-//				if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isEmpty(advanceContractList.toArray())
-//						&&ArrayUtil.isNotEmpty(payCompleteContractList.toArray())) {
-//					//瀹㈡埛棰勪粯鏈夊墿浣� 娌℃湁棰勪粯娆剧殑鍚堝悓
-//					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
@@ -486,7 +292,7 @@
 		if (redisTemplate.hasKey("BIP_TOKEN")) {
 			accessToken = (String) redisTemplate.opsForValue().get("BIP_TOKEN");
 		} else {
-			getAccessToken(accessToken);
+			accessToken = getAccessToken(accessToken);
 		}
 		if (StrUtil.isEmpty(accessToken)) {
 			return R.failed("鑾峰彇token澶辫触");
@@ -495,6 +301,16 @@
 		for (String bipCode : bipCodeArr) {
 			//鍏ュ弬
 			JSONObject params = new JSONObject();
+			if (ObjUtil.isNotEmpty(dto.getPageSize())){
+				params.put("pageSize",dto.getPageSize());
+			}else {
+				params.put("pageSize",999);
+			}
+			if (ObjUtil.isNotEmpty(dto.getPageNum())){
+				params.put("pageNum",dto.getPageNum());
+			}else {
+				params.put("pageNum",1);
+			}
 			if (StrUtil.isNotBlank(dto.getStartDate())){
 				params.put("begindate",dto.getStartDate());
 			}else {
@@ -505,6 +321,7 @@
 			}else {
 				params.put("enddate", DateUtil.today());
 			}
+			params.put("dc_flag","1");//1 鍊� 2 璐� 鍙煡鏀舵
 			//鍗曟嵁鐘舵�佸凡瀹℃壒
 			params.put("accentity_code", bipCode);
 			String finalAccessToken = accessToken;
@@ -515,7 +332,7 @@
 					throw new RuntimeException(e);
 				}
 				log.info("閾惰娴佹按鏌ヨ Request:{}", params.toJSONString());
-				String result = HttpUtil.post(url + "/iuap-api-auth/yonbip/ctm/cmp/bankReconciliationQuery?access_token=" + finalAccessToken, params.toJSONString());
+				String result = HttpUtil.post(url + "/yonbip/ctm/cmp/bankReconciliationQuery?access_token=" + finalAccessToken, params.toJSONString());
 				log.info("閾惰娴佹按鏌ヨ Response:{}", result);
 				//淇濆瓨璇锋眰璁板綍
 				saveBipRequestRecord(params, "閾惰娴佹按鏌ヨ鎺ュ彛", result);
@@ -545,7 +362,7 @@
 		} catch (InvalidKeyException e) {
 			throw new RuntimeException(e);
 		}
-		String getTokenResult = HttpUtil.get(url + "/iuap-api-auth/open-auth/selfAppAuth/getAccessToken?appKey=" + appKey + "&timestamp=" + timestamp
+		String getTokenResult = HttpUtil.get(url + "/open-auth/selfAppAuth/getAccessToken?appKey=" + appKey + "&timestamp=" + timestamp
 				+ "&signature=" + signature);
 		log.info("Get access_token Response:{}", getTokenResult);
 		JSONObject tokenJson = JSONObject.parseObject(getTokenResult);
@@ -580,52 +397,33 @@
 			return;
 		}
 		SysDept dept = r.getData();
-		JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
+		JSONObject dataJson = resultJson.getJSONObject("data");
 		if (ObjUtil.isNull(dataJson)) {
 			log.error("杩斿洖缁撴灉涓璬ata瑙f瀽澶辫触");
 			return;
 		}
-		JSONArray recordList = JSONArray.parseArray(resultJson.getString("recordList"));
-		if (ArrayUtil.isEmpty(recordList.toArray())) {
+		JSONArray recordList = dataJson.getJSONArray("recordList");
+		if (CollUtil.isEmpty(recordList)) {
 			log.error("杩斿洖缁撴灉涓璻ecordList涓虹┖");
 			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) {
-
-		if (record.containsKey("to_acct_name")){
-			log.error("浠樻鍗昳d锛歿},浠樻鍏徃涓虹┖", record.getString("id"));
-			return;
-		}
-		BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
-				.eq(BusinessCustomer::getCompanyName, record.getString("to_acct_name"))
-				.eq(BusinessCustomer::getCompId,dept.getDeptId())
-		);
-		if (ObjUtil.isNull(customer)) {
-			log.error("浠樻鍗昳d锛歿},瀹㈠晢鏌ヨ澶辫触锛岃妫�鏌ュ鍟�", record.getString("id"));
-			return;
-		}
-		if (ObjUtil.isNull(record.getBigDecimal("tran_amt"))) {
-			log.error("浠樻鍗昳d锛歿},浜ゆ槗閲戦寮傚父", record.getString("id"));
-			return;
-		}
-		//淇濆瓨浠樻鍗�
-		Boolean codeFlag = saveBankStatement(customer, record);
-		if (codeFlag) {
-			log.error("浠樻鍗昳d锛歿},浠樻鍗曞凡瀛樺湪");
-			return;
-		}
-
-	}
-
-	private Boolean saveBankStatement(BusinessCustomer customer, JSONObject record) {
+	private Boolean saveBankStatement(BusinessCustomer customer, JSONObject record,SysDept dept) {
 		BankStatement bankStatement = new BankStatement();
 		bankStatement.setBankSeqNo(record.getString("bank_seq_no"));
 		bankStatement.setReceiveBankAcc(record.getString("bank_seq_no"));
@@ -635,14 +433,18 @@
 		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());
-		bankStatement.setBusGuestId(customer.getId());
+		if (ObjUtil.isNotNull(customer)) {
+			bankStatement.setBusGuestId(customer.getId());
+		}
 		BankStatement statement = bankStatementMapper.selectOne(Wrappers.<BankStatement>lambdaQuery().eq(BankStatement::getDataId,record.getLong("id")));
 		if (ObjUtil.isNotNull(statement)){
 			return false;
 		}
-		statement.setCompId(customer.getCompId());
+		bankStatement.setRemark(record.getString("remark"));
+		bankStatement.setCompId(dept.getDeptId());
 		bankStatementMapper.insert(bankStatement);
 		return true;
 	}
@@ -657,7 +459,7 @@
 	@Override
 	public R queryAging() {
 		List<AgingCompanyVo> companys = contractPaymentScheduleMapper.selectAgingCompany(null, DataScope.of("comp_id"));
-		if (ArrayUtil.isEmpty(companys.toArray())) {
+		if (CollUtil.isEmpty(companys)) {
 			return R.failed("鏆傛棤璐﹂緞");
 		}
 		List<AgingAnalysisVo> result = new ArrayList<>();
@@ -670,7 +472,7 @@
 			queryDTO.setCompanyId(company.getCompanyId());
 			List<AgingVo> companyAgingVo = contractPaymentScheduleMapper.selectAgingTotal(queryDTO);
 			//琛ュ叏鏁版嵁
-			if (ArrayUtil.isEmpty(companyAgingVo.toArray())) {
+			if (CollUtil.isEmpty(companyAgingVo)) {
 				companyAgingVo = new ArrayList<>();
 				AgingVo agingVo1 = new AgingVo(1L,"0-30澶�", new BigDecimal("0"), null);
 				AgingVo agingVo2 = new AgingVo(2L,"31-60澶�", new BigDecimal("0"), null);
@@ -686,27 +488,27 @@
 				companyAgingVo.add(agingVo6);
 			} else {
 				List<String> overdueRanges = companyAgingVo.stream().map(AgingVo::getOverdueRange).collect(Collectors.toList());
-				if (!ArrayUtil.contains(overdueRanges.toArray(), "0-30澶�")) {
+				if (!CollUtil.contains(overdueRanges, "0-30澶�")) {
 					AgingVo agingVo1 = new AgingVo(1L,"0-30澶�", new BigDecimal("0"), null);
 					companyAgingVo.add(agingVo1);
 				}
-				if (!ArrayUtil.contains(overdueRanges.toArray(), "31-60澶�")) {
+				if (!CollUtil.contains(overdueRanges, "31-60澶�")) {
 					AgingVo agingVo2 = new AgingVo(2L,"31-60澶�", new BigDecimal("0"), null);
 					companyAgingVo.add(agingVo2);
 				}
-				if (!ArrayUtil.contains(overdueRanges.toArray(), "61-90澶�")) {
+				if (!CollUtil.contains(overdueRanges, "61-90澶�")) {
 					AgingVo agingVo3 = new AgingVo(3L,"61-90澶�", new BigDecimal("0"), null);
 					companyAgingVo.add(agingVo3);
 				}
-				if (!ArrayUtil.contains(overdueRanges.toArray(), "91-180澶�")) {
+				if (!CollUtil.contains(overdueRanges, "91-180澶�")) {
 					AgingVo agingVo4 = new AgingVo(4L,"91-180澶�", new BigDecimal("0"), null);
 					companyAgingVo.add(agingVo4);
 				}
-				if (!ArrayUtil.contains(overdueRanges.toArray(), "181-365澶�")) {
+				if (!CollUtil.contains(overdueRanges, "181-365澶�")) {
 					AgingVo agingVo5 = new AgingVo(5L,"181-365澶�", new BigDecimal("0"), null);
 					companyAgingVo.add(agingVo5);
 				}
-				if (!ArrayUtil.contains(overdueRanges.toArray(), "1骞翠互涓�")) {
+				if (!CollUtil.contains(overdueRanges, "1骞翠互涓�")) {
 					AgingVo agingVo6 = new AgingVo(6L,"1骞翠互涓�", new BigDecimal("0"), null);
 					companyAgingVo.add(agingVo6);
 				}

--
Gitblit v1.9.1