modeLayerPlan.js 15 KB

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