UserCouponMapper.xml
9.36 KB
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
<?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.yohoufo.dal.promotion.UserCouponMapper" >
<resultMap id="BaseResultMap" type="com.yohoufo.dal.promotion.model.UserCoupon" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="uid" property="uid" jdbcType="INTEGER" />
<result column="coupon_id" property="couponId" jdbcType="INTEGER" />
<result column="coupon_token" property="couponToken" jdbcType="VARCHAR" />
<result column="coupon_type" property="couponType" jdbcType="INTEGER" />
<result column="coupon_code" property="couponCode" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="order_code" property="orderCode" jdbcType="BIGINT" />
<result column="use_time" property="useTime" jdbcType="INTEGER" />
<result column="start_time" property="startTime" jdbcType="INTEGER" />
<result column="end_time" property="endTime" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, uid, coupon_id, coupon_type, coupon_code, status, order_code, use_time, start_time,
end_time, create_time, coupon_token
</sql>
<sql id="A_Base_Column_List" >
a.id, a.uid, a.coupon_id, a.coupon_type, a.coupon_code, a.status, a.order_code, a.use_time, a.start_time,
a.end_time, a.create_time, a.coupon_token
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_coupon
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByUidAndCouponCodes" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user_coupon
where uid = #{uid}
AND coupon_code IN
<foreach collection="couponCodes" item="couponCode" open="(" separator="," close=")">
#{couponCode,jdbcType=VARCHAR}
</foreach>
</select>
<select id="selectByUidAndToken" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from user_coupon
where uid = #{uid,jdbcType=INTEGER}
and coupon_token = #{couponToken,jdbcType=VARCHAR}
limit 1
</select>
<select id="selectUsableCouponByUid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_coupon
where uid = #{uid,jdbcType=INTEGER}
<include refid="CouponsLogsQueryUsable" />
order by end_time
</select>
<sql id="CouponsLogsQueryUsable" >
<!-- 在有效时间范围内 -->
and <![CDATA[ end_time > #{now, jdbcType=INTEGER} ]]>
<!-- 未使用-->
and status = 0
</sql>
<select id="selectCntUsableCouponByUid" resultType="int" parameterType="java.lang.Integer" >
select
count(1)
from user_coupon,coupon
where coupon.id = user_coupon.coupon_id
<if test="businessClient!=null">
and coupon.business_client = #{businessClient,jdbcType=VARCHAR}
</if>
and user_coupon.uid = #{uid,jdbcType=INTEGER}
<!-- 在有效时间范围内 -->
and <![CDATA[ user_coupon.end_time > #{now, jdbcType=INTEGER} ]]>
<!-- 未使用-->
and user_coupon.status = 0
</select>
<insert id="insert" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
insert into user_coupon (id, uid, coupon_id,
coupon_type, coupon_code, status,
order_code, use_time, start_time,
end_time, create_time,coupon_token)
values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{couponId,jdbcType=INTEGER},
#{couponType,jdbcType=INTEGER}, #{couponCode,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
#{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
#{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR})
</insert>
<insert id="insertWhere" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
insert into user_coupon (id, uid, coupon_id,
coupon_type, coupon_code, status,
order_code, use_time, start_time,
end_time, create_time,coupon_token)
select #{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{couponId,jdbcType=INTEGER},
#{couponType,jdbcType=INTEGER}, #{couponCode,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
#{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
#{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR} from dual
where not EXISTS (select 1 from user_coupon where uid=#{uid,jdbcType=INTEGER} and coupon_token=#{couponToken,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
update user_coupon
<set >
<if test="uid != null" >
uid = #{uid,jdbcType=INTEGER},
</if>
<if test="couponId != null" >
coupon_id = #{couponId,jdbcType=INTEGER},
</if>
<if test="couponType != null" >
coupon_type = #{couponType,jdbcType=TINYINT},
</if>
<if test="couponCode != null" >
coupon_code = #{couponCode,jdbcType=VARCHAR},
</if>
<if test="status != null" >
status = #{status,jdbcType=TINYINT},
</if>
<if test="orderCode != null" >
order_code = #{orderCode,jdbcType=BIGINT},
</if>
<if test="useTime != null" >
use_time = #{useTime,jdbcType=INTEGER},
</if>
<if test="startTime != null" >
start_time = #{startTime,jdbcType=INTEGER},
</if>
<if test="endTime != null" >
end_time = #{endTime,jdbcType=INTEGER},
</if>
<if test="createTime != null" >
create_time = #{createTime,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
update user_coupon
set uid = #{uid,jdbcType=INTEGER},
coupon_id = #{couponId,jdbcType=INTEGER},
coupon_type = #{couponType,jdbcType=TINYINT},
coupon_code = #{couponCode,jdbcType=VARCHAR},
status = #{status,jdbcType=TINYINT},
order_code = #{orderCode,jdbcType=BIGINT},
use_time = #{useTime,jdbcType=INTEGER},
start_time = #{startTime,jdbcType=INTEGER},
end_time = #{endTime,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateCouponNotUse" parameterType="com.yohoufo.dal.promotion.model.CouponCancelUseDO">
update user_coupon
<set>
status = 0,use_time = 0, order_code =0
</set>
where uid= #{uid}
and order_code= #{orderCode}
and status = 1
and coupon_code in
<foreach collection="couponCodes" index="index" item="couponCode"
open="(" separator="," close=")">
#{couponCode}
</foreach>
</update>
<update id="updateCoupon2Invalidate">
update user_coupon
<set>
status = 2
</set>
where uid= #{uid}
and coupon_code= #{couponCode}
and status = 0
</update>
<update id="updateCoupon2Use" parameterType="com.yohoufo.dal.promotion.model.CouponUseDO">
update user_coupon
<set>
<if test="orderCode != null">
order_code= #{orderCode},
</if>
<if test="useTime != null">
use_time= #{useTime},
</if>
status=1
</set>
where uid= #{uid}
and coupon_code in
<foreach collection="couponCodes" index="index" item="couponCode"
open="(" separator="," close=")">
#{couponCode}
</foreach>
and status = 0
and order_code= 0
</update>
<!--extract public where condition -->
<sql id="relationQueryWhereForUserCouponList">
where
a.coupon_id = b.id
and a.uid = #{uid}
<if test="businessClient != null">
and b.business_client = #{businessClient}
</if>
<if test="couponType > 0">
<![CDATA[
AND a.coupon_type = #{couponType}
]]>
</if>
<if test="status == 0">
<![CDATA[
AND a.status = 0
AND a.end_time > #{nowTime}
]]>
</if>
<!-- 已使用 -->
<if test="status == 1">
<![CDATA[
AND a.status = 1
]]>
</if>
<!-- 已失效或已过期的优惠券,只查询一个月数据 -->
<if test="status == 2">
<![CDATA[
AND (
a.status = 2
or (a.status = 0 AND a.end_time < #{nowTime})
)
AND a.end_time >(#{nowTime}-2592000)
]]>
</if>
</sql>
<select id="selectUserCouponListCount" resultType="java.lang.Integer" parameterType="com.yohoufo.dal.promotion.model.UserCouponListQueryParams">
select
count(1)
from user_coupon a, coupon b
<include refid="relationQueryWhereForUserCouponList" />
</select>
<select id="selectUserCouponList" resultMap="BaseResultMap" parameterType="com.yohoufo.dal.promotion.model.UserCouponListQueryParams">
select
<include refid="A_Base_Column_List" />
from user_coupon a, coupon b
<include refid="relationQueryWhereForUserCouponList"/>
<if test="status == 0">
order by a.create_time desc
</if>
<if test="status == 1">
order by a.use_time desc
</if>
<if test="status == 2">
order by a.end_time desc
</if>
<if test="limitStart > 0 and size > 0">
limit #{limitStart}, #{size}
</if>
</select>
</mapper>