CouponUserMapper.xml 5.9 KB

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