huminmin
2 天以前 ba4f2ca4b3ae845d2bd1e92338b948a8f95ffaaa
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
<?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.StockUninventoryMapper">
 
    <resultMap id="BaseResultMap" type="com.ruoyi.stock.pojo.StockUninventory">
        <result column="id" property="id" />
        <result column="product_model_id" property="productModelId" />
        <result column="qualitity" property="qualitity" />
        <result column="type" property="type" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="version" property="version" />
    </resultMap>
 
    <sql id="WasteQueryRecursiveTree">
        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
        )
    </sql>
 
    <sql id="BaseWasteFromClause">
        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
    </sql>
 
    <sql id="WastePageColumns">
        su.id,
        su.qualitity,
        su.type,
        COALESCE(su.locked_quantity, 0) as locked_quantity,
        su.product_model_id,
        su.create_time,
        su.update_time,
        su.version,
        (su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity,
        pm.model,
        pm.unit,
        p.product_name
    </sql>
 
    <update id="updateSubtractStockUnInventory">
        update stock_uninventory
        <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.type != null and ew.type != ''">
                type = #{ew.type},
            </if>
            update_time = now()
        </set>
        where product_model_id = #{ew.productModelId}
          and qualitity >= #{ew.qualitity}
        <if test="ew.type != null and ew.type != ''">
            and type = #{ew.type}
        </if>
        <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="updateAddStockUnInventory">
        update stock_uninventory
        <set>
            <if test="ew.qualitity != null">
                qualitity = qualitity + #{ew.qualitity},
            </if>
            <if test="ew.lockedQuantity != null">
                locked_quantity = locked_quantity + #{ew.lockedQuantity},
            </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.type != null and ew.type != ''">
                type = #{ew.type},
            </if>
            update_time = now()
        </set>
        where product_model_id = #{ew.productModelId}
        <if test="ew.type != null and ew.type != ''">
            and type = #{ew.type}
        </if>
        <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="pageStockUninventory" resultType="com.ruoyi.stock.dto.StockUninventoryDto">
        select
        su.id,
        su.qualitity,
        su.type,
        COALESCE(su.locked_quantity, 0) as locked_quantity,
        su.product_model_id,
        su.create_time,
        su.update_time,
        su.version,
        su.update_time,
        (su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity,
        pm.model,
        pm.unit,
        p.product_name
        <include refid="BaseWasteFromClause" />
        <where>
            <if test="ew.type != null and ew.type != ''">
                and su.type = #{ew.type}
            </if>
            <if test="ew.productName != null and ew.productName != ''">
                and p.product_name like concat('%', #{ew.productName}, '%')
            </if>
        </where>
    </select>
 
    <select id="pageWasteQuery" resultType="com.ruoyi.stock.dto.StockUninventoryDto">
        <include refid="WasteQueryRecursiveTree" />
        select
        <include refid="WastePageColumns" />
        <include refid="BaseWasteFromClause" />
        <where>
            and su.type = 'waste'
            <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 su.batch_no like concat('%', #{ew.batchNo}, '%')
            </if>
            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
                and p.id in (select id from product_tree)
            </if>
        </where>
 
        order by su.update_time desc, su.id desc
    </select>
 
    <select id="listStockInventoryExportData" resultType="com.ruoyi.stock.execl.StockUnInventoryExportData">
        select
        su.*,
        pm.model,
        pm.unit,
        p.product_name
        <include refid="BaseWasteFromClause" />
        <where>
            <if test="ew.type != null and ew.type != ''">
                and su.type = #{ew.type}
            </if>
            <if test="ew.productName != null and ew.productName != ''">
                and p.product_name like concat('%', #{ew.productName}, '%')
            </if>
        </where>
    </select>
 
    <select id="listWasteQueryExportData" resultType="com.ruoyi.stock.execl.StockUnInventoryExportData">
        <include refid="WasteQueryRecursiveTree" />
        select
        su.*,
        pm.model,
        pm.unit,
        p.product_name,
        (su.qualitity - COALESCE(su.locked_quantity, 0)) as un_locked_quantity
        <include refid="BaseWasteFromClause" />
        <where>
            and su.type = 'waste'
            <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 su.batch_no like concat('%', #{ew.batchNo}, '%')
            </if>
            <if test="ew.topParentProductId != null and ew.topParentProductId > 0">
                and p.id in (select id from product_tree)
            </if>
        </where>
        order by su.update_time desc, su.id desc
    </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>
 
</mapper>