优化:按分析章节整合图表解读与完整报告导出
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
type Row = Record<string, unknown>;
|
||||
const record = (value: unknown): Row => value && typeof value === 'object' && !Array.isArray(value) ? value as Row : {};
|
||||
const rows = (value: unknown): Row[] => Array.isArray(value) ? value.map(record) : [];
|
||||
const text = (value: unknown) => value === null || value === undefined || value === '' ? '—' : String(value);
|
||||
const rate = (value: unknown) => typeof value === 'number' && Number.isFinite(value) ? `${(value * 100).toFixed(2)}%` : '—';
|
||||
const number = (value: unknown) => typeof value === 'number' && Number.isFinite(value) ? value.toLocaleString('zh-CN', { maximumFractionDigits: 2 }) : '—';
|
||||
const period = (value: unknown) => { const c = record(value); return c.year ? `${c.year}年${c.month ? `${c.month}月` : c.quarter ? `第${c.quarter}季度` : ''}` : ''; };
|
||||
const stageName = (value: unknown) => value === 'black' ? '黑片' : value === 'bright' ? '亮片' : '';
|
||||
const metricName = (value: unknown) => ({ BLACK_RATE: '黑片合格率', BRIGHT_RATE: '亮片合格率', COMPOSITE_RATE: '综合合格率', COMPOSITE_RATE_WITHOUT_HEADQUARTERS_SCRAP: '综合合格率(不含本部报废)', BRIGHT_RATE_WITHOUT_HEADQUARTERS_SCRAP: '亮片合格率(不含本部报废)' }[String(value)] || '合格率');
|
||||
|
||||
export function AnalysisLegacyResults({ turn }: { turn: Row }) {
|
||||
const result = Object.keys(record(turn.result)).length ? record(turn.result) : turn;
|
||||
const results = rows(result.results || turn.results);
|
||||
const comparisons = results.filter(item => item.current && item.comparison && item.delta);
|
||||
const snapshots = results.filter(item => item.summary && item.context);
|
||||
const main = comparisons.length ? record(comparisons[0].current) : snapshots[0] || {};
|
||||
const summary = record(main.summary);
|
||||
const black = record(summary.black); const bright = record(summary.bright);
|
||||
const coverage = record(summary.coverage);
|
||||
const selectedMetric = String(comparisons[0]?.metric || record(record(result.task || turn.task).conditions).metricCode || 'COMPOSITE_RATE');
|
||||
const adjusted = selectedMetric.includes('WITHOUT_HEADQUARTERS_SCRAP');
|
||||
if (!results.length) return null;
|
||||
return <section className="q-report-legacy-results"><h2>保存的统计数据</h2>
|
||||
{Object.keys(summary).length > 0 && <div className="q-kpis">
|
||||
<div className="q-kpi"><span>{adjusted ? '综合合格率(不含本部报废)' : '综合合格率'}</span><strong>{coverage.blackRows === 0 && coverage.brightRows === 0 ? '—' : rate(adjusted ? summary.compositeRateWithoutHeadquartersScrap : summary.compositeRate)}</strong></div>
|
||||
<div className="q-kpi"><span>黑片合格率</span><strong>{coverage.blackRows === 0 ? '—' : rate(black.rate)}</strong></div>
|
||||
<div className="q-kpi"><span>{adjusted ? '亮片合格率(不含本部报废)' : '亮片合格率'}</span><strong>{coverage.brightRows === 0 ? '—' : rate(adjusted ? bright.rateWithoutHeadquartersScrap : bright.rate)}</strong></div>
|
||||
</div>}
|
||||
{comparisons.map((item, index) => {
|
||||
const current = record(item.current), comparison = record(item.comparison), delta = record(item.delta), comparable = record(item.comparability);
|
||||
const value = (side: Row) => { const s = record(side.summary); switch (item.metric) { case 'BLACK_RATE': return rate(record(s.black).rate); case 'BRIGHT_RATE': return rate(record(s.bright).rate); case 'BRIGHT_RATE_WITHOUT_HEADQUARTERS_SCRAP': return rate(record(s.bright).rateWithoutHeadquartersScrap); case 'COMPOSITE_RATE_WITHOUT_HEADQUARTERS_SCRAP': return rate(s.compositeRateWithoutHeadquartersScrap); default: return rate(s.compositeRate); } };
|
||||
return <section key={`compare-${index}`}><h2>{metricName(item.metric)} · 期间比较</h2><table><thead><tr><th>序号</th><th>期间</th><th>正式结果</th></tr></thead><tbody><tr><td>1</td><td>{period(current.context)}</td><td>{value(current)}</td></tr><tr><td>2</td><td>{period(comparison.context)}</td><td>{value(comparison)}</td></tr><tr><td>3</td><td>变化(百分点)</td><td>{number(delta.percentagePoints)}</td></tr></tbody></table>{comparable.comparable === false && <p className="q-note">{text(comparable.reason || (Array.isArray(comparable.reasons) ? comparable.reasons.join(';') : '所选期间不可比,不给出变化结论。'))}</p>}</section>;
|
||||
})}
|
||||
{results.filter(item => Array.isArray(item.groups)).map((item, index) => {
|
||||
const available = item.available === true;
|
||||
const ranked = [...rows(item.groups)].filter(row => typeof row[available ? 'impactPP' : 'scrapDelta'] === 'number').sort((a, b) => available ? Number(a.impactPP) - Number(b.impactPP) : Number(b.scrapDelta) - Number(a.scrapDelta)).slice(0, 10);
|
||||
if (!ranked.length) return null;
|
||||
return <section key={`impact-${index}`}><h2>{stageName(item.stage)} · 优先核查对象</h2><table><thead><tr><th>序号</th><th>产品 / 规格</th><th>当前合格率</th><th>比较期合格率</th><th>{available ? '影响(百分点)' : '报废增量(kg)'}</th></tr></thead><tbody>{ranked.map((row, i) => <tr key={i}><td>{i + 1}</td><td>{text(row.label || row.key)}</td><td>{rate(row.currentRate)}</td><td>{rate(row.comparisonRate)}</td><td>{number(available ? row.impactPP : row.scrapDelta)}</td></tr>)}</tbody></table>{!available && <p className="q-note">按报废增量列出前10项。分组无法完整回加,仅作核查线索,不能当作整体下降贡献。</p>}</section>;
|
||||
})}
|
||||
{results.filter(item => item.kind === 'analysis_statistics').map((item, index) => <section key={`trend-${index}`}><h2>{stageName(item.stage)} · 历史趋势</h2><table><thead><tr><th>序号</th><th>期间</th><th>阶段合格率</th></tr></thead><tbody>{rows(item.trendChange).map((row, i) => <tr key={i}><td>{i + 1}</td><td>{text(row.label || row.period)}</td><td>{rate(row.stageRate)}</td></tr>)}</tbody></table></section>)}
|
||||
{results.filter(item => Array.isArray(item.rows) && item.dimension && item.stage).map((item, index) => <section key={`top-${index}`}><h2>{stageName(item.stage)} · TOP核查对象</h2><table><thead><tr><th>序号</th><th>对象</th><th>报废重量(kg)</th><th>占比</th></tr></thead><tbody>{rows(item.rows).slice(0, 10).map((row, i) => <tr key={i}><td>{i + 1}</td><td>{text(row.label || row.key)}</td><td>{number(row.weight)}</td><td>{rate(row.share)}</td></tr>)}</tbody></table></section>)}
|
||||
</section>;
|
||||
}
|
||||
Reference in New Issue
Block a user