InventoryMapper.xml 14 KB

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