CouponUserMapper.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cyksj.mapper.manage.coupon.CouponUserMapper">
  4. <select id="getAvailableCouponList" resultType="com.cyksj.model.views.CouponUserView">
  5. select * from (select distinct cu.id,
  6. cp.id as coupon_id,
  7. cp.name,
  8. cp.goods_type,
  9. cp.type,
  10. cp.goods_sku_ids,
  11. cp.discount,
  12. cp.reduce_condition,
  13. cp.reduce_money,
  14. cp.is_valid_time,
  15. cu.valid_start_time,
  16. cu.valid_end_time,
  17. cp.valid_days,
  18. cp.valid_hours,
  19. cp.use_scope,
  20. cu.created_time,
  21. cu.status as couponUserStatus,
  22. if(type = 'discount',${skuMoney} * (1-cp.discount),reduce_money) as couponMoney,
  23. if(ck.id is null, 0, 1) as 'isAvailable',
  24. if(ca.id is not null and ca.expiry_status = 0,false,true) flag,
  25. (case
  26. when cu.valid_start_time > now() then 1
  27. when cu.valid_end_time &lt; now() then 2
  28. when cu.status = 'unused' then 0
  29. end) as orderby
  30. from `coupon_user` cu
  31. left join `coupon` cp on cp.id = cu.coupon_id
  32. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  33. left join `coupon_sku` ck on ck.coupon_id = cp.id
  34. <if test="goodsId != null">
  35. and ck.goods_id = #{goodsId}
  36. </if>
  37. <if test="goodsId == null">
  38. and ck.sku_id = #{skuId}
  39. </if>
  40. <choose>
  41. <when test="isRenew != null and isRenew">
  42. and cp.use_scope != 'orders'
  43. </when>
  44. <otherwise>
  45. and cp.use_scope != 'renew'
  46. </otherwise>
  47. </choose>
  48. where cu.user_id = #{userId}
  49. and cu.channel != 'exCode'
  50. and cu.valid_end_time > now()
  51. <if test="couponId == null">
  52. order by isAvailable desc,orderby,couponMoney desc
  53. </if>
  54. <if test="couponId != null">
  55. order by case when cp.id = #{couponId} then 2 else isAvailable end desc,orderby,couponMoney desc
  56. </if>)s where flag = 1 and couponUserStatus != 'used' and orderby != 2
  57. </select>
  58. <select id="countAvailableCouponList" resultType="com.cyksj.model.views.CouponUserView">
  59. select distinct id,reduce_condition,`type`
  60. from (select cu.id,ca.expiry_status as flag,
  61. cp.reduce_condition,
  62. cp.type
  63. from `coupon_user` cu
  64. left join `coupon` cp on cp.id = cu.coupon_id
  65. left join `coupon_sku` ck on ck.coupon_id = cp.id
  66. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  67. where
  68. <if test="goodsId != null">
  69. ck.goods_id = #{goodsId}
  70. </if>
  71. <if test="goodsId == null">
  72. ck.sku_id = #{skuId}
  73. </if>
  74. and cu.user_id = #{userId}
  75. and cu.status = 'unused'
  76. and cu.channel != 'exCode'
  77. and cu.valid_start_time &lt; now()
  78. and cu.valid_end_time > now()
  79. <choose>
  80. <when test="isRenew != null and isRenew">
  81. and cp.use_scope != 'orders'
  82. </when>
  83. <otherwise>
  84. and cp.use_scope != 'renew'
  85. </otherwise>
  86. </choose>
  87. )s where flag != 0 or flag is null
  88. </select>
  89. <select id="couponCenterList" resultType="com.cyksj.model.views.CouponActiveView">
  90. select *
  91. from (select ca.id,
  92. c.id as coupon_id,
  93. c.name,
  94. c.goods_type,
  95. c.type,
  96. c.reduce_condition,
  97. c.reduce_money,
  98. c.discount,
  99. c.scope,
  100. c.goods_sku_ids,
  101. c.is_valid_time,
  102. c.valid_start_time,
  103. c.valid_end_time,
  104. c.valid_days,
  105. c.valid_hours,
  106. ca.num,
  107. ca.`status`,
  108. ca.expiry_status,
  109. ca.remain_num,
  110. (case
  111. when ca.remain_num &lt;= 0 then 'over'
  112. when cu.id is null then 'collecting'
  113. when cu.id is not null and cu.status != 'unused' then 'used'
  114. when cu.id is not null and cu.status = 'unused' then 'received'
  115. end) as couponStatus,
  116. (case
  117. when remain_num &lt;= 0 then 2
  118. when cu.id is null then 0
  119. when cu.id is not null and cu.status = 'unused' then 1
  120. end) as `order`,
  121. if(c.is_valid_time is true and c.valid_end_time &lt; now(), false, true) as flag
  122. from coupon_active ca
  123. left join coupon c
  124. on c.id = ca.coupon_id
  125. left join coupon_user cu on cu.coupon_id = c.id and cu.user_id = #{userId}
  126. where ca.deleted is true
  127. and c.deleted is true
  128. and cu.id is null
  129. and ca.expiry_status != 0
  130. and ca.`status` != 0
  131. ) s
  132. where couponStatus in ('over', 'collecting')
  133. and flag = 1
  134. order by `order`
  135. </select>
  136. <select id="couponPersonalList" resultType="com.cyksj.model.views.CouponUserView">
  137. select s.* from (select cu.id,
  138. cu.channel,
  139. cu.user_id,
  140. c.id as couponId,
  141. ca.id as couponActiveId,
  142. c.name,
  143. c.goods_type,
  144. c.type,
  145. c.reduce_condition,
  146. c.reduce_money,
  147. c.discount,
  148. c.use_scope,
  149. c.goods_sku_ids,
  150. c.is_valid_time,
  151. cu.valid_start_time,
  152. c.valid_days,
  153. c.valid_hours,
  154. cu.valid_end_time,
  155. (case
  156. when cu.status = 'used' then 'used'
  157. when cu.valid_end_time &lt; now() then 'expired'
  158. when ca.id is not null and ca.expiry_status = 0 then 'invalid'
  159. when cu.valid_start_time > now() then 'notEffective'
  160. else cu.status end) as couponUserStatus,
  161. (case
  162. when cu.status = 'used' then 4
  163. when cu.valid_end_time &lt; now() then 2
  164. when ca.id is not null and ca.expiry_status = 0 then 3
  165. when cu.valid_start_time > now() then 1
  166. when cu.status = 'unused' then 0
  167. end) as orderby
  168. from `coupon_user` cu
  169. left join `coupon` c on c.id = cu.coupon_id
  170. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  171. where cu.user_id = #{userId}
  172. <if test="couponId !=null">
  173. and cu.coupon_id = #{couponId}
  174. </if>
  175. and cu.channel != 'exCode'
  176. order by `orderby`)s where s.couponUserStatus != 'expired' and s.couponUserStatus != 'invalid'
  177. </select>
  178. <select id="getNewUserWelfareCouponsByUserId" resultType="com.cyksj.model.views.CouponUserView">
  179. select cu.*,
  180. c.name,
  181. c.goods_type,
  182. c.type,
  183. c.reduce_condition,
  184. c.reduce_money,
  185. c.discount,
  186. c.use_scope,
  187. c.goods_sku_ids,
  188. cu.valid_start_time,
  189. cu.valid_end_time,
  190. unix_timestamp(cu.valid_end_time) * 1000 as validEndTimestamp
  191. from (select coupon_id,
  192. valid_start_time,
  193. valid_end_time,
  194. status as couponUserStatus
  195. from `coupon_user`
  196. where coupon_id = #{couponId}
  197. and user_id = #{userId}
  198. and status = 'unused' limit 1) cu
  199. inner join coupon c on c.id = cu.coupon_id
  200. </select>
  201. <select id="getCouponByCouponUserId" resultType="com.cyksj.model.entity.CouponUser">
  202. select *
  203. from coupon_user
  204. where id = #{couponUserId} limit 1
  205. </select>
  206. <select id="getCouponDetailById" resultType="com.cyksj.model.views.CouponUserView">
  207. select cu.*,
  208. c.name,
  209. c.goods_type,
  210. c.type,
  211. c.reduce_condition,
  212. c.reduce_money,
  213. c.discount,
  214. c.use_scope,
  215. c.goods_sku_ids,
  216. cu.valid_start_time,
  217. cu.valid_end_time,
  218. unix_timestamp(cu.valid_end_time) * 1000 as validEndTimestamp
  219. from (select coupon_id,
  220. valid_start_time,
  221. valid_end_time,
  222. status as couponUserStatus
  223. from `coupon_user`
  224. where user_id = #{userId}
  225. and coupon_id = #{couponId} limit 1) cu
  226. inner join coupon c on c.id = cu.coupon_id
  227. </select>
  228. <select id="selectCouponUserNumData" resultType="java.util.Map">
  229. select count(if(status = 'used', 1, NULL)) usedNum, count(*) receiptNum
  230. from coupon_user
  231. where coupon_id = #{couponId}
  232. </select>
  233. <select id="couponPersonalByCouponIds" resultType="com.cyksj.model.views.CouponUserView">
  234. select cu.id,
  235. cu.channel,
  236. cu.user_id,
  237. c.id as couponId,
  238. ca.id as couponActiveId,
  239. c.name,
  240. c.goods_type,
  241. c.type,
  242. c.reduce_condition,
  243. c.reduce_money,
  244. c.discount,
  245. c.use_scope,
  246. c.goods_sku_ids,
  247. c.is_valid_time,
  248. cu.valid_start_time,
  249. c.valid_days,
  250. c.valid_hours,
  251. cu.valid_end_time,
  252. (case
  253. when cu.status = 'used' then 'used'
  254. when cu.valid_end_time &lt; now() then 'expired'
  255. when ca.id is not null and ca.expiry_status = 0 then 'invalid'
  256. when cu.valid_start_time > now() then 'notEffective'
  257. else cu.status end) as couponUserStatus,
  258. (case
  259. when cu.status = 'used' then 4
  260. when cu.valid_end_time &lt; now() then 2
  261. when ca.id is not null and ca.expiry_status = 0 then 3
  262. when cu.valid_start_time > now() then 1
  263. when cu.status = 'unused' then 0
  264. end) as orderby
  265. from `coupon_user` cu
  266. left join `coupon` c on c.id = cu.coupon_id
  267. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  268. where cu.user_id in
  269. <foreach collection="userIds" item="userId" open="(" separator="," close=")">
  270. #{userId}
  271. </foreach>
  272. and cu.coupon_id in
  273. <foreach collection="couponIds" item="item" open="(" separator="," close=")">
  274. #{item}
  275. </foreach>
  276. and cu.channel != 'exCode'
  277. order by `orderby`
  278. </select>
  279. <select id="selectUserCouponByGidAndPrice" resultType="com.cyksj.model.entity.Coupon">
  280. select c.id,
  281. c.discount,
  282. c.reduce_money,
  283. cu.valid_start_time,
  284. cu.valid_end_time,
  285. if(type = 'discount', sku.price * (1 - c.discount), c.reduce_money) as couponMoney
  286. from (select * from coupon_user where user_id = #{userId} and status = 'unused' and valid_start_time &lt; now() and valid_end_time > now()) cu
  287. inner join coupon c on c.id = cu.coupon_id and c.use_scope != 'renew'
  288. inner join coupon_sku cs on cs.coupon_id = c.id and cs.goods_id = #{goodsId}
  289. inner join goods_don_sku sku on sku.id = cs.sku_id and sku.status is true and sku.price = #{price}
  290. order by couponMoney desc limit 1
  291. </select>
  292. <select id="selectUserCouponBySkuId" resultType="com.cyksj.model.entity.Coupon">
  293. select c.id,
  294. c.discount,
  295. c.reduce_money,
  296. cu.valid_start_time,
  297. cu.valid_end_time,
  298. if(type = 'discount', sku.price * (1 - c.discount), c.reduce_money) as couponMoney
  299. from (select * from coupon_user where user_id = #{userId} and status = 'unused' and valid_start_time &lt; now() and valid_end_time > now()) cu
  300. inner join coupon c on c.id = cu.coupon_id
  301. inner join coupon_sku cs on cs.coupon_id = c.id and cs.sku_id = #{skuId}
  302. inner join goods_don_sku sku on sku.id = cs.sku_id and sku.status is true
  303. and c.use_scope != 'renew'
  304. order by couponMoney desc limit 1
  305. </select>
  306. </mapper>