AccountItemMapper.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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.mac.AccountItemMapper">
  4. <!-- 通用设置 -->
  5. <!-- 通用查询列 -->
  6. <sql id="Base_Column_List">
  7. item_id, acc_item_type, object_id, org_id, staff_id, biznis_type, biznis_id, biznis_no, acc_date, rec_status, amt_rec, amt_pay, amt_should, amt_handle, amt_waive, amt_residue, flg_lock, remarks, make_staff, make_time, flg_valid, cp_id
  8. </sql>
  9. <!-- 通用查询映射结果 -->
  10. <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mac.AccountItem">
  11. <id column="item_id" property="itemId"/>
  12. <result column="acc_item_type" property="accItemType"/>
  13. <result column="object_id" property="objectId" typeHandler="UuidTypeHandler"/>
  14. <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
  15. <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
  16. <result column="biznis_type" property="biznisType"/>
  17. <result column="biznis_id" property="biznisId" typeHandler="UuidTypeHandler"/>
  18. <result column="biznis_no" property="biznisNo"/>
  19. <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
  20. <result column="rec_status" property="recStatus"/>
  21. <result column="amt_rec" property="amtRec"/>
  22. <result column="amt_pay" property="amtPay"/>
  23. <result column="amt_should" property="amtShould"/>
  24. <result column="amt_handle" property="amtHandle"/>
  25. <result column="amt_waive" property="amtWaive"/>
  26. <result column="amt_residue" property="amtResidue"/>
  27. <result column="flg_lock" property="flgLock"/>
  28. <result column="remarks" property="remarks"/>
  29. <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
  30. <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
  31. <result column="flg_valid" property="flgValid"/>
  32. <result column="cp_id" property="cpId"/>
  33. </resultMap>
  34. <!-- 通用查询映射结果 -->
  35. <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.mac.AccountItemResponse">
  36. <id column="item_id" property="itemId"/>
  37. <result column="acc_item_type" property="accItemType"/>
  38. <result column="object_id" property="objectId" typeHandler="UuidTypeHandler"/>
  39. <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
  40. <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
  41. <result column="biznis_type" property="biznisType"/>
  42. <result column="biznis_id" property="biznisId" typeHandler="UuidTypeHandler"/>
  43. <result column="biznis_no" property="biznisNo"/>
  44. <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
  45. <result column="rec_status" property="recStatus"/>
  46. <result column="amt_rec" property="amtRec"/>
  47. <result column="amt_pay" property="amtPay"/>
  48. <result column="amt_should" property="amtShould"/>
  49. <result column="amt_handle" property="amtHandle"/>
  50. <result column="amt_waive" property="amtWaive"/>
  51. <result column="amt_residue" property="amtResidue"/>
  52. <result column="flg_lock" property="flgLock"/>
  53. <result column="remarks" property="remarks"/>
  54. <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
  55. <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
  56. <result column="flg_valid" property="flgValid"/>
  57. <result column="cp_id" property="cpId"/>
  58. </resultMap>
  59. <!-- 通用条件列 -->
  60. <sql id="Condition">
  61. <if test="accItemType != null and accItemType != ''">
  62. AND t.acc_item_type = #{accItemType}
  63. </if>
  64. <if test="objectId != null and objectId != ''">
  65. AND t.object_id = #{objectId}::uuid
  66. </if>
  67. <if test="orgId != null and orgId != ''">
  68. AND t.org_id = #{orgId}::uuid
  69. </if>
  70. <if test="staffId != null and staffId != ''">
  71. AND t.staff_id = #{staffId}::uuid
  72. </if>
  73. <if test="biznisType != null and biznisType != ''">
  74. AND t.biznis_type = #{biznisType}
  75. </if>
  76. <if test="biznisId != null and biznisId != ''">
  77. AND t.biznis_id = #{biznisId}
  78. </if>
  79. <if test="biznisNo != null and biznisNo != ''">
  80. AND t.biznis_no = #{biznisNo}
  81. </if>
  82. <if test="accDate != null">
  83. AND t.acc_date = #{accDate}
  84. </if>
  85. <if test="recStatus != null and recStatus != ''">
  86. AND t.rec_status = #{recStatus}
  87. </if>
  88. <if test="amtRec != null">
  89. AND t.amt_rec = #{amtRec}
  90. </if>
  91. <if test="amtPay != null">
  92. AND t.amt_pay = #{amtPay}
  93. </if>
  94. <if test="amtShould != null">
  95. AND t.amt_should = #{amtShould}
  96. </if>
  97. <if test="amtHandle != null">
  98. AND t.amt_handle = #{amtHandle}
  99. </if>
  100. <if test="amtWaive != null">
  101. AND t.amt_waive = #{amtWaive}
  102. </if>
  103. <if test="amtResidueFlg != null">
  104. AND t.amt_residue > 0
  105. </if>
  106. <if test="amtResidueZeroFlg != null">
  107. AND t.amt_residue != 0
  108. </if>
  109. <if test="amtResidue != null">
  110. AND t.amt_residue = #{amtResidue}
  111. </if>
  112. <if test="flgLock != null">
  113. AND t.flg_lock = #{flgLock}
  114. </if>
  115. <if test="remarks != null and remarks != ''">
  116. AND t.remarks = #{remarks}
  117. </if>
  118. <if test="makeStaff != null and makeStaff != ''">
  119. AND t.make_staff = #{makeStaff}
  120. </if>
  121. <if test="makeTime != null">
  122. AND t.make_time = #{makeTime}
  123. </if>
  124. <if test="makeTimeStart != null and makeTimeEnd != null">
  125. AND t.make_time &gt;= #{makeTimeStart}::timestamp with time zone
  126. AND t.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
  127. </if>
  128. <if test="flgValid != null">
  129. AND t.flg_valid = #{flgValid}
  130. </if>
  131. <if test="cpId != null">
  132. AND t.cp_id = #{cpId}
  133. </if>
  134. <!-- 如果是退货,只差剩余应收小于0的 -->
  135. <if test="formType == 'refund'">
  136. AND t.amt_residue <![CDATA[ < ]]> 0
  137. </if>
  138. <if test="orderId != null">
  139. AND tpo.from_id = #{orderId}::uuid
  140. </if>
  141. <if test="outTypeList != null and outTypeList.size()>0">
  142. AND tpo.out_type =any(#{outTypeList,typeHandler=StringListTypeHandler})
  143. </if>
  144. <if test="intoTypeList != null and intoTypeList.size()>0">
  145. AND tpi.into_type =any(#{intoTypeList,typeHandler=StringListTypeHandler})
  146. </if>
  147. </sql>
  148. <sql id="idsForeach">
  149. <!-- 根据主键itemId批量操作 -->
  150. WHERE item_id in
  151. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  152. #{item}
  153. </foreach>
  154. </sql>
  155. <!-- 根据主键查询表t_mac_account_item的一行数据 -->
  156. <select id="selectByCond" resultMap="BaseResultMap">
  157. SELECT
  158. <include refid="Base_Column_List"/>
  159. FROM dkic_b.t_mac_account_item as t
  160. where 1 = 1
  161. <include refid="Condition"/>
  162. </select>
  163. <!-- 查询表t_mac_account_item,(条件查询)个数 -->
  164. <select id="countByCond" resultType="Long">
  165. SELECT
  166. count(1)
  167. FROM dkic_b.t_mac_account_item as t
  168. where 1 = 1
  169. <include refid="Condition"/>
  170. </select>
  171. <!--查询应收和收款汇总(根据制单员权限)-->
  172. <select id="getReceivableAccountSum" resultType="Map">
  173. SELECT SUM
  174. ( CASE WHEN tmai.acc_item_type = '账款类型-应收' THEN tmai.amt_should ELSE 0 END ) AS receivable,
  175. SUM ( CASE WHEN tmai.acc_item_type = '账款类型-收款' THEN tmai.amt_should ELSE 0 END ) AS receipt
  176. FROM
  177. dkic_b.t_mac_account_item tmai
  178. WHERE
  179. tmai.flg_valid
  180. AND tmai.cp_id = #{cpId}
  181. AND (
  182. tmai.make_staff = #{staffId}::uuid
  183. OR EXISTS ( SELECT 1 FROM dkic_b.t_mst_staff_purview tmsp WHERE tmsp.staff_id = tmai.make_staff AND
  184. tmsp.purview_code = 'PU10000003' )
  185. )
  186. <!--开始日期-->
  187. <if test="accDateStart != null">
  188. AND tmai.acc_date &gt;= #{accDateStart,typeHandler=TimestampTypeHandler}
  189. </if>
  190. <!--截止日期-->
  191. <if test="accDateEnd != null">
  192. AND tmai.acc_date &lt;= #{accDateEnd,typeHandler=TimestampTypeHandler}
  193. </if>
  194. </select>
  195. <!-- 查询应收账款明细 -->
  196. <select id="getReceivableAccountItem" resultMap="BaseResultMapResponse">
  197. SELECT t.item_id,
  198. t.item_id as "accItemId",
  199. t.acc_item_type,
  200. t.object_id,
  201. tmc.cus_code as "cusCode",
  202. tmc.cus_name as "cusName",
  203. tmc.cus_phone as "cusPhone",
  204. tmc.address_full as "addressFull",
  205. t.org_id,
  206. tmo.org_name as "orgName",
  207. t.staff_id,
  208. tms.staff_name as "staffName",
  209. t.biznis_type,
  210. t.biznis_id,
  211. t.biznis_no,
  212. t.acc_date,
  213. t.rec_status,
  214. sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
  215. coalesce(sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}), '其他收入') as "biznisTypeName",
  216. t.amt_rec,
  217. t.amt_pay,
  218. t.amt_should,
  219. t.amt_handle,
  220. t.amt_waive,
  221. t.amt_residue as "amtNotHandle",
  222. t.amt_residue as "amtReceivableHandle",
  223. t.amt_residue,
  224. t.remarks,
  225. t.make_staff,
  226. t.make_time,
  227. t.flg_valid,
  228. t.cp_id
  229. FROM dkic_b.t_mac_account_item as t
  230. left join dkic_b.t_psi_outbound as tpo on tpo.out_id = t.biznis_id and t.biznis_type = 't_psi_outbound'
  231. LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpo.out_type
  232. left join dkic_b.t_mac_other_receivable as tmor
  233. on tmor.receivable_id = t.biznis_id and t.biznis_type = 't_mac_other_receivable'
  234. left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
  235. left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
  236. left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
  237. left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
  238. where t.flg_valid
  239. and t.acc_item_type = '账款类型-应收'
  240. and t.amt_residue <![CDATA[ <> ]]> 0
  241. <include refid="Condition"/>
  242. order by t.acc_date
  243. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  244. limit #{end} offset #{start}
  245. </if>
  246. </select>
  247. <!-- 查询应付账款明细 -->
  248. <select id="getPayableAccountItem" resultMap="BaseResultMapResponse">
  249. SELECT t.item_id,
  250. t.item_id as "accItemId",
  251. t.acc_item_type,
  252. t.object_id,
  253. supplier.sup_code as "supCode",
  254. supplier.sup_name as "supName",
  255. t.org_id,
  256. tmo.org_name as "orgName",
  257. t.staff_id,
  258. tms.staff_name as "staffName",
  259. t.biznis_type,
  260. t.biznis_id,
  261. t.biznis_no,
  262. t.acc_date,
  263. t.rec_status,
  264. sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
  265. sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "biznisTypeName",
  266. t.amt_rec,
  267. t.amt_pay,
  268. t.amt_should,
  269. t.amt_handle,
  270. t.amt_waive,
  271. t.amt_residue,
  272. t.amt_residue as "amtNotHandle",
  273. t.amt_residue as "amtPayableHandle",
  274. t.remarks,
  275. t.make_staff,
  276. t.make_time,
  277. t.flg_valid,
  278. t.cp_id
  279. FROM dkic_b.t_mac_account_item as t
  280. left join dkic_b.t_psi_inbound as tpi on tpi.into_id = t.biznis_id and t.biznis_type = 't_psi_inbound'
  281. LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpi.into_type
  282. left join dkic_b.t_mst_supplier supplier on supplier.sup_id = t.object_id
  283. left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
  284. left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
  285. left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
  286. where t.flg_valid
  287. and t.acc_item_type = '账款类型-应付'
  288. and t.amt_residue <![CDATA[<>]]> 0
  289. <include refid="Condition"/>
  290. order by t.acc_date
  291. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  292. limit #{end} offset #{start}
  293. </if>
  294. </select>
  295. <!-- 查询应收账款明细 -->
  296. <select id="getReceivableAccountItemForUpdate" resultMap="BaseResultMapResponse">
  297. SELECT t.item_id,
  298. t.item_id as "accItemId",
  299. t.acc_item_type,
  300. t.object_id,
  301. t.org_id,
  302. t.staff_id,
  303. t.biznis_type,
  304. t.biznis_id,
  305. t.biznis_no,
  306. t.acc_date,
  307. t.rec_status,
  308. t.amt_rec,
  309. t.amt_pay,
  310. t.amt_should,
  311. t.amt_handle,
  312. t.amt_waive,
  313. t.amt_residue as "amtNotHandle",
  314. t.amt_residue as "amtReceivableHandle",
  315. t.amt_residue,
  316. t.remarks,
  317. t.make_staff,
  318. t.make_time,
  319. t.flg_valid,
  320. t.cp_id
  321. FROM dkic_b.t_mac_account_item as t
  322. inner join dkic_b.t_psi_outbound as tpo on tpo.out_id = t.biznis_id and t.biznis_type = 't_psi_outbound'
  323. where t.flg_valid
  324. and t.acc_item_type = '账款类型-应收'
  325. and t.amt_residue <![CDATA[<>]]> 0
  326. AND t.object_id = #{objectId}::uuid
  327. AND tpo.from_id = #{orderId}::uuid
  328. order by t.acc_date
  329. for update
  330. </select>
  331. <!-- 根据主键查询表t_mac_account_item的一行数据 -->
  332. <select id="selectById" resultMap="BaseResultMap">
  333. SELECT
  334. <include refid="Base_Column_List"/>
  335. FROM dkic_b.t_mac_account_item
  336. WHERE item_id = #{id}::uuid
  337. </select>
  338. <!-- 根据主键锁定表t_mac_account_item的一行数据 -->
  339. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  340. SELECT
  341. <include refid="Base_Column_List"/>
  342. FROM dkic_b.t_mac_account_item
  343. WHERE item_id = #{id}::uuid
  344. for update
  345. </select>
  346. <!-- 根据主键锁定表t_mac_account_item的多行数据 -->
  347. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  348. SELECT
  349. <include refid="Base_Column_List"/>
  350. FROM dkic_b.t_mac_account_item
  351. <include refid="idsForeach"/>
  352. for update
  353. </select>
  354. <insert id="insertBatch">
  355. insert into dkic_b.t_mac_account_item
  356. (
  357. <trim suffixOverrides=",">
  358. acc_item_type,
  359. object_id,
  360. org_id,
  361. staff_id,
  362. biznis_type,
  363. biznis_id,
  364. biznis_no,
  365. acc_date,
  366. rec_status,
  367. amt_rec,
  368. amt_pay,
  369. amt_should,
  370. amt_handle,
  371. amt_waive,
  372. amt_residue,
  373. remarks,
  374. make_staff,
  375. make_time,
  376. cp_id,
  377. op_app_code,
  378. </trim>
  379. )
  380. values
  381. <foreach collection="list" index="index" item="item" separator=",">
  382. (
  383. <trim suffixOverrides=",">
  384. #{item.accItemType},
  385. #{item.objectId}::uuid,
  386. #{item.orgId}::uuid,
  387. #{item.staffId}::uuid,
  388. #{item.biznisType},
  389. #{item.biznisId}::uuid,
  390. #{item.biznisNo},
  391. #{item.accDate},
  392. #{item.recStatus},
  393. #{item.amtRec},
  394. #{item.amtPay},
  395. #{item.amtShould},
  396. #{item.amtHandle},
  397. #{item.amtWaive},
  398. #{item.amtResidue},
  399. #{item.remarks},
  400. #{item.makeStaff}::uuid,
  401. #{item.makeTime},
  402. #{item.cpId},
  403. #{item.opAppCode},
  404. </trim>
  405. )
  406. </foreach>
  407. </insert>
  408. <!-- 查询收款总额 -->
  409. <select id="getSumAmtRec" resultType="java.util.Map">
  410. SELECT COALESCE(sum(t.amt_rec), 0) as "sumAmtRec"
  411. FROM dkic_b.t_mac_account_item as t
  412. where t.flg_valid
  413. and t.flg_lock = false
  414. and t.acc_item_type = '账款类型-收款'
  415. and t.object_id = #{id}::uuid
  416. </select>
  417. <!-- 查询收款总额(锁定金额) -->
  418. <select id="getSumAmtRecLock" resultType="java.util.Map">
  419. SELECT COALESCE(sum(t.amt_rec), 0) as "sumAmtRecLock"
  420. FROM dkic_b.t_mac_account_item as t
  421. where t.flg_valid
  422. and t.flg_lock
  423. and t.acc_item_type = '账款类型-收款'
  424. and t.object_id = #{id}::uuid
  425. </select>
  426. <!-- 查询付款总额 -->
  427. <select id="getSumAmtPay" resultType="java.util.Map">
  428. SELECT COALESCE(sum(t.amt_pay), 0) as "sumAmtPay"
  429. FROM dkic_b.t_mac_account_item as t
  430. where t.flg_valid
  431. and t.acc_item_type = '账款类型-付款'
  432. and t.object_id = #{id}::uuid
  433. </select>
  434. <!-- 查询订单未出库金额 -->
  435. <select id="getSumAmtOrder" resultType="java.util.Map">
  436. SELECT COALESCE(sum(tpo.sum_amount - tpo.out_amt), 0) as "sumAmtOrder"
  437. FROM dkic_b.t_psi_order as tpo
  438. where tpo.flg_valid
  439. and tpo.out_status in ('出库状态-出库中', '出库状态-待出库')
  440. and tpo.cus_id = #{id}::uuid
  441. </select>
  442. <!-- 查询采购未入库金额 -->
  443. <select id="getSumAmtPur" resultType="java.util.Map">
  444. SELECT COALESCE(sum(tpp.sum_amount - tpp.into_amt), 0) as "sumAmtPur"
  445. FROM dkic_b.t_psi_purchase as tpp
  446. where tpp.flg_valid
  447. and tpp.into_status in ('入库状态-待入库', '入库状态-入库中')
  448. and tpp.sup_id = #{id}::uuid
  449. </select>
  450. <delete id="deleteById">
  451. DELETE
  452. FROM dkic_b.t_mac_account_item
  453. WHERE item_id = #{id}::uuid;
  454. </delete>
  455. </mapper>