CustomerMapper.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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="followStaffsName" property="followStaffsName" />
  99. <result column="outboundAmount" property="outboundAmount"/>
  100. <result column="outboundNotAmount" property="outboundNotAmount"/>
  101. <result column="outboundNotQuantity" property="outboundNotQuantity"/>
  102. <result column="outboundReturnQuantity" property="outboundReturnQuantity"/>
  103. <result column="outboundReturnAmount" property="outboundReturnAmount"/>
  104. <result column="receipt_lock" property="receiptLock"/>
  105. <result column="startAmount" property="startAmount"/>
  106. </resultMap>
  107. <!-- 通用条件列 -->
  108. <sql id="Condition">
  109. <where>
  110. <if test="cusCode != null and cusCode != ''">
  111. AND tmc.cus_code = #{cusCode}
  112. </if>
  113. <if test="cusName != null and cusName != ''">
  114. AND tmc.cus_name like concat('%', my_ex.likequery(#{cusName}) , '%')
  115. </if>
  116. <if test="cusPhone != null and cusPhone != ''">
  117. AND tmc.cus_phone like concat('%', my_ex.likequery(#{cusPhone}) , '%')
  118. </if>
  119. <if test="addressArea != null and addressArea != ''">
  120. AND tmc.address_area = #{addressArea}
  121. </if>
  122. <if test="addressName != null and addressName != ''">
  123. AND tmc.address_name = #{addressName}
  124. </if>
  125. <if test="addressNo != null and addressNo != ''">
  126. AND tmc.address_no = #{addressNo}
  127. </if>
  128. <if test="addressGcj02 != null and addressGcj02 != ''">
  129. AND tmc.address_gcj02 = #{addressGcj02}
  130. </if>
  131. <if test="addressFull != null and addressFull != ''">
  132. AND tmc.address_full = #{addressFull}
  133. </if>
  134. <if test="contactName != null and contactName != ''">
  135. AND tmc.contact_name like concat('%', my_ex.likequery(#{contactName}) , '%')
  136. </if>
  137. <if test="contactPhone != null and contactPhone != ''">
  138. AND tmc.contact_phone like concat('%', my_ex.likequery(#{contactPhone}) , '%')
  139. </if>
  140. <if test="cusFrom != null and cusFrom != ''">
  141. AND tmc.cus_from = #{cusFrom}::uuid
  142. </if>
  143. <if test="channelId != null and channelId != ''">
  144. AND tmc.channel_id = #{channelId}::uuid
  145. </if>
  146. <if test="orgId != null and orgId != ''">
  147. AND tmc.org_id = #{orgId}::uuid
  148. </if>
  149. <if test="staffId != null and staffId != ''">
  150. AND tmc.staff_id = #{staffId}::uuid
  151. </if>
  152. <if test="reportStaff != null and reportStaff != ''">
  153. AND tmc.report_staff = #{reportStaff}::uuid
  154. </if>
  155. <if test="reportTime != null">
  156. AND tmc.report_time = #{reportTime}
  157. </if>
  158. <if test="saleStatus != null and saleStatus != ''">
  159. AND tmc.sale_status = #{saleStatus}
  160. </if>
  161. <if test="remarks != null and remarks != ''">
  162. AND tmc.remarks = #{remarks}
  163. </if>
  164. <if test="followStaffs != null and followStaffs != ''">
  165. AND tmc.follow_staffs = #{followStaffs}::uuid
  166. </if>
  167. <if test="lastFollowStaff != null and lastFollowStaff != ''">
  168. AND tmc.last_follow_staff = #{lastFollowStaff}::uuid
  169. </if>
  170. <if test="lastFollowId != null and lastFollowId != ''">
  171. AND tmc.last_follow_id = #{lastFollowId}
  172. </if>
  173. <if test="lastFollowStatus != null and lastFollowStatus != ''">
  174. AND tmc.last_follow_status = #{lastFollowStatus}
  175. </if>
  176. <if test="lastFollowTime != null">
  177. AND tmc.last_follow_time = #{lastFollowTime}
  178. </if>
  179. <if test="followCount != null">
  180. AND tmc.follow_count = #{followCount}
  181. </if>
  182. <if test="flgValid != null">
  183. AND tmc.flg_valid = #{flgValid}
  184. </if>
  185. <if test="cpId != null">
  186. AND tmc.cp_id = #{cpId}
  187. </if>
  188. <if test="searchText != null">
  189. AND (tmc.cus_code like concat('%', my_ex.likequery(#{searchText}) , '%')
  190. OR tmc.cus_name like concat('%', my_ex.likequery(#{searchText}) , '%')
  191. OR tmc.cus_phone like concat('%', my_ex.likequery(#{searchText}) , '%')
  192. OR tmc.address_full like concat('%', my_ex.likequery(#{searchText}) , '%')
  193. OR tmc.remarks like concat('%', my_ex.likequery(#{searchText}) , '%')
  194. )
  195. </if>
  196. </where>
  197. </sql>
  198. <sql id="idsForeach">
  199. <!-- 根据主键cusId批量操作 -->
  200. WHERE cus_id in
  201. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  202. #{item}
  203. </foreach>
  204. </sql>
  205. <!-- 查询表t_mst_customer,(条件查询+分页)列表 -->
  206. <select id="selectByCond" resultMap="BaseResultMapResponse">
  207. SELECT
  208. <include refid="Base_Column_List_Response"/>,
  209. tmd.data_value AS "cusFromName",
  210. tmsc.channel_name AS "channelName",
  211. tmo.org_name AS "orgName",
  212. tms.staff_name AS "staffName",
  213. tms2.staff_name AS "reportStaffName",
  214. tdk.kind_name_i18n ->> #{i18n} AS "saleStatusName",
  215. tcf.next_follow_time,
  216. tma.receivable_residue,
  217. tma.receipt_residue
  218. FROM dkic_b.t_mst_customer tmc
  219. 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
  220. LEFT JOIN dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tmc.cus_from
  221. LEFT JOIN dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
  222. LEFT JOIN dkic_b.t_mst_org tmo on tmo.org_id = tmc.org_id
  223. LEFT JOIN dkic_b.t_mst_staff tms on tms.staff_id = tmc.staff_id
  224. LEFT JOIN dkic_b.t_mst_staff tms2 on tms2.staff_id = tmc.report_staff
  225. LEFT JOIN sys.t_data_kind tdk on tdk.kind_code = tmc.sale_status
  226. LEFT JOIN dkic_b.t_mac_account tma on tma.object_id = tmc.cus_id
  227. <include refid="Condition"/>
  228. order by tmc.cus_id DESC
  229. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  230. limit #{end} offset #{start}
  231. </if>
  232. </select>
  233. <!-- 查询表t_mst_customer,(条件查询)个数 -->
  234. <select id="countByCond" resultType="Long">
  235. SELECT
  236. count(1)
  237. FROM dkic_b.t_mst_customer tmc
  238. <include refid="Condition"/>
  239. </select>
  240. <!-- 根据主键查询表t_mst_customer的一行数据 -->
  241. <select id="selectById" resultMap="BaseResultMap">
  242. SELECT
  243. <include refid="Base_Column_List"/>
  244. FROM dkic_b.t_mst_customer
  245. WHERE cus_id = #{id}::uuid
  246. </select>
  247. <!-- 根据主键查询表t_mst_customer的一行数据 -->
  248. <select id="selectCustomerById" resultMap="BaseResultMapResponse">
  249. SELECT
  250. <include refid="Base_Column_List_Response"/>
  251. ,tma.receivable_residue
  252. ,tma.receipt_residue
  253. FROM dkic_b.t_mst_customer tmc
  254. LEFT JOIN dkic_b.t_mac_account tma on tma.object_id = tmc.cus_id
  255. WHERE tmc.cus_id = #{id}::uuid
  256. </select>
  257. <select id="selectByIdRespone" resultMap="BaseResultMapResponse">
  258. SELECT
  259. <include refid="Base_Column_List_Response"/>,
  260. tmd.data_value AS "cusFromName",
  261. tmsc.channel_name AS "channelName",
  262. tmo.org_name AS "orgName",
  263. tms.staff_name AS "staffName",
  264. tms2.staff_name AS "reportStaffName",
  265. tdk.kind_name_i18n ->> #{i18n} AS "saleStatusName",
  266. (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
  267. tpo.flg_valid) AS "orderQuantity",
  268. (SELECT sum(tpo.fact_amt) FROM dkic_b.t_psi_order tpo WHERE tpo.cus_id = tmc.cus_id AND tpo.cp_id = tmc.cp_id
  269. AND tpo.flg_valid) AS "orderAmount",
  270. (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
  271. tpob.flg_valid AND tpob.out_type = '出库类型-销售出库' AND tpob.out_status = '出库状态-已出库') AS
  272. "outboundQuantity",
  273. (SELECT sum(tpob.fact_amt) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  274. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销售出库' AND tpob.out_status = '出库状态-已出库') AS
  275. "outboundAmount",
  276. (SELECT COUNT(1) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  277. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销售出库' AND tpob.out_status = '出库状态-出库中') AS
  278. "outboundNotQuantity",
  279. (SELECT sum(tpob.outing_amt) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  280. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销售出库' AND tpob.out_status = '出库状态-出库中') AS
  281. "outboundNotAmount",
  282. (SELECT COUNT(1) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  283. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销退回库') AS
  284. "outboundReturnQuantity",
  285. (SELECT sum(CASE WHEN tpob.out_status = '出库状态-出库中' THEN tpob.outing_amt ELSE tpob.out_amt END) FROM dkic_b.t_psi_outbound tpob WHERE tpob.cus_id = tmc.cus_id AND tpob.cp_id =
  286. tmc.cp_id AND tpob.flg_valid AND tpob.out_type = '出库类型-销退回库') AS
  287. "outboundReturnAmount",
  288. tma.receipt,
  289. tma.receivable,
  290. tma.receivable_residue,
  291. tma.receipt_residue,
  292. tma.receipt_lock,
  293. tmor.sum_amt_receivable AS "startAmount"
  294. -- concat(tms3.staff_name) AS "followStaffsName"
  295. FROM dkic_b.t_mst_customer tmc
  296. LEFT JOIN dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tmc.cus_from
  297. LEFT JOIN dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
  298. LEFT JOIN dkic_b.t_mst_org tmo on tmo.org_id = tmc.org_id
  299. LEFT JOIN dkic_b.t_mst_staff tms on tms.staff_id = tmc.staff_id
  300. LEFT JOIN dkic_b.t_mst_staff tms2 on tms2.staff_id = tmc.report_staff
  301. -- LEFT JOIN dkic_b.t_mst_staff tms3 on tms3.staff_id = Any(tmc.follow_staffs)
  302. LEFT JOIN sys.t_data_kind tdk on tdk.kind_code = tmc.sale_status
  303. LEFT JOIN dkic_b.t_mac_account tma on tma.object_id = tmc.cus_id
  304. LEFT JOIN dkic_b.t_mac_other_receivable tmor on tmor.object_id = tmc.cus_id and tmor.business_type = 0
  305. WHERE tmc.cus_id = #{id}::uuid
  306. </select>
  307. <!-- 根据主键锁定表t_mst_customer的一行数据 -->
  308. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  309. SELECT
  310. <include refid="Base_Column_List"/>
  311. FROM dkic_b.t_mst_customer
  312. WHERE cus_id = #{id} ::uuid
  313. for update
  314. </select>
  315. <!-- 根据主键锁定表t_mst_customer的多行数据 -->
  316. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  317. SELECT
  318. <include refid="Base_Column_List"/>
  319. FROM dkic_b.t_mst_customer
  320. <include refid="idsForeach"/>
  321. for update
  322. </select>
  323. <insert id="insertBatch">
  324. insert into dkic_b.t_mst_customer
  325. (
  326. <trim suffixOverrides=",">
  327. cus_code,
  328. cus_name,
  329. cus_phone,
  330. address_area,
  331. address_name,
  332. address_no,
  333. address_gcj02,
  334. address_full,
  335. contact_name,
  336. contact_phone,
  337. cus_from,
  338. channel_id,
  339. org_id,
  340. staff_id,
  341. report_staff,
  342. report_time,
  343. sale_status,
  344. remarks,
  345. follow_staffs,
  346. last_follow_staff,
  347. last_follow_id,
  348. last_follow_status,
  349. last_follow_time,
  350. follow_count,
  351. cp_id,
  352. op_app_code,
  353. </trim>
  354. )
  355. values
  356. <foreach collection="list" index="index" item="item" separator=",">
  357. (
  358. <trim suffixOverrides=",">
  359. #{item.cusCode},
  360. #{item.cusName},
  361. #{item.cusPhone},
  362. #{item.addressArea},
  363. #{item.addressName},
  364. #{item.addressNo},
  365. #{item.addressGcj02},
  366. #{item.addressFull},
  367. #{item.contactName},
  368. #{item.contactPhone},
  369. #{item.cusFrom}::uuid,
  370. #{item.channelId}::uuid,
  371. #{item.orgId}::uuid,
  372. #{item.staffId}::uuid,
  373. #{item.reportStaff}::uuid,
  374. #{item.reportTime},
  375. #{item.saleStatus},
  376. #{item.remarks},
  377. #{item.followStaffs},
  378. #{item.lastFollowStaff}::uuid,
  379. #{item.lastFollowId}::uuid,
  380. #{item.lastFollowStatus},
  381. #{item.lastFollowTime},
  382. #{item.followCount},
  383. #{item.cpId},
  384. #{item.opAppCode},
  385. </trim>
  386. )
  387. </foreach>
  388. </insert>
  389. <!-- 查询跟进人-->
  390. <select id="selectFollowStaffs" resultType="Object">
  391. SELECT follow_staffs
  392. from dkic_b.t_mst_customer
  393. WHERE cus_id = #{id}::uuid
  394. </select>
  395. <!-- 公海的客户需要把跟进人清空-->
  396. <update id="updateFollowStaffs">
  397. update dkic_b.t_mst_customer
  398. set
  399. follow_staffs = #{followStaffs,typeHandler=UuidListTypeHandler}
  400. where cus_id = #{cusId}::uuid
  401. </update>
  402. </mapper>