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
<?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.coupon.CouponTemplateMapperCustom">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.mzl.flower.entity.coupon.CouponTemplateDO">
        <id column="id" property="id" />
        <result column="create_by" property="createBy" />
        <result column="create_time" property="createTime" />
        <result column="update_by" property="updateBy" />
        <result column="update_time" property="updateTime" />
        <result column="deleted" property="deleted" />
        <result column="category" property="category" />
        <result column="coupon_code" property="couponCode" />
        <result column="coupon_name" property="couponName" />
        <result column="coupon_description" property="couponDescription" />
        <result column="coupon_amount" property="couponAmount" />
        <result column="coupon_discount_type" property="couponDiscountType" />
        <result column="coupon_discount_value" property="couponDiscountValue" />
        <result column="min_order_amount" property="minOrderAmount" />
        <result column="max_discount_amount" property="maxDiscountAmount" />
        <result column="get_type" property="getType" />
        <result column="get_user_type" property="getUserType" />
        <result column="get_start_date" property="getStartDate" />
        <result column="get_end_date" property="getEndDate" />
        <result column="get_limit" property="getLimit" />
        <result column="usage_type" property="usageType" />
        <result column="usage_limit" property="usageLimit" />
        <result column="usage_per_user" property="usagePerUser" />
        <result column="usage_start_date" property="usageStartDate" />
        <result column="usage_end_date" property="usageEndDate" />
        <result column="usage_time_type" property="usageTimeType" />
        <result column="usage_time_num" property="usageTimeNum" />
        <result column="status" property="status" />
        <result column="point" property="point" />
        <result column="member_id" property="memberId" />
    </resultMap>
 
 
    <update id="activeBatchCouponTemplate">
        update t_coupon_template set status='active',update_by=#{param.updateBy},update_time=now() where id in
        <foreach collection="param.idList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <update id="expireBatchCouponTemplate">
        update t_coupon_template set status='expired',update_by=#{param.updateBy},update_time=now() where id in
        <foreach collection="param.idList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
 
    <update id="deleteBatchCouponTemplate">
        update t_coupon_template set deleted=true ,update_by=#{param.updateBy},update_time=now() where id in
        <foreach collection="param.idList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <update id="expireHomeActivityCouponTemplate">
        update t_coupon_template set status='expired'  where  category='activity' and get_type='home' and status='active'
    </update>
    <update id="expireActivityCouponTemplateAll">
        update t_coupon_template set status='expired'  where category='activity' and  get_end_date &lt; NOW()
    </update>
 
    <select id="getCouponTemplatePage" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        <include refid="QuerySql2"/>
    </select>
    <select id="getList" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        <include refid="QuerySql2"/>
    </select>
    <select id="getCouponPointList" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        <include refid="QueryPointSql" />
    </select>
    <select id="getPointPage" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        <include refid="QueryPointSql" />
    </select>
 
<!--    app活动优惠券-->
    <select id="getActivityEffectList" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
 
        SELECT *
        FROM t_coupon_template t
        WHERE t.deleted = FALSE
        <if test="param.category != null and param.category != '' and param.category == 'activity' ">
            AND t.get_start_date &lt;= NOW()
            AND NOW() &lt;= t.get_end_date
        </if>
 
        <if test="param.category != null and param.category != ''">
            AND category =  #{param.category}
        </if>
        <if test="param.status != null and param.status != ''">
            AND status =  #{param.status}
        </if>
        <if test="param.getType != null and param.getType != ''">
            AND t.get_type =  #{param.getType}
        </if>
        ORDER BY t.create_time DESC
 
    </select>
    <select id="getCouponCustomerList"
            resultType="com.mzl.flower.dto.response.coupon.CouponTemplateCustomerVO">
        select ci.id,ci.name,u.tel
        from t_coupon_template c
        left JOIN t_coupon_template_customer cc
        on c.id=cc.coupon_id
        left join t_customer_info ci
        on cc.custom_id=ci.id
        left join t_user u
        on ci.user_id=u.id
        where c.deleted= false
        and c.id=#{id}
 
    </select>
    <select id="getVipList" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        <include refid="QuerySqlVip" />
    </select>
    <select id="getCouponTemplateVipPage" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        <include refid="QuerySqlVip" />
    </select>
    <select id="getHomeActivityEffectAlert" resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        SELECT *
        FROM t_coupon_template t
        WHERE t.deleted = FALSE
 
        <if test="param.category != null and param.category != '' and param.category == 'activity' ">
            AND t.get_start_date &lt;= NOW()
            AND NOW() &lt;= t.get_end_date
        </if>
 
        <if test="param.category != null and param.category != ''">
            AND t.category =  #{param.category}
        </if>
        <if test="param.status != null and param.status != ''">
            AND t.status =  #{param.status}
        </if>
        <if test="param.getType != null and param.getType != ''">
            AND t.get_type =  #{param.getType}
        </if>
        ORDER BY t.create_time DESC
        limit 1
    </select>
    <select id="getActivityEffectListWithMine"
            resultType="com.mzl.flower.dto.response.coupon.CouponTemplateVO">
        select t2.*,t2.get_limit,IFNULL(t2.get_limit- t2.getNum,0) as left_num  from (
            SELECT
                t.*,
                (
                    SELECT count( 1 )
                    FROM t_coupon_record cr
                    WHERE  cr.coupon_id = t.id AND cr.deleted = 0
                    <if test="param.userId != null and param.userId != ''">
                        AND cr.user_id = #{param.userId}
                    </if>
 
                ) AS getNum,
                (
                    SELECT min( cr.create_time )
                    FROM t_coupon_record cr
                    WHERE cr.coupon_id = t.id AND cr.deleted = 0
                    <if test="param.userId != null and param.userId != ''">
                        AND cr.user_id = #{param.userId}
                    </if>
                ) AS record_create_time
            FROM
            t_coupon_template t
            WHERE
            t.deleted = FALSE
            <if test="param.category != null and param.category != '' and param.category == 'activity' ">
                AND t.get_start_date &lt;= NOW()
                AND NOW() &lt;= t.get_end_date
            </if>
 
            <if test="param.category != null and param.category != ''">
                AND category =  #{param.category}
            </if>
            <if test="param.status != null and param.status != ''">
                AND status =  #{param.status}
            </if>
            <if test="param.getType != null and param.getType != ''">
                AND t.get_type =  #{param.getType}
            </if>
 
        ) t2
 
        ORDER BY
        left_num desc,
        create_time DESC,
        record_create_time ASC
    </select>
 
    <sql id="QueryPointSql">
        select *
        from t_coupon_template
        where deleted=0
        <if test="param.category != null and param.category != ''">
            AND category =  #{param.category}
        </if>
        <if test="param.status != null and param.status != ''">
            AND status =  #{param.status}
        </if>
        <if test="param.minPoint != null ">
            AND point &gt;=  #{param.minPoint}
        </if>
        <if test="param.maxPoint != null ">
            AND point &lt;=  #{param.maxPoint}
        </if>
        <if test="param.orderStr != null and param.orderStr != '' ">
            order by ${param.orderStr}
        </if>
 
    </sql>
 
    <sql id="QuerySql2">
        select *
        from (
            select t.*,
            create_by_user.nick_name as createByName,
            IFNULL(get_num.cnt, 0) as getNum,
            IFNULL(t.coupon_amount, 0) - IFNULL(get_num.cnt, 0) as unGetNum
        from t_coupon_template t
        left join t_user create_by_user
        on t.create_by =create_by_user.id
        left join (select coupon_id,count(1) as cnt  from t_coupon_record where deleted=0 group by coupon_id) get_num
        on t.id=get_num.coupon_id
        ) t
        where t.deleted=0
        <if test="param.name != null and param.name != ''">
            AND t.coupon_name like concat('%', #{param.name},'%')
        </if>
        <if test="param.couponDiscountType != null and param.couponDiscountType != ''">
            AND t.coupon_discount_type =  #{param.couponDiscountType}
        </if>
        <if test="param.getType != null and param.getType != ''">
            AND t.get_type =  #{param.getType}
        </if>
        <if test="param.getUserType != null and param.getUserType != ''">
            AND t.get_user_type =  #{param.getUserType}
        </if>
        <if test="param.id != null and param.id != ''">
            AND t.id =  #{param.id}
        </if>
        <if test="param.category != null and param.category != ''">
            AND t.category =  #{param.category}
        </if>
        <if test="param.status != null and param.status != ''">
            AND t.status =  #{param.status}
        </if>
        order by t.create_time desc
    </sql>
 
    <sql id="QuerySqlVip">
        select *
        from (
            select t.*,
            create_by_user.nick_name as createByName,
            IFNULL(get_num.cnt, 0) as getNum,
            IFNULL(t.coupon_amount, 0) - IFNULL(get_num.cnt, 0) as unGetNum,
            m.`name` as member_name
            from t_coupon_template t
            left join t_user create_by_user
            on t.create_by =create_by_user.id
            left join (select coupon_id,count(1) as cnt  from t_coupon_record where deleted=0 group by coupon_id) get_num
            on t.id=get_num.coupon_id
            left join t_member m
            on t.member_id=m.id
        ) t
        where t.deleted=0
        <if test="param.name != null and param.name != ''">
            AND t.coupon_name like concat('%', #{param.name},'%')
        </if>
        <if test="param.couponDiscountType != null and param.couponDiscountType != ''">
            AND t.coupon_discount_type =  #{param.couponDiscountType}
        </if>
        <if test="param.getType != null and param.getType != ''">
            AND t.get_type =  #{param.getType}
        </if>
        <if test="param.getUserType != null and param.getUserType != ''">
            AND t.get_user_type =  #{param.getUserType}
        </if>
        <if test="param.id != null and param.id != ''">
            AND t.id =  #{param.id}
        </if>
        <if test="param.category != null and param.category != ''">
            AND t.category =  #{param.category}
        </if>
        <if test="param.status != null and param.status != ''">
            AND t.status =  #{param.status}
        </if>
        order by t.create_time desc
    </sql>
 
</mapper>