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/resources/mapper/stock/StockInventoryMapper.xml | 92 +++++++++++++++++++++++++++++++++++++++------
1 files changed, 79 insertions(+), 13 deletions(-)
diff --git a/src/main/resources/mapper/stock/StockInventoryMapper.xml b/src/main/resources/mapper/stock/StockInventoryMapper.xml
index d592b11..dc519a6 100644
--- a/src/main/resources/mapper/stock/StockInventoryMapper.xml
+++ b/src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -52,6 +52,21 @@
where product_model_id = #{ew.productModelId} and qualitity >= #{ew.qualitity}
</update>
<select id="pagestockInventory" resultType="com.ruoyi.stock.dto.StockInventoryDto">
+ WITH RECURSIVE get_root AS (
+ SELECT
+ id,
+ parent_id,
+ id AS root_id
+ FROM product
+ WHERE parent_id IS NULL -- 鏍硅妭鐐�
+ UNION ALL
+ SELECT
+ p.id,
+ p.parent_id,
+ gr.root_id
+ FROM product p
+ INNER JOIN get_root gr ON p.parent_id = gr.id
+ )
SELECT
si.id,
si.qualitity,
@@ -68,12 +83,15 @@
pm.uid_no AS uidNo,
p.product_name,
si.batch_no,
- si.customer
+ si.customer,
+ rp.product_type AS productScope,
+ si.production_date
FROM
stock_inventory si
LEFT JOIN product_model pm ON si.product_model_id = pm.id
LEFT JOIN product p ON pm.product_id = p.id
- LEFT JOIN product p1 ON p.parent_id = p1.id
+ LEFT JOIN get_root gr ON gr.id = p.id
+ LEFT JOIN product rp ON rp.id = gr.root_id
<where>
<if test="ew.productName != null and ew.productName != ''">
AND p.product_name LIKE CONCAT('%', #{ew.productName}, '%')
@@ -90,11 +108,15 @@
<if test="ew.productScope != null and ew.productScope != ''">
<choose>
<when test="ew.productScope == '鎴愬搧'">
- AND p1.product_name = #{ew.productScope}
+ AND rp.product_type = '鎴愬搧'
</when>
- <otherwise>
- AND p1.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>
@@ -103,6 +125,10 @@
select si.qualitity,
pm.model,
pm.unit,
+ si.batchNo,
+ pm.uid_no,
+ si.production_date,
+ si.customer,
p.product_name,
coalesce(si.warn_num, 0) as warn_num,
coalesce(si.locked_quantity, 0) as locked_quantity,
@@ -239,27 +265,65 @@
</select>
<select id="selectDailyStockInCounts" resultType="java.util.Map">
+ WITH RECURSIVE get_root AS (SELECT id,
+ parent_id,
+ id AS root_id
+ FROM product
+ WHERE parent_id IS NULL
+ UNION ALL
+ SELECT p.id,
+ p.parent_id,
+ gr.root_id
+ FROM product p
+ INNER JOIN get_root gr ON p.parent_id = gr.id)
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 >= #{startDate}
- AND sir.create_time <= #{endDate}
+ JOIN get_root gr ON gr.id = p.id
+ JOIN product rp ON rp.id = gr.root_id
+ WHERE
+ <choose>
+ <when test="productScope == '鎴愬搧'">rp.product_type = '鎴愬搧'</when>
+ <when test="productScope == '鍗婃垚鍝�'">rp.product_type = '鍗婃垚鍝�'</when>
+ <otherwise>(rp.product_type IS NULL
+ OR (rp.product_type != '鎴愬搧' AND rp.product_type != '鍗婃垚鍝�'))</otherwise>
+ </choose>
+ AND sir.create_time >= #{startDate}
+ AND sir.create_time <= #{endDate}
GROUP BY DATE(sir.create_time)
ORDER BY DATE(sir.create_time) ASC
</select>
<select id="selectDailyStockOutCounts" resultType="java.util.Map">
+ WITH RECURSIVE get_root AS (SELECT id,
+ parent_id,
+ id AS root_id
+ FROM product
+ WHERE parent_id IS NULL
+ UNION ALL
+ SELECT p.id,
+ p.parent_id,
+ gr.root_id
+ FROM product p
+ INNER JOIN get_root gr ON p.parent_id = gr.id)
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 >= #{startDate}
- AND sor.create_time <= #{endDate}
+ JOIN get_root gr ON gr.id = p.id
+ JOIN product rp ON rp.id = gr.root_id
+ WHERE
+ <choose>
+ <when test="productScope == '鎴愬搧'">rp.product_type = '鎴愬搧'</when>
+ <when test="productScope == '鍗婃垚鍝�'">rp.product_type = '鍗婃垚鍝�'</when>
+ <otherwise>(rp.product_type IS NULL
+ OR (rp.product_type != '鎴愬搧' AND rp.product_type != '鍗婃垚鍝�'))</otherwise>
+ </choose>
+ AND sor.create_time >= #{startDate}
+ AND sor.create_time <= #{endDate}
GROUP BY DATE(sor.create_time)
ORDER BY DATE(sor.create_time) ASC
</select>
@@ -337,7 +401,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