InventoryMapper.xml 15 KB

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