CouponMapper.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.caimei365.commodity.mapper.CouponMapper">
  6. <select id="findCouponClub" resultType="com.caimei365.commodity.model.vo.CouponVo">
  7. SELECT
  8. cc.`id` AS "couponId",
  9. cc.`couponAmount`,
  10. cc.`touchPrice`,
  11. if(cc.vipFlag = 1,cc.startDate,a.createDate) as startDate,
  12. if(cc.vipFlag = 1 or cc.useTimeFlag = 1,cc.endDate,date_add(a.createDate,interval cc.usePeriod day)) as endDate,
  13. cc.`couponType`,
  14. cc.`userId`,
  15. cc.`shopId`,
  16. cc.`productType`,
  17. cc.moneyCouponPrice,
  18. cc.moneyCouponFlag,
  19. cc.moneyCouponType,
  20. cc.`categoryType`,
  21. cc.couponPayWay
  22. FROM
  23. cm_coupon_club a
  24. LEFT JOIN cm_coupon cc ON a.couponId = cc.id
  25. WHERE
  26. IF(cc.moneyCouponFlag=2,cc.delflag = 0,cc.id>0)
  27. AND a.delFlag = 0
  28. AND a.userId = #{userId}
  29. <if test="status == 1">
  30. AND a.status = 1
  31. AND if(cc.vipFlag = 1,
  32. NOW() BETWEEN cc.startDate AND cc.endDate,
  33. NOW() <![CDATA[ < ]]> IF(cc.usePeriod IS NULL,cc.endDate,DATE_ADD(a.createDate,INTERVAL cc.usePeriod DAY)))
  34. </if>
  35. <if test="status == 2">
  36. AND a.status = 2
  37. </if>
  38. <if test="status == 3">
  39. AND a.status = 1
  40. AND if(cc.vipFlag = 1,
  41. NOW() > cc.endDate,
  42. NOW() <![CDATA[ < ]]> IF(cc.usePeriod IS NULL,cc.endDate,DATE_ADD(a.createDate,INTERVAL cc.usePeriod DAY)))
  43. </if>
  44. AND cc.status != 2
  45. ORDER BY
  46. a.createDate DESC
  47. </select>
  48. <select id="findCouponById" resultType="com.caimei365.commodity.model.vo.ActivityCouponVo">
  49. SELECT id AS "couponId",
  50. NAME,
  51. pcBanner,
  52. appletsBanner,
  53. productType
  54. FROM cm_coupon
  55. WHERE productType = 2
  56. AND delFlag = 0
  57. AND couponType = 0
  58. AND NOW() BETWEEN startDate
  59. AND endDate
  60. AND status != 2
  61. AND id = #{couponId}
  62. </select>
  63. <select id="findCouponProduct" resultType="com.caimei365.commodity.model.vo.ProductItemVo">
  64. SELECT
  65. p.productID AS productId,
  66. p.actStatus,
  67. p.name,
  68. p.aliasName,
  69. p.mainImage AS image,
  70. (select unit from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1)as unit,
  71. p.productCode AS CODE,
  72. p.priceFlag,
  73. (select price from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1)as price,
  74. (select costPrice from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1)as costPrice,
  75. (select costCheckFlag from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1)as costCheckFlag,
  76. p.shopID AS shopId,
  77. p.searchKey AS keyword,
  78. (select minBuyNumber from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1) AS minBuyNumber,
  79. (select ladderPriceFlag from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1)as ladderPriceFlag,
  80. (select normalPrice from cm_sku where productId=p.productID and organizeId = 0 order by price asc limit 1)as normalPrice,
  81. p.step,
  82. p.shopID AS shopId,
  83. p.taxPoint AS taxRate,
  84. p.includedTax,
  85. p.invoiceType,
  86. p.productCategory AS productCategory,
  87. IFNULL((SELECT validFlag FROM cm_organize_product_info WHERE productId = p.productID AND organizeId = 0), 999) AS validFlag,
  88. p.featuredFlag,
  89. p.commodityType,
  90. p.bigTypeID AS bigTypeId,
  91. p.smallTypeID AS smallTypeId,
  92. p.tinyTypeID AS tinyTypeId,
  93. p.productType
  94. FROM
  95. cm_coupon_product a
  96. LEFT JOIN product p ON a.productId = p.productID
  97. left join cm_organize_product_info copi on copi.productId = p.productID and copi.organizeId = 0
  98. WHERE
  99. a.delFlag = 0
  100. AND a.couponId = #{couponId}
  101. <if test="source == 1">
  102. AND a.pcStatus = 1
  103. </if>
  104. <if test="source == 2">
  105. AND a.appletsStatus = 1
  106. </if>
  107. <choose>
  108. <when test="identity == 1">
  109. AND copi.validFlag IN (2,3,9)
  110. </when>
  111. <when test="identity == 5">
  112. AND copi.validFlag = 2
  113. </when>
  114. <when test="identity == 2">
  115. AND p.visibility IN (1,2,3) AND copi.validFlag = 2
  116. </when>
  117. <when test="identity == 4">
  118. AND p.visibility IN (2,3) AND copi.validFlag = 2
  119. </when>
  120. <otherwise>
  121. AND p.visibility = 3 AND copi.validFlag = 2
  122. </otherwise>
  123. </choose>
  124. ORDER BY
  125. -a.sort DESC
  126. </select>
  127. <select id="findCouponList" resultType="com.caimei365.commodity.model.vo.CouponVo">
  128. SELECT
  129. `id` AS "couponId",
  130. `couponAmount`,
  131. `touchPrice`,
  132. <if test="userId == null || userId == 0">
  133. startDate,
  134. endDate,
  135. </if>
  136. <if test="userId != null and userId > 0">
  137. if(#{registerTime} <![CDATA[ > ]]> startDate and #{registerTime} <![CDATA[ < ]]>
  138. endDate,#{registerTime},startDate) as startDate,
  139. if(useTimeFlag = 1 or receiveFlag = 1,endDate,
  140. date_add(if(#{registerTime} <![CDATA[ > ]]> startDate and #{registerTime} <![CDATA[ < ]]> endDate,
  141. #{registerTime},startDate),interval receivePeriod day)) as endDate,
  142. </if>
  143. moneyCouponPrice,
  144. moneyCouponFlag,
  145. moneyCouponType,
  146. `couponType`,
  147. `userId`,
  148. `shopId`,
  149. `productType`,
  150. `categoryType`,
  151. couponPayWay
  152. FROM
  153. cm_coupon
  154. WHERE
  155. delFlag = 0 AND vipFlag != 1
  156. AND status != 2
  157. AND couponsMode = 0
  158. <if test="userId == null or userId == 0">
  159. AND couponType != 2
  160. AND NOW() <![CDATA[ > ]]> startDate
  161. AND NOW() <![CDATA[ < ]]> if(useTimeFlag = 1 or receiveFlag = 1,endDate,date_add(startDate,interval
  162. receivePeriod day))
  163. </if>
  164. <if test="userId != null and userId > 0">
  165. AND id NOT IN(SELECT couponId FROM cm_coupon_club WHERE userId = #{userId})
  166. AND (couponType IN (0,1,3)
  167. OR (couponType = 2 AND userId = #{userId})
  168. OR (#{registerTime} <![CDATA[ >= ]]> startDate and #{registerTime} <![CDATA[ < ]]> endDate AND couponType =
  169. 4))
  170. and NOW() <![CDATA[ > ]]> startDate
  171. and NOW() <![CDATA[ < ]]> if(useTimeFlag = 1 or receiveFlag = 1,endDate,
  172. date_add(if(#{registerTime} <![CDATA[ > ]]> startDate and #{registerTime} <![CDATA[ < ]]>
  173. endDate,#{registerTime},startDate),interval receivePeriod day)
  174. )
  175. </if>
  176. ORDER BY
  177. createDate DESC
  178. </select>
  179. <select id="getCoupons" resultType="com.caimei365.commodity.model.vo.CouponVo">
  180. SELECT `id` AS "couponId",
  181. `couponAmount`,
  182. `touchPrice`,
  183. if(#{registerTime} <![CDATA[ > ]]> startDate and #{registerTime} <![CDATA[ < ]]> endDate, #{registerTime}, startDate) as startDate,
  184. if(useTimeFlag = 1 or receiveFlag = 1, endDate,
  185. date_add(if(#{registerTime} <![CDATA[ > ]]> startDate and #{registerTime} <![CDATA[ < ]]> endDate, #{registerTime}, startDate),
  186. interval receivePeriod day)) as endDate,
  187. if(usePeriod is null, endDate, date_add(NOW(), interval usePeriod day)) as usePeriod,
  188. -- `endDate`,
  189. `couponType`,
  190. `userId`,
  191. `shopId`,
  192. `productType`,
  193. `categoryType`
  194. FROM cm_coupon
  195. WHERE delFlag = 0
  196. AND status != 2
  197. AND id = #{couponId}
  198. </select>
  199. <select id="findCouponRedemptionCode" resultType="com.caimei365.commodity.model.po.CouponRedemptionCodePo">
  200. SELECT `id`,
  201. `couponId`,
  202. `clubCouponId`,
  203. `redemptionCode`,
  204. `status`,
  205. `redemptionTime`,
  206. `addTime`
  207. FROM `cm_coupon_redemption_code`
  208. WHERE redemptionCode = #{redemptionCode}
  209. </select>
  210. <select id="findCouponClubByUserId" resultType="integer">
  211. SELECT `id`
  212. FROM `cm_coupon_club`
  213. WHERE userId = #{userId}
  214. AND couponId = #{couponId}
  215. </select>
  216. <insert id="insertCouponClub" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
  217. INSERT INTO `cm_coupon_club` (`userId`,
  218. `couponId`,
  219. `orderId`,
  220. `source`,
  221. `status`,
  222. `createDate`,
  223. `useDate`,
  224. `delFlag`)
  225. VALUES (#{userId},
  226. #{couponId},
  227. #{orderId},
  228. #{source},
  229. #{status},
  230. #{createDate},
  231. #{useDate},
  232. #{delFlag})
  233. </insert>
  234. <update id="updateRedemptionCode">
  235. UPDATE
  236. cm_coupon_redemption_code
  237. SET STATUS = 2,
  238. redemptionTime = NOW(),
  239. clubCouponId = #{clubCouponId}
  240. WHERE id = #{id}
  241. </update>
  242. <select id="findUserRegisterTime" resultType="date">
  243. SELECT registerTime
  244. FROM user
  245. WHERE userID = #{userId}
  246. </select>
  247. <select id="findVip" resultType="java.lang.Integer">
  248. select userId
  249. from cm_svip_user
  250. where userId = #{userId}
  251. AND delFlag = 0
  252. AND endtime > now()
  253. </select>
  254. <select id="findVipCoupon" resultType="java.lang.Integer">
  255. select couponId
  256. from cm_svip_coupon
  257. </select>
  258. <select id="findMoneyCoupons" resultType="com.caimei365.commodity.model.vo.CouponVo">
  259. SELECT cc.`id` AS "couponId",
  260. cc.`couponAmount`,
  261. cc.`touchPrice`,
  262. if(#{registerTime} <![CDATA[ > ]]> cc.startDate and #{registerTime} <![CDATA[ < ]]> cc.endDate, #{registerTime},
  263. cc.startDate) as startDate,
  264. if(useTimeFlag = 1 or receiveFlag = 1, cc.endDate, date_add(
  265. if(#{registerTime} <![CDATA[ > ]]> cc.startDate and #{registerTime} <![CDATA[ < ]]> cc.endDate, #{registerTime},
  266. cc.startDate),
  267. interval receivePeriod day)) as endDate,
  268. cc.moneyCouponPrice,
  269. cc.moneyCouponFlag,
  270. cc.moneyCouponType,
  271. cc.`couponType`,
  272. cc.`userId`,
  273. cc.`shopId`,
  274. cc.`productType`,
  275. cc.`categoryType`,
  276. cc.couponPayWay,
  277. s.name as shopName
  278. FROM cm_coupon cc
  279. LEFT JOIN shop s ON cc.shopId = s.shopId
  280. WHERE cc.delFlag = 0
  281. AND cc.vipFlag != 1
  282. AND cc.id != 49
  283. AND cc.status != 2
  284. AND cc.couponsMode = 0
  285. AND cc.moneyCouponFlag = 1
  286. AND (cc.couponType IN (0,1,3)
  287. OR (cc.couponType = 2 AND cc.userId = #{userId})
  288. OR (#{registerTime} <![CDATA[ >= ]]> cc.startDate
  289. and #{registerTime} <![CDATA[ < ]]> cc.endDate
  290. AND cc.couponType = 4))
  291. and NOW() <![CDATA[ > ]]> cc.startDate
  292. and NOW() <![CDATA[ < ]]> if(receiveFlag = 1
  293. , cc.endDate
  294. , date_add(if(#{registerTime} <![CDATA[ > ]]> cc.startDate
  295. and #{registerTime} <![CDATA[ < ]]> cc.endDate
  296. , #{registerTime}
  297. , cc.startDate)
  298. , interval receivePeriod day)
  299. )
  300. ORDER BY
  301. cc.createDate DESC
  302. </select>
  303. <select id="findCouponDetail" resultType="com.caimei365.commodity.model.vo.CouponVo">
  304. SELECT cc.`id` AS "couponId",
  305. cc.`couponAmount`,
  306. cc.`touchPrice`,
  307. cc.startDate,
  308. cc.endDate,
  309. cc.`couponType`,
  310. cc.`userId`,
  311. cc.`shopId`,
  312. cc.`productType`,
  313. cc.`categoryType`,
  314. cc.moneyCouponPrice,
  315. cc.moneyCouponFlag,
  316. s.name as shopName,
  317. cc.couponPayWay
  318. FROM cm_coupon cc
  319. LEFT JOIN shop s ON cc.shopId = s.shopId
  320. where id = #{couponId}
  321. </select>
  322. <select id="findUserPermission" resultType="java.lang.Integer">
  323. select userIdentity
  324. from user
  325. where userID = #{userId}
  326. </select>
  327. </mapper>