CouponSendMapper.xml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.dk.oauth.mapper.integral.CouponSendMapper">
  4. <!-- 通用设置 -->
  5. <!-- 通用查询列 -->
  6. <sql id="Base_Column_List">
  7. send_id,cp_id, coupon_id, send_date, end_date, user_num, flg_valid,send_staff,send_user
  8. </sql>
  9. <!-- 通用查询列Response -->
  10. <sql id="Base_Column_List_Response">
  11. tmcs.send_id,tmcs.cp_id, tmcs.coupon_id, tmcs.send_date, tmcs.end_date, tmcs.user_num,
  12. tmcs.flg_valid,tmcs.send_staff,tmcs.send_user
  13. </sql>
  14. <!-- 通用查询映射结果 -->
  15. <resultMap id="BaseResultMap" type="com.dk.oauth.model.pojo.integral.CouponSend">
  16. <id column="send_id" property="sendId" typeHandler="UuidTypeHandler"/>
  17. <result column="cp_id" property="cpId"/>
  18. <result column="coupon_id" property="couponId" typeHandler="UuidTypeHandler"/>
  19. <result column="send_date" property="sendDate" typeHandler="TimestampTypeHandler"/>
  20. <result column="end_date" property="endDate" typeHandler="TimestampTypeHandler"/>
  21. <result column="user_num" property="userNum"/>
  22. <result column="flg_valid" property="flgValid"/>
  23. <result column="send_staff" property="sendStaff"/>
  24. <result column="send_user" property="sendUser" typeHandler="UuidTypeHandler"/>
  25. </resultMap>
  26. <!-- 通用查询映射结果 -->
  27. <resultMap id="BaseResultMapResponse" type="com.dk.oauth.model.response.integral.CouponSendResponse">
  28. <id column="send_id" property="sendId" typeHandler="UuidTypeHandler"/>
  29. <result column="cp_id" property="cpId"/>
  30. <result column="coupon_id" property="couponId" typeHandler="UuidTypeHandler"/>
  31. <result column="send_date" property="sendDate" typeHandler="TimestampTypeHandler"/>
  32. <result column="end_date" property="endDate" typeHandler="TimestampTypeHandler"/>
  33. <result column="user_num" property="userNum"/>
  34. <result column="flg_valid" property="flgValid"/>
  35. <result column="cp_name" property="cpName"/>
  36. <result column="coupon_name" property="couponName"/>
  37. <result column="send_staff" property="sendStaff"/>
  38. <result column="coupon_receive_count" property="couponReceiveCount"/>
  39. <result column="send_user" property="sendUser" typeHandler="UuidTypeHandler"/>
  40. </resultMap>
  41. <!-- 通用条件列 -->
  42. <sql id="Condition">
  43. <where>
  44. <if test="cpId != null and cpId != ''">
  45. AND tmcs.cp_id = #{cpId}
  46. </if>
  47. <if test="couponId != null and couponId != ''">
  48. AND tmcs.coupon_id = #{couponId}::uuid
  49. </if>
  50. <if test="sendDate != null">
  51. AND tmcs.send_date = #{sendDate}
  52. </if>
  53. <if test="endDate != null">
  54. AND tmcs.end_date = #{endDate}
  55. </if>
  56. <if test="userNum != null">
  57. AND tmcs.user_num = #{userNum}
  58. </if>
  59. <if test="flgValid != null">
  60. AND tmcs.flg_valid = #{flgValid}
  61. </if>
  62. <if test="cpName != null and cpName != ''">
  63. AND tac.cp_name LIKE concat('%',my_ex.likequery(#{cpName}),'%')
  64. </if>
  65. <if test="couponName != null and couponName != ''">
  66. AND tmc.coupon_name LIKE concat('%',my_ex.likequery(#{couponName}),'%')
  67. </if>
  68. <if test="sendUser != null and sendUser != ''">
  69. AND tmcs.send_user = #{sendUser}::uuid
  70. </if>
  71. </where>
  72. </sql>
  73. <sql id="idsForeach">
  74. <!-- 根据主键cpId批量操作 -->
  75. WHERE send_id in
  76. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  77. #{item}
  78. </foreach>
  79. </sql>
  80. <!-- 查询表dkic_a.t_mst_coupon_send,(条件查询+分页)列表 -->
  81. <select id="selectByCond" resultMap="BaseResultMapResponse">
  82. SELECT
  83. <include refid="Base_Column_List_Response"/>
  84. ,tac.cp_name
  85. ,tmc.coupon_name
  86. FROM dkic_a.t_mst_coupon_send tmcs
  87. left join dkic_a.t_a_company tac on tac.cp_id = tmcs.cp_id
  88. left join dkic_a.t_mst_coupon tmc on tmc.coupon_id = tmcs.coupon_id
  89. <include refid="Condition"/>
  90. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  91. limit #{end} offset #{start}
  92. </if>
  93. </select>
  94. <!-- 查询表dkic_a.t_mst_coupon_send,(条件查询+分页)列表 -->
  95. <select id="selectByCondHistory" resultMap="BaseResultMapResponse">
  96. SELECT
  97. <include refid="Base_Column_List_Response"/>
  98. ,tac.cp_name
  99. ,tmc.coupon_name
  100. ,(select count(tmcr.send_id) from dkic_a.t_mst_coupon_receive tmcr where tmcr.send_id = tmcs.send_id ) coupon_receive_count
  101. FROM dkic_a.t_mst_coupon_send tmcs
  102. left join dkic_a.t_a_company tac on tac.cp_id = tmcs.cp_id
  103. left join dkic_a.t_mst_coupon tmc on tmc.coupon_id = tmcs.coupon_id
  104. <include refid="Condition"/>
  105. order by tmcs.send_date desc
  106. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  107. limit #{end} offset #{start}
  108. </if>
  109. </select>
  110. <!-- 查询表dkic_a.t_mst_coupon_send,(条件查询)个数 -->
  111. <select id="countByCond" resultType="Long">
  112. SELECT
  113. count(1)
  114. FROM dkic_a.t_mst_coupon_send tmcs
  115. <include refid="Condition"/>
  116. </select>
  117. <!-- 根据主键查询表dkic_a.t_mst_coupon_send的一行数据 -->
  118. <select id="selectById" resultMap="BaseResultMapResponse">
  119. SELECT
  120. <include refid="Base_Column_List_Response"/>
  121. ,tac.cp_name
  122. ,tmc.coupon_name
  123. FROM dkic_a.t_mst_coupon_send tmcs
  124. left join dkic_a.t_a_company tac on tac.cp_id = tmcs.cp_id
  125. left join dkic_a.t_mst_coupon tmc on tmc.coupon_id = tmcs.coupon_id
  126. WHERE tmcs.send_id = #{sendId}::uuid
  127. </select>
  128. <!-- 根据主键锁定表dkic_a.t_mst_coupon_send的一行数据 -->
  129. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  130. SELECT
  131. <include refid="Base_Column_List"/>
  132. FROM dkic_a.t_mst_coupon_send
  133. WHERE send_id = #{sendId}::uuid
  134. for update
  135. </select>
  136. <!-- 根据主键锁定表dkic_a.t_mst_coupon_send的多行数据 -->
  137. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  138. SELECT
  139. <include refid="Base_Column_List"/>
  140. FROM dkic_a.t_mst_coupon_send
  141. <include refid="idsForeach"/>
  142. for update
  143. </select>
  144. <insert id="insertBatch">
  145. insert into dkic_a.t_mst_coupon_send
  146. (
  147. <trim suffixOverrides=",">
  148. cp_id,
  149. coupon_id,
  150. send_date,
  151. end_date,
  152. user_num,
  153. </trim>
  154. )
  155. values
  156. <foreach collection="list" index="index" item="item" separator=",">
  157. (
  158. <trim suffixOverrides=",">
  159. #{item.cpId},
  160. #{item.couponId}::uuid,
  161. #{item.sendDate},
  162. #{item.endDate},
  163. #{item.userNum},
  164. </trim>
  165. )
  166. </foreach>
  167. </insert>
  168. <!-- 根据公司id和优惠券id查询截止时间 -->
  169. <select id="selectEndTime" resultMap="BaseResultMapResponse">
  170. SELECT end_date
  171. FROM dkic_a.t_mst_coupon_send
  172. WHERE send_id = #{sendId}::uuid
  173. </select>
  174. <select id="selectCouponSendItem" resultType="java.util.Map">
  175. select
  176. tmcs.send_id as "sendId",
  177. tmcs.cp_id as "cpId",
  178. tmcs.coupon_id as "couponId",
  179. tmcs.send_date as "sendDate",
  180. tmcs.end_date as "endDate",
  181. tmcs.user_num as "userNum",
  182. tmcs.send_staff as "sendStaff",
  183. tmcs.send_user as "sendUser",
  184. ( select COUNT (1) from dkic_a.t_mst_coupon_receive tmcr
  185. where tmcr.send_id = tmcs.send_id
  186. ) as "userHelpNum",
  187. now() as "nowTime"
  188. from dkic_a.t_mst_coupon_send tmcs
  189. left join dkic_a.t_mst_coupon tmc
  190. on tmc.coupon_id = tmcs.coupon_id
  191. where tmcs.flg_valid
  192. AND tmcs.send_user = #{sendUser}::uuid
  193. and ( select COUNT (1) from dkic_a.t_mst_coupon_receive tmcr
  194. where tmcr.send_id = tmcs.send_id
  195. ) &lt; tmc.user_num
  196. and tmcs.cp_id = #{cpId}
  197. and tmcs.end_date > now()
  198. ORDER BY tmcs.send_date desc
  199. limit 1
  200. </select>
  201. </mapper>