From e03206c08c9604decc4723c36fc25573681f2fee Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 18 六月 2026 15:02:00 +0800
Subject: [PATCH] refactor(database): 优化库存管理相关数据库查询和业务逻辑

---
 src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml b/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml
index b0166ca..97b1b19 100644
--- a/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml
+++ b/src/main/resources/mapper/procurementrecord/ProcurementRecordMapper.xml
@@ -138,6 +138,8 @@
         sum(t1.inbound_num) as inboundNum,
         sum(t1.inbound_num) as inboundNum0,
         t1.inbound_num as totalInboundNum,
+        COALESCE(out_sum.total_out_num, 0) as totalInboundNum,
+        sum(t1.inbound_num) - COALESCE(out_sum.total_out_num, 0) as inboundNum0,
         t1.create_time,
         t1.update_time,
         t1.create_by,
@@ -145,6 +147,13 @@
         from  procurement_record_storage t1
         left join sales_ledger_product t2 on t2.id = t1.sales_ledger_product_id and t2.type = 2
         left join purchase_ledger t3 on t3.id = t2.sales_ledger_id
+        left join (
+            select t_in.sales_ledger_product_id, t_in.unit_price, sum(t_out.inbound_num) as total_out_num
+            from procurement_record_storage t_in
+            inner join procurement_record_out t_out on t_out.procurement_record_storage_id = t_in.id and t_out.type = 1
+            where t_in.type = 1
+            group by t_in.sales_ledger_product_id, t_in.unit_price
+        ) out_sum on out_sum.sales_ledger_product_id = t1.sales_ledger_product_id and out_sum.unit_price = t1.unit_price
         <where>
             t1.type = 1
             <if test="req.supplierName != null and req.supplierName != ''">
@@ -295,6 +304,8 @@
         t1.unit_price,
         sum(t1.inbound_num) as inboundNum,
         sum(t1.inbound_num) as inboundNum0,
+        COALESCE(out_sum.total_out_num, 0) as totalInboundNum,
+        sum(t1.inbound_num) - COALESCE(out_sum.total_out_num, 0) as inboundNum0,
         t1.create_time,
         t1.update_time,
         t1.create_by,
@@ -303,6 +314,13 @@
         from  procurement_record_storage t1
         left join sales_ledger_product t2 on t2.id = t1.sales_ledger_product_id and t2.type = 1
         left join sales_ledger t3 on t3.id = t2.sales_ledger_id
+        left join (
+            select t_in.sales_ledger_product_id, t_in.unit_price, sum(t_out.inbound_num) as total_out_num
+            from procurement_record_storage t_in
+            inner join procurement_record_out t_out on t_out.procurement_record_storage_id = t_in.id and t_out.type = 2
+            where t_in.type = 2
+            group by t_in.sales_ledger_product_id, t_in.unit_price
+        ) out_sum on out_sum.sales_ledger_product_id = t1.sales_ledger_product_id and out_sum.unit_price = t1.unit_price
         <where>
             t1.type = 2
             <if test="req.customerName != null and req.customerName != ''">

--
Gitblit v1.9.3