InventoryBatchMapper.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.ivt.InventoryBatchMapper">
  4. <!-- 通用设置 -->
  5. <!-- 通用查询列 -->
  6. <sql id="Base_Column_List">
  7. batch_id, inv_id, from_id, from_item_id, wh_id, sku_id, non_std_code,
  8. sys.f_remove_zero(inv_qty) as inv_qty, cost_price, cost_amt, acc_date, remarks, flg_valid, cp_id, op_create_time,
  9. op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
  10. </sql>
  11. <!-- 通用查询列Response -->
  12. <sql id="Base_Column_List_Response">
  13. tpib.batch_id, tpib.inv_id, tpib.from_id, tpib.from_item_id,
  14. tpib.wh_id, tpib.sku_id, tpib.non_std_code, sys.f_remove_zero(tpib.inv_qty) as inv_qty,
  15. tpib.cost_price, tpib.cost_amt, tpib.acc_date, tpib.remarks,
  16. tpib.flg_valid, tpib.cp_id
  17. </sql>
  18. <!-- 通用查询映射结果 -->
  19. <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.InventoryBatch">
  20. <id column="batch_id" property="batchId"/>
  21. <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
  22. <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
  23. <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
  24. <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
  25. <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
  26. <result column="non_std_code" property="nonStdCode"/>
  27. <result column="inv_qty" property="invQty"/>
  28. <result column="cost_price" property="costPrice"/>
  29. <result column="cost_amt" property="costAmt"/>
  30. <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
  31. <result column="remarks" property="remarks"/>
  32. <result column="flg_valid" property="flgValid"/>
  33. <result column="cp_id" property="cpId"/>
  34. <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
  35. <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
  36. <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
  37. <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
  38. <result column="op_app_code" property="opAppCode"/>
  39. <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
  40. <result column="op_db_user" property="opDbUser"/>
  41. </resultMap>
  42. <!-- 通用查询映射结果Response -->
  43. <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.InventoryBatchResponse">
  44. <id column="batch_id" property="batchId"/>
  45. <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
  46. <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
  47. <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
  48. <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
  49. <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
  50. <result column="non_std_code" property="nonStdCode"/>
  51. <result column="inv_qty" property="invQty"/>
  52. <result column="cost_price" property="costPrice"/>
  53. <result column="cost_amt" property="costAmt"/>
  54. <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
  55. <result column="remarks" property="remarks"/>
  56. <result column="flg_valid" property="flgValid"/>
  57. <result column="cp_id" property="cpId"/>
  58. <result column="sku_code" property="skuCode"/>
  59. <result column="sku_name" property="skuName"/>
  60. <result column="sku_model" property="skuModel"/>
  61. <result column="brand_name" property="brandName"/>
  62. <result column="short_name" property="shortName"/>
  63. <result column="wh_name" property="whName"/>
  64. <result column="from_no" property="fromNo"/>
  65. <result column="from_item_no" property="fromItemNo"/>
  66. </resultMap>
  67. <!-- 通用条件列 -->
  68. <sql id="Condition">
  69. <where>
  70. <if test="invId != null and invId != ''">
  71. AND inv_id = #{invId}::uuid
  72. </if>
  73. <if test="fromId != null and fromId != ''">
  74. AND from_id = #{fromId}
  75. </if>
  76. <if test="fromItemId != null and fromItemId != ''">
  77. AND tpib.from_item_id = #{fromItemId}
  78. </if>
  79. <if test="whId != null and whId != ''">
  80. AND tpib.wh_id = #{whId}
  81. </if>
  82. <if test="skuId != null and skuId != ''">
  83. AND tpib.sku_id = #{skuId}
  84. </if>
  85. <if test="nonStdCode != null and nonStdCode != ''">
  86. AND tpib.non_std_code = #{nonStdCode}
  87. </if>
  88. <if test="invQty != null">
  89. AND tpib.inv_qty = #{invQty}
  90. </if>
  91. <if test="costPrice != null">
  92. AND tpib.cost_price = #{costPrice}
  93. </if>
  94. <if test="costAmt != null">
  95. AND tpib.cost_amt = #{costAmt}
  96. </if>
  97. <if test="accDate != null">
  98. AND tpib.acc_date = #{accDate}
  99. </if>
  100. <if test="flgValid != null">
  101. AND tpib.flg_valid = #{flgValid}
  102. </if>
  103. <if test="cpId != null">
  104. AND tpib.cp_id = #{cpId}
  105. </if>
  106. </where>
  107. </sql>
  108. <sql id="idsForeach">
  109. <!-- 根据主键batchId批量操作 -->
  110. WHERE batch_id in
  111. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  112. #{item}
  113. </foreach>
  114. </sql>
  115. <!-- 查询表dkic_b.t_psi_inventory_batch,(条件查询+分页)列表 -->
  116. <select id="selectByCond" resultMap="BaseResultMapResponse">
  117. SELECT
  118. <include refid="Base_Column_List_Response"/>
  119. ,tmgs.sku_code
  120. ,tmgs.sku_model
  121. ,tmgs.sku_name
  122. ,tmgs.sku_spec
  123. ,tmgb.brand_name
  124. ,tmgb.short_name
  125. ,tmw.wh_name
  126. FROM dkic_b.t_psi_inventory_batch tpib
  127. <include refid="Join_Table"/>
  128. <include refid="Condition"/>
  129. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  130. limit #{end} offset #{start}
  131. </if>
  132. </select>
  133. <!-- 查询表dkic_b.t_psi_inventory_batch,(条件查询)个数 -->
  134. <select id="countByCond" resultType="Long">
  135. SELECT
  136. count(1)
  137. FROM dkic_b.t_psi_inventory_batch tpib
  138. <include refid="Join_Table"/>
  139. <include refid="Condition"/>
  140. </select>
  141. <sql id="Join_Table">
  142. left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpib.sku_id
  143. left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpib.wh_id
  144. left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
  145. </sql>
  146. <!-- 根据主键查询表dkic_b.t_psi_inventory_batch的一行数据 -->
  147. <select id="selectById" resultMap="BaseResultMapResponse">
  148. SELECT
  149. <include refid="Base_Column_List_Response"/>
  150. FROM dkic_b.t_psi_inventory_batch tpib
  151. WHERE tpib.batch_id = #{id}::uuid
  152. </select>
  153. <!-- 根据主键锁定表dkic_b.t_psi_inventory_batch的一行数据 -->
  154. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  155. SELECT
  156. <include refid="Base_Column_List"/>
  157. FROM dkic_b.t_psi_inventory_batch
  158. WHERE batch_id = #{batchId}
  159. for update
  160. </select>
  161. <!-- 根据主键锁定表dkic_b.t_psi_inventory_batch的多行数据 -->
  162. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  163. SELECT
  164. <include refid="Base_Column_List"/>
  165. FROM dkic_b.t_psi_inventory_batch
  166. <include refid="idsForeach"/>
  167. for update
  168. </select>
  169. <insert id="insertBatch">
  170. insert into dkic_b.t_psi_inventory_batch
  171. (
  172. <trim suffixOverrides=",">
  173. inv_id,
  174. from_id,
  175. from_item_id,
  176. wh_id,
  177. sku_id,
  178. non_std_code,
  179. inv_qty,
  180. cost_price,
  181. cost_amt,
  182. acc_date,
  183. remarks,
  184. cp_id,
  185. op_app_code,
  186. </trim>
  187. )
  188. values
  189. <foreach collection="list" index="index" item="item" separator=",">
  190. (
  191. <trim suffixOverrides=",">
  192. #{item.invId}::uuid,
  193. #{item.fromId}::uuid,
  194. #{item.fromItemId}::uuid,
  195. #{item.whId}::uuid,
  196. #{item.skuId}::uuid,
  197. #{item.nonStdCode},
  198. #{item.invQty},
  199. #{item.costPrice},
  200. #{item.costAmt},
  201. #{item.accDate},
  202. #{item.remarks},
  203. #{item.cpId},
  204. #{item.opAppCode},
  205. </trim>
  206. )
  207. </foreach>
  208. </insert>
  209. <!-- 查询库存批次明细 时间正序 目前用于出库减库存 -->
  210. <select id="selectAmountAndQuantity" resultMap="BaseResultMapResponse">
  211. SELECT
  212. <include refid="Base_Column_List_Response"/>
  213. FROM dkic_b.t_psi_inventory_batch tpib
  214. <where>
  215. tpib.inv_qty > 0 and tpib.flg_valid
  216. <if test="invId != null and invId != ''">
  217. AND tpib.inv_id = #{invId,typeHandler=UuidTypeHandler}
  218. </if>
  219. </where>
  220. order by tpib.batch_id asc
  221. </select>
  222. <!--修改库存数量金额-->
  223. <update id="updateAmountAndQuantity" parameterType="com.dk.mdm.model.vo.ivt.InventoryBatchVO">
  224. update dkic_b.t_psi_inventory_batch
  225. <set>
  226. <if test="zeroFlag">
  227. cost_amt = 0,
  228. inv_qty = 0,
  229. </if>
  230. <if test="!zeroFlag">
  231. cost_amt = cost_amt - #{costAmt,jdbcType=NUMERIC},
  232. inv_qty = inv_qty - #{invQty,jdbcType=NUMERIC},
  233. </if>
  234. </set>
  235. where batch_id =#{batchId,typeHandler=UuidTypeHandler}
  236. and inv_qty - #{invQty,jdbcType=NUMERIC}>=0
  237. and cost_amt - #{costAmt,jdbcType=NUMERIC}>=0
  238. </update>
  239. <!--删除数据-->
  240. <delete id="deleteData" parameterType="com.dk.mdm.model.vo.ivt.InventoryBatchVO">
  241. DELETE
  242. FROM dkic_b.t_psi_inventory_batch
  243. where from_id = #{fromId,typeHandler=UuidTypeHandler}
  244. and from_item_id = #{fromItemId,typeHandler=UuidTypeHandler}
  245. </delete>
  246. <!--根据来源信息查询-->
  247. <select id="selectByFromCondition" resultMap="BaseResultMapResponse">
  248. SELECT
  249. <include refid="Base_Column_List_Response"/>
  250. FROM dkic_b.t_psi_inventory_batch tpib
  251. WHERE tpib.from_id = #{fromId}::uuid
  252. and tpib.from_item_id = #{fromItemId}::uuid
  253. </select>
  254. <!-- 修改数量金额直接赋值-->
  255. <update id="updateAmtAndQty" parameterType="com.dk.mdm.model.pojo.ivt.InventoryBatch">
  256. update dkic_b.t_psi_inventory_batch
  257. set cost_amt = #{costAmt,jdbcType=NUMERIC},
  258. inv_qty = #{invQty,jdbcType=NUMERIC}
  259. where batch_id = #{batchId,typeHandler=UuidTypeHandler}
  260. </update>
  261. <!--入库成本核对查询存货批次明细-->
  262. <select id="selectCostCondition" resultMap="BaseResultMapResponse">
  263. SELECT
  264. <include refid="Base_Column_List_Response"/>
  265. FROM dkic_b.t_psi_inventory_batch tpib
  266. WHERE tpib.from_item_id = #{fromItemId}::uuid
  267. AND tpib.flg_valid
  268. </select>
  269. <!-- 修改存货批次明细(入库成本核对)-->
  270. <update id="updateInventoryBatchCost" parameterType="com.dk.mdm.model.pojo.ivt.InventoryBatch">
  271. update dkic_b.t_psi_inventory_batch
  272. set cost_amt = #{costAmt,jdbcType=NUMERIC},
  273. cost_price = #{costPrice,jdbcType=NUMERIC}
  274. where batch_id = #{batchId,typeHandler=UuidTypeHandler}
  275. </update>
  276. </mapper>