| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- /**
- * 运行信息「三项能力指标」的**唯一口径来源**(票 67)。
- *
- * ## 真实契约(2026-10-28 实测 `/infrastructure/operating-information` + 后端源码)
- *
- * 后端把运行指标按 `category` 分成五个枚举(`CockpitInfrastructureOperatingInformationService`
- * 第 34–35 行:SUPPLY / FLOW / PRESSURE / QUALITY / CAPACITY),当前业务数据只产出其中三个:
- *
- * · SUPPLY ← 水厂当前供水能力(`water_plant_current_supply`,单位 万m³/d,带设计能力基准)
- * · FLOW ← 泵站设计流量(`*_pump_station_design_flow`,单位 m³/h)
- * · CAPACITY ← 管网长度(`*_pipe_network_length`,单位 m)与排水工程设施点数(`*_point_count`,单位 个)
- *
- * 依据(后端源码,逐行读过):
- * · `pipe-network-service/zksy-system/.../BusinessCockpitInfrastructureOperatingInformationAdapter.java`
- * 第 63–152 行是四条数据源:水厂 currentCapacity(SUPPLY)、泵站 designFlow(FLOW)、
- * 管网 length(CAPACITY)、排水工程设施逐点计数(CAPACITY)。
- * · 每条指标对应**一个设施**(管网按段逐条,699 条燃气指标共用一个 metricCode)——
- * 所以一类的读数只有**合计**才有业务含义;旧实现取"该 category 第一条"会显示
- * 某一段管道的长度(1.03 m)当作"承载能力",这正是维护者看不懂的根因。
- *
- * ## 行业范围(同一份实测)
- *
- * · 不传 `industry`:后端返回**四个行业块**(GAS / WATER_SUPPLY / DRAINAGE / MANHOLE),
- * `selectedIndustry` 回 null;没指标的行业块 state=EMPTY。
- * · 传 `industry=GAS|WATER_SUPPLY|DRAINAGE|MANHOLE`:只返回该行业有指标,其余块 EMPTY。
- * · 传 `industry=all`:**400**(`industry must be GAS, WATER_SUPPLY, DRAINAGE or MANHOLE`),
- * 即后端不认"all"、也不忽略它。前端 `industryQuery('all')` 回空串=不传参,本来就对。
- * · 旧实现"全部行业只显示燃气"是**纯前端**问题:`operationPanel` 在未筛行业时直接取
- * `industries[0]`(后端块序恒以燃气打头),却没跨块合计、也没把清单铺开。
- *
- * ## 本模块的取舍
- *
- * 1. **合计按单位分组**:单位是硬量纲,不能把「个」加进「m」。取条数最多的那组作为卡面读数,
- * 其余单位在悬停说明里如实点出"未计入",不静默吞掉、也不硬凑一个假总数。
- * 2. **文案只在这里出现**(三项名称/业务含义/量词),`infrastructurePanels.js` 只消费,
- * 模板不认识任何指标键 —— 票 67 验收的"文案唯一来源"。
- * 3. **降级如实**:后端若只回一个行业块(忽略"全部"或部署降级),界面说"仅统计××",
- * 不说"全部行业"。这是验收点名的降级路径。
- */
- import { codeText, numberish, ratioText } from './panelBlocks.js'
- import { DETAIL_PLACEHOLDER } from './detailFlow.js'
- /** 三项能力的固定顺序(也是卡片顺序)。 */
- export const OPERATING_CATEGORY_ORDER = Object.freeze(['SUPPLY', 'FLOW', 'CAPACITY'])
- /**
- * category → 卡面名称。名称必须自明(票 67 要求 3),所以不用接口枚举当标签:
- * 「供应能力 / 输送流量 / 承载能力」各自配一句业务含义(见 `OPERATING_METRIC_CALIBER`)。
- */
- export const OPERATING_CATEGORY_CALIBER = Object.freeze({
- SUPPLY: Object.freeze({ label: '供应能力' }),
- FLOW: Object.freeze({ label: '输送流量' }),
- CAPACITY: Object.freeze({ label: '承载能力' }),
- })
- /**
- * metricCode 后缀 → 业务含义与量词(唯一来源)。
- *
- * 为什么按**后缀**而不是整流:metricCode 带行业前缀(`gas_pipe_network_length` /
- * `water_supply_pipe_network_length` / `drainage_pipe_network_length`),
- * 同一类设施的读数必须合并成一条合计;带前缀匹配会把三个行业拆成三张不同的卡。
- * 认不出的码回退到 category 的名称(不猜业务含义)。
- */
- export const OPERATING_METRIC_CALIBER = Object.freeze({
- water_plant_current_supply: Object.freeze({ business: '水厂实际供水能力', caption: '水厂供水能力合计', countNoun: '座水厂' }),
- pump_station_design_flow: Object.freeze({ business: '泵站设计输送流量', caption: '泵站输送流量合计', countNoun: '座泵站' }),
- pipe_network_length: Object.freeze({ business: '管网长度', caption: '管网长度合计', countNoun: '段管网' }),
- point_count: Object.freeze({ business: '设施点数', caption: '设施点数合计', countNoun: '处设施' }),
- })
- /** 保留两位小数(后端 BigDecimal 统一两位;浮点求和不留 0.0000001 的尾巴)。 */
- function round2(value) {
- return Math.round(value * 100) / 100
- }
- /** metricCode → 业务口径(按后缀最长匹配,避免将来短码抢先命中)。 */
- function caliberOf(metric) {
- const code = typeof metric.code === 'string' ? metric.code : ''
- if (!code) return null
- const suffixes = Object.keys(OPERATING_METRIC_CALIBER).sort((a, b) => b.length - a.length)
- for (const suffix of suffixes) {
- if (code.endsWith(suffix)) return OPERATING_METRIC_CALIBER[suffix]
- }
- return null
- }
- /** 负荷状态 → 语义色键(与单条指标同一条色表,只是阈值在这里按合计负荷重算)。 */
- function toneOfRatio(ratio) {
- if (ratio === null) return 'neutral'
- if (ratio > 1) return 'lv1'
- if (ratio >= 0.8) return 'lv2'
- return 'lv4'
- }
- /** 合计负荷率(仅在整组都带能力基准时才有意义)。 */
- function ratioOf(rows) {
- let value = 0
- let capacity = 0
- for (const metric of rows) {
- if (metric.capacityValue === null || metric.capacityValue === undefined) return null
- value += numberish(metric.value) || 0
- capacity += numberish(metric.capacityValue) || 0
- }
- return capacity > 0 ? value / capacity : null
- }
- /** 一条空卡(该范围没有这一类指标):占位 + 一句可读的说明,不伪造 0。 */
- function emptyCard(category) {
- const caliber = OPERATING_CATEGORY_CALIBER[category] || { label: codeText(category) }
- return {
- key: category,
- label: caliber.label,
- value: DETAIL_PLACEHOLDER,
- unit: '',
- // 8 字以内:窄卡才不会被裁/折行(2026-10-28 维护者反馈)。
- caption: '本范围无此类指标',
- hint: '后端未提供该类指标',
- tone: 'neutral',
- }
- }
- /**
- * 一类能力的卡面读数(票 67 的投影函数:给定任意行业范围的指标集合 → 一项 KPI)。
- *
- * @param {Array} metrics 当前范围的全部指标(已带 industryCode/industryLabel)
- * @param {string} category SUPPLY / FLOW / CAPACITY
- * @param {string} [scopeLabel] 当前行业范围的说法(进悬停说明,满足"数值变化可解释")
- */
- export function operatingCategoryCard(metrics, category, scopeLabel = '') {
- const rows = (Array.isArray(metrics) ? metrics : []).filter((metric) => metric && metric.category === category)
- const caliber = OPERATING_CATEGORY_CALIBER[category] || { label: codeText(category) }
- if (!rows.length) return emptyCard(category)
- // 按**单位**分组:不同量纲不合并;同单位才可合计。
- const groups = new Map()
- for (const metric of rows) {
- const unit = typeof metric.unit === 'string' ? metric.unit : ''
- if (!groups.has(unit)) groups.set(unit, { unit, rows: [], businesses: new Set(), captions: new Set(), countNouns: new Set() })
- const group = groups.get(unit)
- group.rows.push(metric)
- const metricCaliber = caliberOf(metric)
- group.businesses.add(metricCaliber ? metricCaliber.business : '')
- group.captions.add(metricCaliber ? metricCaliber.caption : '')
- group.countNouns.add(metricCaliber ? metricCaliber.countNoun : '')
- }
- // 条数最多的单位组=卡面读数(并列取先出现的,保持后端顺序可预期)。
- let primary = null
- const extras = []
- for (const group of groups.values()) {
- if (primary === null || group.rows.length > primary.rows.length) {
- if (primary) extras.push(primary)
- primary = group
- } else {
- extras.push(group)
- }
- }
- const total = primary.rows.reduce((sum, metric) => sum + (numberish(metric.value) || 0), 0)
- const unit = primary.unit
- const ratio = ratioOf(primary.rows)
- const business = primary.businesses.size === 1 ? [...primary.businesses][0] || '' : ''
- const countNoun = primary.countNouns.size === 1 ? [...primary.countNouns][0] || '' : ''
- // 卡面小字用**短的**口径句("水厂供水能力合计"):2026-10-28 维护者反馈指标卡不要出现省略号,
- // 长句("水厂实际供水能力合计" 11 字)在窄卡会被裁;完整说法(含"实际")仍在悬停说明里,
- // 两者不重复也不丢信息。认不出的码回退到 category 名称。
- const captionSingle = primary.captions.size === 1 ? [...primary.captions][0] || '' : ''
- const caption = captionSingle || `${business || caliber.label}合计`
- const hintParts = []
- if (scopeLabel) hintParts.push(scopeLabel)
- hintParts.push(countNoun ? `按 ${primary.rows.length} ${countNoun}读数合计` : `按 ${primary.rows.length} 条运行指标合计`)
- if (ratio === null) {
- hintParts.push('无能力基准(后端未提供能力基准)')
- } else {
- const capacityTotal = primary.rows.reduce((sum, metric) => sum + (numberish(metric.capacityValue) || 0), 0)
- hintParts.push(`能力基准 ${round2(capacityTotal)} ${unit} · 负荷 ${ratioText(ratio)}`)
- }
- for (const extra of extras) {
- const extraBusiness = extra.businesses.size === 1 ? [...extra.businesses][0] || '同类指标' : '同类指标'
- hintParts.push(`另有 ${extra.rows.length} 项${extraBusiness}(${extra.unit || '无单位'})未计入本卡合计`)
- }
- return {
- key: category,
- label: caliber.label,
- value: String(round2(total)),
- unit,
- caption,
- hint: hintParts.join(';'),
- tone: toneOfRatio(ratio),
- loadRatio: ratio,
- }
- }
- /**
- * 三项能力的**口径说明**(面板顶部折叠摘要用)。
- *
- * 与三项名称/业务含义同属"一句话唯一来源":措辞只在本模块写一次,
- * `infrastructurePanels.js` 只做引用,避免改了卡片名却忘了改说明。
- */
- export const OPERATING_SOURCE_NOTE =
- '运行指标由后端按设施实体逐条生成(水厂/泵站/管网/排水设施四类,一条指标对应一个设施),所以三项能力是同类设施读数的合计,不是某一条指标:供应能力=水厂当前供水能力,输送流量=泵站设计流量,承载能力=管网长度(排水设施点数单独计数,不与长度相加)。合计只合并单位相同的指标,单位不同的在卡片悬停说明里写明未计入;单位与能力基准都取自后端。行业范围取后端回显:选单个行业时只统计该行业;选「全部行业」时把所有有指标的行业合起来,并列出实际参与的行业。'
- /** 三项能力的 KPI 项(顺序固定;只投影、不重复业务逻辑)。 */
- export function operatingCategoryCards(metrics, scopeLabel = '') {
- return OPERATING_CATEGORY_ORDER.map((category) => operatingCategoryCard(metrics, category, scopeLabel))
- }
- /**
- * 当前行业范围的**唯一判定点**(票 67 要求 2/4)。
- *
- * · `selectedIndustry` 有值:只统计该行业块;后端回显什么就说什么。
- * · 回显为空(=选了"全部行业"):把**所有**有指标的行业块合起来统计,并如实列出
- * 实际参与合计的行业;若后端只回了一个行业块,说明"仅统计××"(忽略 all 的降级路径)。
- *
- * @param {Array} blocks 后端 `industries`(已转成领域对象:code/label/state/metrics)
- * @param {string} selectedIndustry 后端回显的选中行业码(空串/undefined=全部)
- */
- export function operatingScope(blocks, selectedIndustry) {
- const list = Array.isArray(blocks) ? blocks : []
- const selected = typeof selectedIndustry === 'string' ? selectedIndustry : ''
- const labelOf = (block) => (block && (block.label || block.code)) || ''
- const withMetrics = list.filter((block) => Array.isArray(block.metrics) && block.metrics.length > 0)
- if (selected) {
- const block = list.find((item) => item && item.code === selected) || null
- const metrics = block && Array.isArray(block.metrics) ? block.metrics : []
- const label = labelOf(block) || selected
- return {
- key: selected,
- code: selected,
- label,
- allView: false,
- degraded: false,
- metrics,
- hintLabel: label,
- note: metrics.length ? `当前统计:${label}(按行业筛选)` : `当前统计:${label}(后端未返回该行业的运行指标)`,
- }
- }
- const metrics = withMetrics.flatMap((block) => block.metrics)
- if (!metrics.length) {
- return { key: 'ALL', code: '', label: '全部行业', allView: true, degraded: false, metrics, hintLabel: '全部行业', note: '当前统计:全部行业(后端未返回运行指标)' }
- }
- if (list.length === 1) {
- // 后端只回了一个行业块:无法证明覆盖全部行业,如实降级(票 67 验收的降级路径)。
- const only = labelOf(list[0]) || '单个行业'
- return {
- key: 'ALL',
- code: '',
- label: '全部行业',
- allView: true,
- degraded: true,
- metrics,
- hintLabel: `仅${only}`,
- note: `当前统计:仅${only}(后端只返回了 1 个行业,无法覆盖全部行业)`,
- }
- }
- const labels = withMetrics.map(labelOf).filter(Boolean)
- return {
- key: 'ALL',
- code: '',
- label: '全部行业',
- allView: true,
- degraded: false,
- metrics,
- hintLabel: '全部行业合计',
- note: labels.length ? `当前统计:全部行业(${labels.join('、')})` : '当前统计:全部行业(后端未返回运行指标)',
- }
- }
|