modeLayerPlan.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /**
  2. * 一级模式图层计划(票 46;规格 18 / 决策票 13)。
  3. *
  4. * 这是「一张图什么时候显示什么」的唯一领域接缝:不碰 Vue/Cesium/接口,不在组件或引擎
  5. * 二次过滤。设施点、管线和异常设备数据尚未全部具备时,计划如实输出 degradedNotes,
  6. * 不把缺数据伪装成空数据,也不伪造设施/管线标记。
  7. */
  8. import { MAP_OBJECT_KIND } from './mapSummary.js'
  9. import { planPointMarkers, pointLegend } from './pointStatus.js'
  10. import { facilityPointMarkers } from './facilityPoints.js'
  11. import { deviceDisplayStatusIndex } from './deviceStatus.js'
  12. export const MODE_LAYER = {
  13. OVERVIEW: 'overview',
  14. INFRASTRUCTURE: 'infrastructure',
  15. MONITORING: 'monitoring',
  16. WARNING_RESPONSE: 'warningResponse',
  17. }
  18. export const MODE_SELECTION = {
  19. PRESERVE: 'preserve',
  20. CLEAR: 'clear',
  21. }
  22. /** 「异常台数」计数口径:报警/预警两态(唯一消费方 `isAttentionDeviceStatus`)。 */
  23. const ATTENTION_STATUSES = new Set(['ALARM', 'WARNING'])
  24. /**
  25. * 预警处置模式下**可上图**的业务异常状态:报警 / 预警 / 工单处置。
  26. *
  27. * 与 `ATTENTION_STATUSES` **刻意分开**(两点不同,不要合并):
  28. * · 这里决定「场景上给谁坐标点」,后者只回答「异常台数怎么数」(票 47 的降级判定);
  29. * · 工单处置**只能看对象自身状态**(点位 `pointStatus` / 独立设备 `deviceStatus`)——
  30. * 设备异常清单 `/monitoring/situation → abnormalEquipment[]` 只装
  31. * WARNING/ALARM/EXPIRED/OFFLINE,不含 WORK_ORDER(见 `monitoringPanels.js` 的注释)。
  32. *
  33. * 2026-10-29 维护者要求:预警处置场景除报警/预警外,也要显示「工单处置中」的坐标点。
  34. */
  35. const VISIBLE_ATTENTION_STATUSES = new Set(['ALARM', 'WARNING', 'WORK_ORDER'])
  36. const MODE_KEYS = new Set(Object.values(MODE_LAYER))
  37. function hasArray(value) {
  38. return Array.isArray(value)
  39. }
  40. function note(key, text, mode) {
  41. return { key, text, mode }
  42. }
  43. /** 关联设备的模式过滤唯一判据;数据可用性仍由 deviceLayer.js 先判。 */
  44. export function isAssociatedDeviceVisible(mode, status) {
  45. if (mode === MODE_LAYER.INFRASTRUCTURE) return false
  46. if (mode === MODE_LAYER.WARNING_RESPONSE) return VISIBLE_ATTENTION_STATUSES.has(status)
  47. return true
  48. }
  49. /**
  50. * 对象**自身状态**是否够格在预警处置模式上图。
  51. * 工单处置态只能从这里来(设备异常清单不含该态),因此导出给需要同一判据的消费方。
  52. */
  53. export function isAttentionPointStatus(status) {
  54. return VISIBLE_ATTENTION_STATUSES.has(status)
  55. }
  56. /** 总览↔分项保留选择;分项↔分项清空选择。 */
  57. export function planModeSelectionTransition({ fromMode, toMode, selection } = {}) {
  58. const current = selection || { level: 'none', pointCode: '', deviceCode: '' }
  59. if (!fromMode || fromMode === toMode) return { action: MODE_SELECTION.PRESERVE, selection: current }
  60. if (fromMode === MODE_LAYER.OVERVIEW || toMode === MODE_LAYER.OVERVIEW) {
  61. return { action: MODE_SELECTION.PRESERVE, selection: current }
  62. }
  63. return {
  64. action: MODE_SELECTION.CLEAR,
  65. selection: { level: 'none', pointCode: '', deviceCode: '' },
  66. }
  67. }
  68. function abnormalCodeSet(abnormalDevices) {
  69. return new Set(
  70. (Array.isArray(abnormalDevices) ? abnormalDevices : [])
  71. .filter((item) => item && ATTENTION_STATUSES.has(item.status))
  72. .map((item) => item.code)
  73. .filter(Boolean),
  74. )
  75. }
  76. function abnormalStatusMap(abnormalDevices) {
  77. return new Map(
  78. (Array.isArray(abnormalDevices) ? abnormalDevices : [])
  79. .filter((item) => item && item.code)
  80. .map((item) => [item.code, item.status || '']),
  81. )
  82. }
  83. /**
  84. * 设备编码 → 显示状态;索引没覆盖到的设备回退到后端单一状态(不猜、不放行)。
  85. * @param {string} code 设备编码
  86. * @param {string} fallback 后端单一状态(地图对象自身 `status`)
  87. * @param {Map<string, string>} statusIndex `deviceDisplayStatusIndex()` 的产物
  88. */
  89. function displayStatusFor(code, fallback, statusIndex) {
  90. const key = String(code || '').trim()
  91. const status = key && statusIndex ? statusIndex.get(key) : ''
  92. return status || fallback || ''
  93. }
  94. /**
  95. * 定位揭示:目标本身是地图对象时,绕过当前模式/图例筛选临时放行。
  96. *
  97. * 「定位」的语义是让目标可见,不是只把相机开过去;但基础设施模式整体不呈现监测设备,
  98. * 该模式不破例。目标是否真能上图仍由 `planPointMarkers` 的坐标准入裁决,这里不伪造。
  99. * @param {Array} visible 已按模式/标签筛过的对象
  100. * @param {Array} objects 未筛的地图摘要对象
  101. * @param {string} mode 一级模式
  102. * @param {{code:string}|null} locateTarget 当前定位目标
  103. */
  104. function withLocateTarget(visible, objects, mode, locateTarget) {
  105. if (!locateTarget || mode === MODE_LAYER.INFRASTRUCTURE) return visible
  106. const code = String(locateTarget.code || '').trim()
  107. if (!code) return visible
  108. const target = (Array.isArray(objects) ? objects : []).find((object) => object && object.code === code)
  109. if (!target) return visible
  110. if (visible.some((object) => object && object.code === target.code)) return visible
  111. return [...visible, target]
  112. }
  113. /**
  114. * 预警处置模式的点位判据(两条路径取并集):
  115. * · **自身状态**是业务异常(报警/预警/工单处置)—— 工单处置只有这条路径可得;
  116. * · 它挂着的**设备**命中设备侧异常集(票 46 规格 Q8 的设备证据口径,保留)。
  117. */
  118. function isAttentionPoint(object, abnormalCodes, statusIndex) {
  119. if (!object || object.kind !== MAP_OBJECT_KIND.MONITORING_POINT) return false
  120. if (isAttentionPointStatus(object.status)) return true
  121. const deviceCodes = Array.isArray(object.deviceCodes) ? object.deviceCodes : []
  122. // 设备侧标签事实与「异常设备清单」取并集:后者只装四态快照,本次实测 0 条 WARNING,
  123. // 但仍可能有一台设备在 `/events/current` 里带预警标签(#108 已锁的标签口径)。
  124. // 这里只认报警/预警(与 abnormalCodeSet 的异常证据同口径);工单处置仍是「对象自身状态」那条路径。
  125. for (const code of deviceCodes) {
  126. if (ATTENTION_STATUSES.has(displayStatusFor(code, '', statusIndex))) return true
  127. if (abnormalCodes.has(code)) return true
  128. }
  129. return false
  130. }
  131. /** 未关联设备同口径:自身显示状态是业务异常,或命中设备侧异常集。 */
  132. function isAttentionEquipment(object, abnormalCodes, statusIndex) {
  133. if (!object) return false
  134. return isAttentionPointStatus(displayStatusFor(object.code, object.status, statusIndex)) || abnormalCodes.has(object.code)
  135. }
  136. function visibleMapObjects(mode, objects, abnormalDevices, deviceFilter, statusIndex, locateTarget) {
  137. const abnormalCodes = abnormalCodeSet(abnormalDevices)
  138. const source = Array.isArray(objects) ? objects : []
  139. // 设备标签筛选:点位只要有任一关联设备命中即显示(监测点是设备聚合点);
  140. // 独立设备按自身编码命中。
  141. const tagFiltered = deviceFilter == null ? source : source.filter((object) => {
  142. if (!object) return false
  143. const codes = deviceFilter.codes || new Set()
  144. if (object.kind === MAP_OBJECT_KIND.MONITORING_POINT) {
  145. const deviceCodes = Array.isArray(object.deviceCodes) ? object.deviceCodes : []
  146. // 空白点位(无关联设备)只在勾选「无设备监测点」时显示;其它标签按关联设备命中。
  147. if (!deviceCodes.length) return deviceFilter.emptyPoints === true
  148. return deviceCodes.some((code) => codes.has(code))
  149. }
  150. if (object.kind === MAP_OBJECT_KIND.UNASSOCIATED_EQUIPMENT) return codes.has(object.code)
  151. return false
  152. })
  153. if (mode === MODE_LAYER.INFRASTRUCTURE) return []
  154. if (mode === MODE_LAYER.WARNING_RESPONSE) {
  155. return withLocateTarget(tagFiltered.filter((object) => {
  156. if (!object) return false
  157. if (object.kind === MAP_OBJECT_KIND.MONITORING_POINT) return isAttentionPoint(object, abnormalCodes, statusIndex)
  158. if (object.kind === MAP_OBJECT_KIND.UNASSOCIATED_EQUIPMENT) return isAttentionEquipment(object, abnormalCodes, statusIndex)
  159. return false
  160. }), source, mode, locateTarget)
  161. }
  162. return withLocateTarget(tagFiltered.filter((object) => object && (
  163. object.kind === MAP_OBJECT_KIND.MONITORING_POINT ||
  164. object.kind === MAP_OBJECT_KIND.UNASSOCIATED_EQUIPMENT
  165. )), source, mode, locateTarget)
  166. }
  167. function planNonPointLayers({ mode, entered, facilities, pipelines, facilityPoints, degradedNotes }) {
  168. const facilityVisible = mode === MODE_LAYER.OVERVIEW || mode === MODE_LAYER.INFRASTRUCTURE
  169. const pipelineVisible = entered && MODE_KEYS.has(mode)
  170. const facilityReady = hasArray(facilities)
  171. const pipelineReady = hasArray(pipelines)
  172. // 票 68:设施点(水厂/泵站/水源地/用水户)与管线/构件同属基础设施对象,显隐跟同一档模式。
  173. const facilityPointReady = hasArray(facilityPoints)
  174. if (facilityVisible && !facilityReady) degradedNotes.push(note('facilities-unready', '设施数据未就绪', mode))
  175. if (pipelineVisible && !pipelineReady) degradedNotes.push(note('pipelines-unready', '管线数据未就绪', mode))
  176. if (facilityVisible && !facilityPointReady) degradedNotes.push(note('facility-points-unready', '设施点数据未就绪', mode))
  177. // 票 33:**原样引用,不 slice()**。每 30s freshness tick 会让计划整体重算——
  178. // 以前这里每次 `slice()` 出新数组,`MapCanvas` 的 `deep:false` watcher 靠引用判变化,
  179. // 于是「数据没变」也被判成变了,批批触发管线全量重同步(票 26 诊断的放大器之一)。
  180. // 计划的消费者(mapService/MapCanvas)只读不写这两个数组,别名是安全的。
  181. return {
  182. facilities: facilityVisible && facilityReady ? facilities : [],
  183. pipelines: pipelineVisible && pipelineReady ? pipelines : [],
  184. // 设施 store 持有档案行;只有此处转换为带唯一身份/可点标志的渲染标记。
  185. facilityPoints: facilityVisible && facilityPointReady ? facilityPointMarkers(facilityPoints) : [],
  186. layerStates: {
  187. facilities: facilityVisible ? (facilityReady ? 'ready' : 'degraded') : 'hidden',
  188. pipelines: pipelineVisible ? (pipelineReady ? 'ready' : 'degraded') : 'hidden',
  189. facilityPoints: facilityVisible ? (facilityPointReady ? 'ready' : 'degraded') : 'hidden',
  190. },
  191. }
  192. }
  193. function selectionVisible({ mode, selection, objects, markers, associatedDevices, abnormalCodes, statusIndex, locateTarget }) {
  194. const current = selection || {}
  195. if (!current.level || current.level === 'none') return false
  196. // 定位目标本身必须可见(#127):定位的语义是「让目标可见」,不是只把相机开过去。
  197. // 基础设施模式整体不呈现监测设备,该模式不破例;目标没被坐标准入收下时也不画面板。
  198. if (locateTarget && mode !== MODE_LAYER.INFRASTRUCTURE) {
  199. const targetCode = String(locateTarget.code || '').trim()
  200. const targetDevice = String(locateTarget.deviceCode || '').trim()
  201. const targetDrawn = Boolean(targetCode) && (markers || []).some((marker) => marker && marker.code === targetCode)
  202. if (targetDrawn && (current.deviceCode === targetCode
  203. || (!current.deviceCode && current.pointCode === targetCode)
  204. || (targetDevice && current.deviceCode === targetDevice))) return true
  205. }
  206. // 设备级选择**优先看设备**(设备级选择仍保留 pointCode 供"返回点位"用,
  207. // 先查 pointCode 会把"设备被本模式隐藏"误判成"点位可见 ⇒ 显示")。
  208. if (current.deviceCode) {
  209. // 该设备是**当前点位的关联设备**(在 `associatedDevices` 里找得到):
  210. // 详情面板与该设备能否上图无关 —— 一台 `no-location`(后端判不可上图)的设备
  211. // 仍然要看它的档案/指标/预警等分区,所以这里**不要求 placement === 'drawn'**,
  212. // 只按模式可见性(预警处置显示报警/预警/工单处置)判定。
  213. // (本票真机人工走查抓到:默认现网边界设备统一 `UNVERIFIED_COORDINATE/no-location`,
  214. // 点设备卡片后 `selectionVisible=false` ⇒ 外壳 `<aside v-if>` 卸载 ⇒ 摘要面板消失。)
  215. const row = (Array.isArray(associatedDevices) ? associatedDevices : [])
  216. .find((item) => item && item.code === current.deviceCode)
  217. if (row) {
  218. const parent = (Array.isArray(objects) ? objects : []).find((item) =>
  219. item && item.kind === MAP_OBJECT_KIND.MONITORING_POINT && item.code === row.pointCode)
  220. return Boolean(parent && isAssociatedDeviceVisible(mode, row.status) &&
  221. (mode !== MODE_LAYER.WARNING_RESPONSE || isAttentionPoint(parent, abnormalCodes, statusIndex)))
  222. }
  223. // 未关联设备:以地图对象身份判(基础设施整体隐藏;预警处置只认异常设备/预警标签)。
  224. const object = (Array.isArray(objects) ? objects : [])
  225. .find((item) => item && item.kind === MAP_OBJECT_KIND.UNASSOCIATED_EQUIPMENT && item.code === current.deviceCode)
  226. return Boolean(object && (mode !== MODE_LAYER.INFRASTRUCTURE) &&
  227. (mode !== MODE_LAYER.WARNING_RESPONSE || isAttentionEquipment(object, abnormalCodes, statusIndex)))
  228. }
  229. if (current.pointCode) {
  230. const point = (Array.isArray(objects) ? objects : []).find((item) =>
  231. item && item.kind === MAP_OBJECT_KIND.MONITORING_POINT && item.code === current.pointCode)
  232. return Boolean(point && mode !== MODE_LAYER.INFRASTRUCTURE &&
  233. (mode !== MODE_LAYER.WARNING_RESPONSE || isAttentionPoint(point, abnormalCodes, statusIndex)))
  234. }
  235. return false
  236. }
  237. /**
  238. * 统一输出当前模式的图层计划。
  239. *
  240. * @returns {{mode:string, entered:boolean, markers:Array, blocked:Array, legend:Array,
  241. * locateTargets:Array, degradedNotes:Array, facilities:Array, pipelines:Array,
  242. * facilityPoints:Array, associatedDevices:Array, layerStates:object, selection:object}}
  243. */
  244. export function planModeLayers({
  245. mode = MODE_LAYER.OVERVIEW,
  246. objects = [],
  247. abnormalDevices,
  248. /** 命中所选设备标签的设备编码集(null=不筛)。筛选与图例同一口径(维护者 2026-11-05)。 */
  249. deviceFilter = null,
  250. /** 设备标签事实(`deviceStatusStore.pools.devices`);未就绪时回退对象自身 `status`。 */
  251. deviceFacts = null,
  252. /** 当前定位目标(`{ code, deviceCode? }`):目标本身是地图对象时临时放行,基础设施模式除外。 */
  253. locateTarget = null,
  254. facilities,
  255. pipelines,
  256. facilityPoints,
  257. associatedDevices = [],
  258. selection,
  259. entered = true,
  260. } = {}) {
  261. const normalizedMode = MODE_KEYS.has(mode) ? mode : MODE_LAYER.OVERVIEW
  262. const degradedNotes = []
  263. if (entered !== true) {
  264. return {
  265. mode: normalizedMode,
  266. entered: false,
  267. markers: [],
  268. blocked: [],
  269. legend: [],
  270. locateTargets: [],
  271. degradedNotes: [],
  272. facilities: [],
  273. pipelines: [],
  274. facilityPoints: [],
  275. associatedDevices: [],
  276. layerStates: { facilities: 'hidden', pipelines: 'hidden', facilityPoints: 'hidden', associatedDevices: 'hidden' },
  277. selection: { visible: false, action: MODE_SELECTION.PRESERVE },
  278. }
  279. }
  280. const statusIndex = deviceDisplayStatusIndex(deviceFacts)
  281. const source = visibleMapObjects(normalizedMode, objects, abnormalDevices, deviceFilter, statusIndex, locateTarget)
  282. // 只有未关联设备需要按标签覆盖显示状态;监测点的视觉仍是后端聚合状态(CONTEXT.md)。
  283. const statusOfObject = (item) => (item && item.kind === MAP_OBJECT_KIND.UNASSOCIATED_EQUIPMENT
  284. ? displayStatusFor(item.code, item.status, statusIndex)
  285. : (item && item.status) || '')
  286. const pointPlan = planPointMarkers(source, statusOfObject)
  287. const abnormalCodes = abnormalCodeSet(abnormalDevices)
  288. const abnormalDataReady = hasArray(abnormalDevices)
  289. if (normalizedMode === MODE_LAYER.WARNING_RESPONSE && !abnormalDataReady) {
  290. degradedNotes.push(note('abnormal-devices-unready', '异常设备数据未就绪,预警处置仅按对象自身状态显示', normalizedMode))
  291. }
  292. const visiblePointCodes = new Set(source.filter((item) => item.kind === MAP_OBJECT_KIND.MONITORING_POINT).map((item) => item.code))
  293. const visibleDevices = (Array.isArray(associatedDevices) ? associatedDevices : [])
  294. .filter((row) => row && visiblePointCodes.has(row.pointCode) && row.placement === 'drawn' && isAssociatedDeviceVisible(normalizedMode, row.status))
  295. const nonPoint = planNonPointLayers({
  296. mode: normalizedMode,
  297. entered: true,
  298. facilities,
  299. pipelines,
  300. facilityPoints,
  301. degradedNotes,
  302. })
  303. const locateTargets = pointPlan.markers.map((marker) => ({
  304. kind: marker.kind,
  305. code: marker.code,
  306. name: marker.name,
  307. longitude: marker.longitude,
  308. latitude: marker.latitude,
  309. }))
  310. return {
  311. mode: normalizedMode,
  312. entered: true,
  313. // `markers` 与 `legend` 都来自同一份 pointPlan,地图服务只消费它们,不重判模式。
  314. markers: pointPlan.markers,
  315. blocked: pointPlan.blocked,
  316. legend: pointLegend(pointPlan.byStatus),
  317. byStatus: pointPlan.byStatus,
  318. locateTargets,
  319. degradedNotes,
  320. facilities: nonPoint.facilities,
  321. pipelines: nonPoint.pipelines,
  322. facilityPoints: nonPoint.facilityPoints,
  323. associatedDevices: visibleDevices,
  324. blockedAssociatedDevices: (Array.isArray(associatedDevices) ? associatedDevices : [])
  325. .filter((row) => row && row.placement !== 'drawn'),
  326. layerStates: {
  327. ...nonPoint.layerStates,
  328. associatedDevices: normalizedMode === MODE_LAYER.INFRASTRUCTURE ? 'hidden' : 'ready',
  329. },
  330. selection: {
  331. visible: selectionVisible({
  332. mode: normalizedMode,
  333. selection,
  334. objects,
  335. markers: pointPlan.markers,
  336. associatedDevices,
  337. abnormalCodes,
  338. statusIndex,
  339. locateTarget,
  340. }),
  341. action: MODE_SELECTION.PRESERVE,
  342. },
  343. }
  344. }
  345. /** 「异常台数」计数判据(报警/预警两态,票 47 降级口径),**不是**场景可见性判据 ——
  346. * 可见性见 `isAttentionPointStatus`(多含工单处置)。 */
  347. export function isAttentionDeviceStatus(status) {
  348. return ATTENTION_STATUSES.has(status)
  349. }
  350. /** 仅供验证说明使用:异常设备状态来源是设备事实,不是点位聚合状态。 */
  351. export function abnormalStatusesFromDevices(abnormalDevices) {
  352. return [...abnormalStatusMap(abnormalDevices).entries()].map(([code, status]) => ({ code, status }))
  353. }