CustomerMapper.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.mdm.mapper.mst.CustomerMapper">
  4. <!-- 通用设置 -->
  5. <!-- 通用查询列 -->
  6. <sql id="Base_Column_List">
  7. cus_id, cus_code, cus_name, cus_phone, address_area, address_name, address_no, address_gcj02, address_full, contact_name, contact_phone, cus_from, channel_id, org_id, staff_id, report_staff, report_time, sale_status, remarks, follow_staffs, last_follow_staff, last_follow_id, last_follow_status, last_follow_time, follow_count, flg_valid, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
  8. </sql>
  9. <sql id="Base_Column_List_Response">
  10. tmc.cus_id, tmc.cus_code,tmc.cus_name, tmc.cus_phone, tmc.address_area, tmc.address_name, tmc.address_no,
  11. tmc.address_gcj02, tmc.address_full, tmc.contact_name, tmc.contact_phone, tmc.cus_from, tmc.channel_id,
  12. tmc.org_id, tmc.staff_id, tmc.report_staff, tmc.report_time, tmc.sale_status, tmc.remarks, tmc.follow_staffs,
  13. tmc.last_follow_staff, tmc.last_follow_id, tmc.last_follow_status, tmc.last_follow_time, tmc.follow_count, tmc.flg_valid, tmc.cp_id
  14. </sql>
  15. <!-- 通用查询映射结果 -->
  16. <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.Customer">
  17. <id column="cus_id" property="cusId"/>
  18. <result column="cus_code" property="cusCode"/>
  19. <result column="cus_name" property="cusName"/>
  20. <result column="cus_phone" property="cusPhone"/>
  21. <result column="address_area" property="addressArea" typeHandler="JsonTypeHandler"/>
  22. <result column="address_name" property="addressName"/>
  23. <result column="address_no" property="addressNo"/>
  24. <result column="address_gcj02" property="addressGcj02" typeHandler="JsonTypeHandler"/>
  25. <result column="address_full" property="addressFull"/>
  26. <result column="contact_name" property="contactName"/>
  27. <result column="contact_phone" property="contactPhone"/>
  28. <result column="cus_from" property="cusFrom" typeHandler="UuidTypeHandler"/>
  29. <result column="channel_id" property="channelId" typeHandler="UuidTypeHandler"/>
  30. <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
  31. <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
  32. <result column="report_staff" property="reportStaff" typeHandler="UuidTypeHandler"/>
  33. <result column="report_time" property="reportTime" typeHandler="TimestampTypeHandler"/>
  34. <result column="sale_status" property="saleStatus"/>
  35. <result column="remarks" property="remarks"/>
  36. <result column="follow_staffs" property="followStaffs" typeHandler="UuidListTypeHandler"/>
  37. <result column="last_follow_staff" property="lastFollowStaff" typeHandler="UuidTypeHandler"/>
  38. <result column="last_follow_id" property="lastFollowId" typeHandler="UuidTypeHandler"/>
  39. <result column="last_follow_status" property="lastFollowStatus"/>
  40. <result column="last_follow_time" property="lastFollowTime" typeHandler="TimestampTypeHandler"/>
  41. <result column="follow_count" property="followCount"/>
  42. <result column="flg_valid" property="flgValid"/>
  43. <result column="cp_id" property="cpId"/>
  44. <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
  45. <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
  46. <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
  47. <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
  48. <result column="op_app_code" property="opAppCode"/>
  49. <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
  50. <result column="op_db_user" property="opDbUser"/>
  51. </resultMap>
  52. <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.mst.CustomerResponse">
  53. <id column="cus_id" property="cusId"/>
  54. <result column="cus_code" property="cusCode"/>
  55. <result column="cus_name" property="cusName"/>
  56. <result column="cus_phone" property="cusPhone"/>
  57. <result column="address_area" property="addressArea" typeHandler="JsonTypeHandler"/>
  58. <result column="address_name" property="addressName"/>
  59. <result column="address_no" property="addressNo"/>
  60. <result column="address_gcj02" property="addressGcj02" typeHandler="JsonTypeHandler"/>
  61. <result column="address_full" property="addressFull"/>
  62. <result column="contact_name" property="contactName"/>
  63. <result column="contact_phone" property="contactPhone"/>
  64. <result column="cus_from" property="cusFrom" typeHandler="UuidTypeHandler"/>
  65. <result column="channel_id" property="channelId" typeHandler="UuidTypeHandler"/>
  66. <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
  67. <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
  68. <result column="report_staff" property="reportStaff" typeHandler="UuidTypeHandler"/>
  69. <result column="report_time" property="reportTime" typeHandler="TimestampTypeHandler"/>
  70. <result column="sale_status" property="saleStatus"/>
  71. <result column="remarks" property="remarks"/>
  72. <result column="follow_staffs" property="followStaffs" typeHandler="UuidListTypeHandler"/>
  73. <result column="last_follow_staff" property="lastFollowStaff" typeHandler="UuidTypeHandler"/>
  74. <result column="last_follow_id" property="lastFollowId" typeHandler="UuidTypeHandler"/>
  75. <result column="last_follow_status" property="lastFollowStatus"/>
  76. <result column="last_follow_time" property="lastFollowTime" typeHandler="TimestampTypeHandler"/>
  77. <result column="follow_count" property="followCount"/>
  78. <result column="flg_valid" property="flgValid"/>
  79. <result column="cp_id" property="cpId"/>
  80. <result column="cusFromName" property="cusFromName"/>
  81. <result column="channelName" property="channelName"/>
  82. <result column="orgName" property="orgName"/>
  83. <result column="staffName" property="staffName"/>
  84. <result column="reportStaffName" property="reportStaffName"/>
  85. <result column="followStaffName" property="followStaffName"/>
  86. <result column="lastFollowStaffName" property="lastFollowStaffName"/>
  87. <result column="saleStatusName" property="saleStatusName"/>
  88. <result column="next_follow_time" property="nextFollowTime" typeHandler="TimestampTypeHandler"/>
  89. <result column="follow_staff" property="followStaff"/>
  90. <result column="follow_staff_name" property="followStaffName"/>
  91. <result column="receivable_residue" property="receivableResidue"/>
  92. <result column="receipt_residue" property="receiptResidue"/>
  93. <result column="receivable" property="receivable"/>
  94. <result column="receipt" property="receipt"/>
  95. <result column="orderQuantity" property="orderQuantity"/>
  96. <result column="orderAmount" property="orderAmount"/>
  97. <result column="outboundQuantity" property="outboundQuantity"/>
  98. <result column="outboundAmount" property="outboundAmount"/>
  99. <result column="outboundNotAmount" property="outboundNotAmount"/>
  100. <result column="outboundNotQuantity" property="outboundNotQuantity"/>
  101. <result column="outboundReturnQuantity" property="outboundReturnQuantity"/>
  102. <result column="outboundReturnAmount" property="outboundReturnAmount"/>
  103. </resultMap>
  104. <!-- 通用条件列 -->
  105. <sql id="Condition">
  106. <where>
  107. <if test="cusCode != null and cusCode != ''">
  108. AND tmc.cus_code = #{cusCode}
  109. </if>
  110. <if test="cusName != null and cusName != ''">
  111. AND tmc.cus_name like concat('%', my_ex.likequery(#{cusName}) , '%')
  112. </if>
  113. <if test="cusPhone != null and cusPhone != ''">
  114. AND tmc.cus_phone like concat('%', my_ex.likequery(#{cusPhone}) , '%')
  115. </if>
  116. <if test="addressArea != null and addressArea != ''">
  117. AND tmc.address_area = #{addressArea}
  118. </if>
  119. <if test="addressName != null and addressName != ''">
  120. AND tmc.address_name = #{addressName}
  121. </if>
  122. <if test="addressNo != null and addressNo != ''">
  123. AND tmc.address_no = #{addressNo}
  124. </if>
  125. <if test="addressGcj02 != null and addressGcj02 != ''">
  126. AND tmc.address_gcj02 = #{addressGcj02}
  127. </if>
  128. <if test="addressFull != null and addressFull != ''">
  129. AND tmc.address_full = #{addressFull}
  130. </if>
  131. <if test="contactName != null and contactName != ''">
  132. AND tmc.contact_name like concat('%', my_ex.likequery(#{contactName}) , '%')
  133. </if>
  134. <if test="contactPhone != null and contactPhone != ''">
  135. AND tmc.contact_phone like concat('%', my_ex.likequery(#{contactPhone}) , '%')
  136. </if>
  137. <if test="cusFrom != null and cusFrom != ''">
  138. AND tmc.cus_from = #{cusFrom}::uuid
  139. </if>
  140. <if test="channelId != null and channelId != ''">
  141. AND tmc.channel_id = #{channelId}::uuid
  142. </if>
  143. <if test="orgId != null and orgId != ''">
  144. AND tmc.org_id = #{orgId}::uuid
  145. </if>
  146. <if test="staffId != null and staffId != ''">
  147. AND tmc.staff_id = #{staffId}::uuid
  148. </if>
  149. <if test="reportStaff != null and reportStaff != ''">
  150. AND tmc.report_staff = #{reportStaff}::uuid
  151. </if>
  152. <if test="reportTime != null">
  153. AND tmc.report_time = #{reportTime}
  154. </if>
  155. <if test="saleStatus != null and saleStatus != ''">
  156. AND tmc.sale_status = #{saleStatus}
  157. </if>
  158. <if test="remarks != null and remarks != ''">
  159. AND tmc.remarks = #{remarks}
  160. </if>
  161. <if test="followStaffs != null and followStaffs != ''">
  162. AND tmc.follow_staffs = #{followStaffs}::uuid
  163. </if>
  164. <if test="lastFollowStaff != null and lastFollowStaff != ''">
  165. AND tmc.last_follow_staff = #{lastFollowStaff}::uuid
  166. </if>
  167. <if test="lastFollowId != null and lastFollowId != ''">
  168. AND tmc.last_follow_id = #{lastFollowId}
  169. </if>
  170. <if test="lastFollowStatus != null and lastFollowStatus != ''">
  171. AND tmc.last_follow_status = #{lastFollowStatus}
  172. </if>
  173. <if test="lastFollowTime != null">
  174. AND tmc.last_follow_time = #{lastFollowTime}
  175. </if>
  176. <if test="followCount != null">
  177. AND tmc.follow_count = #{followCount}
  178. </if>
  179. <if test="flgValid != null">
  180. AND tmc.flg_valid = #{flgValid}
  181. </if>
  182. <if test="cpId != null">
  183. AND tmc.cp_id = #{cpId}
  184. </if>
  185. <if test="searchText != null">
  186. AND (tmc.cus_code like concat('%', my_ex.likequery(#{searchText}) , '%')
  187. OR tmc.cus_name like concat('%', my_ex.likequery(#{searchText}) , '%')
  188. OR tmc.cus_phone like concat('%', my_ex.likequery(#{searchText}) , '%')
  189. OR tmc.address_full like concat('%', my_ex.likequery(#{searchText}) , '%')
  190. OR tmc.remarks like concat('%', my_ex.likequery(#{searchText}) , '%')
  191. )
  192. </if>
  193. </where>
  194. </sql>
  195. <sql id="idsForeach">
  196. <!-- 根据主键cusId批量操作 -->
  197. WHERE cus_id in
  198. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  199. #{item}
  200. </foreach>
  201. </sql>
  202. <!-- 查询表t_mst_customer,(条件查询+分页)列表 -->
  203. <select id="selectByCond" resultMap="BaseResultMapResponse">
  204. SELECT
  205. <include refid="Base_Column_List_Response"/>,
  206. tmd.data_value AS "cusFromName",
  207. tmsc.channel_name AS "channelName",
  208. tmo.org_name AS "orgName",
  209. tms.staff_name AS "staffName",
  210. tms2.staff_name AS "reportStaffName",
  211. tdk.kind_name_i18n ->> #{i18n} AS "saleStatusName",
  212. tcf.next_follow_time,
  213. tma.receivable_residue,
  214. tma.receipt_residue
  215. FROM dkic_b.t_mst_customer tmc
  216. LEFT JOIN ( select tccff.cus_id, max (tccff.next_follow_time)as next_follow_time from dkic_b.t_crm_cus_follow tccff group by tccff.cus_id ) tcf on tmc.cus_id = tcf.cus_id
  217. LEFT JOIN dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tmc.cus_from
  218. LEFT JOIN dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
  219. LEFT JOIN dkic_b.t_mst_org tmo on tmo.org_id = tmc.org_id
  220. LEFT JOIN dkic_b.t_mst_staff tms on tms.staff_id = tmc.staff_id
  221. LEFT JOIN dkic_b.t_mst_staff tms2 on tms2.staff_id = tmc.report_staff
  222. LEFT JOIN sys.t_data_kind tdk on tdk.kind_code = tmc.sale_status
  223. LEFT JOIN dkic_b.t_mac_account tma on tma.object_id = tmc.cus_id
  224. <include refid="Condition"/>
  225. order by tmc.cus_id DESC
  226. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  227. limit #{end} offset #{start}
  228. </if>
  229. </select>
  230. <!-- 查询表t_mst_customer,(条件查询)个数 -->
  231. <select id="countByCond" resultType="Long">
  232. SELECT
  233. count(1)
  234. FROM dkic_b.t_mst_customer tmc
  235. <include refid="Condition"/>
  236. </select>
  237. <!-- 根据主键查询表t_mst_customer的一行数据 -->
  238. <select id="selectById" resultMap="BaseResultMap">
  239. SELECT
  240. <include refid="Base_Column_List"/>
  241. FROM dkic_b.t_mst_customer
  242. WHERE cus_id = #{id}::uuid
  243. </select>
  244. <select id="selectByIdRespone" resultMap="BaseResultMapResponse">
  245. SELECT
  246. <include refid="Base_Column_List_Response"/>,
  247. tmd.data_value AS "cusFromName",
  248. tmsc.channel_name AS "channelName",
  249. tmo.org_name AS "orgName",
  250. tms.staff_name AS "staffName",
  251. tms2.staff_name AS "reportStaffName",
  252. tdk.kind_name_i18n ->> #{i18n} AS "saleStatusName",
  253. -- tccfs.follow_staff as follow_staff,
  254. -- tms1.staff_name as follow_staff_name
  255. (SELECT COUNT(1) FROM dkic_b.t_psi_order tpo WHERE tpo.cus_id = tmc.cus_id AND tpo.cp_id = tmc.cp_id AND
  256. tpo.flg_valid) AS "orderQuantity",
  257. (SELECT sum(tpo.sum_amount) FROM dkic_b.t_psi_order tpo WHERE tpo.cus_id = tmc.cus_id AND tpo.cp_id = tmc.cp_id
  258. AND tpo.flg_valid) AS "orderAmount",
  259. (SELECT COUNT(1) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id = tmc.cp_id AND
  260. tpob.flg_valid AND tpob.out_type = '出库类型-销售出库' AND tpob.out_status = '出库状态-已出库') AS
  261. "outboundQuantity",
  262. (SELECT sum(tpob.out_amt) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  263. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销售出库' AND tpob.out_status = '出库状态-已出库') AS
  264. "outboundAmount",
  265. (SELECT COUNT(1) FROM dkic_b.t_psi_order tpo WHERE tpo.cus_id = tmc.cus_id AND tpo.cp_id =
  266. tmc.cp_id AND tpo.flg_valid AND tpo.out_status = '出库状态-待出库' AND tpo.order_status = '订单状态-成交') AS
  267. "outboundNotQuantity",
  268. (SELECT sum(tpo.sum_amount) FROM dkic_b.t_psi_order tpo WHERE tpo.cus_id = tmc.cus_id AND tpo.cp_id =
  269. tmc.cp_id AND tpo.flg_valid AND tpo.out_status = '出库状态-待出库' AND tpo.order_status = '订单状态-成交') AS
  270. "outboundNotAmount",
  271. (SELECT COUNT(1) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  272. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销退回库') AS
  273. "outboundReturnQuantity",
  274. (SELECT sum(tpob.out_amt) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  275. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销退回库') AS
  276. "outboundReturnAmount",
  277. tma.receipt,
  278. tma.receivable,
  279. tma.receivable_residue,
  280. tma.receipt_residue
  281. FROM dkic_b.t_mst_customer tmc
  282. LEFT JOIN dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tmc.cus_from
  283. LEFT JOIN dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
  284. LEFT JOIN dkic_b.t_mst_org tmo on tmo.org_id = tmc.org_id
  285. LEFT JOIN dkic_b.t_mst_staff tms on tms.staff_id = tmc.staff_id
  286. LEFT JOIN dkic_b.t_mst_staff tms2 on tms2.staff_id = tmc.report_staff
  287. LEFT JOIN sys.t_data_kind tdk on tdk.kind_code = tmc.sale_status
  288. LEFT JOIN dkic_b.t_mac_account tma on tma.object_id = tmc.cus_id
  289. -- LEFT JOIN dkic_b.t_crm_cus_follow_staff tccfs on tccfs.follow_staff = any(tmc.follow_staffs)
  290. -- LEFT JOIN dkic_b.t_mst_staff tms1 on tms1.staff_id = tccfs.follow_staff
  291. WHERE tmc.cus_id = #{id}::uuid
  292. </select>
  293. <!-- 根据主键锁定表t_mst_customer的一行数据 -->
  294. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  295. SELECT
  296. <include refid="Base_Column_List"/>
  297. FROM dkic_b.t_mst_customer
  298. WHERE cus_id = #{id} ::uuid
  299. for update
  300. </select>
  301. <!-- 根据主键锁定表t_mst_customer的多行数据 -->
  302. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  303. SELECT
  304. <include refid="Base_Column_List"/>
  305. FROM dkic_b.t_mst_customer
  306. <include refid="idsForeach"/>
  307. for update
  308. </select>
  309. <insert id="insertBatch">
  310. insert into dkic_b.t_mst_customer
  311. (
  312. <trim suffixOverrides=",">
  313. cus_code,
  314. cus_name,
  315. cus_phone,
  316. address_area,
  317. address_name,
  318. address_no,
  319. address_gcj02,
  320. address_full,
  321. contact_name,
  322. contact_phone,
  323. cus_from,
  324. channel_id,
  325. org_id,
  326. staff_id,
  327. report_staff,
  328. report_time,
  329. sale_status,
  330. remarks,
  331. follow_staffs,
  332. last_follow_staff,
  333. last_follow_id,
  334. last_follow_status,
  335. last_follow_time,
  336. follow_count,
  337. cp_id,
  338. op_app_code,
  339. </trim>
  340. )
  341. values
  342. <foreach collection="list" index="index" item="item" separator=",">
  343. (
  344. <trim suffixOverrides=",">
  345. #{item.cusCode},
  346. #{item.cusName},
  347. #{item.cusPhone},
  348. #{item.addressArea},
  349. #{item.addressName},
  350. #{item.addressNo},
  351. #{item.addressGcj02},
  352. #{item.addressFull},
  353. #{item.contactName},
  354. #{item.contactPhone},
  355. #{item.cusFrom}::uuid,
  356. #{item.channelId}::uuid,
  357. #{item.orgId}::uuid,
  358. #{item.staffId}::uuid,
  359. #{item.reportStaff}::uuid,
  360. #{item.reportTime},
  361. #{item.saleStatus},
  362. #{item.remarks},
  363. #{item.followStaffs},
  364. #{item.lastFollowStaff}::uuid,
  365. #{item.lastFollowId}::uuid,
  366. #{item.lastFollowStatus},
  367. #{item.lastFollowTime},
  368. #{item.followCount},
  369. #{item.cpId},
  370. #{item.opAppCode},
  371. </trim>
  372. )
  373. </foreach>
  374. </insert>
  375. <!-- 查询跟进人-->
  376. <select id="selectFollowStaffs" resultType="Object">
  377. SELECT follow_staffs
  378. from dkic_b.t_mst_customer
  379. WHERE cus_id = #{id}::uuid
  380. </select>
  381. <!-- 公海的客户需要把跟进人清空-->
  382. <update id="updateFollowStaffs">
  383. update dkic_b.t_mst_customer
  384. set
  385. follow_staffs = #{followStaffs,typeHandler=UuidListTypeHandler}
  386. where cus_id = #{cusId}::uuid
  387. </update>
  388. </mapper>