陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?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.mzl.flower.mapper.flower.FlowerMapper">
 
    <select id="selectFlowerList" resultType="com.mzl.flower.dto.response.flower.FlowerListDTO">
        SELECT f.*, s.name supplierName, st.name supplierType, fc.name categoryStr
            , (SELECT GROUP_CONCAT(DISTINCT(fz.id))
                FROM t_flower_zone fz JOIN t_flower_zone_map fzm ON fz.id = fzm.zone_id
                WHERE fzm.flower_id = f.ID
                ORDER BY fz.id
            ) AS zoneId
            , (SELECT GROUP_CONCAT(DISTINCT(fz.name))
                FROM t_flower_zone fz JOIN t_flower_zone_map fzm ON fz.id = fzm.zone_id
                WHERE fzm.flower_id = f.ID
                ORDER BY fz.id
            ) AS  zoneName
        FROM t_flower f
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_supplier_type st on st.id = s.type_id
        left join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = 0
        and f.status in ('PENDING','UP','REJECT','OFF','FORCE_OFF')
        <if test="condition.name != null and condition.name != ''">
            AND f.name LIKE concat('%', #{condition.name},'%')
        </if>
        <if test="condition.supplierName != null and condition.supplierName != ''">
            AND s.name LIKE concat('%', #{condition.supplierName},'%')
        </if>
        <if test="condition.level != null and condition.level != ''">
            AND f.level = #{condition.level}
        </if>
        <if test="condition.tags != null and condition.tags != ''">
            AND f.tags LIKE concat('%', #{condition.tags},'%')
        </if>
        <if test="condition.recommend != null and condition.recommend">
            AND f.recommend = 1
        </if>
        <if test="condition.recommend != null and !condition.recommend">
            AND (f.recommend = 0 or f.recommend is null)
        </if>
        <if test="condition.statusList != null and condition.statusList.size > 0">
            and f.status in
            <foreach collection="condition.statusList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="condition.category != null">
            AND (f.category = #{condition.category}
                or exists (
                    select 1 from t_flower_category fc where fc.id = f.category and fc.parent_id = #{condition.category}
                )
            )
        </if>
        <if test="condition.zoneId != null">
            AND exists (
                select 1
                from t_flower_zone_map fz
                where fz.flower_id = f.id
                and fz.zone_id = #{condition.zoneId}
            )
        </if>
        <if test="condition.zoneName != null and condition.zoneName != ''">
            AND exists (
                select 1
                from t_flower_zone_map fz, t_flower_zone z
                where fz.flower_id = f.id
                and z.id = fz.zone_id
                and z.name LIKE concat('%', #{condition.zoneName},'%')
            )
        </if>
        ORDER BY f.create_time desc
    </select>
 
    <select id="selectFlowerSupplierList" resultType="com.mzl.flower.dto.response.flower.FlowerSupplierListDTO">
        SELECT f.*, fc.name categoryStr
        FROM t_flower f
        left join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = #{deleted}
        AND f.supplier_id = #{supplierId}
        <if test="condition.name != null and condition.name != ''">
            AND f.name LIKE concat('%', #{condition.name},'%')
        </if>
        <if test="condition.level != null and condition.level != ''">
            AND f.level = #{condition.level}
        </if>
        <if test="condition.tags != null and condition.tags != ''">
            AND f.tags LIKE concat('%', #{condition.tags},'%')
        </if>
        <if test="condition.statusList != null and condition.statusList.size > 0">
            and f.status in
            <foreach collection="condition.statusList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="condition.category != null">
            AND (f.category = #{condition.category}
                or exists (
                    select 1 from t_flower_category fc where fc.id = f.category and fc.parent_id = #{condition.category}
                )
            )
        </if>
 
        ORDER BY f.create_time desc
    </select>
 
    <select id="selectFlowerShowList" resultType="com.mzl.flower.dto.response.flower.FlowerShowListDTO">
        SELECT distinct f.*, s.name supplierName, st.name supplierType, fc.name categoryStr
        , (select count(1) from t_collect_flower cf
            where cf.flower_id = f.id
            and cf.user_id = #{condition.userId}
        ) collectCount
        , (select sum(num) from t_cart c
            where c.flower_id = f.id
            and c.create_by = #{condition.userId}
        ) shopnum,
        fzp.rank as zoneRank
        FROM t_flower f
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_supplier_type st on st.id = s.type_id
        left join t_flower_category fc on fc.id = f.category
        left JOIN t_flower_zone_map fzp on fzp.flower_id = f.id
        WHERE f.deleted = 0
        AND f.status = 'UP'
        AND (fc.shown = 1 or fc.shown is null)
        AND (f.shown = 1 or f.shown is null)
        and (s.showed = 1 or s.showed is null)
        and exists (select 1 from t_flower_category fcp where fcp.id = fc.parent_id and (fcp.shown = 1 or fcp.shown is null))
        <if test="condition.name != null and condition.name != ''">
            AND f.name LIKE concat('%', #{condition.name},'%')
        </if>
        <if test="condition.level != null and condition.level != ''">
            AND f.level = #{condition.level}
        </if>
        <if test="condition.tags != null and condition.tags != ''">
            AND f.tags LIKE concat('%', #{condition.tags},'%')
        </if>
        <if test="condition.categoryRoot != null">
            AND fc.parent_id = #{condition.categoryRoot}
        </if>
        <if test="condition.category != null">
            AND f.category = #{condition.category}
        </if>
        <if test="condition.supplierId != null">
            AND f.supplier_id = #{condition.supplierId}
        </if>
        <if test="condition.supplierName != null and condition.supplierName != ''">
            AND s.name LIKE concat('%', #{condition.supplierName},'%')
        </if>
        <if test="condition.recommend != null and condition.recommend">
            AND f.recommend = 1
        </if>
        <if test="condition.zoneId != null">
            AND fzp.zone_id = #{condition.zoneId}
        </if>
 
        <if test="condition.paramId != null and condition.params != null and condition.params.size > 0">
            AND exists (
                select 1 from flower_param_${condition.paramId} pp
                where pp.id = f.id
                and
                <foreach collection="condition.params" item="p" open=" " separator=" and " close="">
                    field_${p.id} = #{p.value}
                </foreach>
            )
        </if>
 
        <choose>
            <when test="condition.column != null and condition.column != '' and (condition.recommend == null or condition.zoneId != null)">
                <choose>
                    <when test="'f.sales'.equals(condition.column) or 'f.stock'.equals(condition.column)">
                        order by ${condition.column} desc
                    </when>
                    <otherwise>
                        order by ${condition.column}, f.price
                    </otherwise>
                </choose>
            </when>
            <when test="(condition.column == null or condition.column == '') and condition.recommend != null and condition.recommend">
                order by f.recommend_rank asc
            </when>
            <when test="(condition.column == null or condition.column == '') and condition.zoneId != null">
                order by fzp.rank asc
            </when>
            <otherwise>
                order by f.price asc
            </otherwise>
        </choose>
    </select>
 
    <select id="selectFlowerCartList" resultType="com.mzl.flower.dto.response.flower.FlowerCartListDTO">
        SELECT f.*, s.name supplierName, concat(fc.parent_name, '/', fc.name) categoryStr
        , c.num, fc.param_id
        , (CASE f.level
            WHEN 'A' THEN fc.weight_a
            WHEN 'B' THEN fc.weight_b
            WHEN 'C' THEN fc.weight_c
            WHEN 'D' THEN fc.weight_d
            WHEN 'E' THEN fc.weight_e
            WHEN 'O' THEN fc.weight_o
        END
        ) weight
        FROM t_flower f
        join t_cart c on c.flower_id = f.id and c.create_by = #{userId}
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = 0
        and f.status = 'UP'
        order by c.create_time desc
    </select>
 
    <select id="selectFlowerOrderList" resultType="com.mzl.flower.dto.response.flower.FlowerCartListDTO">
        SELECT f.*, s.name supplierName, concat(fc.parent_name, '/', fc.name) categoryStr
        , s.station_id stationId, fc.param_id
        , (CASE f.level
            WHEN 'A' THEN fc.weight_a
            WHEN 'B' THEN fc.weight_b
            WHEN 'C' THEN fc.weight_c
            WHEN 'D' THEN fc.weight_d
            WHEN 'E' THEN fc.weight_e
            WHEN 'O' THEN fc.weight_o
        END
        ) weight
        FROM t_flower f
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = 0
        and f.id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        order by f.create_time desc
    </select>
 
 
    <select id="myCollect" resultType="com.mzl.flower.dto.response.flower.FlowerShowListDTO">
        SELECT f.*, s.name supplierName, st.name supplierType, fc.name categoryStr
        FROM t_collect_flower c
        left join t_flower f on c.flower_id = f.id
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_supplier_type st on st.id = s.type_id
        left join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = 0 and c.deleted = 0
--         AND f.status = 'UP'
        <if test="condition.name != null and condition.name != ''">
            AND f.name LIKE concat('%', #{condition.name},'%')
        </if>
        <if test="condition.level != null and condition.level != ''">
            AND f.level = #{condition.level}
        </if>
        <if test="condition.tags != null and condition.tags != ''">
            AND f.tags LIKE concat('%', #{condition.tags},'%')
        </if>
        <if test="condition.category != null">
            AND f.category = #{condition.category}
        </if>
 
        <if test="condition.paramId != null and condition.params != null and condition.params.size > 0">
            AND exists (
            select 1 from flower_param_${condition.paramId} pp
            where pp.id = f.id
            and
            <foreach collection="condition.params" item="p" open=" " separator=" and " close="">
                field_${p.id} = #{p.value}
            </foreach>
            )
        </if>
        <if test="condition.userId != null and condition.userId != ''">
            AND c.user_id = #{condition.userId}
        </if>
 
        <if test="condition.column != null and condition.column != ''">
            order by ${condition.column}
        </if>
    </select>
 
    <select id="myBrowseHistory" resultType="com.mzl.flower.dto.response.flower.FlowerShowListDTO">
        SELECT f.*, s.name supplierName, st.name supplierType, fc.name categoryStr
        FROM t_browse_his c
        left join t_flower f on c.flower_id = f.id
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_supplier_type st on st.id = s.type_id
        left join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = 0 and c.deleted = 0
        <if test="condition.name != null and condition.name != ''">
            AND f.name LIKE concat('%', #{condition.name},'%')
        </if>
        <if test="condition.level != null and condition.level != ''">
            AND f.level = #{condition.level}
        </if>
        <if test="condition.tags != null and condition.tags != ''">
            AND f.tags LIKE concat('%', #{condition.tags},'%')
        </if>
        <if test="condition.category != null">
            AND f.category = #{condition.category}
        </if>
 
        <if test="condition.paramId != null and condition.params != null and condition.params.size > 0">
            AND exists (
            select 1 from flower_param_${condition.paramId} pp
            where pp.id = f.id
            and
            <foreach collection="condition.params" item="p" open=" " separator=" and " close="">
                field_${p.id} = #{p.value}
            </foreach>
            )
        </if>
        <if test="condition.userId != null and condition.userId != ''">
            AND c.user_id = #{condition.userId}
        </if>
 
        <choose>
            <when test="condition.column != null and condition.column != ''">
                <choose>
                    <when test="'f.sales'.equals(condition.column) or 'f.stock'.equals(condition.column)">
                        order by ${condition.column} desc
                    </when>
                    <otherwise>
                        order by ${condition.column} asc
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by c.create_time desc
            </otherwise>
        </choose>
    </select>
 
    <select id="searchInvalidCollect" resultType="java.lang.Long" parameterType="java.lang.String">
        SELECT c.id
        FROM t_collect_flower c left join t_flower f on c.flower_id = f.id
        WHERE f.deleted = 0 and c.deleted = 0 and f.status != 'UP' and c.user_id = #{userId}
    </select>
 
    <update id="addFlowerStock">
        update t_flower set stock = stock + #{num} where id = #{id}
    </update>
 
    <update id="updateFlowerSales">
        update t_flower set real_sales = ifnull(real_sales, 0) + #{num} where id = #{id}
    </update>
 
    <select id="getUpFlowerStock" resultType="java.lang.Integer">
        SELECT sum(f.stock)
        FROM t_flower f
        join t_supplier_info s on s.id = f.supplier_id
        join t_flower_category fc on fc.id = f.category
        WHERE f.deleted = 0
        AND f.status = 'UP'
        AND (fc.shown = 1 or fc.shown is null)
        AND (f.shown = 1 or f.shown is null)
        AND (s.showed = 1 or s.showed is null)
        and exists (select 1 from t_flower_category fcp where fcp.id = fc.parent_id and (fcp.shown = 1 or fcp.shown is null))
    </select>
    <select id="selectZoneFlowerList" resultType="com.mzl.flower.dto.response.flower.FlowerListDTO">
        SELECT f.*, s.name supplierName,fc.name categoryStr,fzp.zone_id as zoneId,fzp.rank as zoneRank
        FROM t_flower f
        left join t_supplier_info s on s.id = f.supplier_id
        left join t_flower_category fc on fc.id = f.category
        left JOIN t_flower_zone_map fzp on fzp.flower_id = f.id
        WHERE f.deleted = 0
        and f.status in ('PENDING','UP','REJECT','OFF','FORCE_OFF')
        <if test="condition.name != null and condition.name != ''">
            AND f.name LIKE concat('%', #{condition.name},'%')
        </if>
        <if test="condition.supplierName != null and condition.supplierName != ''">
            AND s.name LIKE concat('%', #{condition.supplierName},'%')
        </if>
        <if test="condition.level != null and condition.level != ''">
            AND f.level = #{condition.level}
        </if>
        <if test="condition.zoneId != null">
            AND fzp.zone_id = #{condition.zoneId}
        </if>
        ORDER BY fzp.rank asc
    </select>
</mapper>