WmStockTakingResultMapper.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.ktg.mes.wm.mapper.WmStockTakingResultMapper">
  6. <resultMap type="WmStockTakingResult" id="WmStockTakingResultResult">
  7. <result property="resultId" column="result_id" />
  8. <result property="takingId" column="taking_id" />
  9. <result property="itemId" column="item_id" />
  10. <result property="itemCode" column="item_code" />
  11. <result property="itemName" column="item_name" />
  12. <result property="specification" column="specification" />
  13. <result property="unitOfMeasure" column="unit_of_measure" />
  14. <result property="unitName" column="unit_name" />
  15. <result property="quantity" column="quantity" />
  16. <result property="takingQuantity" column="taking_quantity" />
  17. <result property="takingStatus" column="taking_status" />
  18. <result property="remark" column="remark" />
  19. <result property="attr1" column="attr1" />
  20. <result property="attr2" column="attr2" />
  21. <result property="attr3" column="attr3" />
  22. <result property="attr4" column="attr4" />
  23. <result property="createBy" column="create_by" />
  24. <result property="createTime" column="create_time" />
  25. <result property="updateBy" column="update_by" />
  26. <result property="updateTime" column="update_time" />
  27. </resultMap>
  28. <sql id="selectWmStockTakingResultVo">
  29. select result_id, taking_id, item_id, item_code, item_name, specification, unit_of_measure, unit_name, quantity, taking_quantity, taking_status, remark, attr1, attr2, attr3, attr4, create_by, create_time, update_by, update_time from wm_stock_taking_result
  30. </sql>
  31. <select id="selectWmStockTakingResultList" parameterType="WmStockTakingResult" resultMap="WmStockTakingResultResult">
  32. <include refid="selectWmStockTakingResultVo"/>
  33. <where>
  34. <if test="takingId != null "> and taking_id = #{takingId}</if>
  35. <if test="itemId != null "> and item_id = #{itemId}</if>
  36. <if test="itemCode != null and itemCode != ''"> and item_code = #{itemCode}</if>
  37. <if test="itemName != null and itemName != ''"> and item_name like concat('%', #{itemName}, '%')</if>
  38. <if test="specification != null and specification != ''"> and specification = #{specification}</if>
  39. <if test="unitOfMeasure != null and unitOfMeasure != ''"> and unit_of_measure = #{unitOfMeasure}</if>
  40. <if test="unitName != null and unitName != ''"> and unit_name like concat('%', #{unitName}, '%')</if>
  41. <if test="quantity != null "> and quantity = #{quantity}</if>
  42. <if test="takingQuantity != null "> and taking_quantity = #{takingQuantity}</if>
  43. <if test="takingStatus != null and takingStatus != ''"> and taking_status = #{takingStatus}</if>
  44. </where>
  45. </select>
  46. <select id="selectWmStockTakingResultByResultId" parameterType="Long" resultMap="WmStockTakingResultResult">
  47. <include refid="selectWmStockTakingResultVo"/>
  48. where result_id = #{resultId}
  49. </select>
  50. <insert id="calculateOpenWmStockTakingResult" parameterType="Long" useGeneratedKeys="true">
  51. insert into wm_stock_taking_result (taking_id,item_id,item_code,item_name,specification,taking_quantity,quantity,taking_status)
  52. select sl.taking_id,sl.item_id,sl.item_code,sl.item_name,sl.specification,taking_quantity,quantity,
  53. case when ISNULL(taking_quantity,0) &gt; ISNULL(quantity,0) then 'PROFIT'
  54. when ISNULL(taking_quantity,0) &lt; ISNULL(quantity,0) then 'LOSS'
  55. else 'NORMAL' end as taking_status
  56. from wm_stock_taking_line sl
  57. where sl.taking_id = #{takingId}
  58. </insert>
  59. <insert id="calculateWmStockTakingResult" parameterType="Long" useGeneratedKeys="true">
  60. insert into wm_stock_taking_result (item_id,item_code,item_name,specification,taking_quantity,quantity,taking_status)
  61. select ISNULL(m.item_id,t.item_id) as item_id, ISNULL(m.item_code,t.item_code) as item_code,ISNULL(m.item_name,t.item_name) as item_name,ISNULL(m.specification,t.specification) as specification,
  62. ISNULL(t.taking_quantity,0) as taking_quantity,ISNULL(m.onhand,0) as quantity,
  63. case when ISNULL(t.taking_quantity,0) &gt; ISNULL(m.onhand,0) then 'PROFIT'
  64. when ISNULL(t.taking_quantity,0) &lt; ISNULL(m.onhand,0) then 'LOSS'
  65. else 'NORMAL' end as taking_status
  66. from (
  67. select sl.item_id,sl.item_code,sl.item_name,sl.specification,sum(sl.taking_quantity) as taking_quantity
  68. from wm_stock_taking_line sl
  69. where sl.taking_id = #{takingId}
  70. group by sl.item_id,sl.item_code,sl.item_name,sl.specification
  71. ) t
  72. left join (
  73. select ms.item_id,ms.item_code,ms.item_name,ms.specification,sum(ms.quantity_onhand) as onhand
  74. from wm_material_stock ms
  75. where (ms.warehouse_id,ms.location_id,ms.area_id) in (
  76. select warehouse_id,location_id,area_id
  77. from wm_stock_taking
  78. where taking_id = #{takingId}
  79. )
  80. group by ms.item_id,ms.item_code,ms.item_name,ms.specification
  81. ) m
  82. on t.item_id = m.item_id
  83. and t.item_code = m.item_code
  84. where t.taking_quantity !=0
  85. union
  86. select ISNULL(t.item_id,m.item_id) as item_id,ISNULL(t.item_code,m.item_code) as item_code,ISNULL(t.item_name,m.item_name) as item_name,ISNULL(t.specification,m.specification) as specification,
  87. ISNULL(t.taking_quantity,0) as taking_quantity,ISNULL(m.onhand,0) as quantity,
  88. case when ISNULL(t.taking_quantity,0) &gt; ISNULL(m.onhand,0) then 'PROFIT'
  89. when ISNULL(t.taking_quantity,0) &lt; ISNULL(m.onhand,0) then 'LOSS'
  90. else 'NORMAL' end as taking_status
  91. from (
  92. select ms.item_id,ms.item_code,ms.item_name,ms.specification,sum(ms.quantity_onhand) as onhand
  93. from wm_material_stock ms
  94. where (ms.warehouse_id,ms.location_id,ms.area_id) in (
  95. select warehouse_id,location_id,area_id
  96. from wm_stock_taking
  97. where taking_id = #{takingId}
  98. )
  99. group by ms.item_id,ms.item_code,ms.item_name,ms.specification
  100. ) m
  101. left join (
  102. select sl.item_id,sl.item_code,sl.item_name,sl.specification,sum(sl.taking_quantity) as taking_quantity
  103. from wm_stock_taking_line sl
  104. where sl.taking_id = #{takingId}
  105. group by sl.item_id,sl.item_code,sl.item_name,sl.specification
  106. ) t
  107. on t.item_id = m.item_id
  108. and t.item_code = m.item_code
  109. where m.onhand !=0
  110. </insert>
  111. <insert id="insertWmStockTakingResult" parameterType="WmStockTakingResult" useGeneratedKeys="true" keyProperty="resultId">
  112. insert into wm_stock_taking_result
  113. <trim prefix="(" suffix=")" suffixOverrides=",">
  114. <if test="takingId != null">taking_id,</if>
  115. <if test="itemId != null">item_id,</if>
  116. <if test="itemCode != null">item_code,</if>
  117. <if test="itemName != null">item_name,</if>
  118. <if test="specification != null">specification,</if>
  119. <if test="unitOfMeasure != null">unit_of_measure,</if>
  120. <if test="unitName != null">unit_name,</if>
  121. <if test="quantity != null">quantity,</if>
  122. <if test="takingQuantity != null">taking_quantity,</if>
  123. <if test="takingStatus != null and takingStatus != ''">taking_status,</if>
  124. <if test="remark != null">remark,</if>
  125. <if test="attr1 != null">attr1,</if>
  126. <if test="attr2 != null">attr2,</if>
  127. <if test="attr3 != null">attr3,</if>
  128. <if test="attr4 != null">attr4,</if>
  129. <if test="createBy != null">create_by,</if>
  130. <if test="createTime != null">create_time,</if>
  131. <if test="updateBy != null">update_by,</if>
  132. <if test="updateTime != null">update_time,</if>
  133. </trim>
  134. <trim prefix="values (" suffix=")" suffixOverrides=",">
  135. <if test="takingId != null">#{takingId},</if>
  136. <if test="itemId != null">#{itemId},</if>
  137. <if test="itemCode != null">#{itemCode},</if>
  138. <if test="itemName != null">#{itemName},</if>
  139. <if test="specification != null">#{specification},</if>
  140. <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
  141. <if test="unitName != null">#{unitName},</if>
  142. <if test="quantity != null">#{quantity},</if>
  143. <if test="takingQuantity != null">#{takingQuantity},</if>
  144. <if test="takingStatus != null and takingStatus != ''">#{takingStatus},</if>
  145. <if test="remark != null">#{remark},</if>
  146. <if test="attr1 != null">#{attr1},</if>
  147. <if test="attr2 != null">#{attr2},</if>
  148. <if test="attr3 != null">#{attr3},</if>
  149. <if test="attr4 != null">#{attr4},</if>
  150. <if test="createBy != null">#{createBy},</if>
  151. <if test="createTime != null">#{createTime},</if>
  152. <if test="updateBy != null">#{updateBy},</if>
  153. <if test="updateTime != null">#{updateTime},</if>
  154. </trim>
  155. </insert>
  156. <update id="updateWmStockTakingResult" parameterType="WmStockTakingResult">
  157. update wm_stock_taking_result
  158. <trim prefix="SET" suffixOverrides=",">
  159. <if test="takingId != null">taking_id = #{takingId},</if>
  160. <if test="itemId != null">item_id = #{itemId},</if>
  161. <if test="itemCode != null">item_code = #{itemCode},</if>
  162. <if test="itemName != null">item_name = #{itemName},</if>
  163. <if test="specification != null">specification = #{specification},</if>
  164. <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
  165. <if test="unitName != null">unit_name = #{unitName},</if>
  166. <if test="quantity != null">quantity = #{quantity},</if>
  167. <if test="takingQuantity != null">taking_quantity = #{takingQuantity},</if>
  168. <if test="takingStatus != null and takingStatus != ''">taking_status = #{takingStatus},</if>
  169. <if test="remark != null">remark = #{remark},</if>
  170. <if test="attr1 != null">attr1 = #{attr1},</if>
  171. <if test="attr2 != null">attr2 = #{attr2},</if>
  172. <if test="attr3 != null">attr3 = #{attr3},</if>
  173. <if test="attr4 != null">attr4 = #{attr4},</if>
  174. <if test="createBy != null">create_by = #{createBy},</if>
  175. <if test="createTime != null">create_time = #{createTime},</if>
  176. <if test="updateBy != null">update_by = #{updateBy},</if>
  177. <if test="updateTime != null">update_time = #{updateTime},</if>
  178. </trim>
  179. where result_id = #{resultId}
  180. </update>
  181. <delete id="deleteWmStockTakingResultByResultId" parameterType="Long">
  182. delete from wm_stock_taking_result where result_id = #{resultId}
  183. </delete>
  184. <delete id="deleteWmStockTakingResultByResultIds" parameterType="String">
  185. delete from wm_stock_taking_result where result_id in
  186. <foreach item="resultId" collection="array" open="(" separator="," close=")">
  187. #{resultId}
  188. </foreach>
  189. </delete>
  190. <delete id="deleteWmStockTakingResultByTakingId" parameterType="Long">
  191. delete from wm_stock_taking_result where taking_id = #{takingId}
  192. </delete>
  193. </mapper>