detailApi.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * 共享详情领域接口(票 05;消费方是票 10 两级详情、票 11 设备详情、票 12 时间线、票 13/14 事件)。
  3. *
  4. * ADR 0005 的 `detailApi`。本文件里全是「取一个分区」的函数,**每个分区各自成败**——
  5. * 票 11 验收第 3 条要求「任一分区失败时不遮盖其他成功分区」,
  6. * 所以调用方拿到的是若干并列的 `section()`,而不是一个共用成败的详情对象。
  7. */
  8. import { toMonitoringPointDetail } from '../domain/pointDetail.js'
  9. import { toEquipmentArchive, toEquipmentDetailAggregate, toDeviceFreshnessSection, toMetricsPayload, toCollectionRecordsPayload } from '../domain/equipmentDetail.js'
  10. import { toWorkOrder } from '../domain/workOrder.js'
  11. import { isEmptyBy, fetchSection } from './domainRequest.js'
  12. import { deviceMetricKind, deviceMetricPath, toTypedMetricsPayload } from '../domain/deviceMetricSources.js'
  13. import { SECTION_STATE } from '../domain/sectionState.js'
  14. import { districtQuery, industryQuery } from '../domain/filterOptions.js'
  15. function missingCode(kind, code) {
  16. return Promise.resolve({
  17. state: SECTION_STATE.FAILED,
  18. reason: `缺少${kind}编码`,
  19. mode: '',
  20. data: null,
  21. missing: [],
  22. freshness: '',
  23. retained: false,
  24. })
  25. }
  26. /**
  27. * 监测点详情(票 10 的第一级 + 设备卡片)。
  28. * @param {string} pointCode 监测点编码
  29. */
  30. export function fetchMonitoringPointDetail(pointCode, options = {}) {
  31. if (!pointCode) return missingCode('监测点', pointCode)
  32. return fetchSection(`/monitoring-points/${encodeURIComponent(pointCode)}`, {
  33. toData: toMonitoringPointDetail,
  34. // EMPTY 是点位自身的“无设备”业务状态,不是“无此点位”。
  35. // 保留合法点位的身份、坐标原因和真实零设备统计;不能按空分区丢弃载荷。
  36. config: options.config,
  37. fetchImpl: options.fetchImpl,
  38. })
  39. }
  40. /** 设备档案(票 11 的「档案」分区)。 */
  41. export function fetchEquipmentArchive(deviceCode, options = {}) {
  42. if (!deviceCode) return missingCode('设备', deviceCode)
  43. return fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/archive`, {
  44. toData: toEquipmentArchive,
  45. config: options.config,
  46. fetchImpl: options.fetchImpl,
  47. })
  48. }
  49. /** 当前指标(票 11 的「最新指标」分区)。 */
  50. export function fetchEquipmentMetrics(deviceCode, options = {}) {
  51. if (!deviceCode) return missingCode('设备', deviceCode)
  52. return fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/metrics/current`, {
  53. toData: toMetricsPayload,
  54. isEmpty: isEmptyBy((payload) => Array.isArray(payload && payload.metrics) && payload.metrics.length === 0),
  55. freshnessOf: (payload) => (payload && payload.state) || '',
  56. config: options.config,
  57. fetchImpl: options.fetchImpl,
  58. })
  59. }
  60. /**
  61. * 设备详情聚合(票 11 的主路径):一次拿到档案/指标/预警/报警/工单/时间线/图片,
  62. * 每个分区独立成败。
  63. */
  64. export async function fetchEquipmentDetailAggregate(deviceCode, options = {}) {
  65. if (!deviceCode) return missingCode('设备', deviceCode)
  66. let [aggregate, collectionRecords] = await Promise.all([
  67. fetchLegacyEquipmentDetailAggregate(deviceCode, options),
  68. fetchEquipmentCollectionRecords(deviceCode, options),
  69. ])
  70. if (!aggregate.data) {
  71. if (![SECTION_STATE.SUCCESS, SECTION_STATE.EMPTY].includes(collectionRecords.state)) return aggregate
  72. aggregate = {
  73. ...aggregate,
  74. state: SECTION_STATE.SUCCESS,
  75. reason: '',
  76. data: toEquipmentDetailAggregate({ deviceCode, aggregateState: 'PARTIAL' }),
  77. }
  78. }
  79. const sections = aggregate.data.sections
  80. return {
  81. ...aggregate,
  82. data: { ...aggregate.data, sections: { ...sections, metrics: { ...sections.metrics, collectionRecords } } },
  83. }
  84. }
  85. /** 独立采集记录分区;接口未发布如实失败,不fallback旧metrics。 */
  86. export async function fetchEquipmentCollectionRecords(deviceCode, options = {}) {
  87. if (!deviceCode) return missingCode('设备', deviceCode)
  88. const result = await fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/collection-records`, {
  89. toData: toCollectionRecordsPayload,
  90. config: options.config,
  91. fetchImpl: options.fetchImpl,
  92. })
  93. if (result.state !== SECTION_STATE.SUCCESS) return result
  94. const payload = result.data
  95. if (payload.state === 'FAILED') return { ...result, state: SECTION_STATE.FAILED, reason: payload.stateReason, data: null }
  96. if (payload.state === 'UNAVAILABLE') return { ...result, state: SECTION_STATE.UNAVAILABLE, reason: payload.stateReason, data: null }
  97. if (payload.state === 'EMPTY') return { ...result, state: SECTION_STATE.EMPTY }
  98. if (!['READY', 'SUCCESS'].includes(payload.state) || !Array.isArray(payload.recordView?.columns) || !Array.isArray(payload.recordView?.rows)) {
  99. return { ...result, state: SECTION_STATE.FAILED, reason: '采集记录载荷不完整', data: null }
  100. }
  101. if (!payload.recordView.rows.length) return { ...result, state: SECTION_STATE.EMPTY }
  102. return result
  103. }
  104. async function fetchLegacyEquipmentDetailAggregate(deviceCode, options = {}) {
  105. if (!deviceCode) return missingCode('设备', deviceCode)
  106. const aggregate = await fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/detail-aggregate`, {
  107. toData: toEquipmentDetailAggregate,
  108. config: options.config,
  109. fetchImpl: options.fetchImpl,
  110. })
  111. const metrics = aggregate.data?.sections?.metrics
  112. const archive = aggregate.data?.sections?.archive?.data
  113. const kind = deviceMetricKind(archive)
  114. const path = deviceMetricPath(kind, deviceCode)
  115. if (!path || !metrics || metrics.state !== SECTION_STATE.EMPTY) return aggregate
  116. const typed = await fetchSection(path, {
  117. toData: (payload) => toMetricsPayload(toTypedMetricsPayload(payload, deviceCode, kind)),
  118. isEmpty: isEmptyBy((payload) => toTypedMetricsPayload(payload, deviceCode, kind).metrics.length === 0),
  119. config: options.config,
  120. fetchImpl: options.fetchImpl,
  121. })
  122. if (!aggregate.data || typed.state === SECTION_STATE.EMPTY) return aggregate
  123. return { ...aggregate, data: { ...aggregate.data, sections: { ...aggregate.data.sections, metrics: typed } } }
  124. }
  125. /** 设备新鲜度(票 16)。 */
  126. export function fetchEquipmentFreshness(deviceCode, options = {}) {
  127. if (!deviceCode) return missingCode('设备', deviceCode)
  128. return fetchSection(`/equipments/${encodeURIComponent(deviceCode)}/freshness`, {
  129. toData: (payload) => payload,
  130. config: options.config,
  131. fetchImpl: options.fetchImpl,
  132. }).then((result) => {
  133. // 失败/不可用时保持原状;拿到数据再按新鲜度换算成分区。
  134. if (result.state !== SECTION_STATE.SUCCESS) return result
  135. return toDeviceFreshnessSection(result.data)
  136. })
  137. }
  138. /**
  139. * 全量工单分页(票 65:报警效率面板的「当前工单列表」)。
  140. *
  141. * 本函数回答「本窗口有哪些工单、共多少张」(效率面板用,要翻页 + 要时间筛)。
  142. *
  143. * `startTime`/`endTime` 是 2026-10-22 补的:面板按近 7/30/90 天切换时列表必须跟着收窄
  144. * (后端按 `create_time` 过滤,与上方「工单状态分布」同一批工单)。
  145. * @param {{page?:number, pageSize?:number, stateCode?:number|null, startTime?:string, endTime?:string}} [query]
  146. */
  147. export function fetchWorkOrderList(query = {}, options = {}) {
  148. const params = new URLSearchParams()
  149. if (Number.isInteger(query.page) && query.page > 0) params.set('page', String(query.page))
  150. if (Number.isInteger(query.pageSize) && query.pageSize > 0) params.set('pageSize', String(query.pageSize))
  151. if (Number.isInteger(query.stateCode)) params.set('stateCode', String(query.stateCode))
  152. // 时间窗原样透传(ISO-8601 带偏移):掐头去尾的格式化会让「同一窗口」与其它面板不一致。
  153. if (typeof query.startTime === 'string' && query.startTime) params.set('startTime', query.startTime)
  154. if (typeof query.endTime === 'string' && query.endTime) params.set('endTime', query.endTime)
  155. // 票 79:/work-orders 自票 77 起真收 industry(部署后实测非法值 400、GAS/WATER 响应不同);
  156. // 口径与上方「工单状态分布」同一套(工单设备 → 设备类型树 → 行业)。
  157. const industry = industryQuery(options.industry)
  158. if (industry) params.set('industry', industry)
  159. // 票 89:后端已把片区入参补到本端点(响应另带 `filters` 回显,票 81)。0 是合法编码
  160. // (城南),**不能用真值判断**跳过 —— 只挡掉空串(全域=不传)。
  161. const district = districtQuery(options.scope)
  162. if (district !== '') params.set('district', String(district))
  163. const queryString = params.toString()
  164. return fetchSection(`/work-orders${queryString ? `?${queryString}` : ''}`, {
  165. toData: (payload) => {
  166. const paging = (payload && payload.pagination) || null
  167. return {
  168. state: (payload && payload.state) || '',
  169. stateReason: (payload && payload.stateReason) || '',
  170. dictionary: (payload && payload.stateDictionary) || [],
  171. orders: Array.isArray(payload && payload.workOrders) ? payload.workOrders.map(toWorkOrder) : [],
  172. // 分页回显:页码/页大小/总数/总页数都由后端给,前端不自行推算(票 61 纪律)。
  173. pagination: paging
  174. ? {
  175. page: Number(paging.page) || 1,
  176. pageSize: Number(paging.pageSize) || 0,
  177. total: Number(paging.total) || 0,
  178. totalPages: Number(paging.totalPages) || 1,
  179. }
  180. : null,
  181. }
  182. },
  183. isEmpty: isEmptyBy((payload) => Array.isArray(payload && payload.workOrders) && payload.workOrders.length === 0),
  184. config: options.config,
  185. fetchImpl: options.fetchImpl,
  186. })
  187. }