CouponUserMapper.xml 6.8 KB

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