CouponUserMapper.xml 6.1 KB

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