From fb9fca375c78c5b79acf6db990357816f86100fb Mon Sep 17 00:00:00 2001
From: shiyunteng <shiyunteng@example.com>
Date: 星期三, 13 五月 2026 15:39:31 +0800
Subject: [PATCH] feat:合同、出库、资金明细、发票

---
 platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/PaymentConfirmServiceImpl.java |  138 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 134 insertions(+), 4 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 36991f3..8451ea7 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,12 +1,28 @@
 package com.by4cloud.platformx.business.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.ObjUtil;
+import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.by4cloud.platformx.business.entity.PaymentConfirm;
+import com.by4cloud.platformx.business.dto.PaymentConfirmAddDTO;
+import com.by4cloud.platformx.business.entity.*;
+import com.by4cloud.platformx.business.mapper.ContractPaymentScheduleMapper;
+import com.by4cloud.platformx.business.mapper.CurrentOverdueMapper;
+import com.by4cloud.platformx.business.mapper.HistoryOverdueMapper;
 import com.by4cloud.platformx.business.mapper.PaymentConfirmMapper;
+import com.by4cloud.platformx.business.service.BusinessCustomerService;
+import com.by4cloud.platformx.business.service.ContractPaymentScheduleService;
+import com.by4cloud.platformx.business.service.ContractService;
 import com.by4cloud.platformx.business.service.PaymentConfirmService;
+import com.by4cloud.platformx.common.core.util.R;
+import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
 import java.util.Date;
+import java.util.List;
 
 /**
  * 鏀舵纭
@@ -15,11 +31,125 @@
  * @date 2026-04-29 11:33:26
  */
 @Service
+@RequiredArgsConstructor
 public class PaymentConfirmServiceImpl extends ServiceImpl<PaymentConfirmMapper, PaymentConfirm> implements PaymentConfirmService {
 
+	private final ContractService contractService;
+	private final BusinessCustomerService businessCustomerService;
+	private final ContractPaymentScheduleMapper contractPaymentScheduleMapper;
+	private final CurrentOverdueMapper currentOverdueMapper;
+	private final HistoryOverdueMapper historyOverdueMapper;
+
 	@Override
-	public boolean save(PaymentConfirm entity) {
-		entity.setConfirmTime(new Date());
-		return super.save(entity);
+	public R add(PaymentConfirmAddDTO addDTO) {
+		Contract contract = contractService.getOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,addDTO.getContractNo()));
+		if(ObjUtil.isNull(contract)){
+			return R.failed("鍚堝悓鏌ヨ澶辫触锛岃妫�鏌ュ悎鍚岀紪鍙�");
+		}
+		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(customer)){
+			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")){
+			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);
+			}
+			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) {
+					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());
+							historyOverdueMapper.insert(historyOverdue);
+							//鍒犻櫎褰撳墠閫炬湡
+							currentOverdueMapper.deleteById(currentOverdue);
+						}
+					}else {
+						schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus()+"","0")?schedule.getPlannedAmount().add(remain):
+								schedule.getPlannedAmount().subtract(schedule.getActualAmount()).add(remain));
+						schedule.setPaymentDate(addDTO.getConfirmTime());
+						schedule.setPaymentStatus(1);
+						contractPaymentScheduleMapper.updateById(schedule);
+						break;
+					}
+				}
+			}
+		}
+		//杞嚭
+		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);
+		}
+		return R.ok();
 	}
 }
\ No newline at end of file

--
Gitblit v1.9.1