Kaynağa Gözat

fix(cockpit): 资产详情时间统一与字段存在性规则

ChenJZ 8 saat önce
ebeveyn
işleme
58d248429d

+ 36 - 4
src/modules/cockpit/domain/entityDetail.js

@@ -88,12 +88,17 @@ export function prefilledEntityDetail(identity, prefill) {
     return normalizeEntityDetail(identity, {}, prefill)
 }
 
+/** 抬头:类别 + 业务编码;没有业务编码列时只留类别,有列缺值才补「编码未提供」。 */
+function assetSubtitle(kindLabel, displayCode, hasCodeColumn = true) {
+    return [kindLabel, hasCodeColumn ? displayCode || '编码未提供' : ''].filter(Boolean).join(' · ')
+}
+
 export function entityDetailView(detail) {
     const current = detail || {}
     return {
         title: current.title || current.name || '实体详情',
         // 副标题仅用业务编号;缺码不回退主键。
-        subtitle: current.subtitle || [current.kindLabel, current.displayCode || '编码未提供'].filter(Boolean).join(' · '),
+        subtitle: current.subtitle || assetSubtitle(current.kindLabel, current.displayCode),
         tone: current.tone || 'neutral',
         status: current.statusText || '',
         // 等级与状态各有自己的语义色:等级沿用卡片那张四级词表,不与状态色混用。
@@ -115,20 +120,29 @@ export function entityDetailView(detail) {
     }
 }
 
+/** 源表只有内部主键、没有业务编码列的资产类别(目前只有风险评估表)。 */
+const ASSET_WITHOUT_BUSINESS_CODE = new Set(['PROTECTION_TARGET'])
+
 function normalizeAsset(identity, source) {
     const emergency = identity.kind === ENTITY_DETAIL_KIND.EMERGENCY
     const category = text(source.category || identity.category).toUpperCase()
     const attributes = source.attributes && typeof source.attributes === 'object' ? source.attributes : {}
-    const displayCode = text(source.code)
+    // 编码字段只跟随源表的列:没有业务编码列就不生成该字段(防护目标的 riskId 是主键,不是编码)。
+    const hasCodeColumn = !ASSET_WITHOUT_BUSINESS_CODE.has(category)
+    const displayCode = hasCodeColumn ? text(source.code) : ''
     const name = text(source.name || source.facilityName || source.resourceName || source.title) || (emergency ? '应急对象名称未提供' : '设施名称未提供')
     // 状态是码值时翻成中文(设施:0 正常 / 1 异常;后端若直接给人话则原样用)。
     const statusRaw = source.status ?? source.state ?? source.statusName ?? source.stateName
     const status = (category === 'PROTECTION_TARGET' ? riskLevelText(statusRaw) : emergency ? undefinedEnumText(statusRaw) : facilityStatusText(statusRaw)) || '状态未发布'
     const location = text(source.address || source.location || source.position)
     const fields = assetFields(category, attributes)
+    const kindLabel = emergency ? '应急对象' : '设施'
     return {
         ...identity, category, name, title: name, displayCode,
-        kindLabel: emergency ? '应急对象' : '设施', statusText: status, tone: emergency ? 'neutral' : facilityStatusTone(source.status), raw: source,
+        kindLabel,
+        // 抬头只拼接真实存在的业务编码;没有编码列时不留「编码未提供」。
+        subtitle: assetSubtitle(kindLabel, displayCode, hasCodeColumn),
+        statusText: status, tone: emergency ? 'neutral' : facilityStatusTone(source.status), raw: source,
         categoryText: facilityCategoryText(category),
         coordinates: source.coordinates || null,
         summary: assetSummary(category, attributes, status, location),
@@ -137,7 +151,12 @@ function normalizeAsset(identity, source) {
         devices: assetDevices(attributes),
         attachments: assetAttachments(attributes),
         sections: compactSections([
-            section('基本信息', [field('设施编码', displayCode || '未提供'), field('设施类别', facilityCategoryText(category)), field('运行状态', status), field('数据更新时间', source.updatedAt)]),
+            section('基本信息', [
+                ...(hasCodeColumn ? [field('设施编码', displayCode || '未提供')] : []),
+                field('设施类别', facilityCategoryText(category)),
+                field('运行状态', status),
+                field('数据更新时间', formatEventTime(source.updatedAt)),
+            ]),
             section('位置与归属', [field('位置描述', location, true), field('所属片区', source.districtName), field('坐标', coordinatesText(source.coordinates), true)]),
             section('业务详情', fields),
         ]),
@@ -533,6 +552,17 @@ function riskLevelText(raw) {
     return Object.hasOwn(names, value) ? names[value] : undefinedEnumText(raw)
 }
 
+/** 详情里按完整时间戳展示的字段。 */
+const ASSET_TIMESTAMP_KEYS = new Set(['reportTime', 'assessmentTime', 'installTime'])
+/** 纯日期字段:只到年月日,后端若序列化成当天 00:00:00 也不补时刻。 */
+const ASSET_DATE_KEYS = new Set(['buildDate', 'commissionDate', 'rectifyDeadline'])
+
+function formatAssetDate(value) {
+    const raw = text(value)
+    return /^\d{4}-\d{2}-\d{2}/.test(raw) ? raw.slice(0, 10) : raw
+}
+
+/** 原始属性 → 展示值的唯一归一化层:摘要与明细共用;行内 format 只做单字段专门词表。 */
 function assetFieldValue(key, a) {
     if (key === 'equipmentIds' || key === 'equipmentId') {
         const codes = Array.isArray(a.devices) ? a.devices.map((item) => text(item.code)).filter(Boolean) : []
@@ -541,6 +571,8 @@ function assetFieldValue(key, a) {
     if (['networkId', 'workOrderId', 'equipmentTypeId'].includes(key)) return '未提供'
     if (key === 'riskLevel') return riskLevelText(a[key])
     if (key === 'riskColor') return undefinedEnumText(a[key])
+    if (ASSET_TIMESTAMP_KEYS.has(key)) return formatEventTime(a[key])
+    if (ASSET_DATE_KEYS.has(key)) return formatAssetDate(a[key])
     return a[key]
 }
 

+ 104 - 0
tests/entityDetailAssetFields.test.js

@@ -0,0 +1,104 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+
+import { normalizeEntityDetail, entityDetailView } from '../src/modules/cockpit/domain/entityDetail.js'
+
+const EMERGENCY_CATEGORIES = new Set(['HAZARD', 'PROTECTION_TARGET', 'EMERGENCY_RESOURCE'])
+
+function assetView(raw) {
+    const kind = EMERGENCY_CATEGORIES.has(raw.category) ? 'emergency' : 'facility'
+    return entityDetailView(normalizeEntityDetail({ kind, id: raw.id, category: raw.category }, raw))
+}
+
+function hazardView(attributes = {}) {
+    return assetView({
+        category: 'HAZARD',
+        id: 'h1',
+        code: 'YH-20261113175702',
+        name: '322322',
+        attributes: { reportTime: '2026-11-13T17:57:10', rectifyDeadline: '2026-11-13T00:00:00', ...attributes },
+    })
+}
+
+function resourceView(attributes = {}) {
+    return assetView({
+        category: 'EMERGENCY_RESOURCE',
+        id: 'e1',
+        code: '8652052335',
+        name: '漏失监测',
+        attributes: { installTime: '2026-10-26T00:00:00', ...attributes },
+    })
+}
+
+function businessFields(view) {
+    return view.sections.find((group) => group.title === '业务详情').fields
+}
+
+test('防护目标没有业务编码列:不生成编码字段,抬头不拼接「编码未提供」', () => {
+    const view = assetView({
+        category: 'PROTECTION_TARGET',
+        id: 'risk001',
+        code: 'risk001',
+        name: '郑东新区CBD商业中心',
+        status: '1',
+        attributes: { riskLevel: '1', riskScore: 72.5, riskColor: '1' },
+    })
+    assert.equal(view.subtitle, '应急对象')
+    const basic = view.sections.find((group) => group.title === '基本信息')
+    assert.equal(basic.fields.some((item) => item.label === '设施编码'), false)
+})
+
+test('时间戳统一为 yyyy-MM-dd HH:mm:ss:评估时间、数据更新时间(摘要与明细同源)', () => {
+    const view = assetView({
+        category: 'PROTECTION_TARGET',
+        id: 'risk001',
+        code: 'risk001',
+        name: '郑东新区CBD商业中心',
+        status: '1',
+        updatedAt: '2025-01-15T10:00:00+08:00',
+        attributes: { riskLevel: '1', riskScore: 72.5, assessmentTime: '2025-01-15T10:00:00' },
+    })
+    const basic = view.sections.find((group) => group.title === '基本信息')
+    assert.equal(basic.fields.find((item) => item.label === '数据更新时间').value, '2025-01-15 10:00:00')
+    assert.equal(businessFields(view).find((item) => item.label === '评估时间').value, '2025-01-15 10:00:00')
+    assert.equal(view.summary.find((item) => item.label === '评估时间').value, '2025-01-15 10:00:00')
+})
+
+test('纯日期字段只显示 yyyy-MM-dd,不补 00:00:00', () => {
+    assert.equal(businessFields(hazardView()).find((item) => item.label === '整改期限').value, '2026-11-13')
+
+    const pipe = assetView({
+        category: 'PIPE_NETWORK',
+        id: 'p1',
+        code: 'GD-1',
+        name: '测试管线',
+        attributes: { buildDate: '2020-05-01T00:00:00' },
+    })
+    assert.equal(businessFields(pipe).find((item) => item.label === '建设日期').value, '2020-05-01')
+})
+
+test('关联项按源表列存在:有列缺值显示「未提供」,无列不生成该行', () => {
+    // 隐患台账有 network_id / work_order_id 列:缺值仍保留行
+    const hazardLabels = businessFields(hazardView()).map((item) => item.label)
+    assert.equal(hazardLabels.includes('关联管网'), true)
+    assert.equal(hazardLabels.includes('关联工单'), true)
+    assert.equal(businessFields(hazardView()).find((item) => item.label === '关联管网').value, '未提供')
+
+    // 设备台账没有这三列:整行不生成
+    const resourceLabels = businessFields(resourceView()).map((item) => item.label)
+    assert.equal(resourceLabels.some((label) => ['关联管网', '关联设备', '关联工单'].includes(label)), false)
+
+    // 风险评估表有 network_id 列:缺值保留
+    const target = assetView({
+        category: 'PROTECTION_TARGET',
+        id: 'risk001',
+        name: '防护目标',
+        attributes: { riskLevel: '1' },
+    })
+    assert.equal(businessFields(target).find((item) => item.label === '关联管网').value, '未提供')
+})
+
+test('危险源报告时间与应急资源安装时间按完整时间戳展示', () => {
+    assert.equal(businessFields(hazardView()).find((item) => item.label === '报告时间').value, '2026-11-13 17:57:10')
+    assert.equal(businessFields(resourceView()).find((item) => item.label === '安装时间').value, '2026-10-26 00:00:00')
+})