gongchunyi
6 小时以前 f5c9d4c54da8d60d8b798f67679263a8aec1607f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.basic.mapper.ProductModelMapper">
 
    <!-- 通用查询映射结果 -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ruoyi.basic.pojo.ProductModel">
        <id column="id" property="id" />
        <result column="product_id" property="productId" />
        <result column="model" property="model" />
        <result column="unit" property="unit" />
        <result column="thickness" property="thickness" />
        <result column="speculative_trading_name" property="speculativeTradingName" />
        <result column="tenant_id" property="tenantId" />
        <result column="product_name" property="productName" />
        <result column="product_id" property="productId" />
    </resultMap>
 
    <select id="listPageProductModel" resultType="com.ruoyi.basic.pojo.ProductModel">
        WITH RECURSIVE product_tree AS (
        SELECT
        id,
        product_name,
        id AS top_id,
        product_name AS top_name
        FROM product
        WHERE parent_id IS NULL OR parent_id = 0
 
        UNION ALL
 
        SELECT
        p.id,
        p.product_name,
        t.top_id,
        t.top_name
        FROM product p
        INNER JOIN product_tree t ON p.parent_id = t.id
        )
        SELECT
        pm.*,
        pt.product_name AS productName,
        pt.top_name AS parentName
        FROM product_model pm
        LEFT JOIN product_tree pt ON pm.product_id = pt.id
        <where>
            <if test="c.parentId != null">
                AND pt.top_id = #{c.parentId}
            </if>
 
            <if test="c.model != null and c.model != ''">
                AND pm.model LIKE CONCAT('%', #{c.model}, '%')
            </if>
            <if test="c.productName != null and c.productName != ''">
                AND pt.product_name LIKE CONCAT('%', #{c.productName}, '%')
            </if>
            <if test="c.parentName != null and c.parentName != ''">
                AND pt.top_name LIKE CONCAT('%', #{c.parentName}, '%')
            </if>
        </where>
        ORDER BY pm.id DESC
    </select>
 
    <select id="selectLatestRecord" resultType="com.ruoyi.basic.pojo.ProductModel">
            SELECT * FROM product_model
            ORDER BY create_time DESC, id DESC
            LIMIT 1
    </select>
 
    <select id="listPageProductionStock" resultType="com.ruoyi.basic.pojo.ProductModel">
        SELECT
        pm.id,
        pm.product_id,
        pm.model,
        pm.unit,
        p.product_name,
        GREATEST(s.last_in_time, o.last_out_time) AS create_time,
        COALESCE(s.inboundNum, 0) AS inboundNum,
        COALESCE(o.outboundNum, 0) AS outboundNum,
        COALESCE(s.inboundNum, 0) - COALESCE(o.outboundNum, 0) AS stockQuantity
        FROM product_model pm
        LEFT JOIN product p ON pm.product_id = p.id
 
        -- 入库汇总
        LEFT JOIN (
        SELECT
        product_model_id,
        SUM(inbound_num) AS inboundNum,
        MAX(create_time) AS last_in_time
        FROM procurement_record_storage
        <where>
            <if test="req.timeStr != null and req.timeStr != ''">
                AND create_time &gt;= #{req.timeStr}
                AND create_time &lt; DATE_ADD(#{req.timeStr}, INTERVAL 1 DAY)
            </if>
        </where>
        GROUP BY product_model_id
        ) s ON s.product_model_id = pm.id
 
        -- 出库汇总
        LEFT JOIN (
        SELECT
        product_model_id,
        SUM(inbound_num) AS outboundNum,
        MAX(create_time) AS last_out_time
        FROM procurement_record_out
        <where>
            <if test="req.timeStr != null and req.timeStr != ''">
                AND create_time &gt;= #{req.timeStr}
                AND create_time &lt; DATE_ADD(#{req.timeStr}, INTERVAL 1 DAY)
            </if>
        </where>
        GROUP BY product_model_id
        ) o ON o.product_model_id = pm.id
 
        <where>
            <if test="req.timeStr != null and req.timeStr != ''">
                AND (s.product_model_id IS NOT NULL OR o.product_model_id IS NOT NULL)
            </if>
 
            <if test="req.productCategory != null and req.productCategory != ''">
                AND EXISTS (
                SELECT 1
                FROM sales_ledger_product slp
                WHERE slp.product_id = p.id
                AND slp.product_category LIKE CONCAT('%', #{req.productCategory}, '%')
                )
            </if>
        </where>
 
        ORDER BY pm.id DESC
    </select>
    <select id="getProductAndModelList" resultType="java.util.Map">
        select p.id as id,
               pm.id as modelId,
               p.product_name as productName ,
               pm.model as  model
        from product_model pm
        left join product p on p.id = pm.product_id
        order by p.id,pm.id desc
    </select>
 
</mapper>