ActHxpapplyMapper.xml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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.zd.chemical.mapper.ActHxpapplyMapper">
  6. <resultMap type="com.zd.chemical.domain.ActHxpapply" id="ActHxpapplyResult">
  7. <result property="id" column="id"/>
  8. <result property="applyNum" column="apply_num"/>
  9. <result property="applyReason" column="apply_reason"/>
  10. <result property="subId" column="sub_id"/>
  11. <result property="applyStatus" column="apply_status"/>
  12. <result property="userId" column="user_id"/>
  13. <result property="createBy" column="create_by"/>
  14. <result property="createTime" column="create_time"/>
  15. <result property="updateBy" column="update_by"/>
  16. <result property="updateTime" column="update_time"/>
  17. <result property="remark" column="remark"/>
  18. </resultMap>
  19. <sql id="selectActHxpapplyVo">
  20. select id, apply_num, apply_reason, sub_id, apply_status, user_id, create_by, create_time, update_by, update_time, remark from act_hxpapply
  21. </sql>
  22. <sql id="selectActHxpapplyListVo">
  23. select t.id, t.apply_num, t.apply_reason, t.sub_id, t.apply_status, t.user_id, t.create_by, t.create_time, t.update_by, t.update_time, t.remark from act_hxpapply as t
  24. </sql>
  25. <select id="selectActHxpapplyList" parameterType="com.zd.chemical.domain.vo.ActHxpapplySearch" resultType="com.zd.chemical.domain.vo.ActHxpapplyListVo">
  26. SELECT xx1.* FROM (
  27. SELECT xx.id,xx.applyNum,xx.subscriber,xx.subName,xx.chemicalNames,xx.createTime,xx.overTime,xx.approvalContent,xx.approvalStatus,
  28. CASE WHEN xx.approvalStatus = 0 THEN
  29. (SELECT GROUP_CONCAT(ur.user_id,';',ur.nick_name) FROM sys_user ur
  30. WHERE ur.`user_id`
  31. IN (
  32. SELECT
  33. SUBSTRING_INDEX(SUBSTRING_INDEX(xx.currentApprover,',', b.help_topic_id + 1), ',', -1) result
  34. FROM
  35. mysql.help_topic b
  36. WHERE
  37. b.help_topic_id &lt; (LENGTH(xx.currentApprover) - LENGTH(REPLACE(xx.currentApprover, ',', '')) + 1)
  38. )
  39. )
  40. ELSE ''
  41. END currentApprover,
  42. CASE WHEN xx.approvalStatus = 3 THEN ''
  43. ELSE (SELECT GROUP_CONCAT(ur.user_id,';',ur.nick_name) FROM sys_user ur
  44. WHERE ur.`user_id`
  45. IN (
  46. SELECT
  47. SUBSTRING_INDEX(SUBSTRING_INDEX(xx.historyApprover,',', b.help_topic_id + 1), ',', -1) result
  48. FROM
  49. mysql.help_topic b
  50. WHERE
  51. b.help_topic_id &lt; (LENGTH(xx.historyApprover) - LENGTH(REPLACE(xx.historyApprover, ',', '')) + 1)
  52. )
  53. )
  54. END historyApprover,
  55. xx.currentApprover currentApproverIds,xx.userId
  56. FROM (
  57. SELECT ah.id,ah.`apply_num` applyNum,
  58. su.nick_name subscriber,
  59. st.`name` subName,
  60. GROUP_CONCAT(CONCAT(hd.chemical_name,'-申购量:' ,hd.apply_num , che.chemical_unit) ORDER BY hd.chemical_name DESC,hd.apply_num DESC SEPARATOR '; ') chemicalNames,
  61. ah.`create_time` createTime,
  62. ak.`over_time` overTime,
  63. CASE WHEN ak.approval_status=0 THEN '审批中'
  64. ELSE CASE WHEN ak.approval_status=1 THEN '已通过'
  65. ELSE CASE WHEN ak.approval_status=2 THEN '已拒绝'
  66. ELSE '已撤销'
  67. END
  68. END
  69. END approvalContent,
  70. ak.approval_status approvalStatus,
  71. (SELECT an.apply_user_ids FROM act_applytask apk,act_applytask_node an WHERE an.applytask_id = apk.id AND apk.id = ak.id AND an.node_status = 0
  72. AND apk.approval_status=0 ORDER BY an.node_order ASC LIMIT 1) currentApprover,
  73. (SELECT an.apply_user_ids FROM act_applytask apk,act_applytask_node an WHERE an.applytask_id = apk.id AND apk.id = ak.id AND (an.node_status = 1 OR an.node_status = 2)
  74. AND (apk.approval_status=0 OR apk.approval_status=1 OR apk.approval_status=2) ORDER BY an.node_order DESC LIMIT 1) historyApprover,
  75. ah.user_id userId
  76. FROM act_hxpapply ah LEFT JOIN sys_user su ON su.user_id = ah.`user_id`
  77. LEFT JOIN lab_subject st ON st.id = ah.`sub_id`
  78. LEFT JOIN act_hxpapply_detail hd ON hd.hxpapply_id = ah.id
  79. LEFT JOIN act_applytask ak ON ak.`task_id` = ah.`id`
  80. left join hxp_chemical che on che.id = hd.chemical_id
  81. <where>
  82. ah.apply_status in(1,3)
  83. <if test="ids != null">
  84. and ah.id in
  85. <foreach item="id" collection="ids" open="(" separator="," close=")">
  86. #{id}
  87. </foreach>
  88. </if>
  89. <if test="searchValue != null ">
  90. and (
  91. ah.apply_num like concat('%', #{searchValue}, '%') or
  92. su.nick_name like concat('%', #{searchValue}, '%') or
  93. hd.chemical_name like concat('%', #{searchValue}, '%') or
  94. st.name like concat('%', #{searchValue}, '%')
  95. )
  96. </if>
  97. <if test="approvalStatus != null ">
  98. and ak.approval_status = #{approvalStatus}
  99. </if>
  100. <if test="beginCreateTime != null and beginCreateTime != ''"><!-- 申请开始时间 -->
  101. AND date_format(ak.create_time,'%y%m%d') &gt;= date_format(#{beginCreateTime},'%y%m%d')
  102. </if>
  103. <if test="endCreateTime != null and endCreateTime != ''"><!-- 申请结束时间 -->
  104. AND date_format(ak.create_time,'%y%m%d') &lt;= date_format(#{endCreateTime},'%y%m%d')
  105. </if>
  106. <if test="beginOverTime != null and beginOverTime != ''"><!-- 完成开始时间 -->
  107. AND date_format(ak.over_time,'%y%m%d') &gt;= date_format(#{beginOverTime},'%y%m%d')
  108. </if>
  109. <if test="endOverTime != null and endOverTime != ''"><!-- 完成结束时间 -->
  110. AND date_format(ak.over_time,'%y%m%d') &lt;= date_format(#{endOverTime},'%y%m%d')
  111. </if>
  112. <if test="userId != null and userId != ''"><!-- 创建人id -->
  113. and ah.user_id = #{userId}
  114. </if>
  115. <!-- 数据范围过滤 -->
  116. ${params.dataScope}
  117. </where>
  118. GROUP BY ah.`id`
  119. )xx WHERE FIND_IN_SET(#{loginUserId},xx.currentApprover) ORDER BY xx.createTime DESC
  120. )xx1
  121. UNION
  122. SELECT xx1.* FROM (
  123. SELECT xx.id,xx.applyNum,xx.subscriber,xx.subName,xx.chemicalNames,xx.createTime,xx.overTime,xx.approvalContent,xx.approvalStatus,
  124. CASE WHEN xx.approvalStatus = 0 THEN
  125. (SELECT GROUP_CONCAT(ur.user_id,';',ur.nick_name) FROM sys_user ur
  126. WHERE ur.`user_id`
  127. IN (
  128. SELECT
  129. SUBSTRING_INDEX(SUBSTRING_INDEX(xx.currentApprover,',', b.help_topic_id + 1), ',', -1) result
  130. FROM
  131. mysql.help_topic b
  132. WHERE
  133. b.help_topic_id &lt; (LENGTH(xx.currentApprover) - LENGTH(REPLACE(xx.currentApprover, ',', '')) + 1)
  134. )
  135. )
  136. ELSE ''
  137. END currentApprover,
  138. CASE WHEN xx.approvalStatus = 3 THEN ''
  139. ELSE (SELECT GROUP_CONCAT(ur.user_id,';',ur.nick_name) FROM sys_user ur
  140. WHERE ur.`user_id`
  141. IN (
  142. SELECT
  143. SUBSTRING_INDEX(SUBSTRING_INDEX(xx.historyApprover,',', b.help_topic_id + 1), ',', -1) result
  144. FROM
  145. mysql.help_topic b
  146. WHERE
  147. b.help_topic_id &lt; (LENGTH(xx.historyApprover) - LENGTH(REPLACE(xx.historyApprover, ',', '')) + 1)
  148. )
  149. )
  150. END historyApprover,
  151. xx.currentApprover currentApproverIds,xx.userId
  152. FROM (
  153. SELECT ah.id,ah.`apply_num` applyNum,
  154. su.nick_name subscriber,
  155. st.`name` subName,
  156. GROUP_CONCAT(CONCAT(hd.chemical_name,'-申购量:' ,hd.apply_num ,che.chemical_unit) ORDER BY hd.chemical_name DESC,hd.apply_num DESC SEPARATOR '; ') chemicalNames,
  157. ah.`create_time` createTime,
  158. ak.`over_time` overTime,
  159. CASE WHEN ak.approval_status=0 THEN '审批中'
  160. ELSE CASE WHEN ak.approval_status=1 THEN '已通过'
  161. ELSE CASE WHEN ak.approval_status=2 THEN '已拒绝'
  162. ELSE '已撤销'
  163. END
  164. END
  165. END approvalContent,
  166. ak.approval_status approvalStatus,
  167. (SELECT an.apply_user_ids FROM act_applytask apk,act_applytask_node an WHERE an.applytask_id = apk.id AND apk.id = ak.id AND an.node_status = 0
  168. AND apk.approval_status=0 ORDER BY an.node_order ASC LIMIT 1) currentApprover,
  169. (SELECT an.apply_user_ids FROM act_applytask apk,act_applytask_node an WHERE an.applytask_id = apk.id AND apk.id = ak.id AND (an.node_status = 1 OR an.node_status = 2)
  170. AND (apk.approval_status=0 OR apk.approval_status=1 OR apk.approval_status=2) ORDER BY an.node_order DESC LIMIT 1) historyApprover,
  171. ah.user_id userId
  172. FROM act_hxpapply ah LEFT JOIN sys_user su ON su.user_id = ah.`user_id`
  173. LEFT JOIN lab_subject st ON st.id = ah.`sub_id`
  174. LEFT JOIN act_hxpapply_detail hd ON hd.hxpapply_id = ah.id
  175. LEFT JOIN act_applytask ak ON ak.`task_id` = ah.`id`
  176. left join hxp_chemical che on che.id = hd.chemical_id
  177. <where>
  178. ah.apply_status in(1,3)
  179. <if test="ids != null">
  180. and ah.id in
  181. <foreach item="id" collection="ids" open="(" separator="," close=")">
  182. #{id}
  183. </foreach>
  184. </if>
  185. <if test="searchValue != null ">
  186. and (
  187. ah.apply_num like concat('%', #{searchValue}, '%') or
  188. su.nick_name like concat('%', #{searchValue}, '%') or
  189. hd.chemical_name like concat('%', #{searchValue}, '%') or
  190. st.name like concat('%', #{searchValue}, '%')
  191. )
  192. </if>
  193. <if test="approvalStatus != null ">
  194. and ak.approval_status = #{approvalStatus}
  195. </if>
  196. <if test="beginCreateTime != null and beginCreateTime != ''"><!-- 申请开始时间 -->
  197. AND date_format(ak.create_time,'%y%m%d') &gt;= date_format(#{beginCreateTime},'%y%m%d')
  198. </if>
  199. <if test="endCreateTime != null and endCreateTime != ''"><!-- 申请结束时间 -->
  200. AND date_format(ak.create_time,'%y%m%d') &lt;= date_format(#{endCreateTime},'%y%m%d')
  201. </if>
  202. <if test="beginOverTime != null and beginOverTime != ''"><!-- 完成开始时间 -->
  203. AND date_format(ak.over_time,'%y%m%d') &gt;= date_format(#{beginOverTime},'%y%m%d')
  204. </if>
  205. <if test="endOverTime != null and endOverTime != ''"><!-- 完成结束时间 -->
  206. AND date_format(ak.over_time,'%y%m%d') &lt;= date_format(#{endOverTime},'%y%m%d')
  207. </if>
  208. <if test="userId != null and userId != ''"><!-- 创建人id -->
  209. and ah.user_id = #{userId}
  210. </if>
  211. <!-- 数据范围过滤 -->
  212. ${params.dataScope}
  213. </where>
  214. GROUP BY ah.`id`
  215. )xx ORDER BY xx.createTime DESC
  216. )xx1
  217. </select>
  218. <select id="draftsList" parameterType="com.zd.chemical.domain.vo.ActHxpapplySearch" resultType="com.zd.chemical.domain.vo.ActHxpapplyDraftsListVo">
  219. SELECT xx.id,xx.applyNum,xx.subscriber,xx.subName,xx.chemicalNames,xx.createTime,xx.overTime,xx.approvalContent,xx.approvalStatus,
  220. CASE WHEN xx.approvalStatus = 0 THEN
  221. (SELECT GROUP_CONCAT(ur.user_id,';',ur.nick_name) FROM sys_user ur WHERE FIND_IN_SET(ur.user_id,xx.currentApprover))
  222. ELSE ''
  223. END currentApprover,
  224. CASE WHEN xx.approvalStatus = 3 THEN ''
  225. ELSE (SELECT GROUP_CONCAT(ur.user_id,';',ur.nick_name) FROM sys_user ur WHERE FIND_IN_SET(ur.user_id,xx.historyApprover))
  226. END historyApprover,
  227. xx.currentApprover currentApproverIds
  228. FROM (
  229. SELECT ah.id,ah.`apply_num` applyNum,
  230. su.nick_name subscriber,
  231. st.`name` subName,
  232. GROUP_CONCAT(hd.chemical_name,'-申购量:',hd.apply_num,che.chemical_unit SEPARATOR '; ') chemicalNames,
  233. ah.`create_time` createTime,
  234. ak.`over_time` overTime,
  235. CASE WHEN ak.approval_status=0 THEN '审批中'
  236. ELSE CASE WHEN ak.approval_status=1 THEN '已通过'
  237. ELSE CASE WHEN ak.approval_status=2 THEN '已拒绝'
  238. ELSE '已撤销'
  239. END
  240. END
  241. END approvalContent,
  242. ak.approval_status approvalStatus,
  243. (SELECT an.apply_user_ids FROM act_applytask apk,act_applytask_node an WHERE an.applytask_id = apk.id AND apk.id = ak.id AND an.node_status = 0
  244. AND apk.approval_status=0 ORDER BY an.node_order ASC LIMIT 1) currentApprover,
  245. (SELECT an.apply_user_ids FROM act_applytask apk,act_applytask_node an WHERE an.applytask_id = apk.id AND apk.id = ak.id AND (an.node_status = 1 OR an.node_status = 2)
  246. AND (apk.approval_status=0 OR apk.approval_status=1) ORDER BY an.node_order DESC LIMIT 1) historyApprover
  247. FROM act_hxpapply ah LEFT JOIN sys_user su ON su.user_id = ah.`user_id`
  248. LEFT JOIN lab_subject st ON st.id = ah.`sub_id`
  249. LEFT JOIN act_hxpapply_detail hd ON hd.hxpapply_id = ah.id
  250. LEFT JOIN act_applytask ak ON ak.`task_id` = ah.`id`
  251. left join hxp_chemical che on che.id = hd.chemical_id
  252. <where>
  253. ah.apply_status =2
  254. <if test="searchValue != null ">
  255. and (
  256. ah.apply_num like concat('%', #{searchValue}, '%') or
  257. hd.chemical_name like concat('%', #{searchValue}, '%') or
  258. st.name like concat('%', #{searchValue}, '%')
  259. )
  260. </if>
  261. <if test="approvalStatus != null ">
  262. and ak.approval_status = #{approvalStatus}
  263. </if>
  264. <if test="beginCreateTime != null and beginCreateTime != ''"><!-- 申请开始时间 -->
  265. AND date_format(ah.create_time,'%y%m%d') &gt;= date_format(#{beginCreateTime},'%y%m%d')
  266. </if>
  267. <if test="endCreateTime != null and endCreateTime != ''"><!-- 申请结束时间 -->
  268. AND date_format(ah.create_time,'%y%m%d') &lt;= date_format(#{endCreateTime},'%y%m%d')
  269. </if>
  270. <if test="beginOverTime != null and beginOverTime != ''"><!-- 完成开始时间 -->
  271. AND date_format(ah.over_time,'%y%m%d') &gt;= date_format(#{beginOverTime},'%y%m%d')
  272. </if>
  273. <if test="endOverTime != null and endOverTime != ''"><!-- 完成结束时间 -->
  274. AND date_format(ah.over_time,'%y%m%d') &lt;= date_format(#{endOverTime},'%y%m%d')
  275. </if>
  276. <if test="userId != null and userId != ''"><!-- 创建人id -->
  277. and ah.user_id = #{userId}
  278. </if>
  279. </where>
  280. GROUP BY ah.`id`
  281. )xx ORDER BY xx.createTime DESC
  282. </select>
  283. <select id="getListByIds" resultMap="ActHxpapplyResult">
  284. <include refid="selectActHxpapplyVo"/>
  285. <where>
  286. id in
  287. <foreach item="id" collection="list" open="(" separator="," close=")">
  288. #{id}
  289. </foreach>
  290. </where>
  291. </select>
  292. <select id="selectActHxpapplyById" resultType="com.zd.chemical.domain.vo.ActHxpapplyInfoVo">
  293. select a.id, a.apply_num, a.apply_reason, a.sub_id, a.apply_status, a.user_id, a.create_by, a.create_time, a.update_by
  294. , a.update_time, a.remark,
  295. (select ur.nick_name from sys_user ur where ur.user_id = a.user_id) applyUserName,
  296. st.name subName,st.dept_id deptId
  297. from act_hxpapply a left join lab_subject st on st.id = a.sub_id
  298. where a.id = #{id}
  299. </select>
  300. <select id="getRestStock" resultType="java.util.Map">
  301. SELECT x1.chemical_id chemicalId,CASE WHEN IFNULL(x1.applyNum,0)-IFNULL(x2.applyNum,0)>0
  302. THEN IFNULL(x1.applyNum,0)-IFNULL(x2.applyNum,0) ELSE 0 END restStock FROM (
  303. SELECT hd.`chemical_id`,IFNULL(SUM(hd.`apply_num`),0) applyNum FROM act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  304. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  305. LEFT JOIN lab_subject st ON ah.sub_id = st.id
  306. <where>
  307. ak.`approval_status`=1 AND hd.`tick_status`=1
  308. <if test="deptId != null ">
  309. AND st.dept_id = #{deptId}
  310. </if>
  311. <if test="subId != null ">
  312. AND ah.sub_id = #{subId}
  313. </if>
  314. <if test="chemicalId != null ">
  315. AND hd.`chemical_id` = #{chemicalId}
  316. </if>
  317. <if test="cabinetId != null ">
  318. AND hd.`cabinet_id` = #{cabinetId}
  319. </if>
  320. <if test="applyUserId != null ">
  321. AND ah.`user_id` = #{applyUserId}
  322. </if>
  323. </where>
  324. GROUP BY hd.`chemical_id`
  325. )x1 LEFT JOIN
  326. (
  327. SELECT t1.`chemical_id`,SUM(IFNULL(t1.`chemical_amount`,0)-IFNULL(t1.`tare`,0)) applyNum from(
  328. SELECT DISTINCT cjc.id,hd.`chemical_id`,cjc.chemical_amount,cjc.tare
  329. FROM act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  330. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  331. LEFT JOIN lab_subject st ON ah.sub_id = st.id
  332. inner join hxp_chemical_join_cabinet cjc on hd.`cabinet_id` = cjc.`cabinet_id` and hd.`chemical_id` = cjc.`chemical_id`
  333. <where>
  334. ak.`approval_status`=1 AND hd.`tick_status`=1
  335. <if test="deptId != null ">
  336. AND st.dept_id = #{deptId}
  337. </if>
  338. <if test="subId != null ">
  339. AND ah.sub_id = #{subId}
  340. </if>
  341. <if test="chemicalId != null ">
  342. AND hd.`chemical_id` = #{chemicalId}
  343. </if>
  344. <if test="cabinetId != null ">
  345. AND hd.`cabinet_id` = #{cabinetId}
  346. </if>
  347. <if test="applyUserId != null ">
  348. AND ah.`user_id` = #{applyUserId} AND cjc.`user_id` = #{applyUserId}
  349. </if>
  350. </where>
  351. )t1
  352. GROUP BY t1.`chemical_id`
  353. )x2 ON x1.chemical_id = x2.chemical_id
  354. </select>
  355. <select id="getRestStockClassif" resultType="com.zd.chemical.domain.vo.ActRestStockVo">
  356. SELECT 1 chemicalId,CONCAT(IFNULL(x1.stockNum,0), x1.chemical_unit) restStock FROM (
  357. SELECT cjc.`chemical_id`,SUM(s.usages-s.tare) stockNum, che.chemical_unit FROM hxp_stock s
  358. LEFT JOIN hxp_chemical_join_cabinet cjc ON s.`join_id` = cjc.`id`
  359. LEFT JOIN lab_subject st ON s.sub_id = st.id
  360. left join hxp_chemical che on che.id = cjc.chemical_id
  361. <where>
  362. s.status = 1
  363. <if test="deptId != null ">
  364. AND st.dept_id = #{deptId}
  365. </if>
  366. <if test="chemicalId != null ">
  367. AND cjc.`chemical_id` = #{chemicalId}
  368. </if>
  369. </where>
  370. GROUP BY cjc.`chemical_id`
  371. )x1
  372. UNION ALL
  373. SELECT 2 chemicalId,CONCAT(IFNULL(x1.stockNum,0), x1.chemical_unit) restStock FROM (
  374. SELECT cjc.`chemical_id`,SUM(s.usages-s.tare) stockNum, che.chemical_unit FROM hxp_stock s
  375. LEFT JOIN hxp_chemical_join_cabinet cjc ON s.`join_id` = cjc.`id`
  376. LEFT JOIN lab_subject st ON s.sub_id = st.id
  377. left join hxp_chemical che on che.id = cjc.chemical_id
  378. <where>
  379. s.status = 1
  380. <if test="subId != null ">
  381. AND s.sub_id = #{subId}
  382. </if>
  383. <if test="chemicalId != null ">
  384. AND cjc.`chemical_id` = #{chemicalId}
  385. </if>
  386. </where>
  387. GROUP BY cjc.`chemical_id`
  388. )x1
  389. UNION ALL
  390. SELECT 3 chemicalId,CONCAT(IFNULL(x1.stockNum,0),x1.chemical_unit) restStock FROM (
  391. SELECT cjc.`chemical_id`,SUM(s.usages-s.tare) stockNum, che.chemical_unit FROM hxp_stock s
  392. LEFT JOIN hxp_chemical_join_cabinet cjc ON s.`join_id` = cjc.`id`
  393. LEFT JOIN lab_subject st ON s.sub_id = st.id
  394. left join hxp_chemical che on che.id = cjc.chemical_id
  395. <where>
  396. s.status = 1
  397. <if test="chemicalId != null ">
  398. AND cjc.`chemical_id` = #{chemicalId}
  399. </if>
  400. <if test="cabinetId != null ">
  401. AND cjc.`cabinet_id` = #{cabinetId}
  402. </if>
  403. </where>
  404. GROUP BY cjc.`chemical_id`
  405. )x1
  406. UNION ALL
  407. SELECT 4 chemicalId,CASE WHEN IFNULL(x1.applyNum,0)-IFNULL(x2.applyNum,0)>0
  408. THEN IFNULL(x1.applyNum,0)-IFNULL(x2.applyNum,0) ELSE 0 END restStock FROM (
  409. SELECT hd.`chemical_id`,IFNULL(SUM(hd.`apply_num`),0) applyNum FROM act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  410. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  411. LEFT JOIN lab_subject st ON ah.sub_id = st.id
  412. <where>
  413. ak.`approval_status`=1 AND hd.`tick_status`=1
  414. <if test="deptId != null ">
  415. AND st.dept_id = #{deptId}
  416. </if>
  417. <if test="subId != null ">
  418. AND ah.sub_id = #{subId}
  419. </if>
  420. <if test="chemicalId != null ">
  421. AND hd.`chemical_id` = #{chemicalId}
  422. </if>
  423. <if test="cabinetId != null ">
  424. AND hd.`cabinet_id` = #{cabinetId}
  425. </if>
  426. <if test="applyUserId != null ">
  427. AND ah.`user_id` = #{applyUserId}
  428. </if>
  429. </where>
  430. GROUP BY hd.`chemical_id`
  431. )x1 LEFT JOIN
  432. (
  433. SELECT t1.`chemical_id`,SUM(IFNULL(t1.`chemical_amount`,0)-IFNULL(t1.`tare`,0)) applyNum FROM(
  434. SELECT DISTINCT cjc.id,hd.`chemical_id`,cjc.`chemical_amount`,cjc.`tare`
  435. FROM act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  436. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  437. LEFT JOIN lab_subject st ON ah.sub_id = st.id
  438. inner join hxp_chemical_join_cabinet cjc on hd.`cabinet_id` = cjc.`cabinet_id` and hd.`chemical_id` = cjc.`chemical_id`
  439. <where>
  440. ak.`approval_status`=1 AND hd.`tick_status`=1
  441. <if test="deptId != null ">
  442. AND st.dept_id = #{deptId}
  443. </if>
  444. <if test="subId != null ">
  445. AND ah.sub_id = #{subId}
  446. </if>
  447. <if test="chemicalId != null ">
  448. AND hd.`chemical_id` = #{chemicalId}
  449. </if>
  450. <if test="cabinetId != null ">
  451. AND hd.`cabinet_id` = #{cabinetId}
  452. </if>
  453. <if test="applyUserId != null ">
  454. AND ah.`user_id` = #{applyUserId} AND cjc.`user_id` = #{applyUserId}
  455. </if>
  456. </where>
  457. )t1
  458. GROUP BY t1.`chemical_id`
  459. )x2 ON x1.chemical_id = x2.chemical_id
  460. </select>
  461. <select id="getRestStockByManyCabinet" parameterType="com.zd.chemical.domain.vo.ActRestStockManySearch" resultType="com.zd.chemical.domain.vo.ActRestStockManyVo">
  462. SELECT x1.cabinet_id cabinetId,CASE WHEN CONCAT((IFNULL(x1.applyNum,0)-IFNULL(x2.stockNum,0)))>0
  463. THEN CONCAT((IFNULL(x1.applyNum,0)-IFNULL(x2.stockNum,0))) ELSE 0 END restStock FROM (
  464. SELECT hd.`cabinet_id`,IFNULL(SUM(hd.`apply_num`),0) applyNum FROM act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  465. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  466. LEFT JOIN lab_subject st ON ah.sub_id = st.id
  467. <where>
  468. ak.`approval_status`=1 AND hd.`tick_status`=1
  469. <if test="chemicalId != null ">
  470. and hd.`chemical_id` = #{chemicalId}
  471. </if>
  472. <if test="cabinetIds != null ">
  473. and hd.`cabinet_id` in
  474. <foreach item="id" collection="cabinetIds" open="(" separator="," close=")">
  475. #{id}
  476. </foreach>
  477. </if>
  478. <if test="applyUserId != null ">
  479. AND ah.`user_id` = #{applyUserId}
  480. </if>
  481. </where>
  482. GROUP BY hd.`cabinet_id`
  483. )x1 LEFT JOIN
  484. (
  485. SELECT t1.`chemical_id`,t1.`cabinet_id`,SUM(t1.chemical_amount-t1.tare) stockNum from(
  486. select distinct cjc.id,cjc.`chemical_id`,cjc.`cabinet_id`,cjc.chemical_amount,cjc.tare FROM
  487. act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  488. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  489. INNER JOIN hxp_chemical_join_cabinet cjc ON hd.`cabinet_id` = cjc.`cabinet_id` AND hd.`chemical_id` = cjc.`chemical_id`
  490. <where>
  491. ak.`approval_status`=1 AND hd.`tick_status`=1
  492. <if test="chemicalId != null ">
  493. and cjc.`chemical_id` = #{chemicalId}
  494. </if>
  495. <if test="cabinetIds != null ">
  496. and cjc.`cabinet_id` in
  497. <foreach item="id" collection="cabinetIds" open="(" separator="," close=")">
  498. #{id}
  499. </foreach>
  500. </if>
  501. <if test="applyUserId != null ">
  502. AND ah.`user_id` = #{applyUserId} AND cjc.`user_id` = #{applyUserId}
  503. </if>
  504. </where>
  505. )t1
  506. GROUP BY t1.`cabinet_id`
  507. )x2 ON x1.cabinet_id = x2.cabinet_id
  508. </select>
  509. <select id="getRestStockByManyCabinetAndChemical" parameterType="com.zd.chemical.domain.vo.ActRestStockManySearch" resultType="com.zd.chemical.domain.vo.ActRestStockManyVo">
  510. SELECT x1.chemical_id chemicalId,x1.cabinet_id cabinetId,CASE WHEN CONCAT((IFNULL(x1.applyNum,0)-IFNULL(x2.stockNum,0)))>0
  511. THEN CONCAT((IFNULL(x1.applyNum,0)-IFNULL(x2.stockNum,0))) ELSE 0 END restStock FROM (
  512. SELECT hd.`chemical_id`,hd.`cabinet_id`,IFNULL(SUM(hd.`apply_num`),0) applyNum FROM act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  513. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  514. LEFT JOIN lab_subject st ON ah.sub_id = st.id
  515. WHERE
  516. ak.`approval_status`=1 AND hd.`tick_status`=1
  517. <if test="chemicalIds != null ">
  518. AND hd.`chemical_id` IN
  519. <foreach item="id" collection="chemicalIds" open="(" separator="," close=")">
  520. #{id}
  521. </foreach>
  522. </if>
  523. <if test="cabinetIds != null ">
  524. AND hd.`cabinet_id` IN
  525. <foreach item="id" collection="cabinetIds" open="(" separator="," close=")">
  526. #{id}
  527. </foreach>
  528. </if>
  529. <if test="applyUserId != null ">
  530. AND ah.`user_id` = #{applyUserId}
  531. </if>
  532. GROUP BY hd.`chemical_id`,hd.`cabinet_id`
  533. )x1 LEFT JOIN
  534. (
  535. SELECT t1.`chemical_id`,t1.`cabinet_id`,SUM(t1.chemical_amount-t1.tare) stockNum FROM(
  536. SELECT DISTINCT cjc.id,cjc.`chemical_id`,cjc.`cabinet_id`,cjc.chemical_amount,cjc.tare FROM
  537. act_hxpapply ah LEFT JOIN act_hxpapply_detail hd ON ah.`id` = hd.`hxpapply_id`
  538. LEFT JOIN act_applytask ak ON ah.`id` = ak.`task_id`
  539. INNER JOIN hxp_chemical_join_cabinet cjc ON hd.`cabinet_id` = cjc.`cabinet_id` AND hd.`chemical_id` = cjc.`chemical_id`
  540. WHERE
  541. ak.`approval_status`=1 AND hd.`tick_status`=1
  542. <if test="chemicalIds != null ">
  543. AND cjc.`chemical_id` IN
  544. <foreach item="id" collection="chemicalIds" open="(" separator="," close=")">
  545. #{id}
  546. </foreach>
  547. </if>
  548. <if test="cabinetIds != null ">
  549. AND cjc.`cabinet_id` IN
  550. <foreach item="id" collection="cabinetIds" open="(" separator="," close=")">
  551. #{id}
  552. </foreach>
  553. </if>
  554. <if test="applyUserId != null ">
  555. AND ah.`user_id` = #{applyUserId} AND cjc.`user_id` = #{applyUserId}
  556. </if>
  557. )t1
  558. GROUP BY t1.`chemical_id`,t1.`cabinet_id`
  559. )x2 ON x1.chemical_id = x2.chemical_id AND x1.cabinet_id = x2.cabinet_id
  560. </select>
  561. <insert id="insertActHxpapply" parameterType="com.zd.chemical.domain.ActHxpapply" useGeneratedKeys="true" keyProperty="id">
  562. insert into act_hxpapply
  563. <trim prefix="(" suffix=")" suffixOverrides=",">
  564. <if test="applyNum != null">apply_num,</if>
  565. <if test="applyReason != null">apply_reason,</if>
  566. <if test="subId != null">sub_id,</if>
  567. <if test="applyStatus != null">apply_status,</if>
  568. <if test="userId != null">user_id,</if>
  569. <if test="createBy != null">create_by,</if>
  570. <if test="createTime != null">create_time,</if>
  571. <if test="updateBy != null">update_by,</if>
  572. <if test="updateTime != null">update_time,</if>
  573. <if test="remark != null">remark,</if>
  574. </trim>
  575. <trim prefix="values (" suffix=")" suffixOverrides=",">
  576. <if test="applyNum != null">#{applyNum},</if>
  577. <if test="applyReason != null">#{applyReason},</if>
  578. <if test="subId != null">#{subId},</if>
  579. <if test="applyStatus != null">#{applyStatus},</if>
  580. <if test="userId != null">#{userId},</if>
  581. <if test="createBy != null">#{createBy},</if>
  582. <if test="createTime != null">#{createTime},</if>
  583. <if test="updateBy != null">#{updateBy},</if>
  584. <if test="updateTime != null">#{updateTime},</if>
  585. <if test="remark != null">#{remark},</if>
  586. </trim>
  587. </insert>
  588. <update id="updateActHxpapply" parameterType="com.zd.chemical.domain.ActHxpapply">
  589. update act_hxpapply
  590. <trim prefix="SET" suffixOverrides=",">
  591. <if test="applyNum != null">apply_num = #{applyNum},</if>
  592. <if test="applyReason != null">apply_reason = #{applyReason},</if>
  593. <if test="subId != null">sub_id = #{subId},</if>
  594. <if test="applyStatus != null">apply_status = #{applyStatus},</if>
  595. <if test="userId != null">user_id = #{userId},</if>
  596. <if test="createBy != null">create_by = #{createBy},</if>
  597. <if test="createTime != null">create_time = #{createTime},</if>
  598. <if test="updateBy != null">update_by = #{updateBy},</if>
  599. <if test="updateTime != null">update_time = #{updateTime},</if>
  600. <if test="remark != null">remark = #{remark},</if>
  601. </trim>
  602. where id = #{id}
  603. </update>
  604. <delete id="deleteActHxpapplyById">
  605. delete from act_hxpapply where id = #{id}
  606. </delete>
  607. <delete id="deleteActHxpapplyByIds">
  608. delete from act_hxpapply where id in
  609. <foreach item="id" collection="array" open="(" separator="," close=")">
  610. #{id}
  611. </foreach>
  612. </delete>
  613. </mapper>