| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /**
- * 设备→设施真实关联读模型(维护者 2026-10-22 批准的后端契约落地)。
- *
- * 端点:`GET /api/cockpit/v1/equipments/facility-relations`(免认证只读)。
- * 响应:`{ generatedAt, readOnly, relations: [{ deviceId, deviceCode, facilityCode, facilityKind, relationType }], statistics }`
- *
- * **为什么仍要降级**(2026-10-22 实测,勿把「契约已就绪」当成「真实关联已生效」):
- * · `manhole_device_rel` 6 条未删行里只有 2 条 `manhole_id` 是窨井主键,另 4 条存的是设备 id;
- * · 保留下来的 2 条关系里 `equipment_id` 在 `equipment_base` 查不到 → `deviceCode` 为 `null`。
- *
- * 2026-11-16 维护者裁定:`gas_pipe_point` 在实际页面没有使用场景、其用途由管网表
- * `pipe_network_base` 覆盖,后端已删除该来源(这条链路此前也一直是悬空的全跳过)。
- * 所以现在真实可匹配的关系数仍是 **0**,前端必须继续使用「同业邻近示意」并在界面如实标注。
- *
- * 本模块只做「响应 → Map<deviceCode, relation>」这一步,不做任何按名字/编码的猜测匹配。
- */
- import { fetchSection } from './domainRequest.js'
- import { SECTION_STATE } from '../domain/sectionState.js'
- export const DEVICE_FACILITY_RELATION_PATH = '/equipments/facility-relations'
- function toRelationData(payload) {
- const rows = Array.isArray(payload?.relations) ? payload.relations : []
- const statistics = payload?.statistics || {}
- const relations = []
- for (const row of rows) {
- const deviceCode = typeof row?.deviceCode === 'string' ? row.deviceCode.trim() : ''
- const facilityCode = typeof row?.facilityCode === 'string' ? row.facilityCode.trim() : ''
- // 只接受能**按设备编码**回联的行;只有 deviceId 的行无法与快照设备匹配,不猜。
- if (!deviceCode || !facilityCode) continue
- relations.push({
- deviceCode,
- facilityCode,
- facilityKind: typeof row.facilityKind === 'string' ? row.facilityKind : '',
- relationType: typeof row.relationType === 'string' ? row.relationType : '',
- })
- }
- return { relations, statistics, generatedAt: payload?.generatedAt || '' }
- }
- /**
- * 读取设备→设施真实关联。
- * @param {object} [options] `{ config, fetchImpl }`
- * @returns 分区(`section()`):成功时 `data = { relations, statistics, generatedAt }`
- */
- export function fetchDeviceFacilityRelations(options = {}) {
- return fetchSection(DEVICE_FACILITY_RELATION_PATH, {
- toData: toRelationData,
- // 「没有关联」是合法结果(空数组),不是取数失败。
- config: options.config,
- fetchImpl: options.fetchImpl,
- })
- }
- /** 分区 → `Map<deviceCode, relation>`;非成功态回空 Map(调用方按降级处理)。 */
- export function relationIndex(section) {
- const map = new Map()
- if (!section || section.state !== SECTION_STATE.SUCCESS) return map
- for (const relation of section.data?.relations || []) map.set(relation.deviceCode, relation)
- return map
- }
|