From e02812df9217d3e50a368a8234c07c60c64c0ff4 Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期三, 05 八月 2026 08:34:26 +0800
Subject: [PATCH] feat:新增石煤机 机械厂模版合同/客户数据主数据同步接口/合同履约进度新增质保结束操作/合同开票状态 修改为待开 可开 已开/出库逻辑完善

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java |  820 ++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 510 insertions(+), 310 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 8bf75cf..43eec9f 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
@@ -13,6 +13,7 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.by4cloud.platformx.admin.api.entity.SysDept;
 import com.by4cloud.platformx.admin.api.feign.RemoteDeptService;
+import com.by4cloud.platformx.business.dto.AgingQueryDTO;
 import com.by4cloud.platformx.business.dto.PaymentConfirmAddDTO;
 import com.by4cloud.platformx.business.entity.*;
 import com.by4cloud.platformx.business.mapper.*;
@@ -20,7 +21,11 @@
 import com.by4cloud.platformx.business.service.ContractService;
 import com.by4cloud.platformx.business.service.PaymentConfirmService;
 import com.by4cloud.platformx.business.utils.SignHelper;
+import com.by4cloud.platformx.business.vo.AgingAnalysisVo;
+import com.by4cloud.platformx.business.vo.AgingCompanyVo;
+import com.by4cloud.platformx.business.vo.AgingVo;
 import com.by4cloud.platformx.common.core.util.R;
+import com.by4cloud.platformx.common.data.datascope.DataScope;
 import com.by4cloud.platformx.common.security.util.SecurityUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -30,10 +35,13 @@
 
 import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
 /**
  * 鏀舵纭
@@ -53,6 +61,8 @@
 	private final HistoryOverdueMapper historyOverdueMapper;
 	private final PaymentSlipMapper paymentSlipMapper;
 	private final BipRequestRecordMapper bipRequestRecordMapper;
+	private final ContractExecDateMapper contractExecDateMapper;
+	private final MeterReadRecordMapper meterReadRecordMapper;
 	private final RemoteDeptService remoteDeptService;
 	private final RedisTemplate redisTemplate;
 
@@ -72,9 +82,10 @@
 	public R add(PaymentConfirmAddDTO addDTO) {
 		if (StrUtil.isNotBlank(addDTO.getContractNo())) {
 			Contract contract = contractService.getOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo, addDTO.getContractNo()));
-			BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
-					.eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getCompanyName, addDTO.getBusGuestName())
-					.eq(StrUtil.isNotBlank(addDTO.getBusGuestId()), BusinessCustomer::getId, addDTO.getBusGuestId()));
+			if (ObjUtil.isNull(contract)) {
+				return R.failed("鍚堝悓鏌ヨ澶辫触锛岃妫�鏌ュ悎鍚岀紪鍙�");
+			}
+			BusinessCustomer customer = businessCustomerService.getById(contract.getPartyAId());
 			if (ObjUtil.isNull(customer)) {
 				return R.failed("瀹㈠晢鏌ヨ澶辫触锛岃妫�鏌ュ鍟�");
 			}
@@ -86,8 +97,21 @@
 				lastTotal = lastConfirm.getTotalAmount();
 			}
 
+
 			//杞叆
 			if (StrUtil.equals(addDTO.getInOrOut(), "1")) {
+
+
+				//鏂板浠樻鍗�
+				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 newtotal = addDTO.getTransationAmount().add(lastTotal);
 				PaymentConfirm entity = new PaymentConfirm();
 				entity.setContractId(contract.getId());
@@ -98,7 +122,7 @@
 					entity.setScheduleName(lastConfirm.getScheduleName());
 				}
 				entity.setBusGuestId(customer.getId());
-				entity.setBusGuestName(customer.getCompanyName());
+				entity.setBusGuestName(customer.getRegisterName());
 				entity.setBusinessType("瀹㈡埛浠樻");
 				if (newtotal.compareTo(new BigDecimal("0")) > 0) {
 					entity.setTransationAmount(addDTO.getTransationAmount());
@@ -118,60 +142,61 @@
 					entity.setConfirmTime(new Date());
 					baseMapper.insert(entity);
 				}
-				//鏇存柊浠樻闃舵浠樻
-				List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-						.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
-						.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);
+
+					//鏇存柊浠樻闃舵浠樻
+					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;
 							}
-						} 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);
-								//鏌ヨ鍘嗗彶宸蹭粯閫炬湡閲戦
-								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;
 						}
 					}
 				}
+			//闈炴埧灞嬬璧� 姘寸數
+			if (!StrUtil.equals(contract.getContractCategory(), "water_house")) {
 				//鏇存柊鍚堝悓宸蹭粯娆�
 				BigDecimal paid = new BigDecimal("0");
 				if (ObjUtil.isNotNull(contract.getPaidAmount())) {
@@ -179,10 +204,31 @@
 				}
 				paid = paid.add(addDTO.getTransationAmount());
 				contract.setPaidAmount(paid);
-				if (paid.compareTo(contract.getAmount()) > 0) {
+				if (paid.compareTo(contract.getAmount()) == 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")) {
@@ -192,8 +238,8 @@
 				entity.setContractNo(addDTO.getContractNo());
 
 				entity.setBusGuestId(customer.getId());
-				entity.setBusGuestName(customer.getCompanyName());
-				entity.setBusinessType("");
+				entity.setBusGuestName(customer.getRegisterName());
+				entity.setBusinessType("閫�娆�");
 				entity.setTransationAmount(addDTO.getTransationAmount());
 				BigDecimal newtotal = addDTO.getTransationAmount().multiply(new BigDecimal("-1")).add(lastConfirm.getTotalAmount());
 				if (newtotal.compareTo(new BigDecimal("0")) > 0) {
@@ -207,203 +253,228 @@
 				entity.setConfirmTime(new Date());
 				baseMapper.insert(entity);
 			}
-			// 鏌ヨ鎵�鏈変粯娆鹃樁娈垫槸鍚﹂兘浠樻瀹屾垚
-			List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId()));
-			if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
-				if (scheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
-					contract.setContractStatus(3);
-					contractService.updateById(contract);
-				}
-				;
-			}
-		}else {
 
-			List<Contract> customerContractList = contractService.list(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, addDTO.getBusGuestId())
-					.eq(Contract::getPartyBId,SecurityUtils.getUser().getCompId()).orderByAsc(Contract::getCreateTime).apply(" amount != paid_amount"));
-			if (ArrayUtil.isEmpty(customerContractList.toArray())) {
-				return R.failed("涓烘煡璇㈠埌鏈粯娆惧悎鍚�");
-			}
-			BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
-					.eq(StrUtil.isNotBlank(addDTO.getBusGuestName()), BusinessCustomer::getCompanyName, 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.getCompanyName());
-			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();
-			for (Contract contract : customerContractList) {
-				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();
-				}
-
-				//鍚堝悓鏈�杩戞槑缁嗘�婚涓�0 鎴� > 0 鎯呭喌涓�
-				if (lastTotal.compareTo(new BigDecimal("0")) >= 0) {
-					log.info("褰撳墠鍚堝悓鍙凤細{},鏄庣粏鎬婚鏃犲簲鏀舵", contract.getContractNo());
-					advanceContractList.add(contract);
-					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.getCompanyName());
-				entity.setBusinessType("瀹㈡埛浠樻");
-				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);
-				} 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.setTotalAmount(transtionAmount);
-					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)
-						.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)
-						);
-				if (ArrayUtil.isNotEmpty(payComScheduleList.toArray())){
-					BigDecimal payTotal = payComScheduleList.stream().map(item->StrUtil.equals(item.getPaymentStatus() + "", "0") ? 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);
-					}
-					contractService.updateById(contract);
-					if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
-						payCompleteContractList.add(contract);
-					}
-					//浠樻 涓� 浠樻鏈叏浠�
-					if (contract.getAmount().compareTo(contract.getPaidAmount()) > 0) {
-						payNoCompleteContractList.add(contract);
-					}
-				}
-			});
+		}
+//		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);
-
-				}
-			}
-		}
+//			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();
 	}
 
@@ -433,7 +504,7 @@
 				String result = HttpUtil.post(url + "/yonbip/EFI/collection/list?access_token=" + finalAccessToken, params.toJSONString());
 				log.info("SK_CX Response:{}", result);
 				//淇濆瓨璇锋眰璁板綍
-				saveBipRequestRecord(params, result);
+				saveBipRequestRecord(params, "<XSJZ_SK_CX_001>-鏀舵鍗曟煡璇㈡帴鍙�", result);
 				JSONObject resultJson = JSONObject.parseObject(result);
 				if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) {
 					handleAndSave(bipCode, resultJson);
@@ -444,7 +515,7 @@
 		return R.ok();
 	}
 
-	private JSONObject genParams(String bipCode){
+	private JSONObject genParams(String bipCode) {
 		JSONObject params = new JSONObject();
 		params.put("pageIndex", 1);
 		params.put("pageSize", 999);
@@ -461,7 +532,8 @@
 		return params;
 	}
 
-	private void getAccessToken(String accessToken) {
+	@Override
+	public String getAccessToken(String accessToken) {
 		long timestamp = System.currentTimeMillis();
 		Map<String, Object> paramMap = new HashMap<>();
 		paramMap.put("appKey", appKey);
@@ -482,20 +554,22 @@
 		JSONObject tokenJson = JSONObject.parseObject(getTokenResult);
 		if (!tokenJson.containsKey("code") || !tokenJson.getString("code").equals("200")) {
 			log.error("Get access_token ERROR");
-			return;
+			return "";
 		}
 		if (ObjUtil.isNull(tokenJson.get("data")) || ObjUtil.isNull(JSONObject.parseObject(tokenJson.getString("data")))) {
 			log.error("access_token value exception");
-			return;
+			return "";
 		}
 		accessToken = JSONObject.parseObject(tokenJson.getString("data")).getString("access_token");
 		log.info("access_token value:", accessToken);
 		redisTemplate.opsForValue().set("BIP_TOKEN", accessToken, tokenJson.getIntValue("expire") - 10, TimeUnit.SECONDS);
+		return accessToken;
 	}
 
-	private void saveBipRequestRecord(JSONObject params, String result) {
+	@Override
+	public void saveBipRequestRecord(JSONObject params, String methodName, String result) {
 		BipRequestRecord record = new BipRequestRecord();
-		record.setInterfaceName("<XSJZ_SK_CX_001>-鏀舵鍗曟煡璇㈡帴鍙�");
+		record.setInterfaceName(methodName);
 		record.setRequestParams(params.toJSONString());
 		record.setResponseParams(result);
 		record.setResponseCode(JSONObject.parseObject(result).getString("code"));
@@ -536,7 +610,7 @@
 			return;
 		}
 		BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
-				.eq(StrUtil.isNotBlank(record.getString("bodyItemCustomerName")), BusinessCustomer::getCompanyName, record.getString("bodyItemCustomerName"))
+				.eq(StrUtil.isNotBlank(record.getString("bodyItemCustomerName")), BusinessCustomer::getRegisterName, record.getString("bodyItemCustomerName"))
 		);
 		if (ObjUtil.isNull(customer)) {
 			log.error("浠樻鍗昳d锛歿},瀹㈠晢鏌ヨ澶辫触锛岃妫�鏌ュ鍟�", record.getString("id"));
@@ -547,8 +621,8 @@
 			return;
 		}
 		//淇濆瓨浠樻鍗�
-		Boolean codeFlag = savePaymentSlip(customer,record,dept);
-		if (codeFlag){
+		Boolean codeFlag = savePaymentSlip(customer, record, dept);
+		if (codeFlag) {
 			log.error("浠樻鍗昳d锛歿},浠樻鍗曞凡瀛樺湪");
 			return;
 		}
@@ -565,7 +639,9 @@
 		List<Contract> advanceContractList = new ArrayList<>();
 		//浠樻鏃堕棿
 		Date payDate = DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT);
-		for (Contract contract : contractList) {
+		Iterator<Contract> iterator = contractList.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");
@@ -590,16 +666,23 @@
 				entity.setScheduleName(lastConfirm.getScheduleName());
 			}
 			entity.setBusGuestId(customer.getId());
-			entity.setBusGuestName(customer.getCompanyName());
+			entity.setBusGuestName(customer.getRegisterName());
 			entity.setBusinessType("瀹㈡埛浠樻");
 			entity.setIsBip(1);
 			entity.setPaymentCode(record.getString("code"));
 			entity.setPayType(record.get("bodyItemSettleModeName") != null ? record.getString("bodyItemSettleModeName").contains("鎵垮厬姹囩エ") ? 1 : 0 : null);
 			entity.setBipId(record.getString("id"));
 			if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
-				entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
-				entity.setTotalAmount(new BigDecimal("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);
 			} else if (transtionRemainAmount.compareTo(new BigDecimal("0")) == 0) {
 				entity.setTransationAmount(lastTotal.multiply(new BigDecimal("-1")));
@@ -619,7 +702,7 @@
 		payContractList.stream().forEach(contract -> {
 			//鏇存柊浠樻闃舵浠樻
 			List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-					.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2)
+					.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 2).ne(ContractPaymentSchedule::getPaymentStatus, 3)
 					.orderByAsc(ContractPaymentSchedule::getCreateTime));
 			if (ArrayUtil.isNotEmpty(scheduleList.toArray())) {
 				BigDecimal remain = transtionAmount;
@@ -673,64 +756,83 @@
 			}
 			//鍚堝悓鎵�鏈変粯娆鹃樁娈�
 			List<ContractPaymentSchedule> payComScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
-					.eq(ContractPaymentSchedule::getContractId, contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus, 0)
+					.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() + "", "0") ? item.getPlannedAmount() :
-						item.getActualAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
+			if (ArrayUtil.isNotEmpty(payComScheduleList.toArray())) {
+				BigDecimal payTotal = payComScheduleList.stream().map(item -> StrUtil.equals(item.getPaymentStatus() + "", "0") ? 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);
-				}
-				contractService.updateById(contract);
+//				if (payComScheduleList.stream().allMatch(item -> Objects.equals(item.getPaymentStatus(), 2))) {
+//					contract.setContractStatus(3);
+//				}
+//				contractService.updateById(contract);
+//				if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
+//					payCompleteContractList.add(contract);
+//				}
 				if (contract.getAmount().compareTo(contract.getPaidAmount()) == 0) {
+//					contract.setBillingStatus("2");
+					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.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(payNoCompleteContractList.size() - 1);
-				//淇濆瓨鏄庣粏
-				saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
-			}
-			if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
-				//瀹㈡埛棰勪粯鏈夊墿浣� 娌℃湁棰勪粯娆剧殑鍚堝悓
-				Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
-				//淇濆瓨鏄庣粏
-				saveNewPaymentConfirm(customer,record,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,record,lastContract,transtionRemainAmount,payDate);
-
-			}
-		}
+//		if (transtionRemainAmount.compareTo(new BigDecimal("0")) > 0) {
+//			if (ArrayUtil.isNotEmpty(payNoCompleteContractList.toArray())){
+//				//浣欓 鏀惧埌浠樻鏈畬鎴愬悎鍚� 棰勬敹
+//				Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
+//				//淇濆瓨鏄庣粏
+//				saveNewPaymentConfirm(customer,record,lastContract,transtionRemainAmount,payDate);
+//			}
+//			if (ArrayUtil.isEmpty(payNoCompleteContractList.toArray())&&ArrayUtil.isNotEmpty(advanceContractList.toArray())){
+//				//瀹㈡埛棰勪粯鏈夊墿浣� 娌℃湁棰勪粯娆剧殑鍚堝悓
+//				Contract lastContract = payNoCompleteContractList.get(payNoCompleteContractList.size() - 1);
+//				//淇濆瓨鏄庣粏
+//				saveNewPaymentConfirm(customer,record,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,record,lastContract,transtionRemainAmount,payDate);
+//
+//			}
+//		}
 	}
 
-	private Boolean savePaymentSlip(BusinessCustomer customer, JSONObject record,SysDept dept) {
+	private Boolean savePaymentSlip(BusinessCustomer customer, JSONObject record, SysDept dept) {
 		PaymentSlip slip = new PaymentSlip();
 		slip.setBusGuestId(customer.getId());
-		slip.setBusGuestName(customer.getCompanyName());
+		slip.setBusGuestName(customer.getRegisterName());
 		slip.setPayee(dept.getName());
 		slip.setPayeeCompId(dept.getDeptId());
-		slip.setPaymentTime(ObjUtil.isNotNull(record.get("billDate"))?
-				DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT):new Date());
-		slip.setPaymentAmount(ObjUtil.isNotNull(record.get("bodyItemOriTaxExcludedAmount"))?record.getBigDecimal("bodyItemOriTaxExcludedAmount"):new BigDecimal("0"));
-		slip.setPayType(ObjUtil.isNotNull(record.get("bodyItemSettleModeName"))?record.getString("bodyItemSettleModeName"):"");
-		if (ObjUtil.isNotNull(record.get("code"))){
+		slip.setPaymentTime(ObjUtil.isNotNull(record.get("billDate")) ?
+				DateUtil.parse(record.getString("billDate"), DatePattern.NORM_DATETIME_FORMAT) : new Date());
+		slip.setPaymentAmount(ObjUtil.isNotNull(record.get("bodyItemOriTaxExcludedAmount")) ? record.getBigDecimal("bodyItemOriTaxExcludedAmount") : new BigDecimal("0"));
+		slip.setPayType(ObjUtil.isNotNull(record.get("bodyItemSettleModeName")) ? record.getString("bodyItemSettleModeName") : "");
+		if (ObjUtil.isNotNull(record.get("code"))) {
 			slip.setPaymentNumber(record.getString("code"));
-			PaymentSlip paymentSlip = paymentSlipMapper.selectOne(Wrappers.<PaymentSlip>lambdaQuery().eq(PaymentSlip::getPaymentNumber,slip.getPaymentNumber()));
+			PaymentSlip paymentSlip = paymentSlipMapper.selectOne(Wrappers.<PaymentSlip>lambdaQuery().eq(PaymentSlip::getPaymentNumber, slip.getPaymentNumber()));
 			if (ObjUtil.isNull(paymentSlip)) {
 				paymentSlipMapper.insert(slip);
 				return false;
@@ -739,8 +841,8 @@
 		return true;
 	}
 
-	public void saveNewPaymentConfirm(BusinessCustomer customer,JSONObject record,Contract contract,BigDecimal transtionRemainAmount
-		,Date payDate){
+	public void saveNewPaymentConfirm(BusinessCustomer customer, JSONObject record, Contract contract, BigDecimal transtionRemainAmount
+			, Date payDate) {
 		PaymentConfirm entity = new PaymentConfirm();
 		entity.setContractId(contract.getId());
 		entity.setContractName(contract.getContractName());
@@ -758,9 +860,9 @@
 		}
 
 		entity.setBusGuestId(customer.getId());
-		entity.setBusGuestName(customer.getCompanyName());
+		entity.setBusGuestName(customer.getRegisterName());
 		entity.setBusinessType("瀹㈡埛浠樻");
-		if (ObjUtil.isNotNull(record)){
+		if (ObjUtil.isNotNull(record)) {
 			entity.setIsBip(1);
 			entity.setPaymentCode(record.getString("code"));
 			entity.setPayType(record.get("bodyItemSettleModeName") != null ? record.getString("bodyItemSettleModeName").contains("鎵垮厬姹囩エ") ? 1 : 0 : null);
@@ -769,11 +871,109 @@
 		entity.setConfirmTime(payDate);
 		entity.setTransationAmount(transtionRemainAmount);
 		entity.setTotalAmount(transtionRemainAmount.add(lastTotal));
-		if (entity.getTotalAmount().compareTo(new BigDecimal("0"))>0){
+		if (entity.getTotalAmount().compareTo(new BigDecimal("0")) > 0) {
 			entity.setAdvanceAmount(entity.getTotalAmount());
-		}else if (entity.getTotalAmount().compareTo(new BigDecimal("0"))<0){
+		} else if (entity.getTotalAmount().compareTo(new BigDecimal("0")) < 0) {
 			entity.setReceivableAmount(entity.getTotalAmount().multiply(new BigDecimal("-1")));
 		}
 		baseMapper.insert(entity);
 	}
+
+	@Override
+	public R queryAllBusinessType() {
+		return R.ok(baseMapper.selectList(Wrappers.<PaymentConfirm>lambdaQuery().select(PaymentConfirm::getBusinessType).isNotNull(PaymentConfirm::getBusinessType)
+				.groupBy(PaymentConfirm::getBusinessType)).stream().map(PaymentConfirm::getBusinessType).collect(Collectors.toList()));
+	}
+
+	@Override
+	public R queryAging() {
+		List<AgingCompanyVo> companys = contractPaymentScheduleMapper.selectAgingCompany(null, DataScope.of("comp_id"));
+		if (ArrayUtil.isEmpty(companys.toArray())) {
+			return R.failed("鏆傛棤璐﹂緞");
+		}
+		List<AgingAnalysisVo> result = new ArrayList<>();
+		AgingQueryDTO queryDTO = new AgingQueryDTO();
+		List<AgingVo> agingVoList = contractPaymentScheduleMapper.selectAgingTotal(null);
+		companys.stream().forEach(company -> {
+			BusinessCustomer businessCustomer = businessCustomerService.getById(company.getCompanyId());
+			AgingAnalysisVo agingAnalysisVo = new AgingAnalysisVo();
+			agingAnalysisVo.setCompanyName(businessCustomer.getCompanyName());
+			queryDTO.setCompanyId(company.getCompanyId());
+			List<AgingVo> companyAgingVo = contractPaymentScheduleMapper.selectAgingTotal(queryDTO);
+			//琛ュ叏鏁版嵁
+			if (ArrayUtil.isEmpty(companyAgingVo.toArray())) {
+				companyAgingVo = new ArrayList<>();
+				AgingVo agingVo1 = new AgingVo("0-30澶�", new BigDecimal("0"), null);
+				AgingVo agingVo2 = new AgingVo("31-60澶�", new BigDecimal("0"), null);
+				AgingVo agingVo3 = new AgingVo("61-90澶�", new BigDecimal("0"), null);
+				AgingVo agingVo4 = new AgingVo("91-180澶�", new BigDecimal("0"), null);
+				AgingVo agingVo5 = new AgingVo("181-365澶�", new BigDecimal("0"), null);
+				AgingVo agingVo6 = new AgingVo("1骞翠互涓�", new BigDecimal("0"), null);
+				companyAgingVo.add(agingVo1);
+				companyAgingVo.add(agingVo2);
+				companyAgingVo.add(agingVo3);
+				companyAgingVo.add(agingVo4);
+				companyAgingVo.add(agingVo5);
+				companyAgingVo.add(agingVo6);
+			} else {
+				List<String> overdueRanges = companyAgingVo.stream().map(AgingVo::getOverdueRange).collect(Collectors.toList());
+				if (!ArrayUtil.contains(overdueRanges.toArray(), "0-30澶�")) {
+					AgingVo agingVo1 = new AgingVo("0-30澶�", new BigDecimal("0"), null);
+					companyAgingVo.add(agingVo1);
+				}
+				if (!ArrayUtil.contains(overdueRanges.toArray(), "31-60澶�")) {
+					AgingVo agingVo2 = new AgingVo("31-60澶�", new BigDecimal("0"), null);
+					companyAgingVo.add(agingVo2);
+				}
+				if (!ArrayUtil.contains(overdueRanges.toArray(), "61-90澶�")) {
+					AgingVo agingVo3 = new AgingVo("61-90澶�", new BigDecimal("0"), null);
+					companyAgingVo.add(agingVo3);
+				}
+				if (!ArrayUtil.contains(overdueRanges.toArray(), "91-180澶�")) {
+					AgingVo agingVo4 = new AgingVo("91-180澶�", new BigDecimal("0"), null);
+					companyAgingVo.add(agingVo4);
+				}
+				if (!ArrayUtil.contains(overdueRanges.toArray(), "181-365澶�")) {
+					AgingVo agingVo5 = new AgingVo("181-365澶�", new BigDecimal("0"), null);
+					companyAgingVo.add(agingVo5);
+				}
+				if (!ArrayUtil.contains(overdueRanges.toArray(), "1骞翠互涓�")) {
+					AgingVo agingVo6 = new AgingVo("1骞翠互涓�", new BigDecimal("0"), null);
+					companyAgingVo.add(agingVo6);
+				}
+			}
+			agingAnalysisVo.setAmount(companyAgingVo.stream().map(AgingVo::getTotalAmount).collect(Collectors.toList()));
+			List<AgingVo> finalCompanyAgingVo = companyAgingVo;
+			agingAnalysisVo.setPercent(IntStream.range(0, agingVoList.size())
+					.mapToObj(i -> {
+						BigDecimal part = finalCompanyAgingVo.get(i).getTotalAmount();
+						BigDecimal total = agingVoList.get(i).getTotalAmount();
+						if (total == null || total.compareTo(BigDecimal.ZERO) == 0) {
+							return null;
+						}
+						return part.divide(total, 4, RoundingMode.HALF_UP)
+								.multiply(new BigDecimal("100"))
+								.setScale(2, RoundingMode.HALF_UP);
+					})
+					.collect(Collectors.toList()));
+			result.add(agingAnalysisVo);
+		});
+		//鍚堣
+		AgingAnalysisVo agingAnalysisVo = new AgingAnalysisVo();
+		agingAnalysisVo.setCompanyName("鍚堣");
+		agingAnalysisVo.setAmount(agingVoList.stream().map(AgingVo::getTotalAmount).collect(Collectors.toList()));
+		List<BigDecimal> percent = IntStream.range(0, agingAnalysisVo.getAmount().size())
+				.mapToObj(i -> {
+
+					if (agingAnalysisVo.getAmount().get(i) == null || agingAnalysisVo.getAmount().get(i).compareTo(BigDecimal.ZERO) == 0) {
+						return null;
+					} else {
+						return new BigDecimal("100");
+					}
+				})
+				.collect(Collectors.toList());
+		agingAnalysisVo.setPercent(percent);
+		result.add(agingAnalysisVo);
+		return R.ok(result);
+	}
 }
\ No newline at end of file

--
Gitblit v1.9.1