From d1fac30e634e33edd29e3440de1f91da84c150c1 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期五, 05 六月 2026 15:38:39 +0800
Subject: [PATCH] feat(account): 新增付款状态管理功能并优化生产任务查询

---
 src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java |   72 +++++++++++++++++++++++++++++++++---
 1 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java b/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
index b9953a0..7d0740d 100644
--- a/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
+++ b/src/main/java/com/ruoyi/account/service/impl/purchase/AccountPurchasePaymentServiceImpl.java
@@ -54,14 +54,31 @@
         if (StringUtils.isEmpty(accountPurchasePayment.getPaymentNumber())) {
             accountPurchasePayment.setPaymentNumber(genAccountPurchasePaymentNo());
         }
-        //鏍¢獙绱浠樻閲戦涓嶈兘瓒呰繃鐢宠閲戦
+        //鏍¢獙浠樻鐢宠鏄惁瀛樺湪涓斿鏍搁�氳繃
         AccountPaymentApplication accountPaymentApplication = accountPaymentApplicationMapper.selectById(accountPurchasePayment.getAccountPaymentApplicationId());
-        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectList(Wrappers.<AccountPurchasePayment>lambdaQuery().eq(AccountPurchasePayment::getAccountPaymentApplicationId, accountPurchasePayment.getAccountPaymentApplicationId()));
-        BigDecimal totalPaymentAmount = accountPurchasePayments.stream().map(AccountPurchasePayment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
-        if (accountPaymentApplication.getPaymentAmount().compareTo(totalPaymentAmount) < 0) {
+        if (accountPaymentApplication == null) {
+            throw new ServiceException("浠樻鐢宠涓嶅瓨鍦�");
+        }
+        if (accountPaymentApplication.getStatus() == null || accountPaymentApplication.getStatus() != 1) {
+            throw new ServiceException("浠樻鐢宠鏈鏍搁�氳繃锛屼笉鑳戒粯娆�");
+        }
+        //鏍¢獙绱浠樻閲戦涓嶈兘瓒呰繃鐢宠閲戦
+        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectList(
+                Wrappers.<AccountPurchasePayment>lambdaQuery()
+                        .eq(AccountPurchasePayment::getAccountPaymentApplicationId, accountPurchasePayment.getAccountPaymentApplicationId()));
+        BigDecimal totalPaymentAmount = accountPurchasePayments.stream()
+                .map(AccountPurchasePayment::getPaymentAmount)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+        BigDecimal newTotal = totalPaymentAmount.add(accountPurchasePayment.getPaymentAmount());
+        if (newTotal.compareTo(accountPaymentApplication.getPaymentAmount()) > 0) {
             throw new ServiceException("绱浠樻閲戦涓嶈兘瓒呰繃鐢宠閲戦");
         }
-        return save(accountPurchasePayment);
+        boolean result = save(accountPurchasePayment);
+        // 鏇存柊浠樻鐢宠鐨勪粯娆剧姸鎬�
+        if (result) {
+            updatePaymentStatus(accountPaymentApplication, newTotal);
+        }
+        return result;
     }
 
     @Override
@@ -73,6 +90,9 @@
 
     @Override
     public boolean deleteAccountPurchasePayment(List<Long> ids) {
+        if (CollectionUtils.isEmpty(ids)) {
+            return false;
+        }
         //濡傛灉璇ヤ粯娆惧崟宸茬粡鐢熸垚瀵硅处鍗曞垯鏃犳硶鍒犻櫎
         List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectByIds(ids);
         List<String> strings = accountPurchasePayments.stream().map(AccountPurchasePayment::getPaymentNumber).toList();
@@ -81,7 +101,47 @@
         if (CollectionUtils.isNotEmpty(accountStatementDetails)){
             throw new ServiceException("璇ヤ粯娆惧崟宸茬粡鐢熸垚瀵硅处鍗曪紝鏃犳硶鍒犻櫎");
         }
-        return removeByIds(ids);
+        boolean result = removeByIds(ids);
+        // 鍒犻櫎鎴愬姛鍚庯紝鏇存柊浠樻鐢宠鐨勪粯娆剧姸鎬�
+        if (result) {
+            for (AccountPurchasePayment payment : accountPurchasePayments) {
+                if (payment.getAccountPaymentApplicationId() != null) {
+                    AccountPaymentApplication application = accountPaymentApplicationMapper.selectById(payment.getAccountPaymentApplicationId());
+                    if (application != null) {
+                        // 璁$畻鍓╀綑浠樻閲戦
+                        List<AccountPurchasePayment> remainingPayments = accountPurchasePaymentMapper.selectList(
+                                Wrappers.<AccountPurchasePayment>lambdaQuery()
+                                        .eq(AccountPurchasePayment::getAccountPaymentApplicationId, application.getId()));
+                        BigDecimal remainingAmount = remainingPayments.stream()
+                                .map(AccountPurchasePayment::getPaymentAmount)
+                                .reduce(BigDecimal.ZERO, BigDecimal::add);
+                        updatePaymentStatus(application, remainingAmount);
+                    }
+                }
+            }
+        }
+        return result;
+    }
+
+    /**
+     * 鏇存柊浠樻鐢宠鐨勪粯娆剧姸鎬�
+     * @param application 浠樻鐢宠
+     * @param paidAmount 宸蹭粯娆鹃噾棰�
+     */
+    private void updatePaymentStatus(AccountPaymentApplication application, BigDecimal paidAmount) {
+        BigDecimal applyAmount = application.getPaymentAmount();
+        int newPaymentStatus;
+        if (paidAmount.compareTo(BigDecimal.ZERO) == 0) {
+            newPaymentStatus = 0; // 鏈粯娆�
+        } else if (paidAmount.compareTo(applyAmount) < 0) {
+            newPaymentStatus = 1; // 閮ㄥ垎浠樻
+        } else {
+            newPaymentStatus = 2; // 宸蹭粯娆�
+        }
+        if (application.getPaymentStatus() == null || application.getPaymentStatus() != newPaymentStatus) {
+            application.setPaymentStatus(newPaymentStatus);
+            accountPaymentApplicationMapper.updateById(application);
+        }
     }
 
     private String genAccountPurchasePaymentNo() {

--
Gitblit v1.9.3