From d90a45a49ddb0ae512ef97b5e6fc981caab5e158 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期二, 15 九月 2026 15:49:26 +0800
Subject: [PATCH] refactor(home): 优化产品进出库分析功能的数据库查询逻辑

---
 src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java |   45 ++++++++++++++++++++-------------------------
 1 files changed, 20 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java b/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
index f09323d..93bb807 100644
--- a/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
+++ b/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
@@ -185,28 +185,31 @@
         LambdaQueryWrapper<PurchaseLedger> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.ge(PurchaseLedger::getEntryDate, currentMonth.atDay(1).atStartOfDay()) // 澶т簬绛変簬鏈湀绗竴澶�
                 .lt(PurchaseLedger::getEntryDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 灏忎簬涓嬫湀绗竴澶�
-        // 鎵ц鏌ヨ骞惰绠楁�诲拰
+
         List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(queryWrapper);
         if (!CollectionUtils.isEmpty(purchaseLedgers)) {
-            LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductMapperLambdaQueryWrapperCopy = new LambdaQueryWrapper<SalesLedgerProduct>();
-            salesLedgerProductMapperLambdaQueryWrapperCopy.eq(SalesLedgerProduct::getType, 2)
+            LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductMapperLambdaQueryWrapperCopy = new LambdaQueryWrapper<>();
+            salesLedgerProductMapperLambdaQueryWrapperCopy.eq(SalesLedgerProduct::getType, 2) // 閲囪喘绫诲瀷
                     .in(SalesLedgerProduct::getSalesLedgerId,
                             purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList()));
+
             List<SalesLedgerProduct> salesLedgerProductsCopy = salesLedgerProductMapper
                     .selectList(salesLedgerProductMapperLambdaQueryWrapperCopy);
-            // 鍚堣鍚堝悓閲戦
+
+            // 鍚堣鍚堝悓鎬婚噾棰�
             BigDecimal receiveAmount = purchaseLedgers.stream()
                     .map(PurchaseLedger::getContractAmount)
                     .filter(Objects::nonNull)
                     .reduce(BigDecimal.ZERO, BigDecimal::add);
-            // 鏈紑绁ㄩ噾棰�
+
+            //  寰呬粯娆炬�婚噾棰�
             BigDecimal unReceiptPaymentAmount = salesLedgerProductsCopy.stream()
-                    .map(SalesLedgerProduct::getNoInvoiceAmount)
+                    .map(SalesLedgerProduct::getPendingTicketsTotal)
                     .filter(Objects::nonNull)
                     .reduce(BigDecimal.ZERO, BigDecimal::add);
+
             homeBusinessDto.setMonthPurchaseMoney(receiveAmount.setScale(2, RoundingMode.HALF_UP).toString());
-            homeBusinessDto
-                    .setMonthPurchaseHaveMoney(unReceiptPaymentAmount.setScale(2, RoundingMode.HALF_UP).toString());
+            homeBusinessDto.setMonthPurchaseHaveMoney(unReceiptPaymentAmount.setScale(2, RoundingMode.HALF_UP).toString());
         }
         // 缁熻搴撳瓨
         BigDecimal stockQuantityTotal = stockInventoryMapper.selectTotal();
@@ -1085,26 +1088,18 @@
 
     @Override
     public List<Map<String, Object>> productInOutAnalysis(Integer type) {
-        String targetName;
-        if (type == 1) {
-            targetName = "鍘熸潗鏂�";
+        // 1=鍘熸潗鏂� 2=鎴愬搧 3=鍗婃垚鍝侊紝涓庡墠绔� ProductTypeSwitch 鍙栧�间竴鑷达紱
+        // 鑼冨洿鍒ゅ畾鏀逛负鎸夊垎绫绘爲鏍硅妭鐐圭殑 product_type锛屽吋瀹瑰師鏉愭枡澶氫釜鏍瑰垎绫诲強鎴愬搧澶氱骇鍒嗙被
+        String productScope;
+        if (type == null || type == 1) {
+            productScope = "鍘熸潗鏂�";
         } else if (type == 2) {
-            targetName = "鎴愬搧";
+            productScope = "鎴愬搧";
         } else if (type == 3) {
-            targetName = "鍗婃垚鍝�";
+            productScope = "鍗婃垚鍝�";
         } else {
             return new ArrayList<>();
         }
-
-        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(Product::getProductName, targetName).isNull(Product::getParentId);
-        Product rootCategory = productMapper.selectOne(queryWrapper);
-
-        if (rootCategory == null) {
-            log.warn("鏈壘鍒板悕绉颁负 {} 鐨勯《绾т骇鍝佸垎绫�", targetName);
-            return new ArrayList<>();
-        }
-        Long rootCategoryId = rootCategory.getId();
 
         LocalDate now = LocalDate.now();
         DateTimeFormatter displayDtf = DateTimeFormatter.ofPattern("M/d");
@@ -1112,10 +1107,10 @@
         String startDate = now.minusDays(6).toString();
         String endDate = now.toString();
 
-        List<Map<String, Object>> inCounts = stockInventoryMapper.selectDailyStockInCounts(rootCategoryId, startDate,
+        List<Map<String, Object>> inCounts = stockInventoryMapper.selectDailyStockInCounts(productScope, startDate,
                 endDate + " 23:59:59");
 
-        List<Map<String, Object>> outCounts = stockInventoryMapper.selectDailyStockOutCounts(rootCategoryId, startDate,
+        List<Map<String, Object>> outCounts = stockInventoryMapper.selectDailyStockOutCounts(productScope, startDate,
                 endDate + " 23:59:59");
 
         Map<LocalDate, BigDecimal> inMap = inCounts.stream()

--
Gitblit v1.9.3