AccountItemMapper.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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="flgValid != null">
  125. AND t.flg_valid = #{flgValid}
  126. </if>
  127. <if test="cpId != null">
  128. AND t.cp_id = #{cpId}
  129. </if>
  130. <!-- 如果是退货,只差剩余应收小于0的 -->
  131. <if test="formType == 'refund'">
  132. AND t.amt_residue <![CDATA[ < ]]> 0
  133. </if>
  134. <if test="orderId != null">
  135. AND tpo.from_id = #{orderId}::uuid
  136. </if>
  137. </sql>
  138. <sql id="idsForeach">
  139. <!-- 根据主键itemId批量操作 -->
  140. WHERE item_id in
  141. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  142. #{item}
  143. </foreach>
  144. </sql>
  145. <!-- 根据主键查询表t_mac_account_item的一行数据 -->
  146. <select id="selectByCond" resultMap="BaseResultMap">
  147. SELECT
  148. <include refid="Base_Column_List"/>
  149. FROM dkic_b.t_mac_account_item as t
  150. where 1 = 1
  151. <include refid="Condition"/>
  152. </select>
  153. <!-- 查询表t_mac_account_item,(条件查询)个数 -->
  154. <select id="countByCond" resultType="Long">
  155. SELECT
  156. count(1)
  157. FROM dkic_b.t_mac_account_item as t
  158. where 1 = 1
  159. <include refid="Condition"/>
  160. </select>
  161. <!--查询应收和收款汇总(根据制单员权限)-->
  162. <select id="getReceivableAccountSum" resultType="Map">
  163. SELECT SUM
  164. ( CASE WHEN tmai.acc_item_type = '账款类型-应收' THEN tmai.amt_should ELSE 0 END ) AS receivable,
  165. SUM ( CASE WHEN tmai.acc_item_type = '账款类型-收款' THEN tmai.amt_should ELSE 0 END ) AS receipt
  166. FROM
  167. dkic_b.t_mac_account_item tmai
  168. WHERE
  169. tmai.flg_valid
  170. AND tmai.cp_id = #{cpId}
  171. AND (
  172. tmai.make_staff = #{staffId}::uuid
  173. OR EXISTS ( SELECT 1 FROM dkic_b.t_mst_staff_purview tmsp WHERE tmsp.staff_id = tmai.make_staff AND
  174. tmsp.purview_code = 'PU10000003' )
  175. )
  176. <!--开始日期-->
  177. <if test="accDateStart != null">
  178. AND tmai.acc_date &gt;= #{accDateStart,typeHandler=TimestampTypeHandler}
  179. </if>
  180. <!--截止日期-->
  181. <if test="accDateEnd != null">
  182. AND tmai.acc_date &lt;= #{accDateEnd,typeHandler=TimestampTypeHandler}
  183. </if>
  184. </select>
  185. <!-- 查询应收账款明细 -->
  186. <select id="getReceivableAccountItem" resultMap="BaseResultMapResponse">
  187. SELECT t.item_id,
  188. t.item_id as "accItemId",
  189. t.acc_item_type,
  190. t.object_id,
  191. tmc.cus_code as "cusCode",
  192. tmc.cus_name as "cusName",
  193. tmc.cus_phone as "cusPhone",
  194. tmc.address_full as "addressFull",
  195. t.org_id,
  196. tmo.org_name as "orgName",
  197. t.staff_id,
  198. tms.staff_name as "staffName",
  199. t.biznis_type,
  200. t.biznis_id,
  201. t.biznis_no,
  202. t.acc_date,
  203. t.rec_status,
  204. sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
  205. coalesce(sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}), '其他收入') as "biznisTypeName",
  206. t.amt_rec,
  207. t.amt_pay,
  208. t.amt_should,
  209. t.amt_handle,
  210. t.amt_waive,
  211. t.amt_residue as "amtNotHandle",
  212. t.amt_residue as "amtReceivableHandle",
  213. t.amt_residue,
  214. t.remarks,
  215. t.make_staff,
  216. t.make_time,
  217. t.flg_valid,
  218. t.cp_id
  219. FROM dkic_b.t_mac_account_item as t
  220. left join dkic_b.t_psi_outbound as tpo on tpo.out_id = t.biznis_id and t.biznis_type = 't_psi_outbound'
  221. LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpo.out_type
  222. left join dkic_b.t_mac_other_receivable as tmor
  223. on tmor.receivable_id = t.biznis_id and t.biznis_type = 't_mac_other_receivable'
  224. left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.object_id
  225. left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
  226. left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
  227. left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
  228. where t.flg_valid
  229. and t.acc_item_type = '账款类型-应收'
  230. and t.amt_residue <![CDATA[ <> ]]> 0
  231. <include refid="Condition"/>
  232. order by t.acc_date
  233. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  234. limit #{end} offset #{start}
  235. </if>
  236. </select>
  237. <!-- 查询应付账款明细 -->
  238. <select id="getPayableAccountItem" resultMap="BaseResultMapResponse">
  239. SELECT t.item_id,
  240. t.item_id as "accItemId",
  241. t.acc_item_type,
  242. t.object_id,
  243. supplier.sup_code as "supCode",
  244. supplier.sup_name as "supName",
  245. t.org_id,
  246. tmo.org_name as "orgName",
  247. t.staff_id,
  248. tms.staff_name as "staffName",
  249. t.biznis_type,
  250. t.biznis_id,
  251. t.biznis_no,
  252. t.acc_date,
  253. t.rec_status,
  254. sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "recStatusName",
  255. sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "biznisTypeName",
  256. t.amt_rec,
  257. t.amt_pay,
  258. t.amt_should,
  259. t.amt_handle,
  260. t.amt_waive,
  261. t.amt_residue,
  262. t.amt_residue as "amtNotHandle",
  263. t.amt_residue as "amtPayableHandle",
  264. t.remarks,
  265. t.make_staff,
  266. t.make_time,
  267. t.flg_valid,
  268. t.cp_id
  269. FROM dkic_b.t_mac_account_item as t
  270. left join dkic_b.t_psi_inbound as tpi on tpi.into_id = t.biznis_id and t.biznis_type = 't_psi_inbound'
  271. LEFT JOIN sys.t_data_kind tdk2 ON tdk2.kind_code = tpi.into_type
  272. left join dkic_b.t_mst_supplier supplier on supplier.sup_id = t.object_id
  273. left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
  274. left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
  275. left join sys.t_data_kind as tdk1 on t.rec_status = tdk1.kind_code
  276. where t.flg_valid
  277. and t.acc_item_type = '账款类型-应付'
  278. and t.amt_residue <![CDATA[ <> ]]> 0
  279. <include refid="Condition"/>
  280. order by t.acc_date
  281. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  282. limit #{end} offset #{start}
  283. </if>
  284. </select>
  285. <!-- 根据主键查询表t_mac_account_item的一行数据 -->
  286. <select id="selectById" resultMap="BaseResultMap">
  287. SELECT
  288. <include refid="Base_Column_List"/>
  289. FROM dkic_b.t_mac_account_item
  290. WHERE item_id = #{itemId}::uuid
  291. </select>
  292. <!-- 根据主键锁定表t_mac_account_item的一行数据 -->
  293. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  294. SELECT
  295. <include refid="Base_Column_List"/>
  296. FROM dkic_b.t_mac_account_item
  297. WHERE item_id = #{id}::uuid
  298. for update
  299. </select>
  300. <!-- 根据主键锁定表t_mac_account_item的多行数据 -->
  301. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  302. SELECT
  303. <include refid="Base_Column_List"/>
  304. FROM dkic_b.t_mac_account_item
  305. <include refid="idsForeach"/>
  306. for update
  307. </select>
  308. <insert id="insertBatch">
  309. insert into dkic_b.t_mac_account_item
  310. (
  311. <trim suffixOverrides=",">
  312. acc_item_type,
  313. object_id,
  314. org_id,
  315. staff_id,
  316. biznis_type,
  317. biznis_id,
  318. biznis_no,
  319. acc_date,
  320. rec_status,
  321. amt_rec,
  322. amt_pay,
  323. amt_should,
  324. amt_handle,
  325. amt_waive,
  326. amt_residue,
  327. remarks,
  328. make_staff,
  329. make_time,
  330. cp_id,
  331. op_app_code,
  332. </trim>
  333. )
  334. values
  335. <foreach collection="list" index="index" item="item" separator=",">
  336. (
  337. <trim suffixOverrides=",">
  338. #{item.accItemType},
  339. #{item.objectId}::uuid,
  340. #{item.orgId}::uuid,
  341. #{item.staffId}::uuid,
  342. #{item.biznisType},
  343. #{item.biznisId}::uuid,
  344. #{item.biznisNo},
  345. #{item.accDate},
  346. #{item.recStatus},
  347. #{item.amtRec},
  348. #{item.amtPay},
  349. #{item.amtShould},
  350. #{item.amtHandle},
  351. #{item.amtWaive},
  352. #{item.amtResidue},
  353. #{item.remarks},
  354. #{item.makeStaff}::uuid,
  355. #{item.makeTime},
  356. #{item.cpId},
  357. #{item.opAppCode},
  358. </trim>
  359. )
  360. </foreach>
  361. </insert>
  362. <!-- 查询收款总额 -->
  363. <select id="getSumAmtRec" resultType="java.util.Map">
  364. SELECT COALESCE(sum(t.amt_rec), 0) as "sumAmtRec"
  365. FROM dkic_b.t_mac_account_item as t
  366. where t.flg_valid
  367. and t.flg_lock = false
  368. and t.acc_item_type = '账款类型-收款'
  369. and t.object_id = #{id}::uuid
  370. </select>
  371. <!-- 查询收款总额(锁定金额) -->
  372. <select id="getSumAmtRecLock" resultType="java.util.Map">
  373. SELECT COALESCE(sum(t.amt_rec), 0) as "sumAmtRecLock"
  374. FROM dkic_b.t_mac_account_item as t
  375. where t.flg_valid
  376. and t.flg_lock
  377. and t.acc_item_type = '账款类型-收款'
  378. and t.object_id = #{id}::uuid
  379. </select>
  380. <!-- 查询付款总额 -->
  381. <select id="getSumAmtPay" resultType="java.util.Map">
  382. SELECT COALESCE(sum(t.amt_pay), 0) as "sumAmtPay"
  383. FROM dkic_b.t_mac_account_item as t
  384. where t.flg_valid
  385. and t.acc_item_type = '账款类型-付款'
  386. and t.object_id = #{id}::uuid
  387. </select>
  388. <!-- 查询订单未出库金额 -->
  389. <select id="getSumAmtOrder" resultType="java.util.Map">
  390. SELECT COALESCE(sum(tpo.sum_amount - tpo.out_amt), 0) as "sumAmtOrder"
  391. FROM dkic_b.t_psi_order as tpo
  392. where tpo.flg_valid
  393. and tpo.out_status in('出库状态-出库中','出库状态-待出库')
  394. and tpo.cus_id = #{id}::uuid
  395. </select>
  396. <!-- 查询采购未入库金额 -->
  397. <select id="getSumAmtPur" resultType="java.util.Map">
  398. SELECT COALESCE(sum(tpp.sum_amount - tpp.into_amt), 0) as "sumAmtPur"
  399. FROM dkic_b.t_psi_purchase as tpp
  400. where tpp.flg_valid
  401. and tpp.into_status in('入库状态-待入库','入库状态-入库中')
  402. and tpp.sup_id = #{id}::uuid
  403. </select>
  404. <delete id="deleteById">
  405. DELETE
  406. FROM dkic_b.t_mac_account_item
  407. WHERE item_id = #{id}::uuid;
  408. </delete>
  409. </mapper>