EarlyWarningMapper.xml 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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.zksy.base.warning.mapper.EarlyWarningMapper">
  4. <resultMap id="BaseResultMap" type="com.zksy.base.warning.domain.EarlyWarning">
  5. <id column="warning_id" property="warningId"/>
  6. <result column="warning_no" property="warningNo"/>
  7. <result column="warning_type" property="warningType"/>
  8. <result column="warning_level" property="warningLevel"/>
  9. <result column="warning_special" property="warningSpecial"/>
  10. <result column="disposal_deadline" property="disposalDeadline"/>
  11. <result column="stage_deadline" property="stageDeadline"/>
  12. <result column="item_id" property="itemId"/>
  13. <result column="item_snapshot" property="itemSnapshot"/>
  14. <result column="device_code" property="deviceCode"/>
  15. <result column="device_name" property="deviceName"/>
  16. <result column="location" property="location"/>
  17. <result column="ownership_unit" property="ownershipUnit"/>
  18. <result column="publisher" property="publisher"/>
  19. <result column="publish_time" property="publishTime"/>
  20. <result column="handler" property="handler"/>
  21. <result column="reviewer" property="reviewer"/>
  22. <result column="status" property="status"/>
  23. <result column="warning_content" property="warningContent"/>
  24. <result column="process_instance_id" property="processInstanceId"/>
  25. <result column="create_by" property="createBy"/>
  26. <result column="create_time" property="createTime"/>
  27. <result column="update_by" property="updateBy"/>
  28. <result column="update_time" property="updateTime"/>
  29. <result column="remark" property="remark"/>
  30. </resultMap>
  31. <!-- 预警位置不再落库:统一由设备台账位置回填,出参字段名 location 保持不变。 -->
  32. <sql id="DeviceLocationColumn">(SELECT e.equipment_location FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS location</sql>
  33. <sql id="Base_Column_List">
  34. warning_id, warning_no, warning_type, warning_level, warning_special,
  35. disposal_deadline, stage_deadline, item_id, item_snapshot, device_code, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name,
  36. <include refid="DeviceLocationColumn"/>, ownership_unit, publisher, publish_time, handler, reviewer,
  37. status, warning_content, process_instance_id, create_by, create_time, update_by,
  38. update_time, remark
  39. </sql>
  40. <!-- The city-life project only contains water supply, drainage, gas and manhole warnings. -->
  41. <sql id="SupportedDashboardWarningTypes">
  42. AND EXISTS (SELECT 1 FROM app_user.warning_type wt WHERE wt.warning_code = w.warning_type)
  43. </sql>
  44. <sql id="DashboardWarningTypeFilter">
  45. <if test="warningType != null and warningType != ''">AND w.warning_type = #{warningType}</if>
  46. </sql>
  47. <sql id="DashboardWarningLevelFilter">
  48. <if test="warningLevel != null and warningLevel != ''">AND w.warning_level = #{warningLevel}</if>
  49. </sql>
  50. <sql id="DashboardWarningColumns">
  51. w.warning_id,
  52. w.warning_no,
  53. w.warning_type,
  54. (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  55. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  56. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name,
  57. w.warning_level,
  58. w.warning_special,
  59. <include refid="DeviceLocationColumn"/>,
  60. w.ownership_unit,
  61. w.publisher,
  62. w.publish_time,
  63. w.handler,
  64. w.status,
  65. w.warning_content,
  66. w.create_time,
  67. w.update_time,
  68. w.device_code,
  69. (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name
  70. </sql>
  71. <sql id="DashboardResolvedTimeJoin">
  72. LEFT JOIN (
  73. SELECT warning_id, MAX(create_time) AS resolved_time
  74. FROM app_user.warning_disposal
  75. WHERE disposal_type IN ('RESOLVE', 'CLOSE', 'CLEAR', 'AUTO_CLEAR')
  76. GROUP BY warning_id
  77. ) resolved_record ON resolved_record.warning_id = w.warning_id
  78. </sql>
  79. <!-- 统计今日预警核心指标 -->
  80. <select id="selectTodayWarningStats" resultType="java.util.Map">
  81. SELECT
  82. COUNT(CASE WHEN w.status != 'DRAFT' THEN 1 END) AS today_total,
  83. COUNT(CASE WHEN w.status = 'HANDLED' THEN 1 END) AS handled_today,
  84. COUNT(CASE WHEN w.status = 'CLOSED' THEN 1 END) AS closed_today
  85. FROM app_user.early_warning w
  86. WHERE w.publish_time &gt;= CURRENT_DATE
  87. AND w.publish_time &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  88. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  89. <include refid="SupportedDashboardWarningTypes"/>
  90. </select>
  91. <!-- 查询今日预警列表 -->
  92. <select id="selectTodayWarningList" resultMap="BaseResultMap">
  93. SELECT <include refid="Base_Column_List"/>
  94. FROM app_user.early_warning w
  95. WHERE w.publish_time &gt;= CURRENT_DATE
  96. AND w.publish_time &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  97. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  98. <include refid="SupportedDashboardWarningTypes"/>
  99. <choose>
  100. <when test="status != null and status != ''">
  101. AND w.status = #{status}
  102. </when>
  103. <otherwise>
  104. AND w.status != 'DRAFT'
  105. </otherwise>
  106. </choose>
  107. ORDER BY w.publish_time DESC
  108. </select>
  109. <!-- 按日统计预警发展趋势 -->
  110. <select id="selectWarningTrendByDay" resultType="java.util.Map">
  111. SELECT
  112. TO_CHAR(publish_time, 'YYYY-MM-DD') AS stat_date,
  113. COALESCE(NULLIF(warning_special, ''), '其他') AS warning_special,
  114. COUNT(*) AS warning_count
  115. FROM app_user.early_warning
  116. WHERE status != 'DRAFT'
  117. AND EXISTS (SELECT 1 FROM app_user.warning_type wt WHERE wt.warning_code = early_warning.warning_type)
  118. <if test="startTime != null">
  119. AND publish_time &gt;= #{startTime}
  120. </if>
  121. <if test="endTime != null">
  122. AND publish_time &lt; #{endTime}
  123. </if>
  124. <if test="warningSpecial != null and warningSpecial != ''">
  125. AND COALESCE(NULLIF(warning_special, ''), '其他') = #{warningSpecial}
  126. </if>
  127. GROUP BY TO_CHAR(publish_time, 'YYYY-MM-DD'), COALESCE(NULLIF(warning_special, ''), '其他')
  128. ORDER BY stat_date ASC, warning_special ASC
  129. </select>
  130. <!-- 按月统计预警发展趋势 -->
  131. <select id="selectWarningTrendByMonth" resultType="java.util.Map">
  132. SELECT
  133. TO_CHAR(publish_time, 'YYYY-MM') AS stat_month,
  134. COALESCE(NULLIF(warning_special, ''), '其他') AS warning_special,
  135. COUNT(*) AS warning_count
  136. FROM app_user.early_warning
  137. WHERE status != 'DRAFT'
  138. AND EXISTS (SELECT 1 FROM app_user.warning_type wt WHERE wt.warning_code = early_warning.warning_type)
  139. <if test="startTime != null">
  140. AND publish_time &gt;= #{startTime}
  141. </if>
  142. <if test="endTime != null">
  143. AND publish_time &lt; #{endTime}
  144. </if>
  145. <if test="warningSpecial != null and warningSpecial != ''">
  146. AND COALESCE(NULLIF(warning_special, ''), '其他') = #{warningSpecial}
  147. </if>
  148. GROUP BY TO_CHAR(publish_time, 'YYYY-MM'), COALESCE(NULLIF(warning_special, ''), '其他')
  149. ORDER BY stat_month ASC, warning_special ASC
  150. </select>
  151. <!-- 处置状态分析:按预警状态分组统计数量 -->
  152. <select id="selectWarningGroupByStatus" resultType="java.util.Map">
  153. SELECT
  154. status,
  155. COUNT(warning_id) AS status_count
  156. FROM app_user.early_warning
  157. WHERE status != 'DRAFT'
  158. AND EXISTS (SELECT 1 FROM app_user.warning_type wt WHERE wt.warning_code = early_warning.warning_type)
  159. <if test="startTime != null">
  160. AND publish_time &gt;= #{startTime}
  161. </if>
  162. <if test="endTime != null">
  163. AND publish_time &lt; #{endTime}
  164. </if>
  165. <if test="warningSpecial != null and warningSpecial != ''">
  166. AND warning_special = #{warningSpecial}
  167. </if>
  168. GROUP BY status
  169. ORDER BY status_count DESC
  170. </select>
  171. <select id="selectDisposalGroupByType" resultType="java.util.Map">
  172. SELECT d.disposal_type, COUNT(*) AS disposal_count
  173. FROM app_user.warning_disposal d
  174. INNER JOIN app_user.early_warning w ON w.warning_id = d.warning_id
  175. WHERE w.status != 'DRAFT'
  176. <include refid="SupportedDashboardWarningTypes"/>
  177. <if test="startTime != null">
  178. AND w.publish_time &gt;= #{startTime}
  179. </if>
  180. <if test="endTime != null">
  181. AND w.publish_time &lt; #{endTime}
  182. </if>
  183. <if test="warningSpecial != null and warningSpecial != ''">
  184. AND w.warning_special = #{warningSpecial}
  185. </if>
  186. GROUP BY d.disposal_type
  187. ORDER BY disposal_count DESC
  188. </select>
  189. <!-- 处置时效分析-概览指标统计 -->
  190. <select id="selectDisposeEfficiencyOverview" resultType="java.util.Map">
  191. SELECT
  192. ROUND(AVG(dispose_hours), 2) AS avg_hours,
  193. ROUND(MAX(dispose_hours), 2) AS max_hours,
  194. ROUND(MIN(dispose_hours), 2) AS min_hours,
  195. COUNT(*) AS total_count
  196. FROM (
  197. SELECT
  198. EXTRACT(EPOCH FROM (CAST(MAX(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600 AS dispose_hours
  199. FROM app_user.early_warning w
  200. INNER JOIN app_user.warning_disposal d
  201. ON w.warning_id = d.warning_id
  202. AND d.disposal_type IN ('HANDLE', 'RESOLVE', 'CLEAR', 'CLOSE', 'MISREPORT', 'AUTO_CLEAR')
  203. WHERE w.status IN ('HANDLED', 'CLOSED')
  204. <include refid="SupportedDashboardWarningTypes"/>
  205. <if test="startTime != null">
  206. AND w.publish_time &gt;= #{startTime}
  207. </if>
  208. <if test="endTime != null">
  209. AND w.publish_time &lt; #{endTime}
  210. </if>
  211. <if test="warningSpecial != null and warningSpecial != ''">
  212. AND w.warning_special = #{warningSpecial}
  213. </if>
  214. GROUP BY w.warning_id, w.publish_time
  215. HAVING MAX(d.create_time) &gt;= w.publish_time
  216. ) AS temp
  217. </select>
  218. <!-- 处置时效分析-时效区间分布统计 -->
  219. <select id="selectDisposeEfficiencyRange" resultType="java.util.Map">
  220. SELECT
  221. CASE
  222. WHEN dispose_hours &lt;= 2 THEN '0-2小时'
  223. WHEN dispose_hours &lt;= 6 THEN '2-6小时'
  224. WHEN dispose_hours &lt;= 24 THEN '6-24小时'
  225. ELSE '24小时以上'
  226. END AS time_range,
  227. COUNT(*) AS warning_count
  228. FROM (
  229. SELECT
  230. EXTRACT(EPOCH FROM (CAST(MAX(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600 AS dispose_hours
  231. FROM app_user.early_warning w
  232. INNER JOIN app_user.warning_disposal d
  233. ON w.warning_id = d.warning_id
  234. AND d.disposal_type IN ('HANDLE', 'RESOLVE', 'CLEAR', 'CLOSE', 'MISREPORT', 'AUTO_CLEAR')
  235. WHERE w.status IN ('HANDLED', 'CLOSED')
  236. <include refid="SupportedDashboardWarningTypes"/>
  237. <if test="startTime != null">
  238. AND w.publish_time &gt;= #{startTime}
  239. </if>
  240. <if test="endTime != null">
  241. AND w.publish_time &lt; #{endTime}
  242. </if>
  243. <if test="warningSpecial != null and warningSpecial != ''">
  244. AND w.warning_special = #{warningSpecial}
  245. </if>
  246. GROUP BY w.warning_id, w.publish_time
  247. HAVING MAX(d.create_time) &gt;= w.publish_time
  248. ) AS temp
  249. GROUP BY time_range
  250. ORDER BY
  251. CASE time_range
  252. WHEN '0-2小时' THEN 1
  253. WHEN '2-6小时' THEN 2
  254. WHEN '6-24小时' THEN 3
  255. ELSE 4
  256. END ASC
  257. </select>
  258. <!-- 处置时效分析-按预警专项统计平均时效 -->
  259. <select id="selectDisposeEfficiencyBySpecial" resultType="java.util.Map">
  260. SELECT
  261. COALESCE(NULLIF(warning_special, ''), '其他') AS warning_special,
  262. COUNT(*) AS warning_count,
  263. ROUND(AVG(dispose_hours), 2) AS avg_dispose_hours
  264. FROM (
  265. SELECT
  266. COALESCE(NULLIF(w.warning_special, ''), '其他') AS warning_special,
  267. EXTRACT(EPOCH FROM (CAST(MAX(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600 AS dispose_hours
  268. FROM app_user.early_warning w
  269. INNER JOIN app_user.warning_disposal d
  270. ON w.warning_id = d.warning_id
  271. AND d.disposal_type IN ('HANDLE', 'RESOLVE', 'CLEAR', 'CLOSE', 'MISREPORT', 'AUTO_CLEAR')
  272. WHERE w.status IN ('HANDLED', 'CLOSED')
  273. <include refid="SupportedDashboardWarningTypes"/>
  274. <if test="startTime != null">
  275. AND w.publish_time &gt;= #{startTime}
  276. </if>
  277. <if test="endTime != null">
  278. AND w.publish_time &lt; #{endTime}
  279. </if>
  280. <if test="warningSpecial != null and warningSpecial != ''">
  281. AND COALESCE(NULLIF(w.warning_special, ''), '其他') = #{warningSpecial}
  282. </if>
  283. GROUP BY w.warning_id, COALESCE(NULLIF(w.warning_special, ''), '其他'), w.publish_time
  284. HAVING MAX(d.create_time) &gt;= w.publish_time
  285. ) AS temp
  286. GROUP BY warning_special
  287. ORDER BY avg_dispose_hours DESC
  288. </select>
  289. <!-- 处置时效分析-明细列表 -->
  290. <select id="selectDisposeEfficiencyList" resultType="java.util.Map">
  291. SELECT
  292. w.warning_id,
  293. w.warning_no,
  294. COALESCE(w.warning_special, '其他') AS warning_special,
  295. w.publish_time,
  296. MAX(d.create_time) AS dispose_time,
  297. ROUND(EXTRACT(EPOCH FROM (CAST(MAX(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600, 2) AS dispose_hours,
  298. w.status
  299. FROM app_user.early_warning w
  300. INNER JOIN app_user.warning_disposal d
  301. ON w.warning_id = d.warning_id
  302. AND d.disposal_type IN ('HANDLE', 'RESOLVE', 'CLEAR', 'CLOSE', 'MISREPORT', 'AUTO_CLEAR')
  303. WHERE w.status IN ('HANDLED', 'CLOSED')
  304. <include refid="SupportedDashboardWarningTypes"/>
  305. <if test="startTime != null">
  306. AND w.publish_time &gt;= #{startTime}
  307. </if>
  308. <if test="endTime != null">
  309. AND w.publish_time &lt; #{endTime}
  310. </if>
  311. <if test="warningSpecial != null and warningSpecial != ''">
  312. AND w.warning_special = #{warningSpecial}
  313. </if>
  314. GROUP BY w.warning_id, w.warning_no, w.warning_special, w.publish_time, w.status
  315. HAVING MAX(d.create_time) &gt;= w.publish_time
  316. ORDER BY dispose_hours
  317. <if test="sortType != null and sortType == 'desc'">
  318. DESC
  319. </if>
  320. <if test="sortType == null or sortType == 'asc'">
  321. ASC
  322. </if>
  323. </select>
  324. <!-- 接警效率分析-概览指标统计 -->
  325. <select id="selectReceiveEfficiencyOverview" resultType="java.util.Map">
  326. SELECT
  327. ROUND(AVG(receive_hours), 2) AS avg_hours,
  328. ROUND(MAX(receive_hours), 2) AS max_hours,
  329. ROUND(MIN(receive_hours), 2) AS min_hours,
  330. COUNT(*) AS total_count
  331. FROM (
  332. SELECT
  333. EXTRACT(EPOCH FROM (CAST(MIN(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600 AS receive_hours
  334. FROM app_user.early_warning w
  335. INNER JOIN app_user.warning_disposal d ON w.warning_id = d.warning_id
  336. WHERE w.status != 'DRAFT'
  337. <include refid="SupportedDashboardWarningTypes"/>
  338. <if test="startTime != null">
  339. AND w.publish_time &gt;= #{startTime}
  340. </if>
  341. <if test="endTime != null">
  342. AND w.publish_time &lt; #{endTime}
  343. </if>
  344. <include refid="DashboardWarningTypeFilter"/>
  345. <if test="warningSpecial != null and warningSpecial != ''">
  346. AND w.warning_special = #{warningSpecial}
  347. </if>
  348. <if test="status != null and status != ''">
  349. AND w.status = #{status}
  350. </if>
  351. GROUP BY w.warning_id, w.publish_time
  352. HAVING MIN(d.create_time) &gt;= w.publish_time
  353. ) AS temp
  354. </select>
  355. <!-- 接警效率分析-时效区间分布统计 -->
  356. <select id="selectReceiveEfficiencyRange" resultType="java.util.Map">
  357. SELECT
  358. CASE
  359. WHEN receive_hours &lt;= 0.5 THEN '0-30分钟'
  360. WHEN receive_hours &lt;= 2 THEN '30分钟-2小时'
  361. WHEN receive_hours &lt;= 6 THEN '2-6小时'
  362. ELSE '6小时以上'
  363. END AS time_range,
  364. COUNT(*) AS warning_count
  365. FROM (
  366. SELECT
  367. EXTRACT(EPOCH FROM (CAST(MIN(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600 AS receive_hours
  368. FROM app_user.early_warning w
  369. INNER JOIN app_user.warning_disposal d ON w.warning_id = d.warning_id
  370. WHERE w.status != 'DRAFT'
  371. <include refid="SupportedDashboardWarningTypes"/>
  372. <if test="startTime != null">
  373. AND w.publish_time &gt;= #{startTime}
  374. </if>
  375. <if test="endTime != null">
  376. AND w.publish_time &lt; #{endTime}
  377. </if>
  378. <include refid="DashboardWarningTypeFilter"/>
  379. <if test="warningSpecial != null and warningSpecial != ''">
  380. AND w.warning_special = #{warningSpecial}
  381. </if>
  382. <if test="status != null and status != ''">
  383. AND w.status = #{status}
  384. </if>
  385. GROUP BY w.warning_id, w.publish_time
  386. HAVING MIN(d.create_time) &gt;= w.publish_time
  387. ) AS temp
  388. GROUP BY time_range
  389. ORDER BY
  390. CASE time_range
  391. WHEN '0-30分钟' THEN 1
  392. WHEN '30分钟-2小时' THEN 2
  393. WHEN '2-6小时' THEN 3
  394. ELSE 4
  395. END ASC
  396. </select>
  397. <!-- 接警效率分析-按预警类型统计平均时效 -->
  398. <select id="selectReceiveEfficiencyByType" resultType="java.util.Map">
  399. SELECT
  400. COALESCE(warning_type, '其他') AS warning_type,
  401. COUNT(*) AS warning_count,
  402. ROUND(AVG(receive_hours), 2) AS avg_receive_hours
  403. FROM (
  404. SELECT
  405. w.warning_type,
  406. EXTRACT(EPOCH FROM (CAST(MIN(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600 AS receive_hours
  407. FROM app_user.early_warning w
  408. INNER JOIN app_user.warning_disposal d ON w.warning_id = d.warning_id
  409. WHERE w.status != 'DRAFT'
  410. <include refid="SupportedDashboardWarningTypes"/>
  411. <if test="startTime != null">
  412. AND w.publish_time &gt;= #{startTime}
  413. </if>
  414. <if test="endTime != null">
  415. AND w.publish_time &lt; #{endTime}
  416. </if>
  417. <include refid="DashboardWarningTypeFilter"/>
  418. <if test="warningSpecial != null and warningSpecial != ''">
  419. AND w.warning_special = #{warningSpecial}
  420. </if>
  421. <if test="status != null and status != ''">
  422. AND w.status = #{status}
  423. </if>
  424. GROUP BY w.warning_id, w.warning_type, w.publish_time
  425. HAVING MIN(d.create_time) &gt;= w.publish_time
  426. ) AS temp
  427. GROUP BY warning_type
  428. ORDER BY avg_receive_hours DESC
  429. </select>
  430. <!-- 接警效率分析-明细列表 -->
  431. <select id="selectReceiveEfficiencyList" resultType="java.util.Map">
  432. SELECT
  433. w.warning_id,
  434. w.warning_no,
  435. COALESCE(w.warning_type, '其他') AS warning_type,
  436. COALESCE(w.warning_special, '其他') AS warning_special,
  437. w.publish_time,
  438. MIN(d.create_time) AS receive_time,
  439. ROUND(EXTRACT(EPOCH FROM (CAST(MIN(d.create_time) AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ))) / 3600, 2) AS receive_hours,
  440. w.status
  441. FROM app_user.early_warning w
  442. INNER JOIN app_user.warning_disposal d ON w.warning_id = d.warning_id
  443. WHERE w.status != 'DRAFT'
  444. <include refid="SupportedDashboardWarningTypes"/>
  445. <if test="startTime != null">
  446. AND w.publish_time &gt;= #{startTime}
  447. </if>
  448. <if test="endTime != null">
  449. AND w.publish_time &lt; #{endTime}
  450. </if>
  451. <include refid="DashboardWarningTypeFilter"/>
  452. <if test="warningSpecial != null and warningSpecial != ''">
  453. AND w.warning_special = #{warningSpecial}
  454. </if>
  455. <if test="status != null and status != ''">
  456. AND w.status = #{status}
  457. </if>
  458. GROUP BY w.warning_id, w.warning_no, w.warning_type,
  459. w.warning_special, w.publish_time, w.status
  460. HAVING MIN(d.create_time) &gt;= w.publish_time
  461. ORDER BY receive_hours
  462. <if test="sortType != null and sortType == 'desc'">
  463. DESC
  464. </if>
  465. <if test="sortType == null or sortType == 'asc'">
  466. ASC
  467. </if>
  468. </select>
  469. <!-- ————————————————————————————————————————————————————————————— -->
  470. <!-- Warning overview: headline numbers for current active and resolved-today warnings. -->
  471. <select id="selectDashboardTodaySummary" resultType="java.util.Map">
  472. SELECT
  473. COUNT(CASE
  474. WHEN w.publish_time &gt;= CURRENT_DATE
  475. AND w.publish_time &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  476. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  477. AND w.status != 'DRAFT'
  478. THEN 1 END) AS today_total,
  479. COUNT(CASE
  480. WHEN w.publish_time &lt;= CURRENT_TIMESTAMP
  481. AND w.status NOT IN ('DRAFT', 'CLOSED')
  482. THEN 1 END) AS unresolved_count,
  483. COUNT(CASE
  484. WHEN w.status IN ('CLOSED')
  485. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) &gt;= CURRENT_DATE
  486. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time)
  487. &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  488. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) &lt;= CURRENT_TIMESTAMP
  489. THEN 1 END) AS resolved_count
  490. FROM app_user.early_warning w
  491. <include refid="DashboardResolvedTimeJoin"/>
  492. WHERE 1 = 1
  493. <include refid="SupportedDashboardWarningTypes"/>
  494. </select>
  495. <!-- Small detail lists returned with the today overview cards. -->
  496. <select id="selectDashboardTodayPreview" resultType="java.util.Map">
  497. SELECT
  498. <include refid="DashboardWarningColumns"/>,
  499. COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) AS resolved_time,
  500. ROUND(
  501. EXTRACT(EPOCH FROM (
  502. CAST(CURRENT_TIMESTAMP AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ)
  503. )) / 3600,
  504. 2
  505. ) AS waiting_hours
  506. FROM app_user.early_warning w
  507. <include refid="DashboardResolvedTimeJoin"/>
  508. WHERE 1 = 1
  509. <include refid="SupportedDashboardWarningTypes"/>
  510. <choose>
  511. <when test="category == 'resolved'">
  512. AND w.status IN ('CLOSED')
  513. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) &gt;= CURRENT_DATE
  514. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time)
  515. &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  516. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) &lt;= CURRENT_TIMESTAMP
  517. ORDER BY resolved_time DESC, w.warning_id DESC
  518. </when>
  519. <otherwise>
  520. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  521. AND w.status NOT IN ('DRAFT', 'CLOSED')
  522. ORDER BY
  523. CASE w.warning_level
  524. WHEN '4' THEN 1 WHEN 'RED' THEN 1
  525. WHEN '3' THEN 2 WHEN 'ORANGE' THEN 2
  526. WHEN '2' THEN 3 WHEN 'YELLOW' THEN 3
  527. WHEN '1' THEN 4 WHEN 'BLUE' THEN 4
  528. ELSE 5
  529. END,
  530. w.publish_time ASC,
  531. w.warning_id ASC
  532. </otherwise>
  533. </choose>
  534. LIMIT #{limit}
  535. </select>
  536. <!-- Paged detail list for the two today overview categories. -->
  537. <select id="selectDashboardTodayPage" resultType="java.util.Map">
  538. SELECT
  539. <include refid="DashboardWarningColumns"/>,
  540. COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) AS resolved_time,
  541. ROUND(
  542. EXTRACT(EPOCH FROM (
  543. CAST(CURRENT_TIMESTAMP AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ)
  544. )) / 3600,
  545. 2
  546. ) AS waiting_hours
  547. FROM app_user.early_warning w
  548. <include refid="DashboardResolvedTimeJoin"/>
  549. <where>
  550. <include refid="SupportedDashboardWarningTypes"/>
  551. <choose>
  552. <when test="category == 'resolved'">
  553. AND w.status IN ('CLOSED')
  554. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) &gt;= CURRENT_DATE
  555. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time)
  556. &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  557. AND COALESCE(resolved_record.resolved_time, w.update_time, w.publish_time) &lt;= CURRENT_TIMESTAMP
  558. </when>
  559. <when test="category == 'all'">
  560. AND w.publish_time &gt;= CURRENT_DATE
  561. AND w.publish_time &lt; CAST(CURRENT_DATE AS TIMESTAMP) + CAST('1 day' AS INTERVAL)
  562. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  563. AND w.status != 'DRAFT'
  564. </when>
  565. <otherwise>
  566. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  567. AND w.status NOT IN ('DRAFT', 'CLOSED')
  568. </otherwise>
  569. </choose>
  570. <include refid="DashboardWarningTypeFilter"/>
  571. <include refid="DashboardWarningLevelFilter"/>
  572. <if test="keyword != null and keyword != ''">
  573. AND (
  574. w.warning_no LIKE CONCAT('%', #{keyword}, '%')
  575. OR w.warning_special LIKE CONCAT('%', #{keyword}, '%')
  576. OR w.ownership_unit LIKE CONCAT('%', #{keyword}, '%')
  577. )
  578. </if>
  579. </where>
  580. <choose>
  581. <when test="category == 'resolved'">
  582. ORDER BY resolved_time DESC, w.warning_id DESC
  583. </when>
  584. <when test="category == 'all'">
  585. ORDER BY w.publish_time DESC, w.warning_id DESC
  586. </when>
  587. <otherwise>
  588. ORDER BY
  589. CASE w.warning_level
  590. WHEN '4' THEN 1 WHEN 'RED' THEN 1
  591. WHEN '3' THEN 2 WHEN 'ORANGE' THEN 2
  592. WHEN '2' THEN 3 WHEN 'YELLOW' THEN 3
  593. WHEN '1' THEN 4 WHEN 'BLUE' THEN 4
  594. ELSE 5
  595. END,
  596. w.publish_time ASC,
  597. w.warning_id ASC
  598. </otherwise>
  599. </choose>
  600. </select>
  601. <!-- Historical warning summary. Drafts and unsupported facility types are excluded. -->
  602. <select id="selectDashboardHistorySummary" resultType="java.util.Map">
  603. SELECT
  604. COUNT(*) AS total_count,
  605. COUNT(CASE WHEN w.status IN ('CLOSED') THEN 1 END) AS resolved_count,
  606. COUNT(CASE WHEN w.status NOT IN ('CLOSED') THEN 1 END) AS active_count
  607. FROM app_user.early_warning w
  608. WHERE w.status != 'DRAFT'
  609. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  610. <include refid="SupportedDashboardWarningTypes"/>
  611. </select>
  612. <select id="selectDashboardHistorySpecialStats" resultType="java.util.Map">
  613. WITH special_stats AS (
  614. SELECT
  615. COALESCE(NULLIF(w.warning_special, ''), '未分类') AS special_name,
  616. COUNT(*) AS warning_count
  617. FROM app_user.early_warning w
  618. WHERE w.status != 'DRAFT'
  619. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  620. <include refid="SupportedDashboardWarningTypes"/>
  621. <include refid="DashboardWarningTypeFilter"/>
  622. GROUP BY COALESCE(NULLIF(w.warning_special, ''), '未分类')
  623. )
  624. SELECT
  625. special_name,
  626. warning_count,
  627. ROUND(warning_count * 100.0 / NULLIF(SUM(warning_count) OVER (), 0), 2) AS percentage
  628. FROM special_stats
  629. ORDER BY warning_count DESC, special_name ASC
  630. </select>
  631. <select id="selectDashboardHistoryPage" resultType="java.util.Map">
  632. SELECT <include refid="DashboardWarningColumns"/>
  633. FROM app_user.early_warning w
  634. <where>
  635. AND w.status != 'DRAFT'
  636. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  637. <include refid="SupportedDashboardWarningTypes"/>
  638. <include refid="DashboardWarningTypeFilter"/>
  639. <if test="warningSpecial != null and warningSpecial != ''">
  640. AND COALESCE(NULLIF(w.warning_special, ''), '未分类') = #{warningSpecial}
  641. </if>
  642. <if test="keyword != null and keyword != ''">
  643. AND (
  644. w.warning_no LIKE CONCAT('%', #{keyword}, '%')
  645. OR w.ownership_unit LIKE CONCAT('%', #{keyword}, '%')
  646. )
  647. </if>
  648. </where>
  649. ORDER BY w.publish_time DESC, w.warning_id DESC
  650. </select>
  651. <!-- Outstanding warning list. One most-recent open todo is attached per warning. -->
  652. <select id="selectDashboardUnprocessedPage" resultType="java.util.Map">
  653. SELECT
  654. <include refid="DashboardWarningColumns"/>,
  655. open_todo.todo_id,
  656. open_todo.user_id AS todo_user_id,
  657. open_todo.user_name AS todo_user_name,
  658. open_todo.todo_type,
  659. open_todo.read_status,
  660. open_todo.task_name,
  661. open_todo.create_time AS todo_create_time,
  662. ROUND(
  663. EXTRACT(EPOCH FROM (
  664. CAST(CURRENT_TIMESTAMP AS TIMESTAMPTZ) - CAST(w.publish_time AS TIMESTAMPTZ)
  665. )) / 3600,
  666. 2
  667. ) AS waiting_hours
  668. FROM app_user.early_warning w
  669. LEFT JOIN (
  670. SELECT todo_id, warning_id, user_id, user_name, todo_type, read_status, task_name, create_time
  671. FROM (
  672. SELECT
  673. t.*,
  674. ROW_NUMBER() OVER (
  675. PARTITION BY t.warning_id
  676. ORDER BY t.create_time DESC, t.todo_id DESC
  677. ) AS row_num
  678. FROM app_user.warning_todo t
  679. WHERE t.complete_time IS NULL
  680. AND (t.todo_type IS NULL OR t.todo_type != 'DONE')
  681. ) ranked_todo
  682. WHERE row_num = 1
  683. ) open_todo ON open_todo.warning_id = w.warning_id
  684. <where>
  685. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  686. AND w.status IN ('PENDING', 'RECEIVED', 'PROCESSING')
  687. <include refid="SupportedDashboardWarningTypes"/>
  688. <include refid="DashboardWarningTypeFilter"/>
  689. <include refid="DashboardWarningLevelFilter"/>
  690. <if test="keyword != null and keyword != ''">
  691. AND (
  692. w.warning_no LIKE CONCAT('%', #{keyword}, '%')
  693. OR w.warning_special LIKE CONCAT('%', #{keyword}, '%')
  694. OR w.ownership_unit LIKE CONCAT('%', #{keyword}, '%')
  695. )
  696. </if>
  697. </where>
  698. ORDER BY
  699. CASE w.warning_level
  700. WHEN '4' THEN 1 WHEN 'RED' THEN 1
  701. WHEN '3' THEN 2 WHEN 'ORANGE' THEN 2
  702. WHEN '2' THEN 3 WHEN 'YELLOW' THEN 3
  703. WHEN '1' THEN 4 WHEN 'BLUE' THEN 4
  704. ELSE 5
  705. END,
  706. w.publish_time ASC,
  707. w.warning_id ASC
  708. </select>
  709. <!-- Aggregate cards for the unprocessed list. The filters mirror the page
  710. query so counts remain correct when the user searches or narrows a
  711. warning type/level. -->
  712. <select id="selectDashboardUnprocessedSummary" resultType="java.util.Map">
  713. SELECT
  714. COUNT(*) AS total,
  715. COUNT(DISTINCT NULLIF(w.ownership_unit, '')) AS ownership_count,
  716. MIN(CASE WHEN w.warning_level IN ('1', '2', '3', '4') THEN CAST(w.warning_level AS integer) END) AS highest_level
  717. FROM app_user.early_warning w
  718. <where>
  719. AND w.publish_time &lt;= CURRENT_TIMESTAMP
  720. AND w.status IN ('PENDING', 'RECEIVED', 'PROCESSING')
  721. <include refid="SupportedDashboardWarningTypes"/>
  722. <include refid="DashboardWarningTypeFilter"/>
  723. <include refid="DashboardWarningLevelFilter"/>
  724. <if test="keyword != null and keyword != ''">
  725. AND (
  726. w.warning_no LIKE CONCAT('%', #{keyword}, '%')
  727. OR w.warning_special LIKE CONCAT('%', #{keyword}, '%')
  728. OR w.ownership_unit LIKE CONCAT('%', #{keyword}, '%')
  729. )
  730. </if>
  731. </where>
  732. </select>
  733. <select id="selectWarningList" resultType="java.util.Map">
  734. SELECT w.*, <include refid="DeviceLocationColumn"/>, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  735. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  736. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name
  737. FROM early_warning w
  738. <where>
  739. <include refid="WarningVisibility"/>
  740. <if test="warningNo != null and warningNo != ''">AND w.warning_no LIKE CONCAT('%',#{warningNo},'%')</if>
  741. <if test="warningType != null and warningType != ''">
  742. AND w.warning_type = #{warningType}
  743. </if>
  744. <if test="warningLevel != null and warningLevel != ''">
  745. AND w.warning_level = #{warningLevel}
  746. </if>
  747. <if test="status != null and status != ''">
  748. AND w.status = #{status}
  749. </if>
  750. </where>
  751. ORDER BY w.create_time DESC
  752. </select>
  753. <select id="searchWarnings" resultType="java.util.Map">
  754. SELECT w.*, <include refid="DeviceLocationColumn"/>, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  755. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  756. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name
  757. FROM early_warning w
  758. <where>
  759. <include refid="WarningVisibility"/>
  760. <if test="warningNo != null and warningNo != ''">
  761. AND w.warning_no LIKE CONCAT('%', #{warningNo}, '%')
  762. </if>
  763. <if test="warningType != null and warningType != ''">
  764. AND w.warning_type = #{warningType}
  765. </if>
  766. <if test="warningLevel != null and warningLevel != ''">
  767. AND w.warning_level = #{warningLevel}
  768. </if>
  769. <if test="status != null and status != ''">
  770. AND w.status = #{status}
  771. </if>
  772. <if test="publisher != null and publisher != ''">
  773. AND w.publisher LIKE CONCAT('%', #{publisher}, '%')
  774. </if>
  775. <if test="excludePublisher != null and excludePublisher != ''">
  776. AND (w.publisher IS NULL OR w.publisher NOT LIKE CONCAT('%', #{excludePublisher}, '%'))
  777. </if>
  778. <if test="ownershipUnit != null and ownershipUnit != ''">
  779. AND w.ownership_unit LIKE CONCAT('%', #{ownershipUnit}, '%')
  780. </if>
  781. <if test="deviceCode != null and deviceCode != ''">
  782. AND w.device_code LIKE CONCAT('%', #{deviceCode}, '%')
  783. </if>
  784. <if test="deviceName != null and deviceName != ''">
  785. AND (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) LIKE CONCAT('%', #{deviceName}, '%')
  786. </if>
  787. <if test="startTime != null">
  788. AND w.create_time >= #{startTime}
  789. </if>
  790. <if test="endTime != null">
  791. AND w.create_time &lt;= #{endTime}
  792. </if>
  793. </where>
  794. <choose>
  795. <when test="releaseOrder">
  796. ORDER BY CASE w.status
  797. WHEN 'DRAFT' THEN 0
  798. WHEN 'PENDING' THEN 1
  799. WHEN 'RECEIVED' THEN 2
  800. WHEN 'PROCESSING' THEN 3
  801. WHEN 'HANDLED' THEN 4
  802. WHEN 'CLOSED' THEN 5
  803. ELSE 6 END,
  804. COALESCE(w.update_time, w.create_time) DESC NULLS LAST,
  805. w.create_time DESC NULLS LAST, w.warning_id DESC
  806. </when>
  807. <otherwise>ORDER BY w.create_time DESC</otherwise>
  808. </choose>
  809. </select>
  810. <select id="selectTodoList" resultType="java.util.Map">
  811. SELECT
  812. t.*,
  813. w.warning_no,
  814. w.warning_type,
  815. w.warning_level,
  816. w.handler,
  817. w.publisher,
  818. <include refid="DeviceLocationColumn"/>,
  819. w.ownership_unit,
  820. w.publish_time
  821. FROM warning_todo t
  822. LEFT JOIN early_warning w ON t.warning_id = w.warning_id
  823. <where>
  824. t.user_id = #{userId}
  825. <if test="warningNo != null and warningNo != ''">AND w.warning_no LIKE CONCAT('%',#{warningNo},'%')</if>
  826. <if test="todoType != null and todoType != ''">
  827. AND t.todo_type = #{todoType}
  828. </if>
  829. </where>
  830. ORDER BY t.create_time DESC
  831. </select>
  832. <!-- 身份由服务端登录上下文传入,禁止使用请求中的 userName。 -->
  833. <select id="selectMyTasks" resultType="java.util.Map">
  834. SELECT w.*, <include refid="DeviceLocationColumn"/>, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  835. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  836. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name, t.todo_id,
  837. <choose>
  838. <when test="q.todoType == null or q.todoType == '' or q.todoType == 'TODO'">'TODO' AS todo_type,</when>
  839. <otherwise>t.todo_type,</otherwise>
  840. </choose>
  841. t.action_type, t.task_name, t.read_status,
  842. t.create_time AS task_create_time, t.complete_time,
  843. (SELECT STRING_AGG(DISTINCT a.alarm_no, ',')
  844. FROM alarm_warning_rel rel JOIN alarm_data a ON a.id = rel.alarm_id
  845. WHERE rel.warning_id = w.warning_id) AS alarm_numbers
  846. FROM early_warning w LEFT JOIN (
  847. SELECT task.*, ROW_NUMBER() OVER (
  848. PARTITION BY CASE WHEN task.todo_type = 'NOTICE' THEN task.todo_id ELSE task.warning_id END
  849. ORDER BY task.complete_time DESC NULLS LAST, task.create_time DESC, task.todo_id DESC
  850. ) AS task_rank
  851. FROM warning_todo task
  852. WHERE (task.user_name = #{username} OR task.user_id = #{userId}
  853. OR (task.user_name IS NULL AND task.user_id = #{username}))
  854. <if test="q.todoType != null and q.todoType != ''">AND task.todo_type = #{q.todoType}</if>
  855. <if test="q.todoType == null or q.todoType == ''">AND task.todo_type IN ('TODO', 'DONE')</if>
  856. ) t ON w.warning_id = t.warning_id AND t.task_rank = 1
  857. WHERE 1 = 1
  858. <include refid="WarningVisibility"/>
  859. <choose>
  860. <when test="q.todoType == null or q.todoType == '' or q.todoType == 'TODO'">
  861. <include refid="WarningPending"/>
  862. </when>
  863. <otherwise>AND t.todo_id IS NOT NULL</otherwise>
  864. </choose>
  865. <if test="q.warningNo != null and q.warningNo != ''">AND w.warning_no LIKE CONCAT('%',#{q.warningNo},'%')</if>
  866. <if test="q.warningSpecial != null and q.warningSpecial != ''">AND w.warning_special LIKE CONCAT('%',#{q.warningSpecial},'%')</if>
  867. <if test="q.warningType != null and q.warningType != ''">AND w.warning_type = #{q.warningType}</if>
  868. <if test="q.warningLevel != null and q.warningLevel != ''">AND w.warning_level = #{q.warningLevel}</if>
  869. <if test="q.status != null and q.status != ''">AND w.status = #{q.status}</if>
  870. <if test="q.handler != null and q.handler != ''">AND w.handler LIKE CONCAT('%',#{q.handler},'%')</if>
  871. <if test="q.publisher != null and q.publisher != ''">AND w.publisher LIKE CONCAT('%',#{q.publisher},'%')</if>
  872. <if test="q.ownershipUnit != null and q.ownershipUnit != ''">AND w.ownership_unit LIKE CONCAT('%',#{q.ownershipUnit},'%')</if>
  873. <if test="q.startTime != null and q.startTime != ''">AND COALESCE(w.publish_time, w.create_time) &gt;= CAST(#{q.startTime} AS TIMESTAMP)</if>
  874. <if test="q.endTime != null and q.endTime != ''">AND COALESCE(w.publish_time, w.create_time) &lt;= CAST(#{q.endTime} AS TIMESTAMP)</if>
  875. <if test="q.alarmCode != null and q.alarmCode != ''">
  876. AND EXISTS (SELECT 1 FROM alarm_warning_rel r JOIN alarm_data a ON a.id = r.alarm_id
  877. WHERE r.warning_id = w.warning_id AND a.alarm_no LIKE CONCAT('%',#{q.alarmCode},'%'))
  878. </if>
  879. ORDER BY COALESCE(t.create_time, w.create_time) DESC, w.warning_id DESC
  880. </select>
  881. <select id="selectProcessingWarnings" resultType="java.util.Map">
  882. SELECT w.*, <include refid="DeviceLocationColumn"/>, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  883. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  884. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name
  885. FROM early_warning w
  886. <where>
  887. <include refid="WarningVisibility"/>
  888. AND w.status IN ('PENDING', 'RECEIVED', 'PROCESSING', 'HANDLED')
  889. <if test="warningType != null and warningType != ''">
  890. AND w.warning_type = #{warningType}
  891. </if>
  892. <if test="warningLevel != null and warningLevel != ''">
  893. AND w.warning_level = #{warningLevel}
  894. </if>
  895. </where>
  896. ORDER BY w.publish_time DESC
  897. </select>
  898. <select id="selectCompletedWarnings" resultType="java.util.Map">
  899. SELECT w.*, <include refid="DeviceLocationColumn"/>, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  900. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  901. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name
  902. FROM early_warning w
  903. <where>
  904. <include refid="WarningVisibility"/>
  905. AND w.status = 'CLOSED'
  906. <if test="warningType != null and warningType != ''">
  907. AND w.warning_type = #{warningType}
  908. </if>
  909. <if test="warningLevel != null and warningLevel != ''">
  910. AND w.warning_level = #{warningLevel}
  911. </if>
  912. <if test="startTime != null">
  913. AND w.update_time >= #{startTime}
  914. </if>
  915. <if test="endTime != null">
  916. AND w.update_time &lt;= #{endTime}
  917. </if>
  918. </where>
  919. ORDER BY w.update_time DESC
  920. </select>
  921. <sql id="WarningVisibility">
  922. <if test="!scope.all">
  923. AND (
  924. w.create_by = #{scope.username}
  925. OR w.publisher = #{scope.username}
  926. OR w.handler = #{scope.username}
  927. OR w.reviewer = #{scope.username}
  928. OR (w.create_by = '系统自动' AND #{scope.canPublish} = TRUE)
  929. OR (w.status = 'DRAFT' AND #{scope.canPublish} = TRUE)
  930. )
  931. AND EXISTS (
  932. SELECT 1
  933. FROM app_user.equipment_base eb
  934. INNER JOIN app_user.equipment_type et ON et.id = eb.equipment_type_id
  935. WHERE eb.equipment_code = w.device_code
  936. AND (
  937. <choose>
  938. <when test="scope.parentTypeIds != null and !scope.parentTypeIds.isEmpty()">
  939. et.parent_id IN
  940. <foreach collection="scope.parentTypeIds" item="parentTypeId" open="(" separator="," close=")">
  941. #{parentTypeId}
  942. </foreach>
  943. </when>
  944. <otherwise>1 = 0</otherwise>
  945. </choose>
  946. <if test="scope.sharedManholeVisible">
  947. OR (et.parent_id = 4 AND (w.handler = #{scope.username} OR w.reviewer = #{scope.username}))
  948. </if>
  949. )
  950. )
  951. </if>
  952. </sql>
  953. <sql id="WarningPending">
  954. AND (
  955. (w.handler = #{scope.username} AND #{scope.canHandle} = TRUE
  956. AND w.status IN ('PENDING', 'RECEIVED', 'PROCESSING'))
  957. OR (w.reviewer = #{scope.username} AND #{scope.canAudit} = TRUE AND w.status = 'HANDLED')
  958. OR (w.status = 'DRAFT' AND #{scope.canPublish} = TRUE)
  959. )
  960. </sql>
  961. <select id="selectMobileWarnings" resultType="java.util.Map">
  962. SELECT
  963. w.warning_id AS "warningId",
  964. w.warning_no AS "warningNo",
  965. w.warning_type AS "warningType",
  966. COALESCE((SELECT NULLIF(TRIM(wt.warning_name), '') FROM app_user.warning_type wt
  967. WHERE wt.warning_code = w.warning_type ORDER BY wt.id LIMIT 1), w.warning_type) AS "warningTypeName",
  968. w.warning_level AS "warningLevel",
  969. COALESCE((SELECT NULLIF(TRIM(dl.dict_label), '') FROM app_user.sys_dict_data dl
  970. WHERE dl.dict_type = 'alert_level' AND dl.dict_value = w.warning_level AND dl.status = '0'
  971. ORDER BY dl.dict_sort LIMIT 1), w.warning_level) AS "warningLevelName",
  972. w.warning_content AS "warningContent",
  973. w.device_code AS "deviceCode",
  974. (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS "deviceName",
  975. (SELECT et.type_name FROM app_user.equipment_base e
  976. LEFT JOIN app_user.equipment_type et ON et.id = e.equipment_type_id
  977. WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS "deviceTypeName",
  978. <include refid="DeviceLocationColumn"/>,
  979. w.status,
  980. w.create_by AS "createBy",
  981. w.publisher,
  982. w.handler,
  983. COALESCE(NULLIF(TRIM(hu.nick_name), ''), w.handler) AS "handlerName",
  984. w.reviewer,
  985. COALESCE(NULLIF(TRIM(ru.nick_name), ''), w.reviewer) AS "reviewerName",
  986. w.publish_time AS "publishTime",
  987. w.create_time AS "createTime"
  988. FROM app_user.early_warning w
  989. LEFT JOIN app_user.sys_user hu ON hu.user_name = w.handler
  990. LEFT JOIN app_user.sys_user ru ON ru.user_name = w.reviewer
  991. WHERE 1 = 1
  992. <include refid="WarningVisibility"/>
  993. <if test="pendingOnly">
  994. <include refid="WarningPending"/>
  995. </if>
  996. <if test="keyword != null and keyword != ''">
  997. AND (w.warning_no LIKE CONCAT('%', #{keyword}, '%')
  998. OR w.warning_content LIKE CONCAT('%', #{keyword}, '%')
  999. OR w.device_code LIKE CONCAT('%', #{keyword}, '%')
  1000. OR (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) LIKE CONCAT('%', #{keyword}, '%'))
  1001. </if>
  1002. <if test="status != null and status != ''">AND w.status = #{status}</if>
  1003. <if test="warningType != null and warningType != ''">AND w.warning_type = #{warningType}</if>
  1004. <if test="warningLevel != null and warningLevel != ''">AND w.warning_level = #{warningLevel}</if>
  1005. ORDER BY COALESCE(w.publish_time, w.create_time) DESC, w.warning_id DESC
  1006. </select>
  1007. <select id="countVisibleWarning" resultType="int">
  1008. SELECT COUNT(1)
  1009. FROM app_user.early_warning w
  1010. WHERE w.warning_id = #{warningId}
  1011. <include refid="WarningVisibility"/>
  1012. </select>
  1013. <select id="countCompanyDevice" resultType="int">
  1014. SELECT COUNT(1) FROM app_user.equipment_base eb
  1015. INNER JOIN app_user.equipment_type et ON et.id = eb.equipment_type_id
  1016. WHERE eb.equipment_code = #{deviceCode}
  1017. <if test="!scope.all">
  1018. <choose>
  1019. <when test="scope.parentTypeIds != null and !scope.parentTypeIds.isEmpty()">
  1020. AND et.parent_id IN
  1021. <foreach collection="scope.parentTypeIds" item="parentTypeId" open="(" separator="," close=")">
  1022. #{parentTypeId}
  1023. </foreach>
  1024. </when>
  1025. <otherwise>AND 1 = 0</otherwise>
  1026. </choose>
  1027. </if>
  1028. </select>
  1029. <select id="selectVisibleArchives" resultType="com.zksy.base.warning.domain.WarningArchive">
  1030. SELECT a.* FROM app_user.warning_archive a
  1031. JOIN app_user.early_warning w ON w.warning_id = a.warning_id
  1032. <where>
  1033. <include refid="WarningVisibility"/>
  1034. <if test="warningNo != null and warningNo != ''">AND a.warning_no LIKE CONCAT('%', #{warningNo}, '%')</if>
  1035. <if test="warningType != null and warningType != ''">AND a.warning_type = #{warningType}</if>
  1036. <if test="warningLevel != null and warningLevel != ''">AND a.warning_level = #{warningLevel}</if>
  1037. <if test="startTime != null">AND a.create_time &gt;= #{startTime}</if>
  1038. <if test="endTime != null">AND a.create_time &lt;= #{endTime}</if>
  1039. </where>
  1040. ORDER BY a.create_time DESC
  1041. </select>
  1042. <select id="selectWarningDetail" resultType="java.util.Map">
  1043. SELECT w.*, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  1044. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  1045. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name,
  1046. COALESCE(NULLIF(TRIM(hu.nick_name), ''), w.handler) AS handler_name,
  1047. COALESCE(NULLIF(TRIM(ru.nick_name), ''), w.reviewer) AS reviewer_name,
  1048. COALESCE(NULLIF(TRIM(pu.nick_name), ''), w.publisher) AS publisher_name,
  1049. COALESCE(NULLIF(TRIM(cu.nick_name), ''), w.create_by) AS creator_name,
  1050. eb.equipment_location AS location,
  1051. et.type_name AS device_type_name
  1052. FROM early_warning w
  1053. LEFT JOIN app_user.sys_user hu ON hu.user_name = w.handler
  1054. LEFT JOIN app_user.sys_user ru ON ru.user_name = w.reviewer
  1055. LEFT JOIN app_user.sys_user pu ON pu.user_name = w.publisher
  1056. LEFT JOIN app_user.sys_user cu ON cu.user_name = w.create_by
  1057. LEFT JOIN app_user.equipment_base eb ON eb.equipment_code = w.device_code
  1058. LEFT JOIN app_user.equipment_type et ON et.id = eb.equipment_type_id
  1059. WHERE w.warning_id = #{warningId}
  1060. </select>
  1061. <!-- 驾驶舱只读详情用:列表下发的是**业务编号**(warning_no),详情要的是主键(warning_id)。
  1062. 两条口径都在契约里,因此按编号回查一次,不要求前端改拿主键。 -->
  1063. <select id="selectWarningDetailByNo" resultType="java.util.Map">
  1064. SELECT w.*, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  1065. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  1066. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name,
  1067. COALESCE(NULLIF(TRIM(hu.nick_name), ''), w.handler) AS handler_name,
  1068. COALESCE(NULLIF(TRIM(ru.nick_name), ''), w.reviewer) AS reviewer_name,
  1069. COALESCE(NULLIF(TRIM(pu.nick_name), ''), w.publisher) AS publisher_name,
  1070. COALESCE(NULLIF(TRIM(cu.nick_name), ''), w.create_by) AS creator_name,
  1071. eb.equipment_location AS location,
  1072. et.type_name AS device_type_name
  1073. FROM early_warning w
  1074. LEFT JOIN app_user.sys_user hu ON hu.user_name = w.handler
  1075. LEFT JOIN app_user.sys_user ru ON ru.user_name = w.reviewer
  1076. LEFT JOIN app_user.sys_user pu ON pu.user_name = w.publisher
  1077. LEFT JOIN app_user.sys_user cu ON cu.user_name = w.create_by
  1078. LEFT JOIN app_user.equipment_base eb ON eb.equipment_code = w.device_code
  1079. LEFT JOIN app_user.equipment_type et ON et.id = eb.equipment_type_id
  1080. WHERE w.warning_no = #{warningNo}
  1081. ORDER BY w.create_time DESC NULLS LAST
  1082. LIMIT 1
  1083. </select>
  1084. <select id="selectStatistics" resultType="java.util.Map">
  1085. SELECT
  1086. COUNT(*) AS total,
  1087. COALESCE(SUM(CASE WHEN status = 'DRAFT' THEN 1 ELSE 0 END), 0) AS draftCount,
  1088. COALESCE(SUM(CASE WHEN status = 'PENDING' THEN 1 ELSE 0 END), 0) AS pendingCount,
  1089. COALESCE(SUM(CASE WHEN status = 'PROCESSING' THEN 1 ELSE 0 END), 0) AS processingCount,
  1090. COALESCE(SUM(CASE WHEN status = 'RECEIVED' THEN 1 ELSE 0 END), 0) AS receivedCount,
  1091. COALESCE(SUM(CASE WHEN status = 'HANDLED' THEN 1 ELSE 0 END), 0) AS handledCount,
  1092. COALESCE(SUM(CASE WHEN status = 'CLOSED' THEN 1 ELSE 0 END), 0) AS closedCount,
  1093. COALESCE(SUM(CASE WHEN warning_level = '1' THEN 1 ELSE 0 END), 0) AS level1Count,
  1094. COALESCE(SUM(CASE WHEN warning_level = '2' THEN 1 ELSE 0 END), 0) AS level2Count,
  1095. COALESCE(SUM(CASE WHEN warning_level = '3' THEN 1 ELSE 0 END), 0) AS level3Count,
  1096. COALESCE(SUM(CASE WHEN warning_level = '4' THEN 1 ELSE 0 END), 0) AS level4Count
  1097. FROM early_warning
  1098. </select>
  1099. <!-- 行业处置分析:兼容 device_code 存设备实例编码或设备类别主键两种历史格式。
  1100. 设备类别主键改为整数后,业态由根节点 id 决定:1=供水 2=排水 3=燃气 4=窨井,
  1101. 对外仍以 water_supply/drainage/gas/manhole 作为 industry_code,保持接口契约不变。 -->
  1102. <sql id="IndustryWarningCte">
  1103. WITH RECURSIVE type_tree AS (
  1104. SELECT et.id AS leaf_id, et.id AS current_id,
  1105. et.type_name AS current_name, et.parent_id, CAST(0 AS bigint) AS depth
  1106. FROM app_user.equipment_type et
  1107. UNION ALL
  1108. SELECT tree.leaf_id, parent.id, parent.type_name,
  1109. parent.parent_id, tree.depth + 1
  1110. FROM type_tree tree
  1111. INNER JOIN app_user.equipment_type parent ON parent.id = tree.parent_id
  1112. ),
  1113. root_ranked AS (
  1114. SELECT leaf_id, current_id AS industry_id, current_name AS industry_name,
  1115. ROW_NUMBER() OVER (PARTITION BY leaf_id ORDER BY depth DESC) AS row_num
  1116. FROM type_tree
  1117. ),
  1118. type_roots AS (
  1119. SELECT leaf_id,
  1120. CASE industry_id
  1121. WHEN 1 THEN 'water_supply'
  1122. WHEN 2 THEN 'drainage'
  1123. WHEN 3 THEN 'gas'
  1124. WHEN 4 THEN 'manhole'
  1125. END AS industry_code,
  1126. industry_name
  1127. FROM root_ranked
  1128. WHERE row_num = 1
  1129. AND industry_id IN (1, 2, 3, 4)
  1130. ),
  1131. equipment_map AS (
  1132. SELECT equipment_code, MIN(equipment_type_id) AS equipment_type_id
  1133. FROM app_user.equipment_base
  1134. WHERE del_flag = '0'
  1135. GROUP BY equipment_code
  1136. ),
  1137. warning_industry AS (
  1138. SELECT w.*, (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = w.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name, (SELECT wt.warning_name FROM app_user.warning_type wt WHERE wt.warning_code=w.warning_type ORDER BY wt.id LIMIT 1) AS warning_type_name,
  1139. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='alert_level' AND dict_value=w.warning_level AND status='0' ORDER BY dict_sort LIMIT 1) AS warning_level_name,
  1140. (SELECT dict_label FROM app_user.sys_dict_data WHERE dict_type='pre_alarm_status' AND dict_value=w.status AND status='0' ORDER BY dict_sort LIMIT 1) AS status_name, roots.industry_code, roots.industry_name
  1141. FROM app_user.early_warning w
  1142. LEFT JOIN equipment_map equipment ON equipment.equipment_code = w.device_code
  1143. LEFT JOIN app_user.equipment_type direct_type
  1144. ON CAST(direct_type.id AS varchar) = w.device_code
  1145. INNER JOIN type_roots roots
  1146. ON CAST(roots.leaf_id AS varchar) =
  1147. COALESCE(equipment.equipment_type_id, CAST(direct_type.id AS varchar))
  1148. WHERE w.status != 'DRAFT'
  1149. <if test="startTime != null">
  1150. AND w.publish_time &gt;= #{startTime}
  1151. </if>
  1152. <if test="endTime != null">
  1153. AND w.publish_time &lt; #{endTime}
  1154. </if>
  1155. <if test="industryCode != null and industryCode != ''">
  1156. AND roots.industry_code = #{industryCode}
  1157. </if>
  1158. ),
  1159. completed_actions AS (
  1160. SELECT disposal.warning_id, MAX(disposal.create_time) AS dispose_time
  1161. FROM app_user.warning_disposal disposal
  1162. WHERE disposal.disposal_type IN ('HANDLE', 'RESOLVE', 'CLEAR', 'CLOSE', 'MISREPORT', 'AUTO_CLEAR')
  1163. GROUP BY disposal.warning_id
  1164. )
  1165. </sql>
  1166. <select id="selectIndustryAnalysisSummary" resultType="java.util.Map">
  1167. <include refid="IndustryWarningCte"/>
  1168. SELECT warning.industry_code,
  1169. warning.industry_name,
  1170. COUNT(*) AS total_count,
  1171. SUM(CASE WHEN warning.status IN ('PENDING', 'RECEIVED') THEN 1 ELSE 0 END) AS pending_count,
  1172. SUM(CASE WHEN warning.status = 'PROCESSING' THEN 1 ELSE 0 END) AS processing_count,
  1173. SUM(CASE WHEN warning.status IN ('HANDLED', 'CLOSED') THEN 1 ELSE 0 END) AS completed_count,
  1174. ROUND(100.0 * SUM(CASE WHEN warning.status IN ('HANDLED', 'CLOSED') THEN 1 ELSE 0 END)
  1175. / NULLIF(COUNT(*), 0), 2) AS completion_rate,
  1176. ROUND(AVG(CASE
  1177. WHEN warning.status IN ('HANDLED', 'CLOSED')
  1178. AND action.dispose_time &gt;= warning.publish_time
  1179. THEN EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1180. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600
  1181. END), 2) AS avg_dispose_hours,
  1182. SUM(CASE
  1183. WHEN warning.status IN ('HANDLED', 'CLOSED')
  1184. AND action.dispose_time &gt;= warning.publish_time
  1185. AND EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1186. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600 &gt; 24
  1187. THEN 1 ELSE 0 END) AS over_24h_count
  1188. FROM warning_industry warning
  1189. LEFT JOIN completed_actions action ON action.warning_id = warning.warning_id
  1190. GROUP BY warning.industry_code, warning.industry_name
  1191. ORDER BY CASE warning.industry_code
  1192. WHEN 'gas' THEN 1 WHEN 'water_supply' THEN 2
  1193. WHEN 'drainage' THEN 3 WHEN 'manhole' THEN 4 ELSE 5 END
  1194. </select>
  1195. <select id="selectIndustryStatusDistribution" resultType="java.util.Map">
  1196. <include refid="IndustryWarningCte"/>
  1197. <!-- 处置统计只使用 pre_alarm_status 字典中的 CLOSED 终态。 -->
  1198. SELECT industry_code,
  1199. industry_name,
  1200. CASE WHEN status IN ('CLOSED') THEN 'CLOSED' ELSE status END AS status,
  1201. COUNT(*) AS warning_count
  1202. FROM warning_industry
  1203. GROUP BY industry_code,
  1204. industry_name,
  1205. CASE WHEN status IN ('CLOSED') THEN 'CLOSED' ELSE status END
  1206. ORDER BY industry_name,
  1207. CASE WHEN status IN ('CLOSED') THEN 'CLOSED' ELSE status END
  1208. </select>
  1209. <select id="selectIndustryEfficiencyComparison" resultType="java.util.Map">
  1210. <include refid="IndustryWarningCte"/>
  1211. SELECT warning.industry_code,
  1212. warning.industry_name,
  1213. COUNT(*) AS completed_count,
  1214. ROUND(AVG(EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1215. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600), 2) AS avg_dispose_hours,
  1216. ROUND(MAX(EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1217. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600), 2) AS max_dispose_hours,
  1218. ROUND(MIN(EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1219. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600), 2) AS min_dispose_hours,
  1220. ROUND(100.0 * SUM(CASE WHEN EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1221. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600 &lt;= 24
  1222. THEN 1 ELSE 0 END) / NULLIF(COUNT(*), 0), 2) AS within_24h_rate
  1223. FROM warning_industry warning
  1224. INNER JOIN completed_actions action ON action.warning_id = warning.warning_id
  1225. WHERE warning.status IN ('HANDLED', 'CLOSED')
  1226. AND action.dispose_time &gt;= warning.publish_time
  1227. GROUP BY warning.industry_code, warning.industry_name
  1228. ORDER BY avg_dispose_hours DESC
  1229. </select>
  1230. <select id="selectIndustryAnalysisList" resultType="java.util.Map">
  1231. <include refid="IndustryWarningCte"/>
  1232. SELECT warning.warning_id,
  1233. warning.warning_no,
  1234. warning.device_code,
  1235. (SELECT e.equipment_name FROM app_user.equipment_base e WHERE e.equipment_code = warning.device_code ORDER BY e.equipment_id LIMIT 1) AS device_name,
  1236. warning.industry_code,
  1237. warning.industry_name,
  1238. warning.warning_level,
  1239. warning.publish_time,
  1240. warning.status,
  1241. action.dispose_time,
  1242. CASE WHEN action.dispose_time &gt;= warning.publish_time
  1243. THEN ROUND(EXTRACT(EPOCH FROM (CAST(action.dispose_time AS TIMESTAMPTZ)
  1244. - CAST(warning.publish_time AS TIMESTAMPTZ))) / 3600, 2)
  1245. ELSE NULL END AS dispose_hours
  1246. FROM warning_industry warning
  1247. LEFT JOIN completed_actions action ON action.warning_id = warning.warning_id
  1248. <where>
  1249. <if test="status != null and status != ''">
  1250. <choose>
  1251. <!-- 页面只提供一个“已解除”筛选项,但兼容两种历史状态值。 -->
  1252. <when test="status == 'CLOSED'">
  1253. warning.status IN ('CLOSED')
  1254. </when>
  1255. <otherwise>
  1256. warning.status = #{status}
  1257. </otherwise>
  1258. </choose>
  1259. </if>
  1260. </where>
  1261. ORDER BY dispose_hours
  1262. <if test="sortType != null and sortType == 'desc'">DESC</if>
  1263. <if test="sortType == null or sortType == 'asc'">ASC</if>
  1264. NULLS LAST, warning.publish_time DESC
  1265. </select>
  1266. </mapper>