gongchunyi
2026-09-17 875adbd59a28453cdf443c3244e2691ad1dd02ac
src/main/resources/mapper/stock/StockInventoryMapper.xml
@@ -84,7 +84,7 @@
        p.product_name,
        si.batch_no,
        si.customer,
        rp.product_name AS root_product_name,
        rp.product_type AS productScope,
        si.production_date
        FROM
        stock_inventory si
@@ -108,11 +108,15 @@
            <if test="ew.productScope != null and ew.productScope != ''">
                <choose>
                    <when test="ew.productScope == '成品'">
                        AND rp.product_name = '成品'
                        AND rp.product_type = '成品'
                    </when>
                    <otherwise>
                        AND rp.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>
@@ -260,27 +264,41 @@
                AND create_time &lt;= #{endDate}) AS combined_counts
    </select>
    <!-- 产品库根节点分类:0-原材料 1-半成品 2-成品(先匹配半成品,避免被成品包含) -->
    <sql id="productTypeTree">
        WITH RECURSIVE product_tree AS (SELECT id
                                        FROM product
                                        WHERE parent_id IS NULL
                                          AND ((#{modelType} = 0 AND product_name LIKE '%原材料%')
                                            OR (#{modelType} = 1 AND product_name LIKE '%半成品%')
                                            OR (#{modelType} = 2 AND product_name LIKE '%成品%' AND product_name NOT LIKE '%半成品%'))
                                        UNION ALL
                                        SELECT p.id
                                        FROM product p
                                                 INNER JOIN product_tree pt ON p.parent_id = pt.id)
    </sql>
    <select id="selectDailyStockInCounts" resultType="java.util.Map">
        <include refid="productTypeTree"/>
        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 &gt;= #{startDate}
                 JOIN product_tree pt ON pt.id = pm.product_id
        WHERE sir.create_time &gt;= #{startDate}
          AND sir.create_time &lt;= #{endDate}
        GROUP BY DATE(sir.create_time)
        ORDER BY DATE(sir.create_time) ASC
    </select>
    <select id="selectDailyStockOutCounts" resultType="java.util.Map">
        <include refid="productTypeTree"/>
        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 &gt;= #{startDate}
                 JOIN product_tree pt ON pt.id = pm.product_id
        WHERE sor.create_time &gt;= #{startDate}
          AND sor.create_time &lt;= #{endDate}
        GROUP BY DATE(sor.create_time)
        ORDER BY DATE(sor.create_time) ASC
@@ -359,7 +377,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