deviceFacilityRelations.js 3.1 KB

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