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/stock/StockInventoryMapper.xml |   44 ++++++++++++++++++++++++++++++++------------
 1 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/main/resources/mapper/stock/StockInventoryMapper.xml b/src/main/resources/mapper/stock/StockInventoryMapper.xml
index f373f79..21e70c7 100644
--- a/src/main/resources/mapper/stock/StockInventoryMapper.xml
+++ b/src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -84,7 +84,7 @@
         p.product_name,
         si.batch_no,
         si.customer,
-        rp.product_name AS root_product_name,
+        rp.product_type AS productScope,
         si.production_date
         FROM
         stock_inventory si
@@ -108,11 +108,15 @@
             <if test="ew.productScope != null and ew.productScope != ''">
                 <choose>
                     <when test="ew.productScope == '鎴愬搧'">
-                        AND rp.product_name = '鎴愬搧'
+                        AND rp.product_type = '鎴愬搧'
                     </when>
-                    <otherwise>
-                        AND rp.product_name != '鎴愬搧'
-                    </otherwise>
+                    <when test="ew.productScope == '鍗婃垚鍝�'">
+                        AND rp.product_type = '鍗婃垚鍝�'
+                    </when>
+                    <when test="ew.productScope == '鍘熸潗鏂�'">
+                        AND (rp.product_type IS NULL
+                        OR (rp.product_type != '鎴愬搧' AND rp.product_type != '鍗婃垚鍝�'))
+                    </when>
                 </choose>
             </if>
         </where>
@@ -260,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
@@ -359,7 +377,9 @@
             p.parent_id AS parent_id,
             p1.product_name AS parentName,
             si.batch_no,
-            si.customer
+            si.customer,
+            si.production_date,
+            pm.validity_period
             FROM
             stock_inventory si
             LEFT JOIN product_model pm ON si.product_model_id = pm.id

--
Gitblit v1.9.3