/** * 研产供销端到端流程模型(过程指标层)。 * * 与指标树的分工:指标树看的是**结果指标**(收入、毛利率、OEE…), * 这里看的是**过程指标**——一批订单/项目在每一段价值链上停留多久、流过多少、 * 一次通过多少、返工多少。两者共用同一批 metric_node 作为"关联指标", * 所以点击流程卡上的指标片仍然能联动中央看板。 * * 过程量是确定性模拟:同一 (stage, period) 永远得到同一读数, * 拖动底部时间轴会沿 12 期平滑演变(改善/恶化趋势由各段 improve 决定), * 不会像 Math.random() 那样每次刷新都变,演示与复盘都可复现。 */ export type FlowIcon = 'target' | 'flask' | 'cart' | 'factory' | 'truck' | 'cash'; export interface FlowStage { key: string; zh: string; /** 责任口 · 这一段把什么变成什么 */ roleZh: string; icon: FlowIcon; /** 关联的结果/过程指标(metric_node.code),点击联动中央看板 */ codes: string[]; /** 停留天数基线 / 目标(越短越好) */ dwell: number; dwellTarget: number; /** 月流转量基线(批次口径) */ flow: number; flowUnitZh: string; /** 一次通过率 % 基线 / 目标(越高越好) */ fpqc: number; fpqcTarget: number; /** 月返工/退回件数基线 / 目标(越少越好) */ rework: number; reworkTarget: number; /** 12 期改善强度:+1 = 全期持续变好,-1 = 持续变差 */ improve: number; } /** 研 → 产 → 供 → 销 的完整链路,六段 */ export const STAGES: FlowStage[] = [ { key: 'demand', zh: '需求与方案', roleZh: '市场部 · 商机到方案', icon: 'target', codes: ['M01', 'DR-12', 'DR-13'], dwell: 12.4, dwellTarget: 9.0, flow: 620, flowUnitZh: '条商机', fpqc: 72.5, fpqcTarget: 80, rework: 88, reworkTarget: 60, improve: -0.6, }, { key: 'rd', zh: '研发设计', roleZh: '研究院 · 方案到样机', icon: 'flask', codes: ['G02', 'G04', 'G06', 'G07'], dwell: 42.0, dwellTarget: 36.0, flow: 42, flowUnitZh: '个项目', fpqc: 84.0, fpqcTarget: 90, rework: 46, reworkTarget: 38, improve: 0.45, }, { key: 'buy', zh: '采购齐套', roleZh: '采购本部 · 订单到齐套', icon: 'cart', codes: ['K01', 'DR-05', 'DR-15'], dwell: 8.6, dwellTarget: 7.0, flow: 1240, flowUnitZh: '张采购单', fpqc: 91.2, fpqcTarget: 94, rework: 104, reworkTarget: 80, improve: 0.2, }, { key: 'mfg', zh: '生产制造', roleZh: '制造本部 · 投产到下线', icon: 'factory', codes: ['P01', 'P02', 'DR-07', 'DR-09'], dwell: 3.6, dwellTarget: 3.2, flow: 3600, flowUnitZh: '张工单', fpqc: 96.4, fpqcTarget: 97.5, rework: 132, reworkTarget: 110, improve: 0.8, }, { key: 'deliver', zh: '仓储交付', roleZh: '供应链本部 · 计划到签收', icon: 'truck', codes: ['R12', 'E01', 'DR-14'], dwell: 5.2, dwellTarget: 4.5, flow: 2900, flowUnitZh: '张出货单', fpqc: 94.8, fpqcTarget: 96.5, rework: 68, reworkTarget: 55, improve: 0.3, }, { key: 'cash', zh: '服务与回款', roleZh: '财务/服务部 · 交付到回款', icon: 'cash', codes: ['E03', 'Q08', 'M04', 'R13'], dwell: 9.4, dwellTarget: 8.0, flow: 480, flowUnitZh: '件服务工单', fpqc: 88.6, fpqcTarget: 92, rework: 55, reworkTarget: 45, improve: -0.25, }, ]; /** 两频正弦叠加:让 12 期有起伏但不毛躁,同 seed 同 period 恒定 */ function wave(period: number, seed: number) { const p = period + 1; return Math.sin(p * 0.78 + seed * 1.7) * 0.55 + Math.sin(p * 0.31 + seed * 0.9) * 0.45; } /** 全链流量随季节缓涨(年末交付高峰),到当期约 +12% */ const season = (period: number) => 1 + 0.12 * (period - 5.5) / 5.5; const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v)); const r1 = (v: number) => +v.toFixed(1); export interface StageStats { /** 本段平均停留(周期)天数 */ dwell: number; dwellTarget: number; /** 月流转量 */ flow: number; /** 一次通过率 % */ fpqc: number; fpqcTarget: number; /** 月返工/退回件数 */ rework: number; reworkTarget: number; /** 在制品:Little 法则 WIP = 吞吐 × 停留 / 30 */ wip: number; /** 段达成率 %(停留 40% + 一次通过 40% + 返工 20%) */ score: number; status: 'green' | 'yellow' | 'red'; /** 相对上期的停留变化(天),负 = 在提速 */ dwellDelta: number | null; } export function stageStats(index: number, period: number): StageStats { const s = STAGES[index]; const t = (period - 5.5) / 5.5; // -1 → 0 → +1 的全期趋势轴 const drift = (k: number) => wave(period, index * 7 + k); const dwell = dwellAt(index, period); const fpqc = r1(clamp(s.fpqc + s.improve * 3.4 * t + drift(2) * 1.3, 55, 99.5)); const flow = Math.round(s.flow * season(period) * (1 + drift(3) * 0.035)); const rework = Math.max(0, Math.round(s.rework * (1 - s.improve * 0.16 * t - drift(4) * 0.05))); const wip = Math.round(flow * dwell / 30); const score = ( clamp(s.dwellTarget / dwell, 0.6, 1.2) * 0.4 + clamp(fpqc / s.fpqcTarget, 0.6, 1.2) * 0.4 + clamp(s.reworkTarget / Math.max(1, rework), 0.6, 1.2) * 0.2 ) * 100; return { dwell, dwellTarget: s.dwellTarget, flow, fpqc, fpqcTarget: s.fpqcTarget, rework, reworkTarget: s.reworkTarget, wip, score: +score.toFixed(1), status: score >= 99.5 ? 'green' : score >= 92 ? 'yellow' : 'red', dwellDelta: period > 0 ? r1(dwell - dwellAt(index, period - 1)) : null, }; } /** 单段停留:stageStats 与环比都要用,抽出来保证两处口径一致 */ function dwellAt(index: number, period: number) { const s = STAGES[index]; const t = (period - 5.5) / 5.5; return r1(s.dwell * (1 - s.improve * 0.14 * t + wave(period, index * 7 + 1) * 0.05)); } export interface FlowSummary { /** 端到端总周期(各段停留之和,天) */ cycle: number; cycleBase: number; /** 端到端一次通过率(各段 FPQC 连乘,%) */ e2e: number; e2eBase: number; /** 有效产出 = 链上最小吞吐(约束理论,批次/月) */ throughput: number; constraintKey: string; /** 全链在制品 */ wip: number; /** 达成率最低的一段 */ bottleneckKey: string; bottleneckScore: number; } export function flowStats(period: number): { stats: StageStats[]; summary: FlowSummary } { const stats = STAGES.map((_, i) => stageStats(i, period)); const cycle = +stats.reduce((a, x) => a + x.dwell, 0).toFixed(1); const e2e = +(stats.reduce((a, x) => a * x.fpqc / 100, 1) * 100).toFixed(1); const constraint = stats.reduce((mi, x, i) => (x.flow < stats[mi].flow ? i : mi), 0); const worst = stats.reduce((mi, x, i) => (x.score < stats[mi].score ? i : mi), 0); return { stats, summary: { cycle, cycleBase: +STAGES.reduce((a, s) => a + s.dwell, 0).toFixed(1), e2e, e2eBase: +(STAGES.reduce((a, s) => a * s.fpqc / 100, 1) * 100).toFixed(1), throughput: stats[constraint].flow, constraintKey: STAGES[constraint].key, wip: stats.reduce((a, x) => a + x.wip, 0), bottleneckKey: STAGES[worst].key, bottleneckScore: stats[worst].score, }, }; }