| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- /**
- * 事件详情与定位计划(票 14)。纯函数:不发请求、不碰 DOM 或地图服务。
- * 事件详情保留列表行的领域字段;定位只认后端 coordinateValidation.renderable。
- */
- import { eventLevelTag, eventStatusText, eventTypeText } from './eventList.js'
- import { TIME_LABELS } from './fields.js'
- import { LOCATE_RESULT, LOCATE_TEXT, findMapObject, locateCameraTarget } from './mapLocate.js'
- import { blockReason } from './coordinate.js'
- import { alarmAuditView } from './alarmAudit.js'
- export const EVENT_DETAIL_RESULT = {
- OK: 'ok',
- MISSING: LOCATE_RESULT.MISSING,
- BLOCKED: LOCATE_RESULT.BLOCKED,
- UNAVAILABLE: LOCATE_RESULT.UNAVAILABLE,
- }
- const PLACEHOLDER = '—'
- function valueText(value, unit = '') {
- if (typeof value !== 'number' || !Number.isFinite(value)) return PLACEHOLDER
- return unit ? `${value} ${unit}` : String(value)
- }
- function thresholdText(min, max, unit = '') {
- const hasMin = typeof min === 'number' && Number.isFinite(min)
- const hasMax = typeof max === 'number' && Number.isFinite(max)
- if (!hasMin && !hasMax) return PLACEHOLDER
- const text = hasMin && hasMax ? `${min} ~ ${max}` : hasMin ? `≥ ${min}` : `≤ ${max}`
- return unit ? `${text} ${unit}` : text
- }
- /**
- * 事件的位置描述与「能不能上图」(验收第 5 条)。
- *
- * 「事件仍可查看 + 显示无地图位置原因」= 位置上永远有一句实话:
- * · 后端说可上图 → 显示后端的文本位置;
- * · 后端说不可上图 → 原因沿用票 07 的**同一张词表**(`coordinate.js` 的 `blockReason`),
- * 本层不发明第二套原因码;
- * · 连校验结论都没给 → 如实说「无地图位置」,不假装可定位。
- */
- export function eventLocationView(event) {
- const current = event || {}
- const coordinate = current.coordinate || null
- const renderable = !!coordinate && coordinate.renderable === true
- const text = current.location || ''
- if (!renderable) {
- const reason = blockReason(current).text
- return { hasLocation: false, location: text ? `${text} · ${reason}` : reason }
- }
- return { hasLocation: true, location: text || '已发布坐标(未给文本位置)' }
- }
- /**
- * 详情读的是**列表行**(票 13 的 `eventRowView`),不是后端裸载荷。
- *
- * 所以等级与状态**优先直接沿用行上已经算好的那一份**(`row.level` 的 tone/label、
- * `row.statusText`)—— 这样「列表与详情的等级/状态一致」是**取同一份数据**的结构保证,
- * 而不是两边各自调用一次同一个函数再指望结果相同。行上没带(比如直接喂领域对象)时,
- * 才退回 `domain/eventList.js` 的同一张表,绝不在这里另写一份映射。
- */
- function levelOf(row) {
- const tagged = row && row.level
- if (tagged && typeof tagged === 'object' && tagged.label) return { tone: tagged.tone, label: tagged.label }
- return eventLevelTag(tagged)
- }
- function statusOf(row) {
- if (row && typeof row.statusText === 'string') return row.statusText
- return eventStatusText(row && row.status)
- }
- export function eventDetailView(event) {
- const current = event || {}
- const level = levelOf(current)
- const alarm = String(current.family || '').toUpperCase() === 'ALARM'
- const audit = alarm && current.auditStatus !== undefined && current.auditStatus !== null ? alarmAuditView(current.auditStatus, current.auditStatusName) : null
- const orderText = current.workOrderCode
- ? `${current.workOrderCode} · ${current.workOrderStateName || '状态未发布'}`
- : ''
- return {
- code: current.codeText || '编码未提供',
- name: current.name || PLACEHOLDER,
- deviceCode: current.deviceCode || PLACEHOLDER,
- deviceName: current.deviceName || PLACEHOLDER,
- metric: eventTypeText(current.type, current.typeName) || '未提供',
- threshold: thresholdText(current.thresholdMinValue, current.thresholdMaxValue, current.unit),
- value: valueText(current.value, current.unit),
- level,
- eventTime: current.eventTime || PLACEHOLDER,
- // 事件发生时刻(预警发布 / 报警创建)——标签取自 `TIME_LABELS` 单点词表(票 62)。
- eventTimeLabel: TIME_LABELS.event,
- updatedTime: current.updatedTime || '',
- status: audit ? audit.label : statusOf(current) || PLACEHOLDER,
- auditStatusText: audit ? audit.label : '',
- auditStatusTone: audit ? audit.tone : '',
- workOrderCode: current.workOrderCode || '',
- workOrderStateName: current.workOrderStateName || '',
- ...eventLocationView(current),
- monitoringPointCode: current.monitoringPointCode || '',
- monitoringPointName: current.monitoringPointName || '',
- summary: [level.label, audit ? audit.label : statusOf(current) || PLACEHOLDER, orderText, valueText(current.value, current.unit)].filter(Boolean).join(' · '),
- identityItems: [
- { label: '事件编码', value: current.codeText || '编码未提供', mono: true },
- { label: '设备', value: current.deviceName || current.deviceCode || PLACEHOLDER },
- { label: '监测点', value: current.monitoringPointName || current.monitoringPointCode || PLACEHOLDER },
- ...(audit ? [{ label: '审核结果', value: audit.label }] : []),
- ...(orderText ? [{ label: '关联工单', value: orderText }] : []),
- ],
- readingItems: [
- { label: '指标', value: eventTypeText(current.type, current.typeName) || '未提供' },
- { label: '阈值', value: thresholdText(current.thresholdMinValue, current.thresholdMaxValue, current.unit), mono: true },
- { label: '实际值', value: valueText(current.value, current.unit), mono: true, emphasis: true },
- { label: TIME_LABELS.event, value: current.eventTime || PLACEHOLDER, mono: true },
- ],
- }
- }
- /** 事件设备编码 -> 已加载地图对象。事件自身不创建独立地图标记。 */
- export function planEventLocate(event, objects, mapReady = true) {
- const current = event || {}
- const deviceCode = typeof current.deviceCode === 'string' ? current.deviceCode.trim() : ''
- const pointCode = typeof current.monitoringPointCode === 'string' ? current.monitoringPointCode.trim() : ''
- const found = findMapObjectByDevice(objects, deviceCode, pointCode)
- if (!found && current.coordinate && current.coordinate.validated && current.coordinate.renderable !== true) {
- return { result: EVENT_DETAIL_RESULT.BLOCKED, object: null, deviceCode, pointCode, codes: [deviceCode, pointCode], reasonText: blockReason(current).text }
- }
- if (!found) return { result: EVENT_DETAIL_RESULT.MISSING, object: null, deviceCode, pointCode, codes: [deviceCode, pointCode], reasonText: LOCATE_TEXT.MISSING }
- if (!found.coordinate || found.coordinate.renderable !== true) {
- return {
- result: EVENT_DETAIL_RESULT.BLOCKED,
- object: found,
- deviceCode,
- pointCode,
- codes: [deviceCode, pointCode, found.code],
- reasonText: found.coordinate && found.coordinate.reason ? blockReason(found).text : '无地图位置',
- }
- }
- if (!mapReady) return { result: EVENT_DETAIL_RESULT.UNAVAILABLE, object: found, deviceCode, pointCode, codes: [deviceCode, found.code], reasonText: LOCATE_TEXT.UNAVAILABLE }
- return {
- result: EVENT_DETAIL_RESULT.OK,
- object: found,
- deviceCode,
- pointCode,
- codes: eventHighlightCodes(current, found),
- target: locateCameraTarget(found),
- reasonText: '',
- }
- }
- /**
- * 要高亮的编码(票 14 验收第 3 条:高亮**设备与关联管线**,不新建实体)。
- *
- * 三类编码各有用途,缺一类就有东西高亮不到:
- * · `found.code` —— 已上图的那个**点位标记**(标记按点位码建,设备码匹配不到它);
- * · `deviceCode` —— **关联管线**的匹配键(管线的不变量挂在设备上,不挂在点位上;
- * 见 CONTEXT.md「管线 → 设备」的关联方向),票 22 的管线交付后按它高亮;
- * · `pipelineCodes` —— 后端若在事件行上直接给管线码(当前契约没有),原样带上,不丢。
- * 事件自己的 `longitude/latitude` **不算编码**:它只用来回联,不用来画实体。
- */
- export function eventHighlightCodes(event, mapObject) {
- const current = event || {}
- const found = mapObject || {}
- const deviceCode = typeof current.deviceCode === 'string' ? current.deviceCode.trim() : ''
- const extra = Array.isArray(current.pipelineCodes) ? current.pipelineCodes.map((item) => String(item || '').trim()) : []
- return [found.code, deviceCode, ...extra].filter(Boolean)
- }
- function findMapObjectByDevice(objects, deviceCode, pointCode) {
- const list = Array.isArray(objects) ? objects : []
- const exact = deviceCode && list.find((item) => item &&
- ['unassociated-equipment', 'monitoring-device'].includes(item.kind) && String(item.code).trim() === deviceCode)
- if (exact) return exact
- const parent = deviceCode && list.find((item) => item && Array.isArray(item.deviceCodes) &&
- item.deviceCodes.some((code) => String(code).trim() === deviceCode))
- if (parent) return parent
- return pointCode ? findMapObject(list, pointCode) : null
- }
- export { PLACEHOLDER, thresholdText, valueText }
|