eventDetail.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * 事件详情与定位计划(票 14)。纯函数:不发请求、不碰 DOM 或地图服务。
  3. * 事件详情保留列表行的领域字段;定位只认后端 coordinateValidation.renderable。
  4. */
  5. import { eventLevelTag, eventStatusText, eventTypeText } from './eventList.js'
  6. import { TIME_LABELS } from './fields.js'
  7. import { LOCATE_RESULT, LOCATE_TEXT, findMapObject, locateCameraTarget } from './mapLocate.js'
  8. import { blockReason } from './coordinate.js'
  9. import { alarmAuditView } from './alarmAudit.js'
  10. export const EVENT_DETAIL_RESULT = {
  11. OK: 'ok',
  12. MISSING: LOCATE_RESULT.MISSING,
  13. BLOCKED: LOCATE_RESULT.BLOCKED,
  14. UNAVAILABLE: LOCATE_RESULT.UNAVAILABLE,
  15. }
  16. const PLACEHOLDER = '—'
  17. function valueText(value, unit = '') {
  18. if (typeof value !== 'number' || !Number.isFinite(value)) return PLACEHOLDER
  19. return unit ? `${value} ${unit}` : String(value)
  20. }
  21. function thresholdText(min, max, unit = '') {
  22. const hasMin = typeof min === 'number' && Number.isFinite(min)
  23. const hasMax = typeof max === 'number' && Number.isFinite(max)
  24. if (!hasMin && !hasMax) return PLACEHOLDER
  25. const text = hasMin && hasMax ? `${min} ~ ${max}` : hasMin ? `≥ ${min}` : `≤ ${max}`
  26. return unit ? `${text} ${unit}` : text
  27. }
  28. /**
  29. * 事件的位置描述与「能不能上图」(验收第 5 条)。
  30. *
  31. * 「事件仍可查看 + 显示无地图位置原因」= 位置上永远有一句实话:
  32. * · 后端说可上图 → 显示后端的文本位置;
  33. * · 后端说不可上图 → 原因沿用票 07 的**同一张词表**(`coordinate.js` 的 `blockReason`),
  34. * 本层不发明第二套原因码;
  35. * · 连校验结论都没给 → 如实说「无地图位置」,不假装可定位。
  36. */
  37. export function eventLocationView(event) {
  38. const current = event || {}
  39. const coordinate = current.coordinate || null
  40. const renderable = !!coordinate && coordinate.renderable === true
  41. const text = current.location || ''
  42. if (!renderable) {
  43. const reason = blockReason(current).text
  44. return { hasLocation: false, location: text ? `${text} · ${reason}` : reason }
  45. }
  46. return { hasLocation: true, location: text || '已发布坐标(未给文本位置)' }
  47. }
  48. /**
  49. * 详情读的是**列表行**(票 13 的 `eventRowView`),不是后端裸载荷。
  50. *
  51. * 所以等级与状态**优先直接沿用行上已经算好的那一份**(`row.level` 的 tone/label、
  52. * `row.statusText`)—— 这样「列表与详情的等级/状态一致」是**取同一份数据**的结构保证,
  53. * 而不是两边各自调用一次同一个函数再指望结果相同。行上没带(比如直接喂领域对象)时,
  54. * 才退回 `domain/eventList.js` 的同一张表,绝不在这里另写一份映射。
  55. */
  56. function levelOf(row) {
  57. const tagged = row && row.level
  58. if (tagged && typeof tagged === 'object' && tagged.label) return { tone: tagged.tone, label: tagged.label }
  59. return eventLevelTag(tagged)
  60. }
  61. function statusOf(row) {
  62. if (row && typeof row.statusText === 'string') return row.statusText
  63. return eventStatusText(row && row.status)
  64. }
  65. export function eventDetailView(event) {
  66. const current = event || {}
  67. const level = levelOf(current)
  68. const alarm = String(current.family || '').toUpperCase() === 'ALARM'
  69. const audit = alarm && current.auditStatus !== undefined && current.auditStatus !== null ? alarmAuditView(current.auditStatus, current.auditStatusName) : null
  70. const orderText = current.workOrderCode
  71. ? `${current.workOrderCode} · ${current.workOrderStateName || '状态未发布'}`
  72. : ''
  73. return {
  74. code: current.codeText || '编码未提供',
  75. name: current.name || PLACEHOLDER,
  76. deviceCode: current.deviceCode || PLACEHOLDER,
  77. deviceName: current.deviceName || PLACEHOLDER,
  78. metric: eventTypeText(current.type, current.typeName) || '未提供',
  79. threshold: thresholdText(current.thresholdMinValue, current.thresholdMaxValue, current.unit),
  80. value: valueText(current.value, current.unit),
  81. level,
  82. eventTime: current.eventTime || PLACEHOLDER,
  83. // 事件发生时刻(预警发布 / 报警创建)——标签取自 `TIME_LABELS` 单点词表(票 62)。
  84. eventTimeLabel: TIME_LABELS.event,
  85. updatedTime: current.updatedTime || '',
  86. status: audit ? audit.label : statusOf(current) || PLACEHOLDER,
  87. auditStatusText: audit ? audit.label : '',
  88. auditStatusTone: audit ? audit.tone : '',
  89. workOrderCode: current.workOrderCode || '',
  90. workOrderStateName: current.workOrderStateName || '',
  91. ...eventLocationView(current),
  92. monitoringPointCode: current.monitoringPointCode || '',
  93. monitoringPointName: current.monitoringPointName || '',
  94. summary: [level.label, audit ? audit.label : statusOf(current) || PLACEHOLDER, orderText, valueText(current.value, current.unit)].filter(Boolean).join(' · '),
  95. identityItems: [
  96. { label: '事件编码', value: current.codeText || '编码未提供', mono: true },
  97. { label: '设备', value: current.deviceName || current.deviceCode || PLACEHOLDER },
  98. { label: '监测点', value: current.monitoringPointName || current.monitoringPointCode || PLACEHOLDER },
  99. ...(audit ? [{ label: '审核结果', value: audit.label }] : []),
  100. ...(orderText ? [{ label: '关联工单', value: orderText }] : []),
  101. ],
  102. readingItems: [
  103. { label: '指标', value: eventTypeText(current.type, current.typeName) || '未提供' },
  104. { label: '阈值', value: thresholdText(current.thresholdMinValue, current.thresholdMaxValue, current.unit), mono: true },
  105. { label: '实际值', value: valueText(current.value, current.unit), mono: true, emphasis: true },
  106. { label: TIME_LABELS.event, value: current.eventTime || PLACEHOLDER, mono: true },
  107. ],
  108. }
  109. }
  110. /** 事件设备编码 -> 已加载地图对象。事件自身不创建独立地图标记。 */
  111. export function planEventLocate(event, objects, mapReady = true) {
  112. const current = event || {}
  113. const deviceCode = typeof current.deviceCode === 'string' ? current.deviceCode.trim() : ''
  114. const pointCode = typeof current.monitoringPointCode === 'string' ? current.monitoringPointCode.trim() : ''
  115. const found = findMapObjectByDevice(objects, deviceCode, pointCode)
  116. if (!found && current.coordinate && current.coordinate.validated && current.coordinate.renderable !== true) {
  117. return { result: EVENT_DETAIL_RESULT.BLOCKED, object: null, deviceCode, pointCode, codes: [deviceCode, pointCode], reasonText: blockReason(current).text }
  118. }
  119. if (!found) return { result: EVENT_DETAIL_RESULT.MISSING, object: null, deviceCode, pointCode, codes: [deviceCode, pointCode], reasonText: LOCATE_TEXT.MISSING }
  120. if (!found.coordinate || found.coordinate.renderable !== true) {
  121. return {
  122. result: EVENT_DETAIL_RESULT.BLOCKED,
  123. object: found,
  124. deviceCode,
  125. pointCode,
  126. codes: [deviceCode, pointCode, found.code],
  127. reasonText: found.coordinate && found.coordinate.reason ? blockReason(found).text : '无地图位置',
  128. }
  129. }
  130. if (!mapReady) return { result: EVENT_DETAIL_RESULT.UNAVAILABLE, object: found, deviceCode, pointCode, codes: [deviceCode, found.code], reasonText: LOCATE_TEXT.UNAVAILABLE }
  131. return {
  132. result: EVENT_DETAIL_RESULT.OK,
  133. object: found,
  134. deviceCode,
  135. pointCode,
  136. codes: eventHighlightCodes(current, found),
  137. target: locateCameraTarget(found),
  138. reasonText: '',
  139. }
  140. }
  141. /**
  142. * 要高亮的编码(票 14 验收第 3 条:高亮**设备与关联管线**,不新建实体)。
  143. *
  144. * 三类编码各有用途,缺一类就有东西高亮不到:
  145. * · `found.code` —— 已上图的那个**点位标记**(标记按点位码建,设备码匹配不到它);
  146. * · `deviceCode` —— **关联管线**的匹配键(管线的不变量挂在设备上,不挂在点位上;
  147. * 见 CONTEXT.md「管线 → 设备」的关联方向),票 22 的管线交付后按它高亮;
  148. * · `pipelineCodes` —— 后端若在事件行上直接给管线码(当前契约没有),原样带上,不丢。
  149. * 事件自己的 `longitude/latitude` **不算编码**:它只用来回联,不用来画实体。
  150. */
  151. export function eventHighlightCodes(event, mapObject) {
  152. const current = event || {}
  153. const found = mapObject || {}
  154. const deviceCode = typeof current.deviceCode === 'string' ? current.deviceCode.trim() : ''
  155. const extra = Array.isArray(current.pipelineCodes) ? current.pipelineCodes.map((item) => String(item || '').trim()) : []
  156. return [found.code, deviceCode, ...extra].filter(Boolean)
  157. }
  158. function findMapObjectByDevice(objects, deviceCode, pointCode) {
  159. const list = Array.isArray(objects) ? objects : []
  160. const exact = deviceCode && list.find((item) => item &&
  161. ['unassociated-equipment', 'monitoring-device'].includes(item.kind) && String(item.code).trim() === deviceCode)
  162. if (exact) return exact
  163. const parent = deviceCode && list.find((item) => item && Array.isArray(item.deviceCodes) &&
  164. item.deviceCodes.some((code) => String(code).trim() === deviceCode))
  165. if (parent) return parent
  166. return pointCode ? findMapObject(list, pointCode) : null
  167. }
  168. export { PLACEHOLDER, thresholdText, valueText }