CockpitOverviewModulesMapper.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. <!-- 总体概览四模块聚合查询(规格 2026-10-22 §2–§5)。全部单次扫描 + GROUP BY,不下拉明细到 Java。
  4. 票 81:四端点补 district/industry 入参后,筛选**下推到 WHERE**(此前 district 只出现在 GROUP BY)。
  5. 片区的两条来源刻意分开、不互相冒充:
  6. · 设备/预警/报警类读数 → 沿设备回联 equipment_base.district(权威片区列);
  7. · 管网类读数 → 设施没有片区列,只能取编码前缀(规则在 Java 的 CockpitDistrictPrefix,
  8. 这里只多暴露一个原始前缀列,不在 SQL 里再写一套映射)。 -->
  9. <mapper namespace="com.zksy.cockpit.overview.mapper.CockpitOverviewModulesMapper">
  10. <!-- 设备类型树:根类型(parent_id = 0)→ 任意层级子类型的归属。
  11. 实测存在 equipment_base.equipment_type_id = 11 的悬空行,LEFT JOIN 保留并计入 unknownTypeCount。 -->
  12. <sql id="typeTree">
  13. WITH RECURSIVE type_tree AS (
  14. SELECT t.id AS id, t.id AS root_id
  15. FROM app_user.equipment_type t
  16. WHERE t.parent_id = 0
  17. UNION ALL
  18. SELECT c.id AS id, tt.root_id AS root_id
  19. FROM app_user.equipment_type c
  20. JOIN type_tree tt ON c.parent_id = tt.id
  21. )
  22. </sql>
  23. <!-- 片区的 SQL 判据(票 81),与 Java 侧 CockpitDistrict 逐字对齐:
  24. · 已指派某片区 → eb.district = #{district}
  25. · 未分区 → 没有可回联的设备行,或 eb.district 为 null / 不在 0/1/2 内
  26. (= CockpitDistrict.codeOf 会归一成 null 的那一批)
  27. 行业按设备类型树的根类型判定(1 供水 / 2 排水 / 3 燃气 / 4 窨井),
  28. 与感知设备四张固定卡片同一套根类型,不另立词表。 -->
  29. <sql id="deviceDistrictFilter">
  30. <if test="district != null or unassigned">
  31. AND <choose>
  32. <when test="unassigned">
  33. NOT EXISTS (
  34. SELECT 1 FROM app_user.equipment_base eb
  35. WHERE eb.equipment_code = w.device_code
  36. AND eb.district IN (0, 1, 2)
  37. )
  38. </when>
  39. <otherwise>
  40. EXISTS (
  41. SELECT 1 FROM app_user.equipment_base eb
  42. WHERE eb.equipment_code = w.device_code
  43. AND eb.district = #{district}
  44. )
  45. </otherwise>
  46. </choose>
  47. </if>
  48. <if test="industryRootTypeId != null">
  49. AND EXISTS (
  50. SELECT 1 FROM app_user.equipment_base ebi
  51. JOIN type_tree tti ON tti.id = ebi.equipment_type_id
  52. WHERE ebi.equipment_code = w.device_code
  53. AND tti.root_id = #{industryRootTypeId}
  54. )
  55. </if>
  56. </sql>
  57. <!-- 每台设备的最新状态:status_update_time 最新,并列取 status_id 最大,且 del_flag = '0'(规格 §0/§2)。
  58. 票 81:device 过滤加在 dev CTE 内部(equipment_base 就在这一层),聚合层不再过滤。 -->
  59. <select id="selectDeviceBuckets" resultType="java.util.LinkedHashMap">
  60. <include refid="typeTree"/>,
  61. dev AS (
  62. SELECT eb.equipment_type_id AS equipment_type_id,
  63. eb.district AS district,
  64. tt.root_id AS root_type_id,
  65. CASE WHEN ls.equipment_id IS NULL THEN 0 ELSE 1 END AS has_status,
  66. CASE WHEN ls.online_status = 1 THEN 1 ELSE 0 END AS is_online,
  67. eb.update_time AS source_updated_at
  68. FROM app_user.equipment_base eb
  69. LEFT JOIN type_tree tt ON tt.id = eb.equipment_type_id
  70. LEFT JOIN LATERAL (
  71. SELECT s.equipment_id AS equipment_id, s.online_status AS online_status
  72. FROM app_user.equipment_status s
  73. WHERE s.equipment_id = eb.equipment_id AND s.del_flag = '0'
  74. ORDER BY s.status_update_time DESC NULLS LAST, s.status_id DESC
  75. LIMIT 1
  76. ) ls ON TRUE
  77. WHERE 1 = 1
  78. <if test="district != null or unassigned">
  79. AND <choose>
  80. <when test="unassigned">
  81. (eb.district IS NULL OR eb.district NOT IN (0, 1, 2))
  82. </when>
  83. <otherwise>
  84. eb.district = #{district}
  85. </otherwise>
  86. </choose>
  87. </if>
  88. <if test="industryRootTypeId != null">
  89. AND tt.root_id = #{industryRootTypeId}
  90. </if>
  91. )
  92. SELECT equipment_type_id, district, root_type_id, has_status, is_online,
  93. COUNT(*) AS device_count, MAX(source_updated_at) AS source_updated_at
  94. FROM dev
  95. GROUP BY equipment_type_id, district, root_type_id, has_status, is_online
  96. </select>
  97. <select id="selectMonitoringContents" resultType="java.util.LinkedHashMap">
  98. <include refid="typeTree"/>
  99. SELECT tt.root_id AS root_type_id,
  100. wt.warning_code AS warning_code,
  101. wt.warning_name AS warning_name
  102. FROM app_user.equipment_warning_type ewt
  103. JOIN app_user.warning_type wt ON wt.id = ewt.warning_type_id
  104. JOIN type_tree tt ON tt.id = ewt.equipment_type_id
  105. ORDER BY tt.root_id, wt.id
  106. </select>
  107. <!-- 长度单位是米;吊销行由 del_flag 显式排除。network_type 的语义映射留在 CockpitPipeNetworkType。
  108. 票 81:管网没有片区列,额外 GROUP 出**编码前两位**(district_prefix),
  109. 由 Java 的 CockpitDistrictPrefix 做唯一映射与筛选(CN=城南 / CB=城北 / TC=太常)。 -->
  110. <select id="selectPipeBuckets" resultType="java.util.LinkedHashMap">
  111. SELECT pb.network_type AS network_type,
  112. LEFT(UPPER(COALESCE(pb.network_code, '')), 2) AS district_prefix,
  113. COUNT(*) AS pipe_count,
  114. COALESCE(SUM(pb.length), 0) AS length_meters,
  115. MAX(pb.update_time) AS source_updated_at
  116. FROM app_user.pipe_network_base pb
  117. WHERE pb.del_flag = '0'
  118. GROUP BY pb.network_type, LEFT(UPPER(COALESCE(pb.network_code, '')), 2)
  119. </select>
  120. <!-- 可统计的预警集合:草稿(DRAFT)还没发布,不属于任何对外读数
  121. (维护者 2026-11-12:「预警统计要排除草稿状态的预警」)。
  122. 今日/昨日新增、区间总数与类型排行共用这一个判据,避免四处各写一份日后漂移。
  123. 未解除集合另有白名单,见 selectUnresolvedStatusLevelCounts。
  124. 两条硬约束(评审指出的副作用,勿改):
  125. 1. `status IS NULL` 的要**保留**:改动前这三条查询没有状态条件,NULL 行原本计入;
  126. 单写 `<> 'DRAFT'` 会把它们默默丢掉(NULL 比较得 UNKNOWN),那是本票之外的口径变更。
  127. 2. 片段只能用在**计数**条件上。`selectWarningCounts` 里的 `MAX(w.update_time)`
  128. 是数据新鲜度(服务层 freshness)的数据源,代表"源数据最后动过的时间",
  129. 不能跟着排除草稿:否则编辑草稿后面板会被误判为数据过旧。
  130. 所以那条查询把筛选写进 CASE,而不是放 WHERE(见下面注释)。
  131. 片段写死了别名 `w`(与同文件其他片段一致),只能 include 在同样把预警表别名成 w 的查询里。 -->
  132. <sql id="countableWarningFilter">
  133. AND (w.status IS NULL OR UPPER(TRIM(w.status)) &lt;&gt; 'DRAFT')
  134. </sql>
  135. <!-- 新鲜度(source_updated_at)取全表 MAX,不受草稿筛选影响;只有两个计数带筛选。 -->
  136. <select id="selectWarningCounts" resultType="java.util.LinkedHashMap">
  137. <include refid="typeTree"/>
  138. SELECT COALESCE(SUM(CASE WHEN (w.status IS NULL OR UPPER(TRIM(w.status)) &lt;&gt; 'DRAFT')
  139. AND w.create_time &gt;= #{todayStart} AND w.create_time &lt; #{tomorrowStart}
  140. THEN 1 ELSE 0 END), 0) AS today_new,
  141. COALESCE(SUM(CASE WHEN (w.status IS NULL OR UPPER(TRIM(w.status)) &lt;&gt; 'DRAFT')
  142. AND w.create_time &gt;= #{yesterdayStart} AND w.create_time &lt; #{todayStart}
  143. THEN 1 ELSE 0 END), 0) AS yesterday_new,
  144. MAX(w.update_time) AS source_updated_at
  145. FROM app_user.early_warning w
  146. WHERE 1 = 1
  147. <include refid="deviceDistrictFilter"/>
  148. </select>
  149. <!-- 未解除集合 = PENDING / RECEIVED / PROCESSING / HANDLED(DRAFT、CLOSED 排除,规格 §4)。
  150. 不复用 BusinessCockpitEventAdapter 的 HANDLING 拼写错误常量。 -->
  151. <select id="selectUnresolvedStatusLevelCounts" resultType="java.util.LinkedHashMap">
  152. <include refid="typeTree"/>
  153. SELECT UPPER(TRIM(w.status)) AS status,
  154. w.warning_level AS warning_level,
  155. COUNT(*) AS type_count
  156. FROM app_user.early_warning w
  157. WHERE UPPER(TRIM(w.status)) IN ('PENDING', 'RECEIVED', 'PROCESSING', 'HANDLED')
  158. <include refid="deviceDistrictFilter"/>
  159. GROUP BY UPPER(TRIM(w.status)), w.warning_level
  160. </select>
  161. <select id="selectWarningCountInWindow" resultType="java.util.LinkedHashMap">
  162. <include refid="typeTree"/>
  163. SELECT COUNT(*) AS warning_count
  164. FROM app_user.early_warning w
  165. WHERE w.create_time &gt;= #{startInclusive} AND w.create_time &lt; #{endExclusive}
  166. <include refid="countableWarningFilter"/>
  167. <include refid="deviceDistrictFilter"/>
  168. </select>
  169. <!-- 类型排行:排行的每行与标题「共 XX 起」必须同源,否则读者对不上数
  170. (维护者 2026-10-26:「页面「共27起」而三行只凑出 23 起」;2026-11-12 要求审查本项)。
  171. 所以这里与 selectWarningCountInWindow 用**完全相同的 WHERE**:时间窗 + 草稿排除 + 片区/行业。
  172. 注意 `warning_type` 是 NOT NULL 但可为空串:空串不能单独占一行(服务层会把它丢掉,
  173. 那样行之和就小于 total),所以归入一个明确的占位码 UNKNOWN,由同一套名称降级逻辑展示。 -->
  174. <select id="selectWarningTypeCountsInWindow" resultType="java.util.LinkedHashMap">
  175. <include refid="typeTree"/>
  176. SELECT CASE WHEN w.warning_type IS NULL OR TRIM(w.warning_type) = ''
  177. THEN 'UNKNOWN' ELSE w.warning_type END AS type_code,
  178. COUNT(*) AS type_count
  179. FROM app_user.early_warning w
  180. WHERE w.create_time &gt;= #{startInclusive} AND w.create_time &lt; #{endExclusive}
  181. <include refid="countableWarningFilter"/>
  182. <include refid="deviceDistrictFilter"/>
  183. GROUP BY CASE WHEN w.warning_type IS NULL OR TRIM(w.warning_type) = ''
  184. THEN 'UNKNOWN' ELSE w.warning_type END
  185. ORDER BY COUNT(*) DESC, type_code ASC
  186. </select>
  187. <!-- 基础集合 = alarm_data.audit_status = 1(维护者 2026-10-22 裁定,规格 §0)。
  188. 报警的片区/行业同样沿 a.device_code 回联设备台账(下面各处复用同一判据)。 -->
  189. <sql id="alarmDeviceFilter">
  190. <if test="district != null or unassigned">
  191. AND <choose>
  192. <when test="unassigned">
  193. NOT EXISTS (
  194. SELECT 1 FROM app_user.equipment_base eb
  195. WHERE eb.equipment_code = a.device_code
  196. AND eb.district IN (0, 1, 2)
  197. )
  198. </when>
  199. <otherwise>
  200. EXISTS (
  201. SELECT 1 FROM app_user.equipment_base eb
  202. WHERE eb.equipment_code = a.device_code
  203. AND eb.district = #{district}
  204. )
  205. </otherwise>
  206. </choose>
  207. </if>
  208. <if test="industryRootTypeId != null">
  209. AND EXISTS (
  210. SELECT 1 FROM app_user.equipment_base ebi
  211. JOIN type_tree tti ON tti.id = ebi.equipment_type_id
  212. WHERE ebi.equipment_code = a.device_code
  213. AND tti.root_id = #{industryRootTypeId}
  214. )
  215. </if>
  216. </sql>
  217. <select id="selectAlarmTotals" resultType="java.util.LinkedHashMap">
  218. <include refid="typeTree"/>
  219. SELECT COUNT(*) AS alarm_count,
  220. COALESCE(SUM(CASE WHEN a.create_time &gt;= #{todayStart} AND a.create_time &lt; #{tomorrowStart}
  221. THEN 1 ELSE 0 END), 0) AS today_new,
  222. COALESCE(SUM(CASE WHEN a.create_time &gt;= #{yesterdayStart} AND a.create_time &lt; #{todayStart}
  223. THEN 1 ELSE 0 END), 0) AS yesterday_new,
  224. COALESCE(SUM(CASE WHEN a.work_order_id IS NULL THEN 1 ELSE 0 END), 0) AS unconverted_count,
  225. MAX(a.update_time) AS source_updated_at
  226. FROM app_user.alarm_data a
  227. WHERE a.audit_status = 1
  228. <include refid="alarmDeviceFilter"/>
  229. </select>
  230. <select id="selectAlarmClosedCount" resultType="java.util.LinkedHashMap">
  231. <include refid="typeTree"/>
  232. SELECT COUNT(*) AS closed_count
  233. FROM app_user.alarm_data a
  234. JOIN app_user.work_order w ON w.order_id = a.work_order_id
  235. WHERE a.audit_status = 1 AND w.order_status = 5
  236. <include refid="alarmDeviceFilter"/>
  237. </select>
  238. <select id="selectAlarmLevelCounts" resultType="java.util.LinkedHashMap">
  239. <include refid="typeTree"/>
  240. SELECT a.alarm_level AS alarm_level,
  241. COUNT(*) AS level_count
  242. FROM app_user.alarm_data a
  243. WHERE a.audit_status = 1
  244. <include refid="alarmDeviceFilter"/>
  245. GROUP BY a.alarm_level
  246. </select>
  247. <!-- 按日两个读数(维护者 2026-10-23):
  248. · daily_count —— 当天新增(create_time 落在该日),走面板基础集合 audit_status = 1;
  249. · unhandled_total —— **截至当天结束时仍未处置**的累计条数(按「首次被处置的时刻」判定)。
  250. **口径修正(票 97,2026-10-28)**:旧实现用**当前** alarm_status = 0 过滤历史日,等于
  251. 「按今天的状态回溯过去」—— 今天处置一条,过去几天的值会一起变(维护者实测:处置后七天从
  252. 1,2,2,2,2,2,2 整体塌成 1,1,1,1,1,1,1),且历史值系统性偏低(10-22 那天真实积压 2:一条
  253. 10-16 创建的报警 10-23 才处置,旧实现对 10-22 只记 1)。现在改为:
  254. backlog(D) = #{ create_time ≤ D 结束 且 ( 首次处置时刻为空 或 首次处置时刻 > D 结束 ) },
  255. 首次处置时刻取 MIN(alarm_log.oper_time) WHERE oper_type IN (1,3)(见下方 oper_type 语义);
  256. 没有 type 1/3 日志的 HANDLED 报警退回 update_time 兜底(略偏高,已记票 97)。
  257. **两条线口径不同,是刻意如此**:
  258. · 新增按面板基础集合(已核实报警),与上方「今日新增」等读数同口径;
  259. · 未处理**不受审核状态限制** —— 一条报警处没处理,跟它审没审核是两个维度。
  260. 实测:只用 audit_status=1 时未处理恒为 0(那 2 条未处理的都是未审核),
  261. 这条线会画成一条恒零的直线,"积压还在"的意思就丢了。
  262. 累计必须从**入库最早一条**起算,不能只用窗口内数据 —— 否则窗口之前的积压会漏掉。
  263. 票 81:片区/行业筛选对两条线**都**生效(同一个「范围内」定义),否则新增与积压会互相打架。 -->
  264. <!-- 首次处置时刻(票 97,2026-10-28 在真实库上核验过 alarm_log.oper_type 语义):
  265. 0 = 报警产生(实测其 oper_time 与 alarm_data.create_time 完全相等,不能当处置时刻!);
  266. 1 = 人工确认/转工单;3 = 自动恢复("报警恢复,自动处理")。
  267. 所以处置时刻 = MIN(oper_time) WHERE oper_type IN (1, 3);对同一报警取**最早**一条
  268. (取最后一条会把"处置后又更新"误当成处置时刻,积压会算得偏低)。
  269. 实测:184 条报警里 92 条有日志、其中大量是 type 0 的产生记录 —— 若不过滤 oper_type,
  270. MIN(oper_time) 恒等于 create_time,整条积压线会被算成 0。 -->
  271. <select id="selectAlarmDailyCounts" resultType="java.util.LinkedHashMap">
  272. <include refid="typeTree"/>,
  273. days AS (
  274. SELECT TO_CHAR(d, 'YYYY-MM-DD') AS day, d AS day_date
  275. FROM generate_series(
  276. #{startInclusive}::timestamp,
  277. #{endExclusive}::timestamp - interval '1 day',
  278. interval '1 day'
  279. ) AS d
  280. ),
  281. created AS (
  282. SELECT TO_CHAR(a.create_time, 'YYYY-MM-DD') AS day, COUNT(*) AS c
  283. FROM app_user.alarm_data a
  284. WHERE a.audit_status = 1
  285. AND a.create_time &gt;= #{startInclusive} AND a.create_time &lt; #{endExclusive}
  286. <include refid="alarmDeviceFilter"/>
  287. GROUP BY 1
  288. ),
  289. handled AS (
  290. SELECT l.alarm_id AS alarm_id, MIN(l.oper_time) AS handled_at
  291. FROM app_user.alarm_log l
  292. WHERE l.oper_time IS NOT NULL
  293. AND l.oper_type IN (1, 3)
  294. GROUP BY l.alarm_id
  295. )
  296. SELECT d.day AS day,
  297. COALESCE(cr.c, 0) AS daily_count,
  298. (SELECT COUNT(*)
  299. FROM app_user.alarm_data a2
  300. LEFT JOIN handled h2 ON h2.alarm_id = a2.id
  301. WHERE a2.create_time &lt; (d.day_date + interval '1 day')
  302. AND CASE
  303. WHEN h2.handled_at IS NOT NULL
  304. THEN h2.handled_at &gt;= (d.day_date + interval '1 day')
  305. WHEN a2.alarm_status = 0 THEN TRUE
  306. ELSE COALESCE(a2.update_time, a2.create_time) &gt;= (d.day_date + interval '1 day')
  307. END
  308. <if test="district != null or unassigned">
  309. AND <choose>
  310. <when test="unassigned">
  311. NOT EXISTS (
  312. SELECT 1 FROM app_user.equipment_base eb2
  313. WHERE eb2.equipment_code = a2.device_code
  314. AND eb2.district IN (0, 1, 2)
  315. )
  316. </when>
  317. <otherwise>
  318. EXISTS (
  319. SELECT 1 FROM app_user.equipment_base eb2
  320. WHERE eb2.equipment_code = a2.device_code
  321. AND eb2.district = #{district}
  322. )
  323. </otherwise>
  324. </choose>
  325. </if>
  326. <if test="industryRootTypeId != null">
  327. AND EXISTS (
  328. SELECT 1 FROM app_user.equipment_base ebi2
  329. JOIN type_tree tti2 ON tti2.id = ebi2.equipment_type_id
  330. WHERE ebi2.equipment_code = a2.device_code
  331. AND tti2.root_id = #{industryRootTypeId}
  332. )
  333. </if>
  334. ) AS unhandled_total
  335. FROM days d
  336. LEFT JOIN created cr ON cr.day = d.day
  337. ORDER BY 1
  338. </select>
  339. <!-- 「最近一条」= 同工单同 oper_type 内 oper_time 最大、并列 log_id 最大(规格 §5);
  340. 负时长(日志早于报警)单独计数,由服务层剔除。 -->
  341. <select id="selectAlarmDurationSamples" resultType="java.util.LinkedHashMap">
  342. <include refid="typeTree"/>,
  343. base AS (
  344. SELECT a.work_order_id AS work_order_id, a.create_time AS create_time
  345. FROM app_user.alarm_data a
  346. WHERE a.audit_status = 1 AND a.work_order_id IS NOT NULL AND a.create_time IS NOT NULL
  347. <include refid="alarmDeviceFilter"/>
  348. ), latest_log AS (
  349. SELECT t.work_order_id AS work_order_id, t.oper_type AS oper_type, t.oper_time AS oper_time
  350. FROM (
  351. SELECT l.work_order_id AS work_order_id,
  352. l.oper_type AS oper_type,
  353. l.oper_time AS oper_time,
  354. ROW_NUMBER() OVER (PARTITION BY l.work_order_id, l.oper_type
  355. ORDER BY l.oper_time DESC, l.log_id DESC) AS rn
  356. FROM app_user.work_order_log l
  357. WHERE l.oper_type IN (2, 5) AND l.oper_time IS NOT NULL
  358. AND l.work_order_id IN (SELECT b.work_order_id FROM base b)
  359. ) t WHERE t.rn = 1
  360. )
  361. SELECT ll.oper_type AS oper_type,
  362. COALESCE(SUM(CASE WHEN ll.oper_time &gt;= b.create_time
  363. THEN EXTRACT(EPOCH FROM ll.oper_time) - EXTRACT(EPOCH FROM b.create_time)
  364. ELSE 0 END), 0) AS total_seconds,
  365. COUNT(CASE WHEN ll.oper_time &gt;= b.create_time THEN 1 END) AS valid_samples,
  366. COUNT(CASE WHEN ll.oper_time &lt; b.create_time THEN 1 END) AS invalid_samples
  367. FROM base b
  368. JOIN latest_log ll ON ll.work_order_id = b.work_order_id
  369. GROUP BY ll.oper_type
  370. </select>
  371. </mapper>