import { useCallback, useEffect, useRef, useState } from 'react'; import { Alert, Button, Select, Switch, Tag } from 'antd'; import { api } from './api'; import { usePageCache, useMaintenanceRefresh } from './pageCache'; import { useDelayedBusy } from './useDelayedBusy'; const labels: Record = { 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[]>([]); const [plans, setPlans] = useState[]>([]); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); const [reportsLoaded, setReportsLoaded] = useState(false); const [plansLoaded, setPlansLoaded] = useState(false); const [expanded, setExpanded] = useState(false); const { active } = usePageCache(); const read = useDelayedBusy(); const actionInFlight = useRef(false); const [runDay, setRunDay] = useState(1); const load = useCallback(() => read.run(async () => { const [history, savedPlans] = await Promise.allSettled([api.analysisReports(), api.analysisReportPlans()]); const errors: string[] = []; if (history.status === 'fulfilled') { setReports(history.value.items || []); setReportsLoaded(true); } else errors.push(`报告列表:${history.reason instanceof Error ? history.reason.message : '读取失败'}`); if (savedPlans.status === 'fulfilled') { setPlans(savedPlans.value.items || []); setPlansLoaded(true); } else errors.push(`自动计划:${savedPlans.reason instanceof Error ? savedPlans.reason.message : '读取失败'}`); setError(errors.join(';')); }), [read.run]); useEffect(() => { if (active && expanded && (!reportsLoaded || !plansLoaded)) void load(); }, [active, expanded, reportsLoaded, plansLoaded, load]); useMaintenanceRefresh(() => { if (expanded) void load(); }); const generate = async () => { if (actionInFlight.current) return; actionInFlight.current = true; 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 { actionInFlight.current = false; setBusy(false); } }; const toggle = async (type: string, enabled: boolean) => { if (actionInFlight.current) return; actionInFlight.current = true; 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 { actionInFlight.current = false; setBusy(false); } }; const saveDay = async () => { if (actionInFlight.current) return; actionInFlight.current = true; 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 { actionInFlight.current = false; setBusy(false); } }; return
setExpanded(event.currentTarget.open)}>月报、季报与年报

报告使用统一指标与同一分析任务,包含期间比较、趋势和异常追查。自动计划在本机服务运行时执行。

{error && void load()}>重试 : undefined} />} {read.busy && !reportsLoaded && !plansLoaded &&
正在读取报告与自动计划…
}
{periodKey} · {labels[periodType]} · 总体范围
{plansLoaded &&
自动生成上一完整周期:每个适用周期首月