CouponUserMapper.xml 6.4 KB

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