operatingMetricCaliber.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * 运行信息「三项能力指标」的**唯一口径来源**(票 67)。
  3. *
  4. * ## 真实契约(2026-10-28 实测 `/infrastructure/operating-information` + 后端源码)
  5. *
  6. * 后端把运行指标按 `category` 分成五个枚举(`CockpitInfrastructureOperatingInformationService`
  7. * 第 34–35 行:SUPPLY / FLOW / PRESSURE / QUALITY / CAPACITY),当前业务数据只产出其中三个:
  8. *
  9. * · SUPPLY ← 水厂当前供水能力(`water_plant_current_supply`,单位 万m³/d,带设计能力基准)
  10. * · FLOW ← 泵站设计流量(`*_pump_station_design_flow`,单位 m³/h)
  11. * · CAPACITY ← 管网长度(`*_pipe_network_length`,单位 m)与排水工程设施点数(`*_point_count`,单位 个)
  12. *
  13. * 依据(后端源码,逐行读过):
  14. * · `pipe-network-service/zksy-system/.../BusinessCockpitInfrastructureOperatingInformationAdapter.java`
  15. * 第 63–152 行是四条数据源:水厂 currentCapacity(SUPPLY)、泵站 designFlow(FLOW)、
  16. * 管网 length(CAPACITY)、排水工程设施逐点计数(CAPACITY)。
  17. * · 每条指标对应**一个设施**(管网按段逐条,699 条燃气指标共用一个 metricCode)——
  18. * 所以一类的读数只有**合计**才有业务含义;旧实现取"该 category 第一条"会显示
  19. * 某一段管道的长度(1.03 m)当作"承载能力",这正是维护者看不懂的根因。
  20. *
  21. * ## 行业范围(同一份实测)
  22. *
  23. * · 不传 `industry`:后端返回**四个行业块**(GAS / WATER_SUPPLY / DRAINAGE / MANHOLE),
  24. * `selectedIndustry` 回 null;没指标的行业块 state=EMPTY。
  25. * · 传 `industry=GAS|WATER_SUPPLY|DRAINAGE|MANHOLE`:只返回该行业有指标,其余块 EMPTY。
  26. * · 传 `industry=all`:**400**(`industry must be GAS, WATER_SUPPLY, DRAINAGE or MANHOLE`),
  27. * 即后端不认"all"、也不忽略它。前端 `industryQuery('all')` 回空串=不传参,本来就对。
  28. * · 旧实现"全部行业只显示燃气"是**纯前端**问题:`operationPanel` 在未筛行业时直接取
  29. * `industries[0]`(后端块序恒以燃气打头),却没跨块合计、也没把清单铺开。
  30. *
  31. * ## 本模块的取舍
  32. *
  33. * 1. **合计按单位分组**:单位是硬量纲,不能把「个」加进「m」。取条数最多的那组作为卡面读数,
  34. * 其余单位在悬停说明里如实点出"未计入",不静默吞掉、也不硬凑一个假总数。
  35. * 2. **文案只在这里出现**(三项名称/业务含义/量词),`infrastructurePanels.js` 只消费,
  36. * 模板不认识任何指标键 —— 票 67 验收的"文案唯一来源"。
  37. * 3. **降级如实**:后端若只回一个行业块(忽略"全部"或部署降级),界面说"仅统计××",
  38. * 不说"全部行业"。这是验收点名的降级路径。
  39. */
  40. import { codeText, numberish, ratioText } from './panelBlocks.js'
  41. import { DETAIL_PLACEHOLDER } from './detailFlow.js'
  42. /** 三项能力的固定顺序(也是卡片顺序)。 */
  43. export const OPERATING_CATEGORY_ORDER = Object.freeze(['SUPPLY', 'FLOW', 'CAPACITY'])
  44. /**
  45. * category → 卡面名称。名称必须自明(票 67 要求 3),所以不用接口枚举当标签:
  46. * 「供应能力 / 输送流量 / 承载能力」各自配一句业务含义(见 `OPERATING_METRIC_CALIBER`)。
  47. */
  48. export const OPERATING_CATEGORY_CALIBER = Object.freeze({
  49. SUPPLY: Object.freeze({ label: '供应能力' }),
  50. FLOW: Object.freeze({ label: '输送流量' }),
  51. CAPACITY: Object.freeze({ label: '承载能力' }),
  52. })
  53. /**
  54. * metricCode 后缀 → 业务含义与量词(唯一来源)。
  55. *
  56. * 为什么按**后缀**而不是整流:metricCode 带行业前缀(`gas_pipe_network_length` /
  57. * `water_supply_pipe_network_length` / `drainage_pipe_network_length`),
  58. * 同一类设施的读数必须合并成一条合计;带前缀匹配会把三个行业拆成三张不同的卡。
  59. * 认不出的码回退到 category 的名称(不猜业务含义)。
  60. */
  61. export const OPERATING_METRIC_CALIBER = Object.freeze({
  62. water_plant_current_supply: Object.freeze({ business: '水厂实际供水能力', caption: '水厂供水能力合计', countNoun: '座水厂' }),
  63. pump_station_design_flow: Object.freeze({ business: '泵站设计输送流量', caption: '泵站输送流量合计', countNoun: '座泵站' }),
  64. pipe_network_length: Object.freeze({ business: '管网长度', caption: '管网长度合计', countNoun: '段管网' }),
  65. point_count: Object.freeze({ business: '设施点数', caption: '设施点数合计', countNoun: '处设施' }),
  66. })
  67. /** 保留两位小数(后端 BigDecimal 统一两位;浮点求和不留 0.0000001 的尾巴)。 */
  68. function round2(value) {
  69. return Math.round(value * 100) / 100
  70. }
  71. /** metricCode → 业务口径(按后缀最长匹配,避免将来短码抢先命中)。 */
  72. function caliberOf(metric) {
  73. const code = typeof metric.code === 'string' ? metric.code : ''
  74. if (!code) return null
  75. const suffixes = Object.keys(OPERATING_METRIC_CALIBER).sort((a, b) => b.length - a.length)
  76. for (const suffix of suffixes) {
  77. if (code.endsWith(suffix)) return OPERATING_METRIC_CALIBER[suffix]
  78. }
  79. return null
  80. }
  81. /** 负荷状态 → 语义色键(与单条指标同一条色表,只是阈值在这里按合计负荷重算)。 */
  82. function toneOfRatio(ratio) {
  83. if (ratio === null) return 'neutral'
  84. if (ratio > 1) return 'lv1'
  85. if (ratio >= 0.8) return 'lv2'
  86. return 'lv4'
  87. }
  88. /** 合计负荷率(仅在整组都带能力基准时才有意义)。 */
  89. function ratioOf(rows) {
  90. let value = 0
  91. let capacity = 0
  92. for (const metric of rows) {
  93. if (metric.capacityValue === null || metric.capacityValue === undefined) return null
  94. value += numberish(metric.value) || 0
  95. capacity += numberish(metric.capacityValue) || 0
  96. }
  97. return capacity > 0 ? value / capacity : null
  98. }
  99. /** 一条空卡(该范围没有这一类指标):占位 + 一句可读的说明,不伪造 0。 */
  100. function emptyCard(category) {
  101. const caliber = OPERATING_CATEGORY_CALIBER[category] || { label: codeText(category) }
  102. return {
  103. key: category,
  104. label: caliber.label,
  105. value: DETAIL_PLACEHOLDER,
  106. unit: '',
  107. // 8 字以内:窄卡才不会被裁/折行(2026-10-28 维护者反馈)。
  108. caption: '本范围无此类指标',
  109. hint: '后端未提供该类指标',
  110. tone: 'neutral',
  111. }
  112. }
  113. /**
  114. * 一类能力的卡面读数(票 67 的投影函数:给定任意行业范围的指标集合 → 一项 KPI)。
  115. *
  116. * @param {Array} metrics 当前范围的全部指标(已带 industryCode/industryLabel)
  117. * @param {string} category SUPPLY / FLOW / CAPACITY
  118. * @param {string} [scopeLabel] 当前行业范围的说法(进悬停说明,满足"数值变化可解释")
  119. */
  120. export function operatingCategoryCard(metrics, category, scopeLabel = '') {
  121. const rows = (Array.isArray(metrics) ? metrics : []).filter((metric) => metric && metric.category === category)
  122. const caliber = OPERATING_CATEGORY_CALIBER[category] || { label: codeText(category) }
  123. if (!rows.length) return emptyCard(category)
  124. // 按**单位**分组:不同量纲不合并;同单位才可合计。
  125. const groups = new Map()
  126. for (const metric of rows) {
  127. const unit = typeof metric.unit === 'string' ? metric.unit : ''
  128. if (!groups.has(unit)) groups.set(unit, { unit, rows: [], businesses: new Set(), captions: new Set(), countNouns: new Set() })
  129. const group = groups.get(unit)
  130. group.rows.push(metric)
  131. const metricCaliber = caliberOf(metric)
  132. group.businesses.add(metricCaliber ? metricCaliber.business : '')
  133. group.captions.add(metricCaliber ? metricCaliber.caption : '')
  134. group.countNouns.add(metricCaliber ? metricCaliber.countNoun : '')
  135. }
  136. // 条数最多的单位组=卡面读数(并列取先出现的,保持后端顺序可预期)。
  137. let primary = null
  138. const extras = []
  139. for (const group of groups.values()) {
  140. if (primary === null || group.rows.length > primary.rows.length) {
  141. if (primary) extras.push(primary)
  142. primary = group
  143. } else {
  144. extras.push(group)
  145. }
  146. }
  147. const total = primary.rows.reduce((sum, metric) => sum + (numberish(metric.value) || 0), 0)
  148. const unit = primary.unit
  149. const ratio = ratioOf(primary.rows)
  150. const business = primary.businesses.size === 1 ? [...primary.businesses][0] || '' : ''
  151. const countNoun = primary.countNouns.size === 1 ? [...primary.countNouns][0] || '' : ''
  152. // 卡面小字用**短的**口径句("水厂供水能力合计"):2026-10-28 维护者反馈指标卡不要出现省略号,
  153. // 长句("水厂实际供水能力合计" 11 字)在窄卡会被裁;完整说法(含"实际")仍在悬停说明里,
  154. // 两者不重复也不丢信息。认不出的码回退到 category 名称。
  155. const captionSingle = primary.captions.size === 1 ? [...primary.captions][0] || '' : ''
  156. const caption = captionSingle || `${business || caliber.label}合计`
  157. const hintParts = []
  158. if (scopeLabel) hintParts.push(scopeLabel)
  159. hintParts.push(countNoun ? `按 ${primary.rows.length} ${countNoun}读数合计` : `按 ${primary.rows.length} 条运行指标合计`)
  160. if (ratio === null) {
  161. hintParts.push('无能力基准(后端未提供能力基准)')
  162. } else {
  163. const capacityTotal = primary.rows.reduce((sum, metric) => sum + (numberish(metric.capacityValue) || 0), 0)
  164. hintParts.push(`能力基准 ${round2(capacityTotal)} ${unit} · 负荷 ${ratioText(ratio)}`)
  165. }
  166. for (const extra of extras) {
  167. const extraBusiness = extra.businesses.size === 1 ? [...extra.businesses][0] || '同类指标' : '同类指标'
  168. hintParts.push(`另有 ${extra.rows.length} 项${extraBusiness}(${extra.unit || '无单位'})未计入本卡合计`)
  169. }
  170. return {
  171. key: category,
  172. label: caliber.label,
  173. value: String(round2(total)),
  174. unit,
  175. caption,
  176. hint: hintParts.join(';'),
  177. tone: toneOfRatio(ratio),
  178. loadRatio: ratio,
  179. }
  180. }
  181. /**
  182. * 三项能力的**口径说明**(面板顶部折叠摘要用)。
  183. *
  184. * 与三项名称/业务含义同属"一句话唯一来源":措辞只在本模块写一次,
  185. * `infrastructurePanels.js` 只做引用,避免改了卡片名却忘了改说明。
  186. */
  187. export const OPERATING_SOURCE_NOTE =
  188. '运行指标由后端按设施实体逐条生成(水厂/泵站/管网/排水设施四类,一条指标对应一个设施),所以三项能力是同类设施读数的合计,不是某一条指标:供应能力=水厂当前供水能力,输送流量=泵站设计流量,承载能力=管网长度(排水设施点数单独计数,不与长度相加)。合计只合并单位相同的指标,单位不同的在卡片悬停说明里写明未计入;单位与能力基准都取自后端。行业范围取后端回显:选单个行业时只统计该行业;选「全部行业」时把所有有指标的行业合起来,并列出实际参与的行业。'
  189. /** 三项能力的 KPI 项(顺序固定;只投影、不重复业务逻辑)。 */
  190. export function operatingCategoryCards(metrics, scopeLabel = '') {
  191. return OPERATING_CATEGORY_ORDER.map((category) => operatingCategoryCard(metrics, category, scopeLabel))
  192. }
  193. /**
  194. * 当前行业范围的**唯一判定点**(票 67 要求 2/4)。
  195. *
  196. * · `selectedIndustry` 有值:只统计该行业块;后端回显什么就说什么。
  197. * · 回显为空(=选了"全部行业"):把**所有**有指标的行业块合起来统计,并如实列出
  198. * 实际参与合计的行业;若后端只回了一个行业块,说明"仅统计××"(忽略 all 的降级路径)。
  199. *
  200. * @param {Array} blocks 后端 `industries`(已转成领域对象:code/label/state/metrics)
  201. * @param {string} selectedIndustry 后端回显的选中行业码(空串/undefined=全部)
  202. */
  203. export function operatingScope(blocks, selectedIndustry) {
  204. const list = Array.isArray(blocks) ? blocks : []
  205. const selected = typeof selectedIndustry === 'string' ? selectedIndustry : ''
  206. const labelOf = (block) => (block && (block.label || block.code)) || ''
  207. const withMetrics = list.filter((block) => Array.isArray(block.metrics) && block.metrics.length > 0)
  208. if (selected) {
  209. const block = list.find((item) => item && item.code === selected) || null
  210. const metrics = block && Array.isArray(block.metrics) ? block.metrics : []
  211. const label = labelOf(block) || selected
  212. return {
  213. key: selected,
  214. code: selected,
  215. label,
  216. allView: false,
  217. degraded: false,
  218. metrics,
  219. hintLabel: label,
  220. note: metrics.length ? `当前统计:${label}(按行业筛选)` : `当前统计:${label}(后端未返回该行业的运行指标)`,
  221. }
  222. }
  223. const metrics = withMetrics.flatMap((block) => block.metrics)
  224. if (!metrics.length) {
  225. return { key: 'ALL', code: '', label: '全部行业', allView: true, degraded: false, metrics, hintLabel: '全部行业', note: '当前统计:全部行业(后端未返回运行指标)' }
  226. }
  227. if (list.length === 1) {
  228. // 后端只回了一个行业块:无法证明覆盖全部行业,如实降级(票 67 验收的降级路径)。
  229. const only = labelOf(list[0]) || '单个行业'
  230. return {
  231. key: 'ALL',
  232. code: '',
  233. label: '全部行业',
  234. allView: true,
  235. degraded: true,
  236. metrics,
  237. hintLabel: `仅${only}`,
  238. note: `当前统计:仅${only}(后端只返回了 1 个行业,无法覆盖全部行业)`,
  239. }
  240. }
  241. const labels = withMetrics.map(labelOf).filter(Boolean)
  242. return {
  243. key: 'ALL',
  244. code: '',
  245. label: '全部行业',
  246. allView: true,
  247. degraded: false,
  248. metrics,
  249. hintLabel: '全部行业合计',
  250. note: labels.length ? `当前统计:全部行业(${labels.join('、')})` : '当前统计:全部行业(后端未返回运行指标)',
  251. }
  252. }