Helius
2021-03-10 62d2a24a0d2c5bca61f727ac3d9e6bd7ce162151
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?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.matrix.system.shopXcx.dao.ShopCouponDao">
 
    <!-- 定义ShopCoupon 的复杂关联map -->
    <resultMap type="com.matrix.system.shopXcx.bean.ShopCoupon" id="ShopCouponMap">
        <id property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="cName" column="c_name"/>
        <result property="offsetAmount" column="offset_amount"/>
        <result property="minAmount" column="min_amount"/>
        <result property="beginTime" column="begin_time"/>
        <result property="endTime" column="end_time"/>
        <result property="maxQuantity" column="max_quantity"/>
        <result property="getLimit" column="get_limit"/>
        <result property="quantityReceive" column="quantity_receive"/>
        <result property="couponStatus" column="coupon_status"/>
        <result property="isOpen" column="is_open"/>
        <result property="isAll" column="is_all"/>
        <result property="productIds" column="product_ids"/>
        <result property="attrIds" column="attr_ids"/>
        <result property="tag" column="tag"/>
        <result property="kind" column="kind"/>
        <result property="shopId" column="shop_id"/>
        <result property="shopName" column="shopName"/>
        <result property="companyId" column="company_id" />
 
 
    </resultMap>
 
 
    <!-- 定义ShopCoupon 的简单map  ,本map不添加其他的关联属性 -->
    <resultMap type="com.matrix.system.shopXcx.bean.ShopCoupon" id="ShopCouponSimpleMap">
        <id property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="cName" column="c_name"/>
        <result property="offsetAmount" column="offset_amount"/>
        <result property="minAmount" column="min_amount"/>
        <result property="beginTime" column="begin_time"/>
        <result property="endTime" column="end_time"/>
        <result property="maxQuantity" column="max_quantity"/>
        <result property="getLimit" column="get_limit"/>
        <result property="quantityReceive" column="quantity_receive"/>
        <result property="couponStatus" column="coupon_status"/>
        <result property="isOpen" column="is_open"/>
        <result property="isAll" column="is_all"/>
        <result property="productIds" column="product_ids"/>
        <result property="attrIds" column="attr_ids"/>
        <result property="tag" column="tag"/>
        <result property="kind" column="kind"/>
        <result property="shopId" column="shop_id"/>
        <result property="companyId" column="company_id" />
    </resultMap>
 
    <!-- 字段sql -->
    <sql id="columns">
        create_by,
        create_time,
        update_by,
        update_time,
            id,
            c_name,
            offset_amount,
            min_amount,
            begin_time,
            end_time,
            max_quantity,
            get_limit,
            quantity_receive,
            coupon_status,
            is_open,
            is_all,
            product_ids,
            attr_ids,
            tag,
            kind,
            shop_id,
            company_id
    </sql>
 
    <!-- 属性sql -->
    <sql id="propertys">
        #{item.createBy},
        now(),
        #{item.updateBy},
        now(),
        #{item.id},
        #{item.cName},
        #{item.offsetAmount},
        #{item.minAmount},
        #{item.beginTime},
        #{item.endTime},
        #{item.maxQuantity},
        #{item.getLimit},
        #{item.quantityReceive},
        #{item.couponStatus},
        #{item.isOpen},
        #{item.isAll},
        #{item.productIds},
        #{item.attrIds},
        #{item.tag},
        #{item.kind},
        #{item.shopId},
        #{item.companyId}
    </sql>
 
    <!-- where sql -->
    <sql id="where_sql">
 
        <if test="record!=null">
            <if test="(record.id!=null and record.id!='') or  (record.id!='' and record.id==0)  ">
                and id = #{record.id}
            </if>
            <if test="(record.cName!=null and record.cName!='') or  (record.cName!='' and record.cName==0)  ">
                and c_name = #{record.cName}
            </if>
            <if test="(record.offsetAmount!=null and record.offsetAmount!='') or  (record.offsetAmount!='' and record.offsetAmount==0)  ">
                and offset_amount = #{record.offsetAmount}
            </if>
            <if test="(record.minAmount!=null and record.minAmount!='') or  (record.minAmount!='' and record.minAmount==0)  ">
                and min_amount = #{record.minAmount}
            </if>
            <if test="(record.beginTime!=null and record.beginTime!='') or  (record.beginTime!='' and record.beginTime==0)  ">
                and begin_time = #{record.beginTime}
            </if>
            <if test="(record.endTime!=null and record.endTime!='') or  (record.endTime!='' and record.endTime==0)  ">
                and end_time = #{record.endTime}
            </if>
            <if test="(record.maxQuantity!=null and record.maxQuantity!='') or  (record.maxQuantity!='' and record.maxQuantity==0)  ">
                and max_quantity = #{record.maxQuantity}
            </if>
            <if test="(record.getLimit!=null and record.getLimit!='') or  (record.getLimit!='' and record.getLimit==0)  ">
                and get_limit = #{record.getLimit}
            </if>
            <if test="(record.quantityReceive!=null and record.quantityReceive!='') or  (record.quantityReceive!='' and record.quantityReceive==0)  ">
                and quantity_receive = #{record.quantityReceive}
            </if>
            <if test="(record.couponStatus!=null and record.couponStatus!='') or  (record.couponStatus!='' and record.couponStatus==0)  ">
                and coupon_status = #{record.couponStatus}
            </if>
            <if test="(record.isOpen!=null and record.isOpen!='') or  (record.isOpen!='' and record.isOpen==0)  ">
                and is_open = #{record.isOpen}
            </if>
            <if test="(record.isAll!=null and record.isAll!='') or  (record.isAll!='' and record.isAll==0)  ">
                and is_all = #{record.isAll}
            </if>
            <if test="(record.productIds!=null and record.productIds!='') or  (record.productIds!='' and record.productIds==0)  ">
                and product_ids = #{record.productIds}
            </if>
            <if test="(record.attrIds!=null and record.attrIds!='') or  (record.attrIds!='' and record.attrIds==0)  ">
                and attr_ids = #{record.attrIds}
            </if>
            <if test="(record.tag!=null and record.tag!='') or  (record.tag!='' and record.tag==0)  ">
                and tag = #{record.tag}
            </if>
            <if test="(record.kind!=null and record.kind!='') or  (record.kind!='' and record.kind==0)  ">
                and kind = #{record.kind}
            </if>
 
            <if test="(record.shopId!=null and record.shopId!='') or  (record.shopId!='' and record.shopId==0)  ">
                and shop_id = #{record.shopId}
            </if>
 
            <if test="record.companyId != null and record.companyId !='' ">
                and company_id = #{record.companyId}
            </if>
 
 
        </if>
 
 
    </sql>
 
    <!-- custom where sql - kingsley  -->
    <sql id="custom_where_sql">
 
        <if test="record!=null">
            <if test="(record.id!=null and record.id!='') or  (record.id!='' and record.id==0)  ">
                and id = #{record.id}
            </if>
            <if test="(record.cName!=null and record.cName!='') or  (record.cName!='' and record.cName==0)  ">
                and instr(c_name, #{record.cName})
            </if>
            <if test="(record.offsetAmount!=null and record.offsetAmount!='') or  (record.offsetAmount!='' and record.offsetAmount==0)  ">
                and offset_amount = #{record.offsetAmount}
            </if>
            <if test="(record.minAmount!=null and record.minAmount!='') or  (record.minAmount!='' and record.minAmount==0)  ">
                and min_amount = #{record.minAmount}
            </if>
            <if test="(record.couponBeginTime!=null and record.couponBeginTime!='') or  (record.couponBeginTime!='' and record.couponBeginTime==0)  ">
                and #{record.couponBeginTime} >= date_format(begin_time, '%Y-%m-%d')
            </if>
            <if test="(record.couponEndTime!=null and record.couponEndTime!='') or  (record.couponEndTime!='' and record.couponEndTime==0)  ">
                and date_format(end_time, '%Y-%m-%d') >= #{record.couponEndTime}
            </if>
            <if test="(record.maxQuantity!=null and record.maxQuantity!='') or  (record.maxQuantity!='' and record.maxQuantity==0)  ">
                and max_quantity = #{record.maxQuantity}
            </if>
            <if test="(record.getLimit!=null and record.getLimit!='') or  (record.getLimit!='' and record.getLimit==0)  ">
                and get_limit = #{record.getLimit}
            </if>
            <if test="(record.quantityReceive!=null and record.quantityReceive!='') or  (record.quantityReceive!='' and record.quantityReceive==0)  ">
                and quantity_receive = #{record.quantityReceive}
            </if>
            <if test="(record.couponStatus!=null and record.couponStatus!='') or  (record.couponStatus!='' and record.couponStatus==0)  ">
                and coupon_status = #{record.couponStatus}
            </if>
            <if test="(record.isOpen!=null and record.isOpen!='') or  (record.isOpen!='' and record.isOpen==0)  ">
                and is_open = #{record.isOpen}
            </if>
            <if test="(record.isAll!=null and record.isAll!='') or  (record.isAll!='' and record.isAll==0)  ">
                and is_all = #{record.isAll}
            </if>
            <if test="(record.productIds!=null and record.productIds!='') or  (record.productIds!='' and record.productIds==0)  ">
                and product_ids = #{record.productIds}
            </if>
            <if test="(record.attrIds!=null and record.attrIds!='') or  (record.attrIds!='' and record.attrIds==0)  ">
                and attr_ids = #{record.attrIds}
            </if>
            <if test="(record.tag!=null and record.tag!='') or  (record.tag!='' and record.tag==0)  ">
                and tag = #{record.tag}
            </if>
            <if test="(record.kind!=null and record.kind!='') or  (record.kind!='' and record.kind==0)  ">
                and kind = #{record.kind}
            </if>
            <if test="(record.shopId!=null and record.shopId!='') or  (record.shopId!='' and record.shopId==0)  ">
                and shop_id = #{record.shopId}
            </if>
 
            <if test="record.companyId != null and record.companyId !='' ">
                and company_id = #{record.companyId}
            </if>
 
 
        </if>
 
    </sql>
 
    <!--  插入方法   -->
    <insert id="insert" parameterType="com.matrix.system.shopXcx.bean.ShopCoupon"
            useGeneratedKeys="true" keyProperty="item.id">
        INSERT INTO shop_coupon (
        <include refid="columns"></include>
        )
        VALUES (
        <include refid="propertys"></include>
        )
    </insert>
 
 
    <!--  批量插入   -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO shop_coupon (
        <include refid="columns"></include>
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">(
            <include refid="propertys"></include>
            )
        </foreach>
    </insert>
 
 
    <!--  根据Map更新 部分更新   -->
    <update id="updateByMap" parameterType="java.util.HashMap">
        UPDATE shop_coupon
        <set>
            <if test="_parameter.containsKey('cName')">
                c_name = #{cName},
            </if>
            <if test="_parameter.containsKey('offsetAmount')">
                offset_amount = #{offsetAmount},
            </if>
            <if test="_parameter.containsKey('minAmount')">
                min_amount = #{minAmount},
            </if>
            <if test="_parameter.containsKey('beginTime')">
                begin_time = #{beginTime},
            </if>
            <if test="_parameter.containsKey('endTime')">
                end_time = #{endTime},
            </if>
            <if test="_parameter.containsKey('maxQuantity')">
                max_quantity = #{maxQuantity},
            </if>
            <if test="_parameter.containsKey('getLimit')">
                get_limit = #{getLimit},
            </if>
            <if test="_parameter.containsKey('quantityReceive')">
                quantity_receive = #{quantityReceive},
            </if>
            <if test="_parameter.containsKey('couponStatus')">
                coupon_status = #{couponStatus},
            </if>
            <if test="_parameter.containsKey('isOpen')">
                is_open = #{isOpen},
            </if>
            <if test="_parameter.containsKey('isAll')">
                is_all = #{isAll},
            </if>
            <if test="_parameter.containsKey('productIds')">
                product_ids = #{productIds},
            </if>
            <if test="_parameter.containsKey('attrIds')">
                attr_ids = #{attrIds},
            </if>
            <if test="_parameter.containsKey('tag')">
                tag = #{tag},
            </if>
            <if test="true">
                update_time = now(),
            </if>
            <if test="_parameter.containsKey('kind')">
                kind = #{kind},
            </if>
            <if test="_parameter.containsKey('shopId')">
                shop_id = #{shopId},
            </if>
 
            <if test="_parameter.containsKey('companyId')">
                company_id = #{companyId},
            </if>
        </set>
        WHERE id=#{id}
    </update>
 
 
    <!--  根据对象更新 部分更新   -->
    <update id="updateByModel" parameterType="Integer">
        UPDATE shop_coupon
        <set>
            <if test="record.cName != null and record.cName != '' ">
                c_name = #{record.cName},
            </if>
            <if test="record.offsetAmount != null ">
                offset_amount = #{record.offsetAmount},
            </if>
            <if test="record.minAmount != null ">
                min_amount = #{record.minAmount},
            </if>
            <if test="record.beginTime != null ">
                begin_time = #{record.beginTime},
            </if>
            <if test="record.endTime != null ">
                end_time = #{record.endTime},
            </if>
            <if test="record.maxQuantity != null ">
                max_quantity = #{record.maxQuantity},
            </if>
            <if test="record.getLimit != null ">
                get_limit = #{record.getLimit},
            </if>
            <if test="record.quantityReceive != null ">
                quantity_receive = #{record.quantityReceive},
            </if>
            <if test="record.couponStatus != null ">
                coupon_status = #{record.couponStatus},
            </if>
            <if test="record.isOpen != null ">
                is_open = #{record.isOpen},
            </if>
            <if test="record.isAll != null ">
                is_all = #{record.isAll},
            </if>
            <if test="record.productIds != null and record.productIds != '' ">
                product_ids = #{record.productIds},
            </if>
            <if test="record.shopId != null and record.shopId != '' ">
                shop_id = #{record.shopId},
            </if>
            <if test="record.attrIds != null and record.attrIds != '' ">
                attr_ids = #{record.attrIds},
            </if>
            <if test="record.tag != null">
                tag = #{record.tag},
            </if>
            <if test="record.kind != null">
                kind = #{record.kind},
            </if>
            <if test="true">
                update_time = now()
            </if>
        </set>
        WHERE id=#{record.id}
    </update>
 
    <!-- 批量删除 -->
    <delete id="deleteByIds" parameterType="java.util.List">
        delete from shop_coupon where id in
        <foreach collection="list" index="index" item="item" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </delete>
 
    <!-- 根据id删除-->
    <delete id="deleteById" parameterType="Integer">
        DELETE
        FROM shop_coupon
        where id = #{id}
    </delete>
 
    <!-- 根据对象删除-->
    <delete id="deleteByModel" parameterType="com.matrix.system.shopXcx.bean.ShopCoupon">
        DELETE FROM shop_coupon
        <where>
            <include refid="where_sql"></include>
        </where>
    </delete>
 
 
    <!-- 分页查询 -->
    <select id="selectInPage" resultMap="ShopCouponMap">
        select
        ( select shop_name from sys_shop_info where id=shop_id )as shopName,
        <include refid="columns"></include>
        from shop_coupon
        <where>
            <include refid="custom_where_sql"></include>
        </where>
        <if test="pageVo !=null"><!-- 判断pageVo对象是否为空 -->
            <if test="pageVo.sort !=null  and pageVo.order !=null">
                order by
                ${pageVo.sort} ${pageVo.order}
            </if>
            <if test="pageVo.offset >=0  and pageVo.limit >0">
                limit
                #{pageVo.offset},#{pageVo.limit}
            </if>
        </if>
    </select>
 
    <!-- 查询总条数 -->
    <select id="selectTotalRecord" parameterType="long" resultType="java.lang.Integer">
        select count(*)
        from shop_coupon
        <where>
            <include refid="custom_where_sql"></include>
        </where>
    </select>
 
    <!-- 根据id查询-->
    <select id="selectById" resultMap="ShopCouponMap">
        select
        <include refid="columns"></include>
        from shop_coupon
        where id=#{id}
    </select>
 
 
    <!-- 根据id 锁表查询-->
    <select id="selectForUpdate" resultMap="ShopCouponMap">
        select
        <include refid="columns"></include>
        from shop_coupon
        where id=#{id}
        for update
    </select>
 
 
    <!-- 根据对象查询-->
    <select id="selectByModel" resultMap="ShopCouponMap">
        select
        <include refid="columns"></include>
        from shop_coupon
        <where>
            <include refid="where_sql"></include>
        </where>
    </select>
 
    <select id="selectUsableCoupon" resultMap="ShopCouponMap">
        select
        <include refid="columns"></include>
        from shop_coupon
        where kind=1 and now() >= begin_time and end_time >= now()
        and is_open = 1 and (max_quantity > quantity_receive or 1 > max_quantity)
        <if test="!isNewPeople">
            and get_limit = 1
        </if>
        and id not in (
        select c_id
        from shop_coupon_record
        where user_id = #{userId}
        )
        <if test="pageVo != null"><!-- 判断pageVo对象是否为空 -->
            <if test="pageVo.sort != null  and pageVo.order != null">
                order by
                ${pageVo.sort} ${pageVo.order}
            </if>
            <if test="pageVo.offset >= 0  and pageVo.limit > 0">
                limit
                #{pageVo.offset}, #{pageVo.limit}
            </if>
        </if>
    </select>
 
    <!--微信返回map-->
    <resultMap type="com.matrix.system.shopXcx.bean.ShopCoupon" id="WxShopCouponMap">
        <id property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="cName" column="c_name"/>
        <result property="offsetAmount" column="offset_amount"/>
        <result property="minAmount" column="min_amount"/>
        <result property="beginTime" column="begin_time"/>
        <result property="endTime" column="end_time"/>
        <result property="maxQuantity" column="max_quantity"/>
        <result property="getLimit" column="get_limit"/>
        <result property="quantityReceive" column="quantity_receive"/>
        <result property="couponStatus" column="coupon_status"/>
        <result property="isOpen" column="is_open"/>
        <result property="isAll" column="is_all"/>
        <result property="productIds" column="product_ids"/>
        <result property="attrIds" column="attr_ids"/>
        <result property="canReceive" column="can_receive"/>
        <result property="tag" column="tag"/>
        <result property="isUsing" column="is_using"/>
        <result property="kind" column="kind"/>
        <result property="companyId" column="company_id"/>
    </resultMap>
 
    <select id="selectUsableCouponByProductInfo" resultMap="WxShopCouponMap">
        select sc.*,
        if(isnull(scr.id), 1, 2) can_receive
        from shop_coupon sc left join shop_coupon_record scr
        on sc.id = scr.c_id and user_id = #{userId}
        where sc.kind=1 and now() >= begin_time and end_time >= now() and is_open = 1
        and (is_all = 1 or
        (
        concat('%,', product_ids, ',%') like concat('%,', #{productId}, ',%')
        <if test="attrIds != null and attrIds.size() > 0">
            or
            <foreach collection="attrIds" item="item" open="(" close=")" separator="or">
                concat('%,', attr_ids, ',%') like concat('%,', #{item}, ',%')
            </foreach>
        </if>
        )
        )
        and (
        (if(isnull(scr.id), 1 , 2) = 1 and (max_quantity > quantity_receive or 1 > max_quantity))
        or (if(isnull(scr.id), 1 , 2) = 2 and user_id = #{userId} and is_using = 2)
        )
    </select>
 
    <select id="getCouponByTitle" resultMap="ShopCouponMap">
        select *
        from shop_coupon
        where c_name = #{title} and company_id=#{companyId}
    </select>
 
    <select id="selectHdListByProductId" resultMap="WxShopCouponMap">
        select *
        from shop_coupon
        where
        company_id=#{companyId}
        and kind=2 and now() >= begin_time and end_time >= now()
        and is_open = 1
        and (is_all = 1 or
        (
        concat('%,', product_ids, ',%') like concat('%,', #{productId}, ',%')
        <if test="attrIds != null and attrIds.size() > 0">
            or
            <foreach collection="attrIds" item="item" open="(" close=")" separator="or">
                concat('%,', attr_ids, ',%') like concat('%,', #{item}, ',%')
            </foreach>
        </if>
        )
        )
    </select>
 
 
    <!--根据优惠券标签查询优惠券列表-->
    <select id="selectCouponListByTag" resultMap="WxShopCouponMap">
        select sc.*,
        if(isnull(scr.id), 1, 2) can_receive,
        ifnull(scr.is_using, 0) is_using
        from shop_coupon sc left join shop_coupon_record scr
        on sc.id = scr.c_id and user_id = #{userId}
        where sc.company_id=#{companyId} and sc.kind=1  and now() >= begin_time and end_time >= now() and is_open = 1
        <!--<if test="!isNewPeople">-->
        <!--and get_limit = 1-->
        <!--</if>-->
        and tag = #{tag}
        and (
        if(isnull(scr.id), 1 , 2) = 1
        or (if(isnull(scr.id), 1 , 2) = 2 and user_id = #{userId})
        )
    </select>
 
    <!-- 根据状态批量更新 -->
    <update id="updateStateByStateAndIds">
        update shop_coupon
        <set>
            <if test="state == 1 or state == 2">
                is_open = #{state}
            </if>
        </set>
        where id in
        <foreach collection="list" index="index" item="item" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </update>
 
    <!--更新所有优惠券状态(1=未开始,2=进行中,3=已结束)-->
    <update id="updateAllCouponStatus">
        update shop_coupon
        set coupon_status = if(begin_time > now(), 1, if(now() > end_time, 3, 2))
    </update>
 
    <!--根据优惠券ID更新优惠券状态(1=未开始,2=进行中,3=已结束)-->
    <update id="updateCouponStatusById">
        update shop_coupon
        set coupon_status = if(begin_time > now(), 1, if(now() > end_time, 3, 2))
        where id = #{id}
    </update>
 
    <!-- 根据ID、用户ID查询-->
    <select id="selectByIdAndUserId" resultMap="ShopCouponMap">
        select coupon.*
        from shop_coupon coupon,
             shop_coupon_record coupon_record
        where coupon.id = coupon_record.c_id
          and coupon_record.user_id = #{userId}
          and now() >= coupon.begin_time
          and coupon.end_time >= now()
          and coupon.is_open = 1
          and coupon.id = #{id}
          and coupon_record.is_using = 2
    </select>
 
</mapper>