From 875adbd59a28453cdf443c3244e2691ad1dd02ac Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 17 九月 2026 00:13:08 +0800
Subject: [PATCH] fix: 产品父类名称适配

---
 src/main/resources/mapper/sales/SalesLedgerProductMapper.xml   |   25 +++++++++---
 src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java |    4 +-
 src/main/resources/mapper/stock/StockInventoryMapper.xml       |   26 ++++++++++---
 src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java |   23 +++--------
 4 files changed, 48 insertions(+), 30 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 eaf062b..500b934 100644
--- a/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
+++ b/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
@@ -1088,26 +1088,17 @@
 
     @Override
     public List<Map<String, Object>> productInOutAnalysis(Integer type) {
-        String targetName;
+        // 0-鍘熸潗鏂� 1-鍗婃垚鍝� 2-鎴愬搧
+        Integer modelType;
         if (type == 1) {
-            targetName = "鍘熸潗鏂�";
+            modelType = 0;
         } else if (type == 2) {
-            targetName = "鎴愬搧";
+            modelType = 2;
         } else if (type == 3) {
-            targetName = "鍗婃垚鍝�";
+            modelType = 1;
         } 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");
@@ -1115,10 +1106,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(modelType, startDate,
                 endDate + " 23:59:59");
 
-        List<Map<String, Object>> outCounts = stockInventoryMapper.selectDailyStockOutCounts(rootCategoryId, startDate,
+        List<Map<String, Object>> outCounts = stockInventoryMapper.selectDailyStockOutCounts(modelType, startDate,
                 endDate + " 23:59:59");
 
         Map<LocalDate, BigDecimal> inMap = inCounts.stream()
diff --git a/src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java b/src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java
index 07fd0db..91cc613 100644
--- a/src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java
+++ b/src/main/java/com/ruoyi/stock/mapper/StockInventoryMapper.java
@@ -45,9 +45,9 @@
 
     int selectStorageProductCountByDate(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
 
-    List<Map<String, Object>> selectDailyStockInCounts(@Param("rootCategoryId") Long rootCategoryId, @Param("startDate") String startDate, @Param("endDate") String endDate);
+    List<Map<String, Object>> selectDailyStockInCounts(@Param("modelType") Integer modelType, @Param("startDate") String startDate, @Param("endDate") String endDate);
 
-    List<Map<String, Object>> selectDailyStockOutCounts(@Param("rootCategoryId") Long rootCategoryId, @Param("startDate") String startDate, @Param("endDate") String endDate);
+    List<Map<String, Object>> selectDailyStockOutCounts(@Param("modelType") Integer modelType, @Param("startDate") String startDate, @Param("endDate") String endDate);
 
     BigDecimal selectTotalByDate(@Param("now") LocalDate now);
 
diff --git a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
index 711c961..92cd95f 100644
--- a/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
+++ b/src/main/resources/mapper/sales/SalesLedgerProductMapper.xml
@@ -187,6 +187,14 @@
     </select>
 
     <select id="selectRawMaterialPurchaseAnalysis" resultType="java.util.Map">
+        WITH RECURSIVE product_tree AS (SELECT id
+                                        FROM product
+                                        WHERE parent_id IS NULL
+                                          AND product_name LIKE '%鍘熸潗鏂�%'
+                                        UNION ALL
+                                        SELECT p.id
+                                        FROM product p
+                                                 INNER JOIN product_tree pt ON p.parent_id = pt.id)
         SELECT
             pr.product_name AS name,
             SUM( slp.tax_inclusive_total_price ) AS value
@@ -195,7 +203,7 @@
             JOIN product pr ON slp.product_id = pr.id
         WHERE
             slp.type = 2
-            AND pr.parent_id = ( SELECT id FROM product WHERE product_name = '鍘熸潗鏂�' )
+            AND pr.id IN ( SELECT id FROM product_tree )
         GROUP BY
             pr.id,
             pr.product_name
@@ -206,20 +214,25 @@
 
     <select id="selectProductCountByTypeAndDate" resultType="int">
         SELECT IFNULL(COUNT(*), 0)
-        FROM sales_ledger_product
-        WHERE type = #{type}
+        FROM sales_ledger_product slp
+                 LEFT JOIN sales_ledger sl
+                           ON slp.type = 1 AND sl.id = slp.sales_ledger_id
+                 LEFT JOIN purchase_ledger pl
+                           ON slp.type = 2 AND pl.id = slp.sales_ledger_id
+        WHERE slp.type = #{type}
         <if test="startDate != null">
-            AND register_date &gt;= #{startDate}
+            AND IFNULL(IF(slp.type = 1, sl.entry_date, pl.entry_date), slp.register_date) &gt;= #{startDate}
         </if>
         <if test="endDate != null">
-            AND register_date &lt;= #{endDate}
+            AND IFNULL(IF(slp.type = 1, sl.entry_date, pl.entry_date), slp.register_date) &lt;= #{endDate}
         </if>
     </select>
 
     <select id="selectRawMaterialExpense" resultType="java.math.BigDecimal">
         WITH RECURSIVE product_tree AS (SELECT id
                                         FROM product
-                                        WHERE product_name = '鍘熸潗鏂�'
+                                        WHERE parent_id IS NULL
+                                          AND product_name LIKE '%鍘熸潗鏂�%'
 
                                         UNION ALL
 
diff --git a/src/main/resources/mapper/stock/StockInventoryMapper.xml b/src/main/resources/mapper/stock/StockInventoryMapper.xml
index 517a17d..21e70c7 100644
--- a/src/main/resources/mapper/stock/StockInventoryMapper.xml
+++ b/src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -264,27 +264,41 @@
                 AND create_time &lt;= #{endDate}) AS combined_counts
     </select>
 
+    <!-- 浜у搧搴撴牴鑺傜偣鍒嗙被锛�0-鍘熸潗鏂� 1-鍗婃垚鍝� 2-鎴愬搧锛堝厛鍖归厤鍗婃垚鍝侊紝閬垮厤琚垚鍝佸寘鍚級 -->
+    <sql id="productTypeTree">
+        WITH RECURSIVE product_tree AS (SELECT id
+                                        FROM product
+                                        WHERE parent_id IS NULL
+                                          AND ((#{modelType} = 0 AND product_name LIKE '%鍘熸潗鏂�%')
+                                            OR (#{modelType} = 1 AND product_name LIKE '%鍗婃垚鍝�%')
+                                            OR (#{modelType} = 2 AND product_name LIKE '%鎴愬搧%' AND product_name NOT LIKE '%鍗婃垚鍝�%'))
+                                        UNION ALL
+                                        SELECT p.id
+                                        FROM product p
+                                                 INNER JOIN product_tree pt ON p.parent_id = pt.id)
+    </sql>
+
     <select id="selectDailyStockInCounts" resultType="java.util.Map">
+        <include refid="productTypeTree"/>
         SELECT DATE(sir.create_time) AS date,
                SUM(sir.stock_in_num) AS count
         FROM stock_in_record sir
                  JOIN product_model pm ON sir.product_model_id = pm.id
-                 JOIN product p ON pm.product_id = p.id
-        WHERE (p.parent_id = #{rootCategoryId} OR p.id = #{rootCategoryId})
-          AND sir.create_time &gt;= #{startDate}
+                 JOIN product_tree pt ON pt.id = pm.product_id
+        WHERE sir.create_time &gt;= #{startDate}
           AND sir.create_time &lt;= #{endDate}
         GROUP BY DATE(sir.create_time)
         ORDER BY DATE(sir.create_time) ASC
     </select>
 
     <select id="selectDailyStockOutCounts" resultType="java.util.Map">
+        <include refid="productTypeTree"/>
         SELECT DATE(sor.create_time)  AS date,
                SUM(sor.stock_out_num) AS count
         FROM stock_out_record sor
                  JOIN product_model pm ON sor.product_model_id = pm.id
-                 JOIN product p ON pm.product_id = p.id
-        WHERE (p.parent_id = #{rootCategoryId} OR p.id = #{rootCategoryId})
-          AND sor.create_time &gt;= #{startDate}
+                 JOIN product_tree pt ON pt.id = pm.product_id
+        WHERE sor.create_time &gt;= #{startDate}
           AND sor.create_time &lt;= #{endDate}
         GROUP BY DATE(sor.create_time)
         ORDER BY DATE(sor.create_time) ASC

--
Gitblit v1.9.3