From bd02d873d7bbcae36a2a1262d921f2fc6b0cce09 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期三, 06 五月 2026 14:33:41 +0800
Subject: [PATCH] feat(sales): 新增发货台账详情功能并优化库存管理

---
 src/main/resources/mapper/stock/StockInventoryMapper.xml |  254 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 239 insertions(+), 15 deletions(-)

diff --git a/src/main/resources/mapper/stock/StockInventoryMapper.xml b/src/main/resources/mapper/stock/StockInventoryMapper.xml
index 9db56f9..60fc90e 100644
--- a/src/main/resources/mapper/stock/StockInventoryMapper.xml
+++ b/src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -34,6 +34,12 @@
             update_time = now()
         </set>
         where product_model_id = #{ew.productModelId}
+            <if test="ew.batchNo == null">
+                and batch_no is null
+            </if>
+            <if test="ew.batchNo != null">
+                and batch_no = #{ew.batchNo}
+            </if>
     </update>
     <update id="updateSubtractStockInventory">
         update stock_inventory
@@ -50,6 +56,12 @@
             update_time = now()
         </set>
         where product_model_id = #{ew.productModelId} and qualitity >= #{ew.qualitity}
+            <if test="ew.batchNo == null">
+                and batch_no is null
+            </if>
+            <if test="ew.batchNo != null">
+                and batch_no = #{ew.batchNo}
+            </if>
     </update>
     <select id="pagestockInventory" resultType="com.ruoyi.stock.dto.StockInventoryDto">
         select si.id,
@@ -73,22 +85,203 @@
             and p.product_name like concat('%',#{ew.productName},'%')
         </if>
     </select>
+
+    <select id="pageListCombinedStockInventory" resultType="com.ruoyi.stock.dto.StockInventoryDto">
+        WITH RECURSIVE product_tree AS (
+        SELECT id
+        FROM product
+        WHERE id = #{ew.topParentProductId}
+
+        UNION ALL
+
+        SELECT p.id
+        FROM product p
+        INNER JOIN product_tree pt ON p.parent_id = pt.id
+        )
+        select
+            batch_no,
+            MAX(qualifiedId) as qualifiedId,
+            MAX(unQualifiedId) as unQualifiedId,
+            SUM(qualifiedQuantity) as qualifiedQuantity,
+            SUM(unQualifiedQuantity) as unQualifiedQuantity,
+            SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
+            SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
+            SUM(qualifiedQuantity - qualifiedLockedQuantity - IFNULL(qualifiedPendingOut, 0)) as qualifiedUnLockedQuantity,
+            SUM(unQualifiedQuantity - unQualifiedLockedQuantity - IFNULL(unQualifiedPendingOut, 0)) as unQualifiedUnLockedQuantity,
+            SUM(IFNULL(qualifiedPendingOut, 0)) as qualifiedPendingOutQuantity,
+            SUM(IFNULL(unQualifiedPendingOut, 0)) as unQualifiedPendingOutQuantity,
+            product_model_id,
+            MAX(create_time) as create_time,
+            MAX(update_time) as update_time,
+            MAX(warn_num) as warn_num,
+            MAX(version) as version,
+            model,
+            MAX(remark) as remark,
+            unit,
+            product_name,
+            product_id,
+            'combined' as stockType
+        from (
+            select
+            si.batch_no,
+            si.id as qualifiedId,
+            null as unQualifiedId,
+            si.qualitity as qualifiedQuantity,
+            0 as unQualifiedQuantity,
+            COALESCE(si.locked_quantity, 0) as locked_quantity,
+            COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
+            0 as unQualifiedLockedQuantity,
+            si.product_model_id,
+            si.create_time,
+            si.update_time,
+            COALESCE(si.warn_num, 0) as warn_num,
+            si.version,
+            (si.qualitity - COALESCE(si.locked_quantity, 0)) as un_locked_quantity,
+            pm.model,
+            si.remark,
+            pm.unit,
+            p.product_name,
+            p.id as product_id,
+            (
+                select IFNULL(SUM(sor.stock_out_num), 0)
+                from stock_out_record sor
+                where sor.product_model_id = si.product_model_id
+                  and (si.batch_no is null and sor.batch_no is null or si.batch_no = sor.batch_no)
+                  and sor.type = '0'
+                  and sor.approval_status = 0
+            ) as qualifiedPendingOut,
+            0 as unqualifiedPendingOut
+            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
+
+            union all
+
+            select
+            su.batch_no,
+            null as qualifiedId,
+            su.id as unQualifiedId,
+            0 as qualifiedQuantity,
+            su.qualitity as unQualifiedQuantity,
+            COALESCE(su.locked_quantity, 0) as locked_quantity,
+            0 as qualifiedLockedQuantity,
+            COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
+            su.product_model_id,
+            su.create_time,
+            su.update_time,
+            0 as warn_num,
+            su.version,
+            (su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity,
+            pm.model,
+            su.remark,
+            pm.unit,
+            p.product_name,
+            p.id as product_id,
+            0 as qualifiedPendingOut,
+            (
+                select IFNULL(SUM(sor.stock_out_num), 0)
+                from stock_out_record sor
+                where sor.product_model_id = su.product_model_id
+                  and (su.batch_no is null and sor.batch_no is null or su.batch_no = sor.batch_no)
+                  and sor.type = '1'
+                  and sor.approval_status = 0
+            ) as unqualifiedPendingOut
+            from stock_uninventory su
+            left join product_model pm on su.product_model_id = pm.id
+            left join product p on pm.product_id = p.id
+        ) as combined
+        <where>
+            <if test="ew.productName != null and ew.productName !=''">
+                and combined.product_name in (
+                select distinct p.product_name
+                from product p
+                left join product_model pm on p.id = pm.product_id
+                where p.product_name like concat('%',#{ew.productName},'%') or pm.model like concat('%',#{ew.productName},'%')
+                )
+            </if>
+            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
+                and combined.product_id in (select id from product_tree)
+            </if>
+        </where>
+        group by batch_no, product_model_id, model, unit, product_name, product_id
+    </select>
+
     <select id="listStockInventoryExportData" resultType="com.ruoyi.stock.execl.StockInventoryExportData">
-        select si.qualitity,
-        pm.model,
-        pm.unit,
-        p.product_name,
-        coalesce(si.warn_num, 0) as warn_num,
-        coalesce(si.locked_quantity, 0) as locked_quantity,
-        si.remark,
-        si.update_time
-        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
-        where 1 = 1
-        <if test="ew.productName != null and ew.productName !=''">
-            and p.product_name like concat('%',#{ew.productName},'%')
-        </if>
+        WITH RECURSIVE product_tree AS (
+        SELECT id
+        FROM product
+        WHERE id = #{ew.topParentProductId}
+
+        UNION ALL
+
+        SELECT p.id
+        FROM product p
+        INNER JOIN product_tree pt ON p.parent_id = pt.id
+        )
+        select
+            SUM(qualifiedQuantity) as qualifiedQuantity,
+            SUM(unQualifiedQuantity) as unQualifiedQuantity,
+            SUM(qualifiedLockedQuantity) as qualifiedLockedQuantity,
+            SUM(unQualifiedLockedQuantity) as unQualifiedLockedQuantity,
+            model,
+            unit,
+            product_name,
+            MAX(warn_num) as warn_num,
+            MAX(remark) as remark,
+            MAX(update_time) as update_time
+        from (
+            select
+            si.qualitity as qualifiedQuantity,
+            0 as unQualifiedQuantity,
+            COALESCE(si.locked_quantity, 0) as qualifiedLockedQuantity,
+            0 as unQualifiedLockedQuantity,
+            si.product_model_id,
+            si.create_time,
+            si.update_time,
+            COALESCE(si.warn_num, 0) as warn_num,
+            si.remark,
+            pm.model,
+            pm.unit,
+            p.product_name,
+            p.id as product_id
+            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
+
+            union all
+
+            select
+            0 as qualifiedQuantity,
+            su.qualitity as unQualifiedQuantity,
+            0 as qualifiedLockedQuantity,
+            COALESCE(su.locked_quantity, 0) as unQualifiedLockedQuantity,
+            su.product_model_id,
+            su.create_time,
+            su.update_time,
+            0 as warn_num,
+            su.remark,
+            pm.model,
+            pm.unit,
+            p.product_name,
+            p.id as product_id
+            from stock_uninventory su
+            left join product_model pm on su.product_model_id = pm.id
+            left join product p on pm.product_id = p.id
+        ) as combined
+        <where>
+            <if test="ew.productName != null and ew.productName !=''">
+                and combined.product_name in (
+                select distinct p.product_name
+                from product p
+                left join product_model pm on p.id = pm.product_id
+                where p.product_name like concat('%',#{ew.productName},'%') or pm.model like concat('%',#{ew.productName},'%')
+                )
+            </if>
+            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
+                and combined.product_id in (select id from product_tree)
+            </if>
+        </where>
+        group by product_model_id, model, unit, product_name
     </select>
     <select id="stockInventoryPage" resultType="com.ruoyi.stock.dto.StockInRecordDto">
         select sir.*,si.qualitity as current_stock,
@@ -237,4 +430,35 @@
         ORDER BY DATE(sor.create_time) ASC
     </select>
 
+    <select id="selectPendingOutQuantity" resultType="java.math.BigDecimal">
+        SELECT IFNULL(SUM(sor.stock_out_num), 0)
+        FROM stock_out_record sor
+        WHERE sor.product_model_id = #{productModelId}
+          AND (sor.batch_no = #{batchNo} OR (#{batchNo} IS NULL AND sor.batch_no IS NULL))
+          AND sor.type = #{type}
+          AND sor.approval_status = 0
+    </select>
+
+    <select id="listSelectableBatchNoByProductModelIds" resultType="com.ruoyi.stock.pojo.StockInventory">
+        select distinct si.product_model_id,
+                        si.batch_no
+        from stock_inventory si
+        where si.product_model_id in
+        <foreach collection="productModelIds" item="productModelId" open="(" separator="," close=")">
+            #{productModelId}
+        </foreach>
+          and si.batch_no is not null
+          and si.batch_no != ''
+          and (si.qualitity - ifnull(si.locked_quantity, 0)) > 0
+        order by si.product_model_id, si.batch_no
+    </select>
+    <select id="getByModelId" resultType="com.ruoyi.stock.pojo.StockInventory">
+        select spd.id, spd.batch_no, spd.locked_quantity, (spd.qualitity - IFNULL(sd.qualitity, 0)) as qualitity
+        from stock_inventory spd
+                 left join (select stock_inventory_id, sum(quantity) as qualitity
+                            from shipping_product_detail
+                            group by stock_inventory_id) as sd on sd.stock_inventory_id = spd.id
+        where product_model_id = #{productModelId}
+    </select>
+
 </mapper>

--
Gitblit v1.9.3