CmPromotionMapper.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.caimei.modules.product.dao.CmPromotionDao">
  4. <sql id="cmPromotionColumns">
  5. cp.id AS "id",
  6. cp.name AS "name",
  7. cp.description AS "description",
  8. cp.type AS "type",
  9. cp.mode AS "mode",
  10. cp.touchPrice AS "touchPrice",
  11. cp.reducedPrice AS "reducedPrice",
  12. cp.beginTime AS "beginTime",
  13. cp.endTime AS "endTime",
  14. cp.addTime AS "addTime",
  15. cp.updateTime AS "updateTime",
  16. cp.status AS "status",
  17. cp.delFlag AS "delFlag1",
  18. cp.seen AS "seen",
  19. cp.discount AS "discount"
  20. </sql>
  21. <select id="get" resultType="CmPromotion">
  22. SELECT
  23. <include refid="cmPromotionColumns"/>
  24. FROM cm_promotions cp
  25. WHERE cp.id = #{id}
  26. and cp.delFlag != '2'
  27. </select>
  28. <select id="findList" resultType="CmPromotion">
  29. SELECT
  30. <include refid="cmPromotionColumns"/>
  31. FROM cm_promotions cp
  32. LEFT JOIN cm_promotions_product cpp ON cp.id = cpp.promotionsId
  33. LEFT JOIN product p ON cpp.productId = p.productID
  34. LEFT JOIN shop s ON p.shopID = s.shopID OR cpp.supplierId=s.shopID
  35. <where>
  36. cp.delFlag != 2
  37. <if test="type != null and type != ''">
  38. AND cp.type = #{type,jdbcType=INTEGER}
  39. </if>
  40. <if test="name != null and name != ''">
  41. AND cp.name LIKE CONCAT('%',#{name,jdbcType=VARCHAR},'%')
  42. </if>
  43. <if test="description != null and description != ''">
  44. AND cp.description LIKE CONCAT('%',#{description,jdbcType=VARCHAR},'%')
  45. </if>
  46. <if test="productName != null and productName != ''">
  47. AND p.name LIKE CONCAT('%',#{productName,jdbcType=VARCHAR},'%')
  48. </if>
  49. <if test="shopName != null and shopName != ''">
  50. AND s.name LIKE CONCAT('%',#{shopName,jdbcType=VARCHAR},'%')
  51. </if>
  52. <if test="mode != null and mode != ''">
  53. AND cp.mode = #{mode,jdbcType=VARCHAR}
  54. </if>
  55. <if test="status != null and status != ''">
  56. AND cp.status = #{status,jdbcType=VARCHAR}
  57. </if>
  58. <if test="delFlag1 != null and delFlag1 != ''">
  59. <choose>
  60. <when test="delFlag1 == 1">
  61. AND NOW() <![CDATA[ < ]]> cp.beginTime
  62. AND cp.status != 1
  63. AND cp.delFlag != '1'
  64. </when>
  65. <when test="delFlag1 == 2">
  66. AND (NOW() between cp.beginTime AND cp.endTime
  67. OR cp.status = 1)
  68. AND cp.delFlag != '1'
  69. </when>
  70. <when test="delFlag1 == 3">
  71. AND NOW() <![CDATA[ > ]]> cp.endTime
  72. AND cp.status != 1
  73. AND cp.delFlag != '1'
  74. </when>
  75. <when test="delFlag1 == 0">
  76. AND cp.delFlag = '1'
  77. </when>
  78. <otherwise>
  79. AND cp.delFlag = null
  80. </otherwise>
  81. </choose>
  82. </if>
  83. </where>
  84. group by cp.id
  85. <choose>
  86. <when test="page !=null and page.orderBy != null and page.orderBy != ''">
  87. ORDER BY ${page.orderBy,jdbcType=VARCHAR}
  88. </when>
  89. <otherwise>
  90. ORDER BY cp.addTime DESC
  91. </otherwise>
  92. </choose>
  93. </select>
  94. <select id="findPromotionProduct" resultType="com.caimei.modules.product.entity.Product">
  95. SELECT p.*,
  96. (select price from cm_sku where productId = p.productId order by price asc limit 1) as price,
  97. cp.reducedPrice AS "reducedPrice",
  98. s.name AS "shopName"
  99. FROM cm_promotions cp
  100. LEFT JOIN cm_promotions_product cpp ON cp.id = cpp.promotionsId
  101. LEFT JOIN product p ON cpp.productId = p.productID
  102. LEFT JOIN shop s ON s.shopID = p.shopID
  103. WHERE cp.id = #{id}
  104. and p.productCategory = 1
  105. </select>
  106. <select id="findGiftProduct" resultType="com.caimei.modules.product.entity.Product">
  107. SELECT cpg.skuId AS "skuId",
  108. cs.unit,
  109. p.name,
  110. p.mainImage,
  111. p.productID,
  112. s.name AS "shopName",
  113. cpg.number AS "giftNumber"
  114. FROM cm_promotions cp
  115. LEFT JOIN cm_promotions_gift cpg ON cp.id = cpg.promotionsId
  116. left join cm_sku cs on cpg.skuId = cs.skuId
  117. LEFT JOIN product p ON cpg.productId = p.productID
  118. LEFT JOIN shop s ON s.shopID = p.shopID
  119. WHERE cp.id = #{id}
  120. and p.productCategory = 1
  121. </select>
  122. <select id="findGiftPurchaseProduct" resultType="com.caimei.modules.bulkpurchase.entity.PurchaseProduct">
  123. SELECT p.name name,
  124. s.name shopName,
  125. s.`shopID` shopId,
  126. p.name purchaseProductName,
  127. cs.`price` price,
  128. p.`productID` productId,
  129. p.productCode productNo,
  130. p.mainImage image,
  131. cs.costCheckFlag costCheckFlag,
  132. cs.costPrice costPrice,
  133. cs.costProportional costProportional,
  134. cs.normalPrice AS normalPrice,
  135. cpg.number AS num,
  136. cp.discount as discounts
  137. FROM cm_promotions cp
  138. LEFT JOIN cm_promotions_gift cpg ON cp.id = cpg.promotionsId
  139. left join cm_sku cs on cpg.skuId = cs.skuId
  140. LEFT JOIN product p ON cpg.productId = p.productID
  141. LEFT JOIN shop s ON s.shopID = p.shopID
  142. WHERE cp.id = #{id}
  143. and p.productCategory = 1
  144. </select>
  145. <select id="findPromotionShops" resultType="com.caimei.modules.cibe.entity.Shop">
  146. SELECT s.*
  147. FROM cm_promotions_product cpp
  148. LEFT JOIN shop s ON cpp.supplierId = s.shopID
  149. WHERE cpp.promotionsId = #{id}
  150. </select>
  151. <select id="findAllProduct" resultType="com.caimei.modules.product.entity.Product">
  152. SELECT
  153. p.productId as productID,
  154. p.mainImage as mainImage,
  155. p.name as name,
  156. s.name AS shopName
  157. FROM
  158. product p
  159. LEFT JOIN shop s ON s.shopID = p.shopID
  160. <where>
  161. p.productCategory = 1
  162. and p.validFlag = 2
  163. and p.priceFlag != 1
  164. <if test="productID != null">
  165. AND p.productID = #{productID}
  166. </if>
  167. <if test="name != null and name != ''">
  168. AND p.name LIKE concat('%',#{name},'%')
  169. </if>
  170. <if test="shopName != null and shopName != ''">
  171. AND s.name LIKE concat('%',#{shopName},'%')
  172. </if>
  173. <if test="ids != null and ids.size > 0">
  174. AND p.productID NOT IN
  175. <foreach collection="ids" open="(" close=")" item="id" separator=",">
  176. #{id}
  177. </foreach>
  178. </if>
  179. <if test="promotionType == 1 or promotionType == 2">
  180. AND p.productID NOT IN
  181. (SELECT cpp.productId
  182. FROM cm_promotions_product cpp
  183. LEFT JOIN cm_promotions cp on cpp.promotionsId = cp.id
  184. <where>
  185. cp.type = #{promotionType}
  186. AND cp.delFlag != '2'
  187. AND cpp.productId != 0
  188. <if test="delIdList != null and delIdList.size > 0">
  189. AND cpp.productId NOT IN
  190. <foreach collection="delIdList" open="(" close=")" item="delId" separator=",">
  191. #{delId}
  192. </foreach>
  193. </if>
  194. </where>
  195. )
  196. AND p.productID NOT IN (select productId from cm_svip_product)
  197. </if>
  198. </where>
  199. </select>
  200. <select id="findAllShop" resultType="com.caimei.modules.cibe.entity.Shop">
  201. SELECT
  202. s.*
  203. FROM
  204. shop s
  205. <where>
  206. s.status = 90
  207. AND s.shopID NOT IN
  208. (SELECT supplierId
  209. FROM cm_promotions_product cpp
  210. LEFT JOIN cm_promotions cp on cpp.promotionsId = cp.id
  211. <where>
  212. supplierId != 0
  213. AND cp.delFlag != '2'
  214. <if test="delShopIdList!=null and delShopIdList.size > 0">
  215. AND cpp.supplierId NOT IN
  216. <foreach collection="delShopIdList" open="(" close=")" item="delShopId" separator=",">
  217. #{delShopId}
  218. </foreach>
  219. </if>
  220. </where>
  221. )
  222. <if test="shopID != null">
  223. AND s.shopID = #{shopID}
  224. </if>
  225. <if test="name != null and name != ''">
  226. AND s.name LIKE concat('%',#{name},'%')
  227. </if>
  228. <if test="sname != null and sname != ''">
  229. AND s.sname LIKE concat('%',#{sname},'%')
  230. </if>
  231. <if test="ids != null and ids.size > 0">
  232. AND s.shopID NOT IN
  233. <foreach collection="ids" open="(" close=")" item="id" separator=",">
  234. #{id}
  235. </foreach>
  236. </if>
  237. </where>
  238. </select>
  239. <delete id="deleteAllPromotionProductsAndShops">
  240. DELETE
  241. FROM cm_promotions_product
  242. WHERE promotionsId = #{id}
  243. </delete>
  244. <delete id="deleteAllPromotionGifts">
  245. DELETE
  246. FROM cm_promotions_gift
  247. WHERE promotionsId = #{id}
  248. </delete>
  249. <insert id="insertPromotionProduct">
  250. INSERT INTO cm_promotions_product(promotionsId,
  251. productId,
  252. addTime)
  253. VALUES (#{promotionId},
  254. #{productId},
  255. now())
  256. </insert>
  257. <insert id="insertGiftProduct">
  258. INSERT INTO cm_promotions_gift(promotionsId,
  259. skuId,
  260. productId,
  261. number,
  262. addTime)
  263. VALUES (#{promotionId},
  264. #{giftId},
  265. (select productId from cm_sku where skuId = #{giftId}),
  266. #{number},
  267. now())
  268. </insert>
  269. <insert id="insertPromotionShop">
  270. INSERT INTO cm_promotions_product(promotionsId,
  271. supplierId,
  272. productId,
  273. addTime)
  274. VALUES (#{promotionId},
  275. #{shopId},
  276. 0,
  277. now())
  278. </insert>
  279. <insert id="insert" parameterType="CmPromotion" keyProperty="id" useGeneratedKeys="true">
  280. INSERT INTO cm_promotions(name,
  281. description,
  282. type,
  283. mode,
  284. touchPrice,
  285. reducedPrice,
  286. beginTime,
  287. endTime,
  288. addTime,
  289. updateTime,
  290. status,
  291. delFlag,
  292. discount,
  293. seen)
  294. VALUES (#{name},
  295. #{description},
  296. #{type},
  297. #{mode},
  298. #{touchPrice},
  299. #{reducedPrice},
  300. #{beginTime},
  301. #{endTime},
  302. #{addTime},
  303. #{updateTime},
  304. #{status},
  305. #{delFlag1},
  306. #{discount},
  307. #{seen})
  308. </insert>
  309. <update id="update">
  310. UPDATE cm_promotions
  311. SET name = #{name},
  312. description = #{description},
  313. type = #{type},
  314. mode = #{mode},
  315. touchPrice = #{touchPrice},
  316. reducedPrice = #{reducedPrice},
  317. beginTime = #{beginTime},
  318. endTime = #{endTime},
  319. updateTime = #{updateTime},
  320. status = #{status},
  321. delFlag = #{delFlag1},
  322. discount = #{discount},
  323. seen = #{seen}
  324. WHERE id = #{id}
  325. </update>
  326. <select id="findShop" resultType="com.caimei.modules.cibe.entity.Shop">
  327. SELECT *
  328. from shop
  329. where shopID = #{id}
  330. </select>
  331. <select id="findProductPromotion" resultType="com.caimei.modules.product.entity.CmPromotion">
  332. select pr.id,
  333. pr.name,
  334. pr.type,
  335. pr.mode,
  336. pr.touchPrice,
  337. pr.reducedPrice,
  338. pr.beginTime,
  339. pr.endTime,
  340. pr.status,
  341. prp.productId,
  342. prp.supplierId,
  343. pr.discount,
  344. cpg.skuId
  345. from cm_promotions pr
  346. left join cm_promotions_product prp on pr.id = prp.promotionsId
  347. LEFT JOIN cm_promotions_gift cpg ON pr.id = cpg.promotionsId
  348. where (prp.productId = #{productId}
  349. or prp.supplierId = #{shopId})
  350. and (pr.status = 1 or (pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
  351. and pr.delFlag not in ('1', '2')
  352. order by pr.type desc
  353. limit 1
  354. </select>
  355. <select id="findProductPromotionsGift" resultType="com.caimei.modules.product.entity.CmPromotion">
  356. SELECT DISTINCT pr.id,
  357. pr.name,
  358. pr.type,
  359. pr.mode,
  360. pr.touchPrice,
  361. pr.reducedPrice,
  362. pr.beginTime,
  363. pr.endTime,
  364. pr.status
  365. FROM cm_promotions_gift cpg
  366. LEFT JOIN cm_promotions pr ON pr.id = cpg.promotionsId
  367. WHERE cpg.productId = #{productId}
  368. AND (pr.status = 1 OR (pr.status = 2 AND (NOW() BETWEEN pr.beginTime AND pr.endTime)))
  369. AND pr.mode = 3
  370. AND pr.delFlag not in ('1', '2')
  371. ORDER BY pr.type DESC
  372. LIMIT 1
  373. </select>
  374. <select id="findOrderPromotions" resultType="com.caimei.modules.product.entity.CmPromotion">
  375. SELECT *
  376. FROM cm_promotions_order
  377. WHERE id = #{orderPromotionsId}
  378. </select>
  379. <select id="findPromotionsByProductId" resultType="com.caimei.modules.product.entity.CmPromotion">
  380. select pr.id,
  381. pr.name,
  382. pr.description,
  383. pr.type,
  384. pr.mode,
  385. pr.touchPrice,
  386. pr.reducedPrice,
  387. pr.beginTime,
  388. pr.endTime,
  389. pr.status,
  390. prp.productId,
  391. prp.supplierId
  392. from cm_promotions pr
  393. left join cm_promotions_product prp on pr.id = prp.promotionsId
  394. where (prp.productId = #{productId} or
  395. prp.supplierId = (select p.shopID from product p where p.productID = #{productId})
  396. )
  397. and (pr.status = 1 or (pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
  398. and pr.delFlag not in (1, 2)
  399. order by pr.type desc
  400. limit 1
  401. </select>
  402. <select id="getPresentPriceById" resultType="java.lang.Double">
  403. select price
  404. from cm_sku
  405. where skuId = #{skuId}
  406. </select>
  407. <select id="findProductIdByPromotion" resultType="java.lang.Integer">
  408. select productId
  409. from cm_promotions_product
  410. where promotionsId = #{promotionsId}
  411. </select>
  412. <select id="findProductIdsByPromotion" resultType="java.lang.Integer">
  413. select productID
  414. from product
  415. where shopID in (select supplierId from cm_promotions_product where promotionsId = #{promotionsId})
  416. </select>
  417. </mapper>