From d5b6dcdeb9980b7b4cdafdc6324efc7b791c3357 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 20 三月 2026 09:33:22 +0800
Subject: [PATCH] fix:耗材库存报表查询字段优化

---
 src/main/resources/mapper/consumables/ConsumablesInventoryMapper.xml |   37 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 36 insertions(+), 1 deletions(-)

diff --git a/src/main/resources/mapper/consumables/ConsumablesInventoryMapper.xml b/src/main/resources/mapper/consumables/ConsumablesInventoryMapper.xml
index 110a257..46c284b 100644
--- a/src/main/resources/mapper/consumables/ConsumablesInventoryMapper.xml
+++ b/src/main/resources/mapper/consumables/ConsumablesInventoryMapper.xml
@@ -210,7 +210,8 @@
         p.product_name,
         MAX(current_inventory) as current_stock,
         SUM(CASE WHEN record_type = 'in' THEN amount ELSE 0 END) as total_stock_in,
-        SUM(CASE WHEN record_type = 'out' THEN amount ELSE 0 END) as total_stock_out
+        SUM(CASE WHEN record_type = 'out' THEN amount ELSE 0 END) as total_stock_out,
+        (COALESCE(in_weight.total_net_weight, 0) - COALESCE(out_weight.total_net_weight, 0)) AS current_weight
         FROM (
         SELECT
         product_model_id,
@@ -258,6 +259,40 @@
         </where>
         GROUP BY product_model_id
         ) combined_data
+        -- 宸﹁繛鎺ュ叆搴撳噣閲嶅悎璁�
+        LEFT JOIN (
+        SELECT
+        product_model_id,
+        SUM(net_weight) AS total_net_weight
+        FROM consumables_in_record
+        <where>
+            type = 0
+            <if test="ew.startMonth != null">
+                and create_time &gt;= #{ew.startMonth}
+            </if>
+            <if test="ew.endMonth != null">
+                and create_time &lt;= #{ew.endMonth}
+            </if>
+        </where>
+        GROUP BY product_model_id
+        ) in_weight ON combined_data.product_model_id = in_weight.product_model_id
+        -- 宸﹁繛鎺ュ嚭搴撳噣閲嶅悎璁�
+        LEFT JOIN (
+        SELECT
+        product_model_id,
+        SUM(net_weight) AS total_net_weight
+        FROM consumables_out_record
+        <where>
+            type = 0
+            <if test="ew.startMonth != null">
+                and create_time &gt;= #{ew.startMonth}
+            </if>
+            <if test="ew.endMonth != null">
+                and create_time &lt;= #{ew.endMonth}
+            </if>
+        </where>
+        GROUP BY product_model_id
+        ) out_weight ON combined_data.product_model_id = out_weight.product_model_id
         LEFT JOIN product_model pm ON pm.id = combined_data.product_model_id
         LEFT JOIN product p ON p.id = pm.product_id
         <where>

--
Gitblit v1.9.3