Pārlūkot izejas kodu

fix(cockpit): 管网聚焦按业务编码匹配,补防护目标真实契约回归

- 真实对接发现的缺陷:几何图层里管段的业务编码在 pipe.code(如 TCRQ202609080001),
  而 sourceId/networkId 是 raw_* 内部主键;按管网编码聚焦时只比内部主键,
  会一律落成「尚未取到该管段的可定位几何」。改为业务编码优先,内部主键仍兼容。
- 线上实测 20/20 条防护目标的 network.code 在几何图层唯一命中一段,聚焦链路可用。
- 新增 tests/protectionTargetRealContract.test.js:用线上响应原文钉住真实形状
  (顶层 code 为 null、network 在 attributes、riskLevel/riskColor 是字符串码、riskFactors 是 JSON 字符串)。
- mock 的 hazard_level 标签对齐线上(一级隐患…四级隐患)。
ChenJZ 1 dienu atpakaļ
vecāks
revīzija
7d1db8d2cd

+ 6 - 1
src/modules/cockpit/domain/facilitySelection.js

@@ -61,7 +61,12 @@ export function resolveFacilityPoint({ identity: input, points, detail, network
     let reasonText = ''
     let locationHint = ''
     if (identity.category === 'PIPE_NETWORK') {
-        const matches = (network?.pipes || []).filter(pipe => String(pipe.sourceId ?? '') === identity.id || String(pipe.networkId ?? '') === identity.id)
+        // 业务编码优先:管网聚焦传进来的是 network.code,几何图层的 pipe.code 与它同源
+        // (真实数据:pipe.code = TCRQ202609080001,而 sourceId/networkId 是 raw_P11920 这类内部主键)。
+        // 只比内部主键时,按编码聚焦会一律落成「尚未取到几何」。
+        const matches = (network?.pipes || []).filter(pipe => String(pipe.code ?? '') === identity.id
+            || String(pipe.sourceId ?? '') === identity.id
+            || String(pipe.networkId ?? '') === identity.id)
         const byCode = matches.length ? matches : (network?.pipes || []).filter(pipe => raw.code && pipe.code === raw.code)
         const pipe = byCode.length === 1 ? byCode[0] : null
         if (pipe) {

+ 1 - 1
src/modules/cockpit/mocks/contractMocks.js

@@ -2972,7 +2972,7 @@ const PAYLOADS = {
         ['risk_level', [['0', '重大风险'], ['1', '较大风险'], ['2', '一般风险'], ['3', '低风险']]],
         ['risk_color', [['0', '红'], ['1', '橙'], ['2', '黄'], ['3', '蓝']]],
         ['hazard_type', [['0', '腐蚀'], ['1', '第三方破坏'], ['2', '占压'], ['3', '泄漏']]],
-        ['hazard_level', [['1', '一级'], ['2', '二级'], ['3', '三级'], ['4', '四级隐患']]],
+        ['hazard_level', [['1', '一级隐患'], ['2', '二级隐患'], ['3', '三级隐患'], ['4', '四级隐患']]],
         ['hazard_source', [['0', '巡检'], ['1', '政府检查'], ['2', '企业上报'], ['3', '模型辨识']]],
         ['hazard_status', [['0', '整改中'], ['1', '已整改'], ['2', '待核查'], ['3', '已闭环']]],
     ].map(([type, rows]) => [`/dict/${type}`, (s) => (s === 'empty' ? [] : rows.map(([dictValue, dictLabel]) => ({ dictType: type, dictValue, dictLabel })))])),

+ 55 - 0
tests/facilityNetworkLocate.test.js

@@ -0,0 +1,55 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+
+import { resolveFacilityPoint } from '../src/modules/cockpit/domain/facilitySelection.js'
+
+// 真实几何形状(2026-12-01 线上 /infrastructure/geometry 的 properties):
+// 业务编码在 `code`,sourceId/networkId 是内部主键(raw_* 形)。
+const PIPE = {
+    code: 'TCRQ202609080001',
+    sourceId: 'raw_P11920',
+    networkId: 'raw_P11920',
+    name: '太常片区燃气',
+    industry: 'GAS',
+    positions: [[110.3621848838, 28.4517546414], [110.3621946507, 28.4517581805]],
+}
+
+const networkOf = (pipes) => ({ pipes, components: [], blocked: [] })
+
+test('按管网业务编码聚焦:命中 pipe.code,落成可定位的一段', () => {
+    const point = resolveFacilityPoint({
+        identity: { kind: 'facility', id: 'TCRQ202609080001', category: 'PIPE_NETWORK' },
+        network: networkOf([PIPE]),
+    })
+    assert.equal(point.placement, 'drawn')
+    assert.equal(point.code, 'TCRQ202609080001')
+    assert.equal(point.name, '太常片区燃气')
+    assert.ok(Number.isFinite(point.longitude) && Number.isFinite(point.latitude))
+    assert.match(point.locationHint, /中点/)
+})
+
+test('按内部主键定位的既有调用方不受影响', () => {
+    const point = resolveFacilityPoint({
+        identity: { kind: 'facility', id: 'raw_P11920', category: 'PIPE_NETWORK' },
+        network: networkOf([PIPE]),
+    })
+    assert.equal(point.placement, 'drawn')
+})
+
+test('图层里没有这段管网时如实说没取到几何,不编坐标', () => {
+    const point = resolveFacilityPoint({
+        identity: { kind: 'facility', id: 'TCRQ209912319999', category: 'PIPE_NETWORK' },
+        network: networkOf([PIPE]),
+    })
+    assert.equal(point.placement, 'pending')
+    assert.equal(point.longitude, null)
+    assert.match(point.reasonText, /几何|管段/)
+})
+
+test('同一编码命中多段时不乱挑一段', () => {
+    const point = resolveFacilityPoint({
+        identity: { kind: 'facility', id: 'TCRQ202609080001', category: 'PIPE_NETWORK' },
+        network: networkOf([PIPE, { ...PIPE, sourceId: 'raw_P11921' }]),
+    })
+    assert.equal(point.placement, 'pending')
+})

+ 106 - 0
tests/protectionTargetRealContract.test.js

@@ -0,0 +1,106 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+
+import { normalizeEntityDetail, entityDetailView } from '../src/modules/cockpit/domain/entityDetail.js'
+import { DICT_TYPES, resetDictLabelsForTest, setDictLabels } from '../src/modules/cockpit/domain/dictLabels.js'
+
+/**
+ * 真实契约回归(2026-12-01 线上 8301 实测)。
+ *
+ * 这份 fixture 是从线上响应原样抄下来的,用来钉住「后端实际长什么样」:
+ *   · 防护目标顶层 code 是 null(riskId 不再当编码);
+ *   · network 在 attributes 里,给 { code, name };
+ *   · riskLevel / riskColor 是**字符串**码值;status 也是字符串;
+ *   · riskFactors 是 **JSON 字符串**,不是对象。
+ * 合成数据再过一遍看不出这些形状差异,所以单独留一份。
+ */
+
+const RISK_LEVEL = [
+    { dictValue: '0', dictLabel: '重大风险' }, { dictValue: '1', dictLabel: '较大风险' },
+    { dictValue: '2', dictLabel: '一般风险' }, { dictValue: '3', dictLabel: '低风险' },
+]
+const RISK_COLOR = [
+    { dictValue: '0', dictLabel: '红' }, { dictValue: '1', dictLabel: '橙' },
+    { dictValue: '2', dictLabel: '黄' }, { dictValue: '3', dictLabel: '蓝' },
+]
+
+const REAL_PAYLOAD = {
+    requestId: '176382a308a74519a53d195d1f265786',
+    apiVersion: 'v1',
+    readOnly: true,
+    category: 'PROTECTION_TARGET',
+    id: 'risk001',
+    code: null,
+    name: '郑东新区CBD商业中心、河南省政府、郑州国际会展中心',
+    status: '1',
+    location: null,
+    district: null,
+    districtName: null,
+    coordinates: null,
+    attributes: {
+        network: { code: 'TCRQ202609080001', name: '太常片区燃气' },
+        riskLevel: '1',
+        riskScore: 72.5,
+        riskColor: '1',
+        assessmentModel: 'Kent Muhlbauer风险评估模型V2.1',
+        assessmentTime: '2025-01-15T10:00:00',
+        riskFactors: '{"管材":"钢管","压力":"高压1.6MPa","管龄":"14年","防腐状况":"局部腐蚀","土壤腐蚀性":"中等"}',
+        hazardSources: '周边地铁1号线、地下综合管廊施工',
+        remark: '高后果区(HCAs)管线,需加强巡检',
+    },
+    updatedAt: '2025-01-15T10:00:00+08:00',
+}
+
+function realView(overrides = {}) {
+    resetDictLabelsForTest()
+    setDictLabels(DICT_TYPES.riskLevel, RISK_LEVEL)
+    setDictLabels(DICT_TYPES.riskColor, RISK_COLOR)
+    const payload = { ...REAL_PAYLOAD, ...overrides }
+    return entityDetailView(normalizeEntityDetail(
+        { kind: 'emergency', id: 'risk001', category: 'PROTECTION_TARGET' },
+        payload,
+    ))
+}
+
+const businessRows = (view) => view.sections.find((group) => group.title === '业务详情').fields
+
+test('真实响应:风险等级与颜色按字典翻成中文,抬头写「风险等级」', () => {
+    const view = realView()
+    assert.equal(view.status, '较大风险')
+    assert.equal(view.statusCaption, '风险等级')
+    assert.equal(view.tone, 'risk-2')
+    assert.ok(!JSON.stringify(view).includes('未定义'))
+})
+
+test('真实响应:code 为 null 时不生成编码字段,抬头只有「应急对象」', () => {
+    const view = realView()
+    assert.equal(view.subtitle, '应急对象')
+    const basic = view.sections.find((group) => group.title === '基本信息').fields
+    assert.equal(basic.some((row) => row.label.includes('编码')), false)
+})
+
+test('真实响应:关联管网按 { code, name } 显示并给聚焦入口', () => {
+    const view = realView()
+    const row = businessRows(view).find((item) => item.label === '关联管网')
+    assert.equal(row.value, '太常片区燃气(TCRQ202609080001)')
+    assert.deepEqual(row.action, { kind: 'focus-network', code: 'TCRQ202609080001' })
+})
+
+test('真实响应:riskFactors 是 JSON 字符串,排成键值网格', () => {
+    const row = businessRows(realView()).find((item) => item.label === '风险因素')
+    assert.equal(row.pairs.length, 5)
+    assert.deepEqual(row.pairs[0], ['管材', '钢管'])
+})
+
+test('真实响应:风险等级与风险颜色不出现在业务明细里', () => {
+    const labels = businessRows(realView()).map((item) => item.label)
+    assert.equal(labels.includes('风险等级'), false)
+    assert.equal(labels.includes('风险颜色'), false)
+})
+
+test('真实响应缺 network 时显示「未提供」,不出现空括号或 id', () => {
+    const view = realView({ attributes: { ...REAL_PAYLOAD.attributes, network: undefined } })
+    const row = businessRows(view).find((item) => item.label === '关联管网')
+    assert.equal(row.value, '未提供')
+    assert.equal(row.action, undefined)
+})