/** * 共享详情领域接口(票 05;消费方是票 10 两级详情、票 11 设备详情、票 12 时间线、票 13/14 事件)。 * * ADR 0005 的 `detailApi`。本文件里全是「取一个分区」的函数,**每个分区各自成败**—— * 票 11 验收第 3 条要求「任一分区失败时不遮盖其他成功分区」, * 所以调用方拿到的是若干并列的 `section()`,而不是一个共用成败的详情对象。 */ import { toMonitoringPointDetail } from '../domain/pointDetail.js' import { toEquipmentArchive, toEquipmentDetailAggregate, toDeviceFreshnessSection, toMetricsPayload, toCollectionRecordsPayload } from '../domain/equipmentDetail.js' import { toWorkOrder } from '../domain/workOrder.js' import { isEmptyBy, fetchSection } from './domainRequest.js' import { deviceMetricKind, deviceMetricPath, toTypedMetricsPayload } from '../domain/deviceMetricSources.js' import { SECTION_STATE } from '../domain/sectionState.js' import { districtQuery, industryQuery } from '../domain/filterOptions.js' function missingCode(kind, code) { return Promise.resolve({ state: SECTION_STATE.FAILED, reason: `缺少${kind}编码`, mode: '', data: null, missing: [], freshness: '', retained: false, }) } /** * 监测点详情(票 10 的第一级 + 设备卡片)。 * @param {string} pointCode 监测点编码 */ export function fetchMonitoringPointDetail(pointCode, options = {}) { if (!pointCode) return missingCode('监测点', pointCode) return fetchSection(`/monitoring-points/${encodeURIComponent(pointCode)}`, { toData: toMonitoringPointDetail, // EMPTY 是点位自身的“无设备”业务状态,不是“无此点位”。 // 保留合法点位的身份、坐标原因和真实零设备统计;不能按空分区丢弃载荷。 config: options.config, fetchImpl: options.fetchImpl, }) } /** 设备档案(票 11 的「档案」分区)。 */ export function fetchEquipmentArchive(deviceCode, options = {}) { if (!deviceCode) return missingCode('设备', deviceCode) return fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/archive`, { toData: toEquipmentArchive, config: options.config, fetchImpl: options.fetchImpl, }) } /** 当前指标(票 11 的「最新指标」分区)。 */ export function fetchEquipmentMetrics(deviceCode, options = {}) { if (!deviceCode) return missingCode('设备', deviceCode) return fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/metrics/current`, { toData: toMetricsPayload, isEmpty: isEmptyBy((payload) => Array.isArray(payload && payload.metrics) && payload.metrics.length === 0), freshnessOf: (payload) => (payload && payload.state) || '', config: options.config, fetchImpl: options.fetchImpl, }) } /** * 设备详情聚合(票 11 的主路径):一次拿到档案/指标/预警/报警/工单/时间线/图片, * 每个分区独立成败。 */ export async function fetchEquipmentDetailAggregate(deviceCode, options = {}) { if (!deviceCode) return missingCode('设备', deviceCode) let [aggregate, collectionRecords] = await Promise.all([ fetchLegacyEquipmentDetailAggregate(deviceCode, options), fetchEquipmentCollectionRecords(deviceCode, options), ]) if (!aggregate.data) { if (![SECTION_STATE.SUCCESS, SECTION_STATE.EMPTY].includes(collectionRecords.state)) return aggregate aggregate = { ...aggregate, state: SECTION_STATE.SUCCESS, reason: '', data: toEquipmentDetailAggregate({ deviceCode, aggregateState: 'PARTIAL' }), } } const sections = aggregate.data.sections return { ...aggregate, data: { ...aggregate.data, sections: { ...sections, metrics: { ...sections.metrics, collectionRecords } } }, } } /** 独立采集记录分区;接口未发布如实失败,不fallback旧metrics。 */ export async function fetchEquipmentCollectionRecords(deviceCode, options = {}) { if (!deviceCode) return missingCode('设备', deviceCode) const result = await fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/collection-records`, { toData: toCollectionRecordsPayload, config: options.config, fetchImpl: options.fetchImpl, }) if (result.state !== SECTION_STATE.SUCCESS) return result const payload = result.data if (payload.state === 'FAILED') return { ...result, state: SECTION_STATE.FAILED, reason: payload.stateReason, data: null } if (payload.state === 'UNAVAILABLE') return { ...result, state: SECTION_STATE.UNAVAILABLE, reason: payload.stateReason, data: null } if (payload.state === 'EMPTY') return { ...result, state: SECTION_STATE.EMPTY } if (!['READY', 'SUCCESS'].includes(payload.state) || !Array.isArray(payload.recordView?.columns) || !Array.isArray(payload.recordView?.rows)) { return { ...result, state: SECTION_STATE.FAILED, reason: '采集记录载荷不完整', data: null } } if (!payload.recordView.rows.length) return { ...result, state: SECTION_STATE.EMPTY } return result } async function fetchLegacyEquipmentDetailAggregate(deviceCode, options = {}) { if (!deviceCode) return missingCode('设备', deviceCode) const aggregate = await fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/detail-aggregate`, { toData: toEquipmentDetailAggregate, config: options.config, fetchImpl: options.fetchImpl, }) const metrics = aggregate.data?.sections?.metrics const archive = aggregate.data?.sections?.archive?.data const kind = deviceMetricKind(archive) const path = deviceMetricPath(kind, deviceCode) if (!path || !metrics || metrics.state !== SECTION_STATE.EMPTY) return aggregate const typed = await fetchSection(path, { toData: (payload) => toMetricsPayload(toTypedMetricsPayload(payload, deviceCode, kind)), isEmpty: isEmptyBy((payload) => toTypedMetricsPayload(payload, deviceCode, kind).metrics.length === 0), config: options.config, fetchImpl: options.fetchImpl, }) if (!aggregate.data || typed.state === SECTION_STATE.EMPTY) return aggregate return { ...aggregate, data: { ...aggregate.data, sections: { ...aggregate.data.sections, metrics: typed } } } } /** 设备新鲜度(票 16)。 */ export function fetchEquipmentFreshness(deviceCode, options = {}) { if (!deviceCode) return missingCode('设备', deviceCode) return fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/freshness`, { toData: (payload) => payload, config: options.config, fetchImpl: options.fetchImpl, }).then((result) => { // 失败/不可用时保持原状;拿到数据再按新鲜度换算成分区。 if (result.state !== SECTION_STATE.SUCCESS) return result return toDeviceFreshnessSection(result.data) }) } /** * 全量工单分页(票 65:报警效率面板的「当前工单列表」)。 * * 本函数回答「本窗口有哪些工单、共多少张」(效率面板用,要翻页 + 要时间筛)。 * * `startTime`/`endTime` 是 2026-10-22 补的:面板按近 7/30/90 天切换时列表必须跟着收窄 * (后端按 `create_time` 过滤,与上方「工单状态分布」同一批工单)。 * @param {{page?:number, pageSize?:number, stateCode?:number|null, startTime?:string, endTime?:string}} [query] */ export function fetchWorkOrderList(query = {}, options = {}) { const params = new URLSearchParams() if (Number.isInteger(query.page) && query.page > 0) params.set('page', String(query.page)) if (Number.isInteger(query.pageSize) && query.pageSize > 0) params.set('pageSize', String(query.pageSize)) if (Number.isInteger(query.stateCode)) params.set('stateCode', String(query.stateCode)) // 时间窗原样透传(ISO-8601 带偏移):掐头去尾的格式化会让「同一窗口」与其它面板不一致。 if (typeof query.startTime === 'string' && query.startTime) params.set('startTime', query.startTime) if (typeof query.endTime === 'string' && query.endTime) params.set('endTime', query.endTime) // 票 79:/work-orders 自票 77 起真收 industry(部署后实测非法值 400、GAS/WATER 响应不同); // 口径与上方「工单状态分布」同一套(工单设备 → 设备类型树 → 行业)。 const industry = industryQuery(options.industry) if (industry) params.set('industry', industry) // 票 89:后端已把片区入参补到本端点(响应另带 `filters` 回显,票 81)。0 是合法编码 // (城南),**不能用真值判断**跳过 —— 只挡掉空串(全域=不传)。 const district = districtQuery(options.scope) if (district !== '') params.set('district', String(district)) const queryString = params.toString() return fetchSection(`/work-orders${queryString ? `?${queryString}` : ''}`, { toData: (payload) => { const paging = (payload && payload.pagination) || null return { state: (payload && payload.state) || '', stateReason: (payload && payload.stateReason) || '', dictionary: (payload && payload.stateDictionary) || [], orders: Array.isArray(payload && payload.workOrders) ? payload.workOrders.map(toWorkOrder) : [], // 分页回显:页码/页大小/总数/总页数都由后端给,前端不自行推算(票 61 纪律)。 pagination: paging ? { page: Number(paging.page) || 1, pageSize: Number(paging.pageSize) || 0, total: Number(paging.total) || 0, totalPages: Number(paging.totalPages) || 1, } : null, } }, isEmpty: isEmptyBy((payload) => Array.isArray(payload && payload.workOrders) && payload.workOrders.length === 0), config: options.config, fetchImpl: options.fetchImpl, }) }