From 8824690922b92b6a7e8879766570622dc21bc7cc Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期一, 26 十月 2026 20:53:02 +0800
Subject: [PATCH] feat(account): 对账支持按日,检验报告日期与单位修正

---
 src/main/java/com/ruoyi/account/service/impl/AccountStatementServiceImpl.java |   61 +++++++++++++++++-------------
 1 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/src/main/java/com/ruoyi/account/service/impl/AccountStatementServiceImpl.java b/src/main/java/com/ruoyi/account/service/impl/AccountStatementServiceImpl.java
index 411d595..aa9d78a 100644
--- a/src/main/java/com/ruoyi/account/service/impl/AccountStatementServiceImpl.java
+++ b/src/main/java/com/ruoyi/account/service/impl/AccountStatementServiceImpl.java
@@ -37,6 +37,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.YearMonth;
 import java.time.format.DateTimeFormatter;
@@ -69,11 +70,18 @@
 
     @Override
     public StatementAccountVo getAccountStatementDetailsByMonth(StatementAccountDto statementAccountDto) {
-        //瀵硅处鏈堜唤杞崲鎴愬紑濮嬫棩鏈熷拰缁撴潫鏃ユ湡鍖洪棿
-        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
-        YearMonth yearMonth = YearMonth.parse(statementAccountDto.getStatementMonth(), formatter);
-        statementAccountDto.setStartDate(yearMonth.atDay(1));
-        statementAccountDto.setEndDate(yearMonth.atEndOfMonth());
+        //瀵硅处鏈熼棿鏀寔鎸夋棩(yyyy-MM-dd)鎴栨寜鏈�(yyyy-MM)锛岃浆鎹㈡垚寮�濮嬫棩鏈熷拰缁撴潫鏃ユ湡鍖洪棿
+        String statementMonth = statementAccountDto.getStatementMonth();
+        if (statementMonth != null && statementMonth.length() == 10) {
+            LocalDate day = LocalDate.parse(statementMonth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+            statementAccountDto.setStartDate(day);
+            statementAccountDto.setEndDate(day);
+        } else {
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
+            YearMonth yearMonth = YearMonth.parse(statementMonth, formatter);
+            statementAccountDto.setStartDate(yearMonth.atDay(1));
+            statementAccountDto.setEndDate(yearMonth.atEndOfMonth());
+        }
         if (statementAccountDto.getAccountType() == 1){
             //搴旀敹瀵硅处--Customer
             return getAccountStatementDetailsByCustomerAndMonth(statementAccountDto);
@@ -85,13 +93,13 @@
 
     @Override
     public boolean addAccountStatement(StatementAccountVo statementAccountVo) {
-        //鍚屼竴瀹㈡埛鎴栬�呭悓涓�渚涘簲鍟�,涓�涓湀浠藉彧鑳芥湁涓�涓璐﹀崟
+        //鍚屼竴瀹㈡埛鎴栬�呭悓涓�渚涘簲鍟�,鍚屼竴瀵硅处鏈熼棿鍙兘鏈変竴涓璐﹀崟
         List<AccountStatement> accountStatements = accountStatementMapper.selectList(Wrappers.<AccountStatement>lambdaQuery()
                 .eq(AccountStatement::getStatementMonth, statementAccountVo.getStatementMonth())
                 .eq(AccountStatement::getAccountType, statementAccountVo.getAccountType())
                 .eq(AccountStatement::getCustomerId, statementAccountVo.getCustomerId()));
         if (CollectionUtils.isNotEmpty(accountStatements)){
-            throw new ServiceException("鍚屼竴瀹㈡埛鎴栬�呭悓涓�渚涘簲鍟�,涓�涓湀浠藉彧鑳芥湁涓�涓璐﹀崟");
+            throw new ServiceException("鍚屼竴瀹㈡埛鎴栬�呭悓涓�渚涘簲鍟�,鍚屼竴瀵硅处鏈熼棿鍙兘鏈変竴涓璐﹀崟");
         }
         AccountStatement accountStatement = new AccountStatement();
         BeanUtils.copyProperties(statementAccountVo, accountStatement);
@@ -187,16 +195,8 @@
             accountStatementDetails.setRemark("浜у搧閫�璐э紝鍘熷洜锛�"+salesReturnVo.getReturnReason());
             accountStatementDetailsList.add(accountStatementDetails);
         });
-        //鏈熷垵浣欓=涓婁釜鏈堢殑鏈熸湯浣欓
-        statementAccountVo.setOpeningBalance(BigDecimal.ZERO);
-        List<AccountStatement> accountStatements = accountStatementMapper.selectList(Wrappers.<AccountStatement>lambdaQuery()
-                .eq(AccountStatement::getAccountType, 1)
-                .eq(AccountStatement::getCustomerId, statementAccountDto.getCustomerId())
-                .eq(AccountStatement::getStatementMonth,
-                        YearMonth.parse(statementAccountDto.getStatementMonth()).minusMonths(1).toString()));
-        if (CollectionUtils.isNotEmpty(accountStatements)){
-            statementAccountVo.setOpeningBalance(accountStatements.get(accountStatements.size() - 1).getClosingBalance());
-        }
+        //鏈熷垵浣欓=涓婁竴鏈燂紙涓婁竴鏈�/涓婁竴澶╋級瀵硅处鍗曠殑鏈熸湯浣欓
+        statementAccountVo.setOpeningBalance(getPreviousClosingBalance(1, statementAccountDto.getCustomerId(), statementAccountDto.getStatementMonth()));
         //鏈湡搴旀敹=鍑哄簱-閫�璐ч噾棰濈疮璁�
         statementAccountVo.setCurrentPlan(salesOutboundVos.stream().map(SalesOutboundVo::getOutboundAmount).reduce(BigDecimal.ZERO, BigDecimal::add)
                 .subtract(salesReturnVos.stream().map(SalesReturnVo::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add)));
@@ -271,16 +271,8 @@
             accountStatementDetails.setRemark("浜у搧閫�璐э紝閫�璐ф柟寮忥細"+purchaseReturnVo.getReturnType());
             accountStatementDetailsList.add(accountStatementDetails);
         });
-        //鏈熷垵浣欓=涓婁釜鏈堢殑鏈熸湯浣欓
-        statementAccountVo.setOpeningBalance(BigDecimal.ZERO);
-        List<AccountStatement> accountStatements = accountStatementMapper.selectList(Wrappers.<AccountStatement>lambdaQuery()
-                .eq(AccountStatement::getAccountType, 2)
-                .eq(AccountStatement::getCustomerId, statementAccountDto.getCustomerId())
-                .eq(AccountStatement::getStatementMonth,
-                        YearMonth.parse(statementAccountDto.getStatementMonth()).minusMonths(1).toString()));
-        if (CollectionUtils.isNotEmpty(accountStatements)){
-            statementAccountVo.setOpeningBalance(accountStatements.get(accountStatements.size() - 1).getClosingBalance());
-        }
+        //鏈熷垵浣欓=涓婁竴鏈燂紙涓婁竴鏈�/涓婁竴澶╋級瀵硅处鍗曠殑鏈熸湯浣欓
+        statementAccountVo.setOpeningBalance(getPreviousClosingBalance(2, statementAccountDto.getCustomerId(), statementAccountDto.getStatementMonth()));
         //鏈湡搴斾粯=鍏ュ簱-閫�璐ч噾棰濈疮璁�
         statementAccountVo.setCurrentPlan(purchaseInboundVos.stream().map(PurchaseInboundVo::getInboundAmount).reduce(BigDecimal.ZERO, BigDecimal::add)
                 .subtract(purchaseReturnVos.stream().map(PurchaseReturnVo::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add)));
@@ -292,6 +284,21 @@
         return statementAccountVo;
     }
 
+    /**
+     * 鍙栧悓涓�瀹㈡埛/渚涘簲鍟嗗湪褰撳墠瀵硅处鏈熼棿涔嬪墠鏈�杩戜竴鏈熷璐﹀崟鐨勬湡鏈綑棰濓紝浣滀负鏈湡鏈熷垵浣欓銆�
+     * 鏀寔鎸夋湀(yyyy-MM)鍜屾寜鏃�(yyyy-MM-dd)涓ょ瀵硅处鏈熼棿銆�
+     */
+    private BigDecimal getPreviousClosingBalance(Integer accountType, Long customerId, String statementMonth) {
+        AccountStatement previous = accountStatementMapper.selectOne(Wrappers.<AccountStatement>lambdaQuery()
+                .eq(AccountStatement::getAccountType, accountType)
+                .eq(AccountStatement::getCustomerId, customerId)
+                .lt(AccountStatement::getStatementMonth, statementMonth)
+                .orderByDesc(AccountStatement::getStatementMonth)
+                .last("LIMIT 1"));
+        return previous == null || previous.getClosingBalance() == null
+                ? BigDecimal.ZERO : previous.getClosingBalance();
+    }
+
     private String genStatementAccountNo() {
         return "DZ" + LocalDateTime.now().format(CODE_TIME_FORMATTER) + new Random().nextInt(10);
     }

--
Gitblit v1.9.3