buhuazhen
2026-05-28 0d7c3e7923c420ae6d3f8feff280bf4fcaac5ce7
src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -209,6 +209,12 @@
            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
                and combined.product_id in (select id from product_tree)
            </if>
            <if test="ew.model != null and ew.model !=''">
                and combined.model like concat('%',#{ew.model},'%')
            </if>
            <if test="ew.batchNo != null and ew.batchNo !=''">
                and combined.batch_no like concat('%',#{ew.batchNo},'%')
            </if>
        </where>
        group by
        product_model_id,
@@ -216,6 +222,7 @@
        unit,
        product_name,
        product_id
        order by combined.create_time desc
    </select>
    <select id="listStockInventoryExportData" resultType="com.ruoyi.stock.execl.StockInventoryExportData">
@@ -361,6 +368,7 @@
                and sir.create_time &lt;= #{ew.endMonth}
            </if>
        </where>
        order by sir.id desc
    </select>
    <select id="stockInAndOutRecord" resultType="com.ruoyi.stock.dto.StockInventoryDto">
@@ -506,8 +514,9 @@
          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 si.id, si.batch_no, si.locked_quantity, (si.qualitity - IFNULL(sd.qualitity, 0)) as qualitity
    <select id="getByModelId" resultType="com.ruoyi.stock.dto.StockInventoryDto">
        select si.id, si.batch_no, si.locked_quantity, (si.qualitity - IFNULL(sd.qualitity, 0)) as qualitity,
               p.product_name, pm.model, pm.unit
        from stock_inventory si
                 left join (
                    select spd.stock_inventory_id, sum(spd.quantity) as qualitity
@@ -524,7 +533,10 @@
                    )
                    group by spd.stock_inventory_id
                 ) as sd on sd.stock_inventory_id = si.id
                 left join product_model pm on si.product_model_id = pm.id
                 left join product p on pm.product_id = p.id
        where si.product_model_id = #{productModelId}
        and si.qualitity > IFNULL(sd.qualitity, 0)
    </select>
    <select id="getBatchNoQty" resultType="com.ruoyi.stock.dto.StockInventoryDto">
@@ -661,4 +673,58 @@
        batch_no
    </select>
    <!-- 分页查询产品库存和领用量(按批号区分) -->
    <select id="pageStockAndBorrow" 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
            pm.id as product_model_id,
            pm.model,
            pm.product_code,
            pm.unit,
            p.product_name,
            p.id as product_id,
            si.batch_no,
            IFNULL(si.qualitity, 0) as qualitity,
            IFNULL(si.locked_quantity, 0) as locked_quantity,
            IFNULL(borrowed.borrowed_quantity, 0) as borrowed_quantity,
            IFNULL(si.qualitity, 0) - IFNULL(borrowed.borrowed_quantity, 0) as available_quantity
        FROM product_model pm
        LEFT JOIN product p ON pm.product_id = p.id
        LEFT JOIN stock_inventory si ON pm.id = si.product_model_id
        LEFT JOIN (
            SELECT
                product_model_id,
                batch_no,
                SUM(borrow_quantity - IFNULL(returned_quantity, 0)) as borrowed_quantity
            FROM product_borrow
            WHERE approval_status = 1
              AND status != 2
            GROUP BY product_model_id, batch_no
        ) borrowed ON pm.id = borrowed.product_model_id
            AND (si.batch_no = borrowed.batch_no OR (si.batch_no IS NULL AND borrowed.batch_no IS NULL))
        <where>
            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
                AND p.id IN (SELECT id FROM product_tree)
            </if>
            <if test="ew.productName != null and ew.productName != ''">
                AND p.product_name LIKE CONCAT('%', #{ew.productName}, '%')
            </if>
            <if test="ew.model != null and ew.model != ''">
                AND pm.model LIKE CONCAT('%', #{ew.model}, '%')
            </if>
            <if test="ew.batchNo != null and ew.batchNo != ''">
                AND si.batch_no LIKE CONCAT('%', #{ew.batchNo}, '%')
            </if>
        </where>
        ORDER BY pm.id DESC, si.batch_no
    </select>
</mapper>