CmPromotionMapper.xml 20 KB

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