InventoryMapper.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.InventoryMapper">
  4. <!-- 通用设置 -->
  5. <!-- 通用查询列 -->
  6. <sql id="Base_Column_List">
  7. inv_id, wh_id, sku_id, non_std_code, sys.f_remove_zero(inv_qty) as inv_qty, sys.f_remove_zero(outing_qty) as outing_qty,
  8. sys.f_remove_zero(usable_qty) as usable_qty, cost_price, cost_amt, 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, sys.f_remove_zero(freeze_qty) as freeze_qty
  10. </sql>
  11. <!-- 通用查询列Response -->
  12. <sql id="Base_Column_List_Response">
  13. tpi.inv_id, tpi.wh_id, tpi.sku_id, tpi.non_std_code, sys.f_remove_zero(tpi.inv_qty) as inv_qty,
  14. sys.f_remove_zero(tpi.outing_qty) as outing_qty,
  15. sys.f_remove_zero(tpi.usable_qty) as usable_qty, tpi.cost_price, tpi.cost_amt, tpi.remarks, tpi.flg_valid,
  16. tpi.cp_id,sys.f_remove_zero(tpi.freeze_qty) as freeze_qty
  17. </sql>
  18. <!-- 通用查询映射结果 -->
  19. <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.Inventory">
  20. <id column="inv_id" property="invId"/>
  21. <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
  22. <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
  23. <result column="non_std_code" property="nonStdCode"/>
  24. <result column="inv_qty" property="invQty"/>
  25. <result column="outing_qty" property="outingQty"/>
  26. <result column="usable_qty" property="usableQty"/>
  27. <result column="cost_price" property="costPrice"/>
  28. <result column="cost_amt" property="costAmt"/>
  29. <result column="remarks" property="remarks"/>
  30. <result column="flg_valid" property="flgValid"/>
  31. <result column="cp_id" property="cpId"/>
  32. <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
  33. <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
  34. <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
  35. <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
  36. <result column="op_app_code" property="opAppCode"/>
  37. <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
  38. <result column="op_db_user" property="opDbUser"/>
  39. <result column="freeze_qty" property="freezeQty"/>
  40. <result column="decimalPlaces" property="decimalPlaces"/>
  41. </resultMap>
  42. <!-- 通用查询映射结果Response -->
  43. <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.InventoryResponse">
  44. <id column="inv_id" property="invId"/>
  45. <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
  46. <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
  47. <result column="non_std_code" property="nonStdCode"/>
  48. <result column="inv_qty" property="invQty"/>
  49. <result column="outing_qty" property="outingQty"/>
  50. <result column="usable_qty" property="usableQty"/>
  51. <result column="cost_price" property="costPrice"/>
  52. <result column="cost_amt" property="costAmt"/>
  53. <result column="remarks" property="remarks"/>
  54. <result column="flg_valid" property="flgValid"/>
  55. <result column="cp_id" property="cpId"/>
  56. <result column="sku_code" property="skuCode"/>
  57. <result column="sku_name" property="skuName"/>
  58. <result column="sku_spec" property="skuSpec"/>
  59. <result column="sku_model" property="skuModel"/>
  60. <result column="brand_name" property="brandName"/>
  61. <result column="short_name" property="shortName"/>
  62. <result column="wh_name" property="whName"/>
  63. <result column="price_purchase" property="pricePurchase"/>
  64. <result column="price_standard" property="priceStandard"/>
  65. <result column="price_wholesale" property="priceWholesale"/>
  66. <result column="price_limited" property="priceLimited"/>
  67. <result column="unit_id" property="unitId" typeHandler="UuidTypeHandler"/>
  68. <result column="sub_unit_id" property="subUnitId" typeHandler="UuidTypeHandler"/>
  69. <result column="pack_box" property="packBox"/>
  70. <result column="subUnitName" property="subUnitName"/>
  71. <result column="unitName" property="unitName"/>
  72. <result column="flg_sub_unit" property="flgSubUnit"/>
  73. <result column="seriesName" property="seriesName"/>
  74. <result column="catName" property="catName"/>
  75. <result column="price_other" property="priceOther"/>
  76. <result column="warning_upper_limit" property="warningUpperLimit"/>
  77. <result column="warning_lower_limit" property="warningLowerLimit"/>
  78. <result column="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
  79. <result column="decimalPlaces" property="decimalPlaces"/>
  80. </resultMap>
  81. <!-- 通用条件列 -->
  82. <sql id="Condition">
  83. <where>
  84. <if test="whId != null and whId != ''">
  85. AND tpi.wh_id = #{whId}::UUID
  86. </if>
  87. <if test="whIds != null and whIds.size()>0">
  88. AND tpi.wh_id =
  89. any(#{whIds, typeHandler=UuidListTypeHandler})
  90. </if>
  91. <if test="skuId != null and skuId != ''">
  92. AND tpi.sku_id = #{skuId}
  93. </if>
  94. <if test="nonStdCode != null and nonStdCode != ''">
  95. AND tpi.non_std_code = #{nonStdCode}
  96. </if>
  97. <if test="invQty != null">
  98. AND tpi.inv_qty = #{invQty}
  99. </if>
  100. <if test="outingQty != null">
  101. AND tpi.outing_qty = #{outingQty}
  102. </if>
  103. <if test="usableQty != null">
  104. AND tpi.usable_qty = #{usableQty}
  105. </if>
  106. <if test="costPrice != null">
  107. AND tpi.cost_price = #{costPrice}
  108. </if>
  109. <if test="costAmt != null">
  110. AND tpi.cost_amt = #{costAmt}
  111. </if>
  112. <if test="flgValid != null">
  113. AND tpi.flg_valid = #{flgValid}
  114. </if>
  115. <if test="flgZero == true">
  116. AND tpi.inv_qty != 0
  117. </if>
  118. <if test="cpId != null">
  119. AND tpi.cp_id = #{cpId}
  120. </if>
  121. <if test="freezeQty != null">
  122. AND tpi.freeze_qty = #{freezeQty}
  123. </if>
  124. <if test="categoryId != null and categoryId != ''">
  125. AND tmgs.category_id = #{categoryId}::UUID
  126. </if>
  127. <if test="brandId != null and brandId != ''">
  128. AND tmgs.brand_id = #{brandId}::UUID
  129. </if>
  130. <if test="searchText != null">
  131. AND (tmgs.sku_model like concat('%', my_ex.likequery(#{searchText}) , '%')
  132. OR tmgs.sku_name like concat('%', my_ex.likequery(#{searchText}) , '%')
  133. OR tpi.non_std_code like concat('%', my_ex.likequery(#{searchText}) , '%')
  134. )
  135. </if>
  136. <if test="invQtyFlag != null and invQtyFlag = true">
  137. and tpi.inv_qty != 0
  138. </if>
  139. </where>
  140. </sql>
  141. <sql id="idsForeach">
  142. <!-- 根据主键invId批量操作 -->
  143. WHERE inv_id in
  144. <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
  145. #{item}
  146. </foreach>
  147. </sql>
  148. <!-- 查询表dkic_b.t_psi_inventory,(条件查询+分页)列表 -->
  149. <select id="selectByCond" resultMap="BaseResultMapResponse">
  150. SELECT
  151. <include refid="Base_Column_List_Response"/>
  152. ,tmgs.sku_code
  153. ,tmgs.sku_model
  154. ,tmgs.sku_name
  155. ,tmgs.sku_spec
  156. ,tmgs.price_purchase
  157. ,tmgs.price_standard
  158. ,tmgs.price_wholesale
  159. ,tmgs.price_other
  160. ,tmgs.price_limited
  161. ,tmgs.sku_images
  162. ,tmgs.unit_id
  163. ,tmgs.sub_unit_id
  164. ,tmgs.warning_upper_limit
  165. ,tmgs.warning_lower_limit
  166. ,tmgs.pack_box
  167. ,tmu.unit_name AS "unitName"
  168. ,subtmu.unit_name AS "subUnitName"
  169. ,tmgs.flg_sub_unit
  170. ,tmgb.brand_name
  171. ,tmgb.short_name
  172. ,tmw.wh_name
  173. ,tmgc.cat_name as "catName"
  174. ,tmgseries.series_name as "seriesName"
  175. ,tmu.decimal_places as "decimalPlaces"
  176. FROM dkic_b.t_psi_inventory tpi
  177. <include refid="Join_Table"/>
  178. <include refid="Condition"/>
  179. <if test="desc != null and desc != ''">
  180. order by tpi.usable_qty desc
  181. </if>
  182. <if test="desc == null or desc == ''">
  183. order by tpi.usable_qty
  184. </if>
  185. <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
  186. limit #{end} offset #{start}
  187. </if>
  188. </select>
  189. <!-- 查询表dkic_b.t_psi_inventory,(条件查询)个数 -->
  190. <select id="countByCond" resultType="Long">
  191. SELECT
  192. count(1)
  193. FROM dkic_b.t_psi_inventory tpi
  194. <include refid="Join_Table"/>
  195. <include refid="Condition"/>
  196. </select>
  197. <sql id="Join_Table">
  198. left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpi.sku_id
  199. left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
  200. LEFT JOIN dkic_b.t_mst_goods_brand tmgb ON tmgb.brand_id = tmgs.brand_id
  201. LEFT JOIN dkic_b.t_mst_unit tmu ON tmu.unit_id = tmgs.unit_id
  202. LEFT JOIN dkic_b.t_mst_unit subtmu ON subtmu.unit_id = tmgs.sub_unit_id
  203. left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
  204. left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
  205. </sql>
  206. <!-- 根据主键查询表dkic_b.t_psi_inventory的一行数据 -->
  207. <select id="selectById" resultMap="BaseResultMapResponse">
  208. SELECT
  209. <include refid="Base_Column_List_Response"/>
  210. ,tmu.decimal_places as "decimalPlaces"
  211. FROM dkic_b.t_psi_inventory tpi
  212. left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpi.sku_id
  213. LEFT JOIN dkic_b.t_mst_unit tmu ON tmu.unit_id = tmgs.unit_id
  214. WHERE tpi.inv_id = #{id}::uuid
  215. </select>
  216. <!-- 根据主键锁定表dkic_b.t_psi_inventory的一行数据 -->
  217. <select id="selectByIdForUpdate" resultMap="BaseResultMap">
  218. SELECT
  219. <include refid="Base_Column_List"/>
  220. FROM dkic_b.t_psi_inventory
  221. WHERE inv_id = #{id}::uuid
  222. for update
  223. </select>
  224. <!-- 根据主键锁定表dkic_b.t_psi_inventory的多行数据 -->
  225. <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
  226. SELECT
  227. <include refid="Base_Column_List"/>
  228. FROM dkic_b.t_psi_inventory
  229. <include refid="idsForeach"/>
  230. for update
  231. </select>
  232. <insert id="insertBatch">
  233. insert into dkic_b.t_psi_inventory
  234. (
  235. <trim suffixOverrides=",">
  236. wh_id,
  237. sku_id,
  238. non_std_code,
  239. inv_qty,
  240. outing_qty,
  241. usable_qty,
  242. cost_price,
  243. cost_amt,
  244. remarks,
  245. cp_id,
  246. op_app_code,
  247. freeze_qty,
  248. </trim>
  249. )
  250. values
  251. <foreach collection="list" index="index" item="item" separator=",">
  252. (
  253. <trim suffixOverrides=",">
  254. #{item.whId}::uuid,
  255. #{item.skuId}::uuid,
  256. #{item.nonStdCode},
  257. #{item.invQty},
  258. #{item.outingQty},
  259. #{item.usableQty},
  260. #{item.costPrice},
  261. #{item.costAmt},
  262. #{item.remarks},
  263. #{item.cpId},
  264. #{item.opAppCode},
  265. #{item.freezeQty},
  266. </trim>
  267. )
  268. </foreach>
  269. </insert>
  270. <!-- 查询表dkic_b.t_psi_inventory,(条件查询+分页)列表 -->
  271. <select id="selectByOther" resultMap="BaseResultMapResponse">
  272. SELECT
  273. <include refid="Base_Column_List_Response"/>
  274. ,tmu.decimal_places as "decimalPlaces"
  275. FROM dkic_b.t_psi_inventory tpi
  276. left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpi.sku_id
  277. LEFT JOIN dkic_b.t_mst_unit tmu ON tmu.unit_id = tmgs.unit_id
  278. <where>
  279. <if test="whId != null and whId != ''">
  280. AND tpi.wh_id = #{whId}::uuid
  281. </if>
  282. <if test="skuId != null and skuId != ''">
  283. AND tpi.sku_id = #{skuId}::uuid
  284. </if>
  285. <if test="nonStdCode != null and nonStdCode != ''">
  286. AND tpi.non_std_code = #{nonStdCode}
  287. </if>
  288. <if test="nonStdCode == null and nonStdCode == ''">
  289. AND tpi.non_std_code = '-'
  290. </if>
  291. </where>
  292. </select>
  293. <!--修改库存数量金额-->
  294. <update id="updateAmountAndQuantity" parameterType="com.dk.mdm.model.vo.ivt.InventoryVO">
  295. update dkic_b.t_psi_inventory
  296. <set>
  297. <if test="invQty != null">
  298. inv_qty = inv_qty + #{invQty,jdbcType=NUMERIC},
  299. usable_qty = (inv_qty + #{invQty,jdbcType=NUMERIC}) - outing_qty - freeze_qty,
  300. <if test="zeroFlag">
  301. cost_amt = 0,
  302. cost_price = 0 ,
  303. </if>
  304. <if test="!zeroFlag">
  305. cost_amt = cost_amt + #{costAmt,jdbcType=NUMERIC},
  306. cost_price = (cost_amt + #{costAmt,jdbcType=NUMERIC}) / (inv_qty + #{invQty,jdbcType=NUMERIC}) ,
  307. </if>
  308. </if>
  309. </set>
  310. where inv_id =#{invId,typeHandler=UuidTypeHandler}
  311. and inv_qty + #{invQty,jdbcType=NUMERIC}>=0
  312. and cost_amt + #{costAmt,jdbcType=NUMERIC}>=0
  313. </update>
  314. <!--修改库存数量金额(删除用)-->
  315. <update id="updateAmtAndQty" parameterType="com.dk.mdm.model.pojo.ivt.Inventory">
  316. update dkic_b.t_psi_inventory
  317. <set>
  318. <if test="invQty != null">
  319. inv_qty = #{invQty,jdbcType=NUMERIC},
  320. </if>
  321. <if test="usableQty != null">
  322. usable_qty = #{usableQty,jdbcType=NUMERIC},
  323. </if>
  324. <if test="costPrice != null">
  325. cost_price = #{costPrice,jdbcType=NUMERIC},
  326. </if>
  327. <if test="costAmt != null">
  328. cost_amt = #{costAmt,jdbcType=NUMERIC},
  329. </if>
  330. </set>
  331. where inv_id =#{invId,typeHandler=UuidTypeHandler}
  332. and #{invQty,jdbcType=NUMERIC}>=0
  333. and #{costAmt,jdbcType=NUMERIC}>=0
  334. </update>
  335. <!--删除数据-->
  336. <delete id="deleteData" parameterType="com.dk.mdm.model.pojo.ivt.InOutRecord">
  337. DELETE
  338. FROM dkic_b.t_psi_inventory
  339. where inv_id = #{invId,typeHandler=UuidTypeHandler}
  340. </delete>
  341. <!--修改存货单价/存货资产-->
  342. <update id="updateCost" parameterType="com.dk.mdm.model.pojo.ivt.Inventory">
  343. update dkic_b.t_psi_inventory
  344. <set>
  345. <if test="costPrice != null">
  346. cost_price = #{costPrice,jdbcType=NUMERIC},
  347. </if>
  348. <if test="costAmt != null">
  349. cost_amt = #{costAmt,jdbcType=NUMERIC},
  350. </if>
  351. </set>
  352. where inv_id =#{invId,typeHandler=UuidTypeHandler}
  353. </update>
  354. </mapper>