CmPromotionMapper.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. cs.organizeId,
  110. p.name,
  111. p.mainImage,
  112. p.productID,
  113. s.name AS "shopName",
  114. cpg.number AS "giftNumber"
  115. FROM cm_promotions cp
  116. LEFT JOIN cm_promotions_gift cpg ON cp.id = cpg.promotionsId
  117. left join cm_sku cs on cpg.skuId = cs.skuId
  118. LEFT JOIN product p ON cpg.productId = p.productID
  119. LEFT JOIN shop s ON s.shopID = p.shopID
  120. WHERE cp.id = #{id} and p.groundMall like CONCAT('%', cs.organizeId, '%')
  121. and p.productCategory = 1
  122. </select>
  123. <select id="findGiftPurchaseProduct" resultType="com.caimei.modules.bulkpurchase.entity.PurchaseProduct">
  124. SELECT p.name name,
  125. s.name shopName,
  126. s.`shopID` shopId,
  127. p.name purchaseProductName,
  128. cs.`price` price,
  129. p.`productID` productId,
  130. p.productCode productNo,
  131. p.mainImage image,
  132. cs.costCheckFlag costCheckFlag,
  133. cs.costPrice costPrice,
  134. cs.organizeId organizeId,
  135. cs.shopPercent shopPercent,
  136. cs.normalPrice AS normalPrice,
  137. cpg.number AS num,
  138. cp.discount as discounts
  139. FROM cm_promotions cp
  140. LEFT JOIN cm_promotions_gift cpg ON cp.id = cpg.promotionsId
  141. left join cm_sku cs on cpg.skuId = cs.skuId
  142. LEFT JOIN product p ON cpg.productId = p.productID
  143. LEFT JOIN shop s ON s.shopID = p.shopID
  144. WHERE cp.id = #{id} and p.groundMall like CONCAT('%', cs.organizeId, '%')
  145. and p.productCategory = 1
  146. </select>
  147. <select id="findPromotionShops" resultType="com.caimei.modules.product.entity.Shop">
  148. SELECT s.*
  149. FROM cm_promotions_product cpp
  150. LEFT JOIN shop s ON cpp.supplierId = s.shopID
  151. WHERE cpp.promotionsId = #{id}
  152. </select>
  153. <select id="findAllProduct" resultType="com.caimei.modules.product.entity.Product">
  154. SELECT
  155. p.productId as productID,
  156. p.mainImage as mainImage,
  157. p.name as name,
  158. s.name AS shopName
  159. FROM
  160. product p
  161. left join cm_organize_product_info copi on copi.productId = p.productID
  162. LEFT JOIN shop s ON s.shopID = p.shopID
  163. <where>
  164. p.productCategory = 1
  165. and copi.validFlag = 2
  166. and p.priceFlag != 1 and p.groundMall like CONCAT('%', copi.organizeId, '%')
  167. <if test="productID != null">
  168. AND p.productID = #{productID}
  169. </if>
  170. <if test="name != null and name != ''">
  171. AND p.name LIKE concat('%',#{name},'%')
  172. </if>
  173. <if test="shopName != null and shopName != ''">
  174. AND s.name LIKE concat('%',#{shopName},'%')
  175. </if>
  176. <if test="ids != null and ids.size > 0">
  177. AND p.productID NOT IN
  178. <foreach collection="ids" open="(" close=")" item="id" separator=",">
  179. #{id}
  180. </foreach>
  181. </if>
  182. <if test="promotionType == 1 or promotionType == 2">
  183. AND p.productID NOT IN
  184. (SELECT cpp.productId
  185. FROM cm_promotions_product cpp
  186. LEFT JOIN cm_promotions cp on cpp.promotionsId = cp.id
  187. <where>
  188. cp.type = #{promotionType}
  189. AND cp.delFlag != '2'
  190. AND cpp.productId != 0
  191. <if test="delIdList != null and delIdList.size > 0">
  192. AND cpp.productId NOT IN
  193. <foreach collection="delIdList" open="(" close=")" item="delId" separator=",">
  194. #{delId}
  195. </foreach>
  196. </if>
  197. </where>
  198. )
  199. AND p.productID NOT IN (select productId from cm_svip_product)
  200. </if>
  201. </where>
  202. </select>
  203. <select id="findAllShop" resultType="com.caimei.modules.product.entity.Shop">
  204. SELECT
  205. s.*
  206. FROM
  207. shop s
  208. <where>
  209. s.status = 90
  210. AND s.shopID NOT IN
  211. (SELECT supplierId
  212. FROM cm_promotions_product cpp
  213. LEFT JOIN cm_promotions cp on cpp.promotionsId = cp.id
  214. <where>
  215. supplierId != 0
  216. AND cp.delFlag != '2'
  217. <if test="delShopIdList!=null and delShopIdList.size > 0">
  218. AND cpp.supplierId NOT IN
  219. <foreach collection="delShopIdList" open="(" close=")" item="delShopId" separator=",">
  220. #{delShopId}
  221. </foreach>
  222. </if>
  223. </where>
  224. )
  225. <if test="shopID != null">
  226. AND s.shopID = #{shopID}
  227. </if>
  228. <if test="name != null and name != ''">
  229. AND s.name LIKE concat('%',#{name},'%')
  230. </if>
  231. <if test="sname != null and sname != ''">
  232. AND s.sname LIKE concat('%',#{sname},'%')
  233. </if>
  234. <if test="ids != null and ids.size > 0">
  235. AND s.shopID NOT IN
  236. <foreach collection="ids" open="(" close=")" item="id" separator=",">
  237. #{id}
  238. </foreach>
  239. </if>
  240. </where>
  241. </select>
  242. <delete id="deleteAllPromotionProductsAndShops">
  243. DELETE
  244. FROM cm_promotions_product
  245. WHERE promotionsId = #{id}
  246. </delete>
  247. <delete id="deleteAllPromotionGifts">
  248. DELETE
  249. FROM cm_promotions_gift
  250. WHERE promotionsId = #{id}
  251. </delete>
  252. <insert id="insertPromotionProduct">
  253. INSERT INTO cm_promotions_product(promotionsId,
  254. productId,
  255. addTime)
  256. VALUES (#{promotionId},
  257. #{productId},
  258. now())
  259. </insert>
  260. <insert id="insertGiftProduct">
  261. INSERT INTO cm_promotions_gift(promotionsId,
  262. skuId,
  263. productId,
  264. number,
  265. addTime)
  266. VALUES (#{promotionId},
  267. #{giftId},
  268. (select productId from cm_sku where skuId = #{giftId}),
  269. #{number},
  270. now())
  271. </insert>
  272. <insert id="insertPromotionShop">
  273. INSERT INTO cm_promotions_product(promotionsId,
  274. supplierId,
  275. productId,
  276. addTime)
  277. VALUES (#{promotionId},
  278. #{shopId},
  279. 0,
  280. now())
  281. </insert>
  282. <insert id="insert" parameterType="CmPromotion" keyProperty="id" useGeneratedKeys="true">
  283. INSERT INTO cm_promotions(name,
  284. description,
  285. type,
  286. mode,
  287. touchPrice,
  288. reducedPrice,
  289. beginTime,
  290. endTime,
  291. addTime,
  292. updateTime,
  293. status,
  294. delFlag,
  295. discount,
  296. seen)
  297. VALUES (#{name},
  298. #{description},
  299. #{type},
  300. #{mode},
  301. #{touchPrice},
  302. #{reducedPrice},
  303. #{beginTime},
  304. #{endTime},
  305. #{addTime},
  306. #{updateTime},
  307. #{status},
  308. #{delFlag1},
  309. #{discount},
  310. #{seen})
  311. </insert>
  312. <insert id="insertNewTouchPrice">
  313. insert into cm_promotion_sku(promotionId, skuId, productId, touchPrice)
  314. values (#{promotionId}, #{skuId}, (select productId from cm_sku where skuId = #{skuId}), #{touchPrice})
  315. </insert>
  316. <update id="update">
  317. UPDATE cm_promotions
  318. SET name = #{name},
  319. description = #{description},
  320. type = #{type},
  321. mode = #{mode},
  322. touchPrice = #{touchPrice},
  323. reducedPrice = #{reducedPrice},
  324. beginTime = #{beginTime},
  325. endTime = #{endTime},
  326. updateTime = #{updateTime},
  327. status = #{status},
  328. delFlag = #{delFlag1},
  329. discount = #{discount},
  330. seen = #{seen}
  331. WHERE id = #{id}
  332. </update>
  333. <select id="findShop" resultType="com.caimei.modules.product.entity.Shop">
  334. SELECT *
  335. from shop
  336. where shopID = #{id}
  337. </select>
  338. <select id="findProductPromotion" resultType="com.caimei.modules.product.entity.CmPromotion">
  339. select pr.id,
  340. pr.name,
  341. pr.type,
  342. pr.mode,
  343. pr.touchPrice,
  344. pr.reducedPrice,
  345. pr.beginTime,
  346. pr.endTime,
  347. pr.status,
  348. prp.productId,
  349. prp.supplierId,
  350. pr.discount,
  351. cpg.skuId,
  352. cs.unit,
  353. cs.organizeId
  354. from cm_promotions pr
  355. left join cm_promotions_product prp on pr.id = prp.promotionsId
  356. LEFT JOIN cm_promotions_gift cpg ON pr.id = cpg.promotionsId
  357. left join cm_sku cs on cpg.skuId = cs.skuId
  358. where (prp.productId = #{productId}
  359. or prp.supplierId = #{shopId})
  360. and (pr.status = 1 or (pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
  361. and pr.delFlag not in ('1', '2')
  362. order by pr.type desc
  363. limit 1
  364. </select>
  365. <select id="findProductPromotionsGift" resultType="com.caimei.modules.product.entity.CmPromotion">
  366. SELECT DISTINCT pr.id,
  367. pr.name,
  368. pr.type,
  369. pr.mode,
  370. pr.touchPrice,
  371. pr.reducedPrice,
  372. pr.beginTime,
  373. pr.endTime,
  374. pr.status
  375. FROM cm_promotions_gift cpg
  376. LEFT JOIN cm_promotions pr ON pr.id = cpg.promotionsId
  377. WHERE cpg.productId = #{productId}
  378. AND (pr.status = 1 OR (pr.status = 2 AND (NOW() BETWEEN pr.beginTime AND pr.endTime)))
  379. AND pr.mode = 3
  380. AND pr.delFlag not in ('1', '2')
  381. ORDER BY pr.type DESC
  382. LIMIT 1
  383. </select>
  384. <select id="findOrderPromotions" resultType="com.caimei.modules.product.entity.CmPromotion">
  385. SELECT *
  386. FROM cm_promotions_order
  387. WHERE id = #{orderPromotionsId}
  388. </select>
  389. <select id="findPromotionsByProductId" resultType="com.caimei.modules.product.entity.CmPromotion">
  390. select pr.id,
  391. pr.name,
  392. pr.description,
  393. pr.type,
  394. pr.mode,
  395. pr.touchPrice,
  396. pr.reducedPrice,
  397. pr.beginTime,
  398. pr.endTime,
  399. pr.status,
  400. prp.productId,
  401. prp.supplierId
  402. from cm_promotions pr
  403. left join cm_promotions_product prp on pr.id = prp.promotionsId
  404. where (prp.productId = #{productId} or
  405. prp.supplierId = (select p.shopID from product p where p.productID = #{productId})
  406. )
  407. and (pr.status = 1 or (pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
  408. and pr.delFlag not in (1, 2)
  409. order by pr.type desc
  410. limit 1
  411. </select>
  412. <select id="getPresentPriceById" resultType="java.lang.Double">
  413. select price
  414. from cm_sku
  415. where skuId = #{skuId}
  416. </select>
  417. <select id="findProductIdByPromotion" resultType="java.lang.Integer">
  418. select productId
  419. from cm_promotions_product
  420. where promotionsId = #{promotionsId}
  421. </select>
  422. <select id="findProductIdsByPromotion" resultType="java.lang.Integer">
  423. select productID
  424. from product
  425. where shopID in (select supplierId from cm_promotions_product where promotionsId = #{promotionsId})
  426. </select>
  427. <select id="getPromotionSkus" resultType="com.caimei.modules.product.entity.CmSku">
  428. SELECT DISTINCT cs.unit, cs.costCheckFlag, cs.costPrice, cs.shopPercent, cps.touchPrice
  429. , cs.skuId
  430. , cs.organizeId
  431. FROM cm_promotions cp
  432. LEFT JOIN cm_promotions_product cpp ON cp.id = cpp.promotionsId
  433. LEFT JOIN cm_sku cs ON cpp.productId = cs.productId
  434. LEFT JOIN cm_promotion_sku cps ON cs.skuId = cps.skuId
  435. WHERE cp.id = #{id}
  436. </select>
  437. <select id="getPromotionSkusByProduct" resultType="com.caimei.modules.product.entity.CmSku">
  438. SELECT cs.unit, cs.costCheckFlag, cs.costPrice, cs.shopPercent, cps.touchPrice
  439. , cs.organizeId
  440. FROM cm_sku cs
  441. LEFT JOIN cm_promotion_sku cps ON cs.skuId = cps.skuId
  442. WHERE cs.productId = #{productIds}
  443. </select>
  444. <delete id="deletePromotion">
  445. delete
  446. from cm_promotions_gift
  447. where promotionsId = #{id}
  448. and skuId = #{skuId}
  449. </delete>
  450. <delete id="deleteOldTouchPrice">
  451. delete
  452. from cm_promotion_sku
  453. where promotionId = #{promotionId}
  454. </delete>
  455. </mapper>