初始化:质量数据分析平台源码
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Alert, Button, Select, Switch, Tag } from 'antd';
|
||||
import { api } from './api';
|
||||
|
||||
const labels: Record<string, string> = { MONTH: '月报', QUARTER: '季报', YEAR: '年报' };
|
||||
export function AnalysisReportsPanel({ periodType, periodKey, disabled, onOpen }: {
|
||||
periodType: string; periodKey: string; disabled: boolean; onOpen: (sessionId: string, turnId: string) => void;
|
||||
}) {
|
||||
const [reports, setReports] = useState<Record<string, unknown>[]>([]);
|
||||
const [plans, setPlans] = useState<Record<string, unknown>[]>([]);
|
||||
const [error, setError] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [runDay, setRunDay] = useState(1);
|
||||
const load = async () => {
|
||||
try {
|
||||
const [history, savedPlans] = await Promise.all([api.analysisReports(), api.analysisReportPlans()]);
|
||||
setReports(history.items || []); setPlans(savedPlans.items || []);
|
||||
} catch (value) { setError(value instanceof Error ? value.message : '报告读取失败'); }
|
||||
};
|
||||
useEffect(() => { void load(); }, []);
|
||||
const generate = async () => {
|
||||
setBusy(true); setError('');
|
||||
try {
|
||||
const response = await api.createAnalysisReport({ periodType, periodKey });
|
||||
await load();
|
||||
const { sessionId, turnId } = response.report;
|
||||
if (typeof sessionId === 'string' && typeof turnId === 'string') onOpen(sessionId, turnId);
|
||||
else setError('报告已登记,正在准备分析任务,请稍后刷新列表。');
|
||||
} catch (value) { setError(value instanceof Error ? value.message : '报告生成失败'); }
|
||||
finally { setBusy(false); }
|
||||
};
|
||||
const toggle = async (type: string, enabled: boolean) => {
|
||||
setBusy(true); setError('');
|
||||
try {
|
||||
const found = plans.find(plan => plan.periodType === type || plan.reportType === type);
|
||||
const body = { periodType: type, enabled, runDay, runHour: 9 };
|
||||
if (found) await api.updateAnalysisReportPlan(String(found.id), body);
|
||||
else await api.createAnalysisReportPlan(body);
|
||||
await load();
|
||||
} catch (value) { setError(value instanceof Error ? value.message : '报告计划保存失败'); }
|
||||
finally { setBusy(false); }
|
||||
};
|
||||
const saveDay = async () => {
|
||||
setBusy(true); setError('');
|
||||
try {
|
||||
await Promise.all(plans.map(plan => api.updateAnalysisReportPlan(String(plan.id), { runDay, runHour: 9 })));
|
||||
await load();
|
||||
} catch (value) { setError(value instanceof Error ? value.message : '执行日期保存失败'); }
|
||||
finally { setBusy(false); }
|
||||
};
|
||||
return <details className="analysis-reports"><summary>月报、季报与年报</summary>
|
||||
<p>报告使用统一指标与同一分析任务,包含期间比较、趋势和异常追查。自动计划在本机服务运行时执行。</p>
|
||||
{error && <Alert type="warning" showIcon message={error} />}
|
||||
<div className="ai-actions"><span>{periodKey} · {labels[periodType]} · 总体范围</span><Button disabled={disabled || busy} loading={busy} onClick={() => void generate()}>生成本期{labels[periodType]}</Button><Button disabled={busy} onClick={() => void load()}>刷新报告</Button></div>
|
||||
<div className="analysis-report-plans"><span>自动生成上一完整周期:每个适用周期首月</span><Select aria-label="自动报告执行日" value={runDay} onChange={setRunDay} options={Array.from({ length: 28 }, (_, i) => ({ label: `${i + 1}日 09:00`, value: i + 1 }))} />
|
||||
{Object.entries(labels).map(([type, label]) => { const plan = plans.find(item => item.periodType === type || item.reportType === type); return <label key={type}>{label} <Switch aria-label={`自动${label}`} disabled={busy} checked={plan?.enabled === true} onChange={value => void toggle(type, value)} /></label>; })}
|
||||
<Button size="small" disabled={busy || !plans.length} onClick={() => void saveDay()}>保存执行日期</Button>
|
||||
</div>
|
||||
{reports.length > 0 && <ul>{reports.slice(0, 8).map(report => <li key={String(report.id)}><span>{String(report.periodKey)} {labels[String(report.periodType)] || ''}</span> <Tag>{String(report.status)}</Tag><Button type="link" disabled={disabled || !report.sessionId || !report.turnId} onClick={() => onOpen(String(report.sessionId), String(report.turnId))}>查看分析</Button></li>)}</ul>}
|
||||
</details>;
|
||||
}
|
||||
Reference in New Issue
Block a user