Files
ai-platform/src/modules/welltrust-qap/components/qap/QapDataView.tsx
T
2026-10-18 15:27:02 +08:00

372 lines
18 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useMemo, useState } from 'react';
import type { CSSProperties } from 'react';
import { Database, Search, X } from 'lucide-react';
import { useTheme, type ThemeTokens } from '../../context/ThemeContext';
import {
CATALOG, QAP_CALIBER, QAP_SOURCE, SCENARIOS, calc, tablesOfDataset,
type QapDatasetMeta, type QapRole, type QapTableKey,
} from '../../data/qap';
/**
* 数据集总览:把「品质异常预防」演示用的那本 Excel(16 数据集 / 17 张表)在应用里摊开看。
*
* 这是数据层的前端出口——数字全部来自 src/data/qap 里由 xlsx 转出的 JSON,页面不写死任何值;
* 原表里那几个「计算列」(理论产能 / 达成率 / 综合不良率 / 综合良率 / 负荷率 / 滞留天数 / 偏移率)
* 转 JSON 时已剔除,这里用 calc.* 现算后以 accent 标出,保证"表里是值、代码里也是同一套算法"。
* 文案与数据层同源、全是中文业务术语,因此和业务本体总览一样不走 i18n 词典。
*/
/* 角色配色:和「质量流程」的叙事对齐——QE 是瓶颈段,用 danger 提出来 */
const ROLE_TONE: Record<QapRole, 'info' | 'success' | 'danger' | 'warning'> = {
PE: 'info', IE: 'success', QE: 'danger', ME: 'warning',
};
const toneColor = (tk: ThemeTokens, tone: 'info' | 'success' | 'danger' | 'warning') =>
tone === 'success' ? { c: tk.success, bg: tk.successBg }
: tone === 'warning' ? { c: tk.warning, bg: tk.warningBg }
: tone === 'danger' ? { c: tk.danger, bg: tk.dangerBg }
: { c: tk.info, bg: tk.infoBg };
const RolePill = ({ role }: { role: QapRole }) => {
const { tokens } = useTheme();
const c = toneColor(tokens, ROLE_TONE[role]);
return (
<span style={{
fontSize: 10, fontWeight: 800, padding: '1px 7px', borderRadius: 999, whiteSpace: 'nowrap',
color: c.c, background: c.bg, border: `1px solid ${c.c}44`, letterSpacing: '0.04em',
}}>{role}</span>
);
};
/* ── 单元格格式化:比率类字段原表存的是小数,这里按原样显示,不擅自乘 100 ── */
const fmtCell = (v: unknown): string => {
if (v === null || v === undefined || v === '') return '—';
if (typeof v === 'number') {
if (Number.isInteger(v)) return v.toLocaleString('zh-CN');
return String(+v.toFixed(4));
}
return String(v);
};
const pct = (v: number | null) => (v === null ? '—' : `${(v * 100).toFixed(2)}%`);
/** 计算列:header 用原表列名,hint 写原表首行的公式口径 */
interface ComputedCol {
header: string;
hint: string;
get: (r: Record<string, unknown>) => string;
}
const COMPUTED: Partial<Record<QapTableKey, ComputedCol[]>> = {
'DS-02': [
{ header: '理论产能(件)', hint: 'ROUND(480 ÷ 标准工时)', get: r => String(calc.theoryCapacity(r as never)) },
{ header: '产能达成率', hint: '班次产能 ÷ 理论产能', get: r => pct(calc.capacityRate(r as never)) },
],
'DS-09': [{ header: '综合不良率', hint: '四类缺陷率之和', get: r => pct(calc.defectRate(r as never)) }],
'DS-12': [{ header: '综合良率', hint: '1 − 焊瘤率 − 未焊透率', get: r => pct(calc.yieldRate(r as never)) }],
'DS-14': [{ header: '负荷率', hint: '需求节拍 ÷ 实际产能', get: r => pct(calc.loadRate(r as never)) }],
'DS-15': [{ header: '滞留天数', hint: `基准日 ${QAP_CALIBER.warningPoolBaseDate} − 发生日`, get: r => `${calc.dwellDays(r as never)} 天` }],
'DS-16': [{ header: '偏移率', hint: '(实测均值 − 标准值)÷ 标准值', get: r => pct(calc.driftRate(r as never)) }],
};
/** 一张表的渲染:列取自行数据的键,末尾追加现算的计算列 */
function DataTable({ tableKey, rows }: { tableKey: QapTableKey; rows: readonly unknown[] }) {
const { tokens } = useTheme();
const recs = rows as Record<string, unknown>[];
const columns = recs.length ? Object.keys(recs[0]) : [];
const computed = COMPUTED[tableKey] ?? [];
const cell: CSSProperties = {
padding: '5px 10px', fontSize: 12, whiteSpace: 'nowrap',
borderBottom: `1px solid ${tokens.borderSub}`, color: tokens.textSub,
};
const head: CSSProperties = {
...cell, position: 'sticky', top: 0, zIndex: 1,
background: tokens.tableHeaderBg, color: tokens.text, fontWeight: 800, fontSize: 11.5,
letterSpacing: '0.02em', borderBottom: `1px solid ${tokens.borderAccent}`,
};
const longish = (s: string) => s.length > 18;
const bodyCell = (text: string, accent = false): CSSProperties => ({
...cell,
color: accent ? tokens.accent : tokens.textSub,
fontWeight: accent ? 700 : 400,
fontVariantNumeric: 'tabular-nums',
...(longish(text) ? { maxWidth: 240, overflow: 'hidden', textOverflow: 'ellipsis' } : {}),
});
return (
<div style={{
flex: 1, minHeight: 0, overflow: 'auto', borderRadius: 10,
border: `1px solid ${tokens.border}`, background: tokens.cardBg,
}}>
<table style={{ borderCollapse: 'collapse', width: 'max-content', minWidth: '100%' }}>
<thead>
<tr>
<th style={{ ...head, textAlign: 'right', color: tokens.textMuted }}>#</th>
{columns.map(c => <th key={c} style={{ ...head, textAlign: 'left' }}>{c}</th>)}
{computed.map(c => (
<th key={c.header} style={{ ...head, textAlign: 'left', color: tokens.accent }} title={`计算列(现算):${c.hint}`}>
{c.header}
<span style={{
marginLeft: 5, fontSize: 9, fontWeight: 700, padding: '0 4px', borderRadius: 4,
color: tokens.accent, background: tokens.accentBg, border: `1px solid ${tokens.borderAccent}`,
verticalAlign: 'middle',
}}>现算</span>
</th>
))}
</tr>
</thead>
<tbody>
{recs.map((r, i) => (
<tr key={i}>
<td style={{ ...cell, textAlign: 'right', color: tokens.textMuted, fontVariantNumeric: 'tabular-nums' }}>{i + 1}</td>
{columns.map(c => {
const text = fmtCell(r[c]);
return (
<td key={c} style={bodyCell(text)} title={text.length > 18 ? text : undefined}>{text}</td>
);
})}
{computed.map(c => {
const text = c.get(r);
return (
<td key={c.header} style={bodyCell(text, true)} title={c.hint}>{text}</td>
);
})}
</tr>
))}
</tbody>
</table>
</div>
);
}
interface Props {
onExit: () => void;
/** 回答卡跳转过来时定位到指定数据集;n 是重复点击的序号,同一张表也能再次复位筛选 */
focus?: { id: string; n: number } | null;
}
export default function QapDataView({ onExit, focus }: Props) {
const { tokens } = useTheme();
const [selId, setSelId] = useState<string>(CATALOG[0].id);
const [query, setQuery] = useState('');
const [roleFilter, setRoleFilter] = useState<QapRole | 'ALL'>('ALL');
// 从回答卡「回数据集对账」进来:清掉筛选并定位到那张表,避免它被当前筛选挡住
useEffect(() => {
if (!focus?.id) return;
setSelId(focus.id);
setQuery('');
setRoleFilter('ALL');
}, [focus?.id, focus?.n]);
const q = query.trim().toLowerCase();
const visible = useMemo(() => CATALOG.filter(d =>
(roleFilter === 'ALL' || d.role === roleFilter) &&
(!q || [d.id, d.name, d.useCase, d.audit, d.caliber].some(s => (s ?? '').toLowerCase().includes(q)))
), [q, roleFilter]);
const sel: QapDatasetMeta | undefined = useMemo(
() => CATALOG.find(d => d.id === selId) ?? visible[0],
[selId, visible],
);
const totalRows = useMemo(
() => CATALOG.reduce((a, d) => a + tablesOfDataset(d.id).reduce((b, t) => b + t.rows.length, 0), 0),
[],
);
/* DS-06 一个数据集带两张表,所以表数单算,不能用数据集数顶替 */
const totalTables = useMemo(
() => CATALOG.reduce((a, d) => a + tablesOfDataset(d.id).length, 0),
[],
);
const roleCount = (role: QapRole) => CATALOG.filter(d => d.role === role).length;
const chip = (label: string, on: boolean, onClick: () => void, tone?: 'info' | 'success' | 'danger' | 'warning') => {
const c = tone ? toneColor(tokens, tone) : { c: tokens.accent, bg: tokens.accentBg };
return (
<button
key={label}
onClick={onClick}
style={{
cursor: 'pointer', whiteSpace: 'nowrap', padding: '3px 12px', borderRadius: 999,
fontSize: 11.5, fontWeight: on ? 800 : 600, transition: 'background .15s ease, color .15s ease',
color: on ? c.c : tokens.textSub,
background: on ? c.bg : tokens.kpiCardBg,
border: `1px solid ${on ? c.c : tokens.border}`,
}}
>{label}</button>
);
};
return (
<div style={{ flex: 1, minHeight: 0, minWidth: 0, display: 'flex', flexDirection: 'column', gap: 10 }}>
{/* ── 标题栏 ── */}
<div style={{
display: 'flex', alignItems: 'center', gap: 10, flex: '0 0 auto', flexWrap: 'wrap', padding: '2px 2px 0',
}}>
<Database size={19} style={{ color: tokens.accent, flexShrink: 0 }} />
<span style={{ fontSize: 15.5, fontWeight: 800, color: tokens.text, letterSpacing: '0.03em' }}>数据集总览</span>
<span style={{ fontSize: 11.5, color: tokens.textMuted, flexShrink: 0 }}>
前端内置 · 不落后端库
</span>
<span style={{ flex: '1 1 24px' }} />
<div style={{ display: 'flex', alignItems: 'center', gap: 6, flexWrap: 'wrap' }}>
{[
{ l: '数据集', v: CATALOG.length }, { l: '数据表', v: totalTables },
{ l: '记录', v: totalRows }, { l: '场景', v: SCENARIOS.length },
].map(s => (
<span key={s.l} style={{
fontSize: 11, fontWeight: 700, padding: '2px 9px', borderRadius: 999, whiteSpace: 'nowrap',
color: tokens.textSub, background: tokens.accentBg, border: `1px solid ${tokens.border}`,
fontVariantNumeric: 'tabular-nums',
}}>{s.l} {s.v}</span>
))}
</div>
<div style={{
display: 'flex', alignItems: 'center', gap: 6, flex: '1 1 170px', minWidth: 150, maxWidth: 250,
padding: '3px 9px', borderRadius: 999, background: tokens.panelBgAlt, border: `1px solid ${tokens.borderAccent}`,
}}>
<Search size={12} style={{ color: tokens.textMuted, flexShrink: 0 }} />
<input
value={query}
onChange={e => setQuery(e.target.value)}
placeholder="搜索数据集 / 编号 / 用例"
style={{ flex: 1, minWidth: 0, background: 'transparent', border: 'none', outline: 'none', color: tokens.text, fontSize: 12 }}
/>
{query && (
<button onClick={() => setQuery('')} title="清空搜索" style={{ background: 'none', border: 'none', cursor: 'pointer', color: tokens.textMuted, display: 'flex' }}>
<X size={12} />
</button>
)}
</div>
<button
onClick={onExit}
title="退出数据集总览 (ESC)"
style={{
flexShrink: 0, display: 'flex', alignItems: 'center', gap: 5, cursor: 'pointer',
padding: '4px 11px', borderRadius: 8, fontSize: 11.5, fontWeight: 700,
color: tokens.accent, background: tokens.accentBg, border: `1px solid ${tokens.borderAccent}`,
}}
>
<X size={13} /> 退出 ESC
</button>
</div>
{/* ── 角色筛选 ── */}
<div style={{ display: 'flex', gap: 6, flexWrap: 'wrap', flex: '0 0 auto' }}>
{chip(`全部 ${CATALOG.length}`, roleFilter === 'ALL', () => setRoleFilter('ALL'))}
{(['PE', 'IE', 'QE', 'ME'] as QapRole[]).map(r =>
chip(`${r} ${roleCount(r)}`, roleFilter === r, () => setRoleFilter(r), ROLE_TONE[r]))}
</div>
{/* ── 左:目录列表 · 右:选中数据集的表 ── */}
<div style={{ flex: 1, minHeight: 0, display: 'flex', gap: 10 }}>
<div style={{
flex: '0 0 clamp(230px, 23%, 300px)', minWidth: 0, display: 'flex', flexDirection: 'column',
borderRadius: 12, background: tokens.cardBg, border: `1px solid ${tokens.border}`, padding: '8px 8px 6px',
}}>
<div style={{
display: 'flex', alignItems: 'center', gap: 6, padding: '0 2px 7px', marginBottom: 4,
borderBottom: `1px solid ${tokens.border}`,
}}>
<span style={{ fontSize: 13, fontWeight: 800, color: tokens.text }}>数据集</span>
<span style={{ flex: 1 }} />
<span style={{ fontSize: 11, color: q || roleFilter !== 'ALL' ? tokens.warning : tokens.textMuted, fontVariantNumeric: 'tabular-nums' }}>
{visible.length}/{CATALOG.length}
</span>
</div>
<div className="space-scroll" style={{ flex: 1, minHeight: 0, overflowY: 'auto', paddingRight: 4 }}>
{visible.length === 0 && (
<div style={{ fontSize: 12, color: tokens.textMuted, padding: '10px 4px' }}>没有匹配的数据集</div>
)}
{visible.map(d => {
const on = sel?.id === d.id;
const rows = tablesOfDataset(d.id).reduce((a, t) => a + t.rows.length, 0);
return (
<button
key={d.id}
onClick={() => setSelId(d.id)}
style={{
width: '100%', textAlign: 'left', cursor: 'pointer', marginBottom: 6,
padding: '7px 9px', borderRadius: 9,
background: on ? tokens.accentBg : tokens.panelBgAlt,
border: `1px solid ${on ? tokens.accent : tokens.border}`,
transition: 'background .15s ease, border-color .15s ease',
}}
>
<div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 3 }}>
<span style={{ fontSize: 11, fontWeight: 800, color: tokens.accent, fontVariantNumeric: 'tabular-nums' }}>{d.id}</span>
<RolePill role={d.role} />
<span style={{ flex: 1 }} />
<span style={{ fontSize: 10.5, color: tokens.textMuted, fontVariantNumeric: 'tabular-nums' }}>{rows} 行</span>
</div>
<div style={{ fontSize: 12.5, fontWeight: 700, color: on ? tokens.text : tokens.textSub, lineHeight: 1.35 }}>
{d.name}
</div>
<div style={{ fontSize: 10.5, color: tokens.textMuted, marginTop: 2 }}>{d.useCase}</div>
</button>
);
})}
</div>
</div>
{/* 右:详情 */}
<div style={{
flex: '1 1 0', minWidth: 0, display: 'flex', flexDirection: 'column',
borderRadius: 12, background: tokens.cardBg, border: `1px solid ${tokens.border}`, padding: '10px 12px',
}}>
{!sel ? (
<div style={{ fontSize: 12.5, color: tokens.textMuted, padding: 8 }}>左侧选择一个数据集</div>
) : (
<>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap', flex: '0 0 auto' }}>
<span style={{ fontSize: 12, fontWeight: 800, color: tokens.accent, fontVariantNumeric: 'tabular-nums' }}>{sel.id}</span>
<span style={{ fontSize: 15, fontWeight: 800, color: tokens.text }}>{sel.name}</span>
<RolePill role={sel.role} />
<span style={{
fontSize: 10.5, fontWeight: 700, padding: '1px 8px', borderRadius: 999, whiteSpace: 'nowrap',
color: tokens.textSub, background: tokens.accentBg, border: `1px solid ${tokens.border}`,
}}>{sel.useCase}</span>
</div>
<div style={{
marginTop: 8, padding: '7px 10px', borderRadius: 9,
background: tokens.accentBg, border: `1px solid ${tokens.borderAccent}`,
}}>
<span style={{ fontSize: 10.5, fontWeight: 800, color: tokens.accent, letterSpacing: '0.06em' }}>原表口径</span>
<div style={{ fontSize: 12, color: tokens.textSub, marginTop: 3, lineHeight: 1.5 }}>{sel.caliber}</div>
<div style={{ fontSize: 11, color: tokens.textMuted, marginTop: 4 }}>
审计字段:{sel.audit}
</div>
</div>
<div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column', gap: 8, marginTop: 10 }}>
{tablesOfDataset(sel.id).map(({ key, rows }) => (
<div key={key} style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column', gap: 5 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 7, flex: '0 0 auto' }}>
<span style={{ fontSize: 11.5, fontWeight: 800, color: tokens.text }}>{key}</span>
<span style={{ fontSize: 11, color: tokens.textMuted, fontVariantNumeric: 'tabular-nums' }}>{rows.length} 行</span>
<span style={{ flex: 1 }} />
<span style={{ fontSize: 10.5, color: tokens.textMuted }}>
表头带「现算」的是计算列,由代码按原表公式实时算出
</span>
</div>
<DataTable tableKey={key} rows={rows} />
</div>
))}
</div>
<div style={{ flex: '0 0 auto', marginTop: 8, fontSize: 10.5, color: tokens.textMuted, lineHeight: 1.5 }}>
来源 {QAP_SOURCE} · 比率类字段原表存小数(0.685 即 68.5%)· 计算列不落盘,统一由 calc 现算
</div>
</>
)}
</div>
</div>
</div>
);
}