CouponUserMapper.xml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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(ck.id is null, 0, 1) as 'isAvailable',
  23. if(ca.id is not null and ca.expiry_status = 0,false,true) flag,
  24. (case
  25. when cu.valid_start_time > now() then 1
  26. when cu.valid_end_time &lt; now() then 2
  27. when cu.status = 'unused' then 0
  28. end) as orderby
  29. from `coupon_user` cu
  30. left join `coupon` cp on cp.id = cu.coupon_id
  31. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  32. left join `coupon_sku` ck on ck.coupon_id = cp.id
  33. <if test="goodsId != null">
  34. and ck.goods_id = #{goodsId}
  35. </if>
  36. <if test="goodsId == null">
  37. and ck.sku_id = #{skuId}
  38. </if>
  39. <choose>
  40. <when test="isRenew != null and isRenew">
  41. and cp.use_scope != 'orders'
  42. </when>
  43. <otherwise>
  44. and cp.use_scope != 'renew'
  45. </otherwise>
  46. </choose>
  47. where cu.user_id = #{userId}
  48. and cu.channel != 'exCode'
  49. and cu.valid_end_time > now()
  50. <if test="couponId == null">
  51. order by isAvailable desc,orderby
  52. </if>
  53. <if test="couponId != null">
  54. order by case when cp.id = #{couponId} then 2 else isAvailable end desc,orderby
  55. </if>)s where flag = 1 and couponUserStatus != 'used' and orderby != 2
  56. </select>
  57. <select id="countAvailableCouponList" resultType="com.cyksj.model.views.CouponUserView">
  58. select distinct id,reduce_condition,`type`
  59. from (select cu.id,ca.expiry_status as flag,
  60. cp.reduce_condition,
  61. cp.type
  62. from `coupon_user` cu
  63. left join `coupon` cp on cp.id = cu.coupon_id
  64. left join `coupon_sku` ck on ck.coupon_id = cp.id
  65. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  66. where
  67. <if test="goodsId != null">
  68. ck.goods_id = #{goodsId}
  69. </if>
  70. <if test="goodsId == null">
  71. ck.sku_id = #{skuId}
  72. </if>
  73. and cu.user_id = #{userId}
  74. and cu.status = 'unused'
  75. and cu.channel != 'exCode'
  76. and cu.valid_start_time &lt; now()
  77. and cu.valid_end_time > now()
  78. <choose>
  79. <when test="isRenew != null and isRenew">
  80. and cp.use_scope != 'orders'
  81. </when>
  82. <otherwise>
  83. and cp.use_scope != 'renew'
  84. </otherwise>
  85. </choose>
  86. )s where flag != 0 or flag is null
  87. </select>
  88. <select id="couponCenterList" resultType="com.cyksj.model.views.CouponActiveView">
  89. select *
  90. from (select ca.id,
  91. c.id as coupon_id,
  92. c.name,
  93. c.goods_type,
  94. c.type,
  95. c.reduce_condition,
  96. c.reduce_money,
  97. c.discount,
  98. c.scope,
  99. c.goods_sku_ids,
  100. c.is_valid_time,
  101. c.valid_start_time,
  102. c.valid_end_time,
  103. c.valid_days,
  104. c.valid_hours,
  105. ca.num,
  106. ca.`status`,
  107. ca.expiry_status,
  108. ca.remain_num,
  109. (case
  110. when ca.remain_num &lt;= 0 then 'over'
  111. when cu.id is null then 'collecting'
  112. when cu.id is not null and cu.status != 'unused' then 'used'
  113. when cu.id is not null and cu.status = 'unused' then 'received'
  114. end) as couponStatus,
  115. (case
  116. when remain_num &lt;= 0 then 2
  117. when cu.id is null then 0
  118. when cu.id is not null and cu.status = 'unused' then 1
  119. end) as `order`,
  120. if(c.is_valid_time is true and c.valid_end_time &lt; now(), false, true) as flag
  121. from coupon_active ca
  122. left join coupon c
  123. on c.id = ca.coupon_id
  124. left join coupon_user cu on cu.coupon_id = c.id and cu.user_id = #{userId}
  125. where ca.deleted is true
  126. and c.deleted is true
  127. and cu.id is null
  128. and ca.expiry_status != 0
  129. and ca.`status` != 0
  130. ) s
  131. where couponStatus in ('over', 'collecting')
  132. and flag = 1
  133. order by `order`
  134. </select>
  135. <select id="couponPersonalList" resultType="com.cyksj.model.views.CouponUserView">
  136. select s.* from (select cu.id,
  137. cu.channel,
  138. cu.user_id,
  139. c.id as couponId,
  140. ca.id as couponActiveId,
  141. c.name,
  142. c.goods_type,
  143. c.type,
  144. c.reduce_condition,
  145. c.reduce_money,
  146. c.discount,
  147. c.use_scope,
  148. c.goods_sku_ids,
  149. c.is_valid_time,
  150. cu.valid_start_time,
  151. c.valid_days,
  152. c.valid_hours,
  153. cu.valid_end_time,
  154. (case
  155. when cu.status = 'used' then 'used'
  156. when cu.valid_end_time &lt; now() then 'expired'
  157. when ca.id is not null and ca.expiry_status = 0 then 'invalid'
  158. when cu.valid_start_time > now() then 'notEffective'
  159. else cu.status end) as couponUserStatus,
  160. (case
  161. when cu.status = 'used' then 4
  162. when cu.valid_end_time &lt; now() then 2
  163. when ca.id is not null and ca.expiry_status = 0 then 3
  164. when cu.valid_start_time > now() then 1
  165. when cu.status = 'unused' then 0
  166. end) as orderby
  167. from `coupon_user` cu
  168. left join `coupon` c on c.id = cu.coupon_id
  169. left join `coupon_active` ca on ca.id = cu.coupon_active_id
  170. where cu.user_id = #{userId}
  171. and cu.channel != 'exCode'
  172. order by `orderby`)s where s.couponUserStatus != 'expired' and s.couponUserStatus != 'invalid'
  173. </select>
  174. <select id="getNewUserWelfareCouponsByUserId" resultType="com.cyksj.model.views.CouponUserView">
  175. select cu.*,
  176. c.name,
  177. c.goods_type,
  178. c.type,
  179. c.reduce_condition,
  180. c.reduce_money,
  181. c.discount,
  182. c.use_scope,
  183. c.goods_sku_ids,
  184. cu.valid_start_time,
  185. cu.valid_end_time,
  186. unix_timestamp(cu.valid_end_time) * 1000 as validEndTimestamp
  187. from (select coupon_id,
  188. valid_start_time,
  189. valid_end_time,
  190. status as couponUserStatus
  191. from `coupon_user`
  192. where channel = #{channel}
  193. and user_id = #{userId}
  194. and valid_end_time > #{now}
  195. and status = 'unused' limit 1) cu
  196. inner join coupon c on c.id = cu.coupon_id
  197. </select>
  198. </mapper>