CouponUserMapper.xml 6.6 KB

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