huminmin
3 天以前 bbff5bb89738ea3fabb480d3ca3ba13f78e18ced
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?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.stock.mapper.StockInventoryMapper">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ruoyi.stock.pojo.StockInventory">
        <result column="id" property="id"/>
        <result column="product_model_id" property="productModelId"/>
        <result column="qualitity" property="qualitity"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="version" property="version"/>
        <result column="locked_quantity" property="lockedQuantity"/>
        <result column="warn_num" property="warnNum"/>
    </resultMap>
    <update id="updateAddStockInventory">
        update stock_inventory
        <set>
            <if test="ew.qualitity != null">
                qualitity = qualitity + #{ew.qualitity},
            </if>
            <if test="ew.version != null">
                version = version + 1,
            </if>
            <if test="ew.remark != null and ew.remark !=''">
                remark = #{ew.remark},
            </if>
            <if test="ew.warnNum != null and ew.warnNum !=''">
                warn_num = #{ew.warnNum},
            </if>
            <if test="ew.lockedQuantity != null and ew.lockedQuantity !=''">
                locked_quantity = locked_quantity + #{ew.lockedQuantity},
            </if>
            update_time = now()
        </set>
        where product_model_id = #{ew.productModelId}
    </update>
    <update id="updateSubtractStockInventory">
        update stock_inventory
        <set>
            <if test="ew.qualitity != null">
                qualitity = qualitity - #{ew.qualitity},
            </if>
            <if test="ew.version != null">
                version = version + 1,
            </if>
            <if test="ew.remark != null and ew.remark !=''">
                remark = #{ew.remark},
            </if>
            update_time = now()
        </set>
        where product_model_id = #{ew.productModelId} and qualitity >= #{ew.qualitity}
    </update>
    <select id="pagestockInventory" resultType="com.ruoyi.stock.dto.StockInventoryDto">
        select si.id,
        si.qualitity,
        COALESCE(si.locked_quantity, 0) as locked_quantity,
        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
        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>
    </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>
    </select>
    <select id="stockInventoryPage" resultType="com.ruoyi.stock.dto.StockInRecordDto">
        select sir.*,si.qualitity,
        pm.model,
        pm.unit,
        p.product_name
        from
        stock_in_record sir
        left join stock_inventory si on sir.product_model_id = si.product_model_id
        left join product_model pm on sir.product_model_id = pm.id
        left join product p on pm.product_id = p.id
        <where>
            <if test="ew.reportDate != null">
                and sir.create_time >= #{ew.reportDate}
                and sir.create_time &lt; DATE_ADD(#{ew.reportDate}, INTERVAL 1 DAY)
            </if>
            <if test="ew.startMonth != null">
                and sir.create_time &gt;= #{ew.startMonth}
            </if>
            <if test="ew.endMonth != null">
                and sir.create_time &lt;= #{ew.endMonth}
            </if>
        </where>
    </select>
 
    <select id="stockInAndOutRecord" resultType="com.ruoyi.stock.dto.StockInventoryDto">
        SELECT
        pm.model,
        pm.unit,
        p.product_name,
        MAX(current_inventory) as current_stock,
        SUM(CASE WHEN record_type = 'in' THEN amount ELSE 0 END) as total_stock_in,
        SUM(CASE WHEN record_type = 'out' THEN amount ELSE 0 END) as total_stock_out
        FROM (
        SELECT
        product_model_id,
        SUM(qualitity) as current_inventory,
        0 as amount,
        '' as record_type
        FROM stock_inventory
        GROUP BY product_model_id
 
        UNION ALL
 
        SELECT
        product_model_id,
        0 as current_inventory,
        SUM(stock_in_num) as amount,
        'in' as record_type
        FROM stock_in_record
        <where>
            type = 0
            <if test="ew.startMonth != null">
                and stock_in_record.create_time &gt;= #{ew.startMonth}
            </if>
            <if test="ew.endMonth != null">
                and stock_in_record.create_time &lt;= #{ew.endMonth}
            </if>
        </where>
        GROUP BY product_model_id
 
        UNION ALL
 
        SELECT
        product_model_id,
        0 as current_inventory,
        SUM(stock_out_num) as amount,
        'out' as record_type
        FROM stock_out_record
        <where>
            type = 0
            <if test="ew.startMonth != null">
                and stock_out_record.create_time &gt;= #{ew.startMonth}
            </if>
            <if test="ew.endMonth != null">
                and stock_out_record.create_time &lt;= #{ew.endMonth}
            </if>
        </where>
        GROUP BY product_model_id
        ) combined_data
        LEFT JOIN product_model pm ON pm.id = combined_data.product_model_id
        LEFT JOIN product p ON p.id = pm.product_id
        <where>
            <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>
        </where>
        GROUP BY
        pm.model,
        pm.unit,
        p.product_name
    </select>
    <select id="selectTotal" resultType="java.math.BigDecimal">
        select ifnull(sum(qualitity), 0)
        from stock_inventory
    </select>
    <select id="selectTotalByDate" resultType="java.math.BigDecimal">
        select sum(qualitity)
        from stock_inventory
        where
           create_time &gt;= #{now} and create_time &lt; DATE_ADD(#{now}, INTERVAL 1 DAY)
    </select>
 
    <select id="selectStorageProductCountByDate" resultType="int">
        SELECT COUNT(*)
        FROM (SELECT create_time
              FROM stock_inventory
              UNION ALL
              SELECT create_time
              FROM stock_uninventory) combined
        WHERE create_time &gt;= #{startDate}
          AND create_time &lt;= #{endDate}
    </select>
 
    <select id="selectDailyStockInCounts" resultType="java.util.Map">
        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}
          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">
        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}
          AND sor.create_time &lt;= #{endDate}
        GROUP BY DATE(sor.create_time)
        ORDER BY DATE(sor.create_time) ASC
    </select>
 
</mapper>