diff --git a/furniture-3d-system/src/app/intake/oa-board/OaBoardClient.tsx b/furniture-3d-system/src/app/intake/oa-board/OaBoardClient.tsx index ad7e25f2..f80cd5c0 100644 --- a/furniture-3d-system/src/app/intake/oa-board/OaBoardClient.tsx +++ b/furniture-3d-system/src/app/intake/oa-board/OaBoardClient.tsx @@ -17,7 +17,12 @@ import { Sparkles, X, } from 'lucide-react' -import { pickCurrentOverdueStageMetric } from '@/lib/oa-board-overtime' +import { + getOaBoardFollowupDeadlineStatus, + listHistoricalOverdueStageMetrics, + OA_BOARD_APPROVAL_STAGE_TIMELINE_METRIC_KEYS, + pickCurrentOverdueStageMetric, +} from '@/lib/oa-board-overtime' import { OA_BOARD_DEFAULT_TRACKED_STATUSES } from '@/server/services/oa-board.constants' import type { OaBoardDashboardDTO, @@ -139,6 +144,16 @@ type OvertimeStage = { overtimeDays: number } +type HistoricalOvertimeRecord = { + id: string + boardKey: string + openingCode: string | null + spu: string | null + stage: OvertimeStage + followup: OaBoardTimeoutFollowup | null + item: OaBoardProjectListItemDTO | null +} + type TimeoutFollowupSaveEntry = { stage: OvertimeStage draft: TimeoutFollowupDraft @@ -752,6 +767,23 @@ function getOvertimeStages(item: OaBoardProjectListItemDTO): OvertimeStage[] { }] } +function getHistoricalOvertimeStages(item: OaBoardProjectListItemDTO): OvertimeStage[] { + const currentStageNames = splitStageText( + item.formSummary.approvalStageDisplay || item.currentApprovalStageName, + ) + + return listHistoricalOverdueStageMetrics(currentStageNames, item.timelineMetrics).map((stage) => ({ + stageKey: stage.metricKey, + stageLabel: stage.metricLabel, + stageContextLabel: '历史超时', + nodeName: '-', + ownerLabel: '-', + targetDays: stage.targetDays, + actualDays: stage.actualDays, + overtimeDays: stage.overtimeDays, + })) +} + function resolveTimeoutFollowupOwner(item: OaBoardProjectListItemDTO, stageKey: string) { const stage = getOvertimeStages(item).find((entry) => entry.stageKey === stageKey) if (stage?.ownerLabel?.trim()) return stage.ownerLabel.trim() @@ -763,6 +795,20 @@ function getTimeoutFollowupStatusLabel(status: TimeoutFollowupStatus) { return TIMEOUT_FOLLOWUP_STATUS_OPTIONS.find((option) => option.value === status)?.label ?? '待填写' } +function getFollowupDeadlineStatus(followup: OaBoardTimeoutFollowup | null | undefined) { + if (!followup) return null + + return getOaBoardFollowupDeadlineStatus( + followup.expectedResolveDate, + followup.status === 'RESOLVED' || followup.status === 'NOT_REQUIRED', + ) +} + +function getFollowupDeadlineLabel(status: ReturnType) { + if (!status) return null + return status.state === 'DUE_TODAY' ? '今日到期' : `已逾期 ${status.overdueDays} 天` +} + function createTimeoutFollowupDraft( followup: OaBoardTimeoutFollowup | undefined, defaultOwner: string, @@ -791,22 +837,103 @@ function buildHistoricalFollowupStage(followup: OaBoardTimeoutFollowup): Overtim function getFollowupDisplayStages( currentOvertimeStages: OvertimeStage[], + historicalOvertimeStages: OvertimeStage[], followups: OaBoardTimeoutFollowup[], ) { - const currentStageKeys = new Set(currentOvertimeStages.map((stage) => stage.stageKey)) - const historicalStages = followups - .filter((followup) => !currentStageKeys.has(followup.stageKey)) - .map(buildHistoricalFollowupStage) + const stagesByKey = new Map(currentOvertimeStages.map((stage) => [stage.stageKey, stage])) - return [...currentOvertimeStages, ...historicalStages] + historicalOvertimeStages.forEach((stage) => { + if (!stagesByKey.has(stage.stageKey)) stagesByKey.set(stage.stageKey, stage) + }) + + followups.forEach((followup) => { + if (!stagesByKey.has(followup.stageKey)) { + stagesByKey.set(followup.stageKey, buildHistoricalFollowupStage(followup)) + } + }) + + return Array.from(stagesByKey.values()) +} + +function buildHistoricalOvertimeRecords( + items: OaBoardProjectListItemDTO[], + followups: OaBoardTimeoutFollowup[], +): HistoricalOvertimeRecord[] { + const itemByBoardKey = new Map(items.map((item) => [item.boardKey, item])) + const followupByStageKey = new Map( + followups.map((followup) => [`${followup.boardKey}:${followup.stageKey}`, followup]), + ) + const records = new Map() + + items.forEach((item) => { + getHistoricalOvertimeStages(item).forEach((stage) => { + const id = `${item.boardKey}:${stage.stageKey}` + records.set(id, { + id, + boardKey: item.boardKey, + openingCode: item.formSummary.openingCode || item.openingCode || null, + spu: item.formSummary.spu || item.spu || null, + stage, + followup: followupByStageKey.get(id) ?? null, + item, + }) + }) + }) + + followups.forEach((followup) => { + const id = `${followup.boardKey}:${followup.stageKey}` + if (records.has(id)) return + + const item = itemByBoardKey.get(followup.boardKey) + const currentMetricKeys = new Set( + splitStageText(item?.formSummary.approvalStageDisplay || item?.currentApprovalStageName) + .map((stageName) => OA_BOARD_APPROVAL_STAGE_TIMELINE_METRIC_KEYS[stageName]) + .filter((metricKey): metricKey is string => Boolean(metricKey)), + ) + const currentStageKeys = new Set(item ? getOvertimeStages(item).map((stage) => stage.stageKey) : []) + if (currentStageKeys.has(followup.stageKey) || currentMetricKeys.has(followup.stageKey)) return + + records.set(id, { + id, + boardKey: followup.boardKey, + openingCode: followup.openingCode, + spu: followup.spu, + stage: buildHistoricalFollowupStage(followup), + followup, + item: item ?? null, + }) + }) + + return Array.from(records.values()).sort((left, right) => { + const leftUpdatedAt = left.followup?.updatedAt ?? '' + const rightUpdatedAt = right.followup?.updatedAt ?? '' + if (leftUpdatedAt !== rightUpdatedAt) return rightUpdatedAt.localeCompare(leftUpdatedAt) + return right.stage.overtimeDays - left.stage.overtimeDays + }) } function getTimeoutFollowupSummary( overtimeStages: OvertimeStage[], followups: OaBoardTimeoutFollowup[], ) { + const relevantFollowups = + overtimeStages.length === 0 + ? followups + : followups.filter((followup) => + overtimeStages.some((stage) => stage.stageKey === followup.stageKey), + ) + const allFollowupDeadlineStatuses = relevantFollowups + .map((followup) => getFollowupDeadlineStatus(followup)) + .filter((status): status is NonNullable => Boolean(status)) + if (overtimeStages.length === 0) { if (followups.length === 0) return null + if (allFollowupDeadlineStatuses.length > 0) { + return { + label: `${allFollowupDeadlineStatuses.length} 项跟进到期`, + tone: 'danger' as const, + } + } return { label: `${followups.length} 项历史跟进`, tone: 'default' as const } } @@ -818,6 +945,12 @@ function getTimeoutFollowupSummary( (record) => record.status === 'RESOLVED' || record.status === 'NOT_REQUIRED', ).length + if (allFollowupDeadlineStatuses.length > 0) { + return { + label: `${allFollowupDeadlineStatuses.length} 项跟进到期`, + tone: 'danger' as const, + } + } if (pending > 0) return { label: `${pending} 项待填写`, tone: 'danger' as const } if (inProgress > 0) return { label: `${inProgress} 项处理中`, tone: 'warning' as const } if (resolved === overtimeStages.length) return { label: '已完成跟进', tone: 'success' as const } @@ -1263,57 +1396,71 @@ function HistoricalTimeoutAnalysis({ const [ownerFilter, setOwnerFilter] = useState('') const [updatedDateStart, setUpdatedDateStart] = useState('') const [updatedDateEnd, setUpdatedDateEnd] = useState('') - const itemByBoardKey = useMemo( - () => new Map(items.map((item) => [item.boardKey, item])), - [items], + const historicalRecords = useMemo( + () => buildHistoricalOvertimeRecords(items, followups), + [followups, items], ) const stageOptions = useMemo( - () => buildOptionList(followups.map((followup) => followup.stageLabel), '全部历史阶段'), - [followups], + () => buildOptionList(historicalRecords.map((record) => record.stage.stageLabel), '全部历史阶段'), + [historicalRecords], ) const ownerOptions = useMemo( - () => buildOptionList(followups.map((followup) => followup.followUpOwner), '全部跟进人'), - [followups], + () => buildOptionList(historicalRecords.map((record) => record.followup?.followUpOwner), '全部跟进人'), + [historicalRecords], ) const statusOptions = useMemo( - () => [{ value: '', label: '全部跟进状态' }, ...TIMEOUT_FOLLOWUP_STATUS_OPTIONS], + () => [ + { value: '', label: '全部跟进状态' }, + { value: 'UNRECORDED', label: '未填写跟进' }, + { value: 'DEADLINE_DUE', label: '跟进已到期' }, + ...TIMEOUT_FOLLOWUP_STATUS_OPTIONS, + ], [], ) - const filteredFollowups = useMemo( + const filteredRecords = useMemo( () => - followups.filter((followup) => { - const updatedDate = String(followup.updatedAt ?? '').slice(0, 10) - if (statusFilter && followup.status !== statusFilter) return false - if (stageFilter && followup.stageLabel !== stageFilter) return false - if (ownerFilter && followup.followUpOwner !== ownerFilter) return false + historicalRecords.filter((record) => { + const updatedDate = String(record.followup?.updatedAt ?? '').slice(0, 10) + if (statusFilter === 'UNRECORDED' && record.followup) return false + if (statusFilter === 'DEADLINE_DUE' && !getFollowupDeadlineStatus(record.followup)) return false + if ( + statusFilter && + statusFilter !== 'UNRECORDED' && + statusFilter !== 'DEADLINE_DUE' && + record.followup?.status !== statusFilter + ) return false + if (stageFilter && record.stage.stageLabel !== stageFilter) return false + if (ownerFilter && record.followup?.followUpOwner !== ownerFilter) return false if (updatedDateStart && updatedDate < updatedDateStart) return false if (updatedDateEnd && updatedDate > updatedDateEnd) return false return true }), - [followups, ownerFilter, stageFilter, statusFilter, updatedDateEnd, updatedDateStart], + [historicalRecords, ownerFilter, stageFilter, statusFilter, updatedDateEnd, updatedDateStart], ) const historySummary = useMemo(() => { - const totalOvertimeDays = filteredFollowups.reduce( - (total, followup) => total + followup.overtimeDays, + const totalOvertimeDays = filteredRecords.reduce( + (total, record) => total + record.stage.overtimeDays, 0, ) - const pending = filteredFollowups.filter( - (followup) => followup.status === 'PENDING' || followup.status === 'IN_PROGRESS', + const pending = filteredRecords.filter( + (record) => !record.followup || record.followup.status === 'PENDING' || record.followup.status === 'IN_PROGRESS', ).length - const resolved = filteredFollowups.filter( - (followup) => followup.status === 'RESOLVED' || followup.status === 'NOT_REQUIRED', + const resolved = filteredRecords.filter( + (record) => record.followup?.status === 'RESOLVED' || record.followup?.status === 'NOT_REQUIRED', ).length + const due = filteredRecords.filter((record) => Boolean(getFollowupDeadlineStatus(record.followup))).length return { - total: filteredFollowups.length, + total: filteredRecords.length, pending, resolved, + due, averageOvertimeDays: - filteredFollowups.length > 0 - ? Number((totalOvertimeDays / filteredFollowups.length).toFixed(1)) + filteredRecords.length > 0 + ? Number((totalOvertimeDays / filteredRecords.length).toFixed(1)) : 0, } - }, [filteredFollowups]) + }, [filteredRecords]) return (
@@ -1327,9 +1474,9 @@ function HistoricalTimeoutAnalysis({
-
+
-

历史跟进项

+

历史超时项

{historySummary.total}

@@ -1340,6 +1487,10 @@ function HistoricalTimeoutAnalysis({

已完成

{historySummary.resolved}

+
+

跟进已到期

+

{historySummary.due}

+

平均超时

@@ -1353,11 +1504,11 @@ function HistoricalTimeoutAnalysis({

历史超时跟进

-

记录来源于已保存的超时跟进,不参与当前超时统计和钉钉待办发送。

+

自动保留已发生的超时;仅当前阶段参与当前超时统计和钉钉待办发送。

- {filteredFollowups.length} 条记录 + {filteredRecords.length} 条记录
- {filteredFollowups.length === 0 ? ( + {filteredRecords.length === 0 ? (
当前筛选范围内没有历史跟进记录
) : (
@@ -1377,23 +1528,38 @@ function HistoricalTimeoutAnalysis({ - {filteredFollowups.map((followup) => { - const item = itemByBoardKey.get(followup.boardKey) + {filteredRecords.map((record) => { + const followup = record.followup + const deadlineStatus = getFollowupDeadlineStatus(followup) return ( - - {formatCellText(followup.openingCode || item?.formSummary.openingCode || item?.openingCode)} - {followup.stageLabel} - - {formatCellText(followup.followUpOwner)} - {followup.targetDays} 天 - {followup.actualDays} 天 - {followup.overtimeDays} 天 - {formatCellText(followup.expectedResolveDate)} - {formatCellText(followup.updatedAt?.replace('T', ' ').slice(0, 16))} + + {formatCellText(record.openingCode)} + {record.stage.stageLabel} - {item ? ( - ) : '-'} @@ -1455,7 +1621,12 @@ function TimeoutFollowupInspector({ onSave: (entries: TimeoutFollowupSaveEntry[]) => Promise }) { const currentOvertimeStages = getOvertimeStages(item) - const overtimeStages = getFollowupDisplayStages(currentOvertimeStages, followups) + const historicalOvertimeStages = getHistoricalOvertimeStages(item) + const overtimeStages = getFollowupDisplayStages( + currentOvertimeStages, + historicalOvertimeStages, + followups, + ) const currentOvertimeStageKeys = new Set(currentOvertimeStages.map((stage) => stage.stageKey)) const [selectedStageKey, setSelectedStageKey] = useState(overtimeStages[0]?.stageKey ?? '') const selectedStage = @@ -1463,6 +1634,7 @@ function TimeoutFollowupInspector({ const selectedFollowup = selectedStage ? followups.find((followup) => followup.stageKey === selectedStage.stageKey) : undefined + const selectedFollowupDeadlineStatus = getFollowupDeadlineStatus(selectedFollowup) const [draftsByStageKey, setDraftsByStageKey] = useState>(() => Object.fromEntries( overtimeStages.map((stage) => [ @@ -1555,6 +1727,7 @@ function TimeoutFollowupInspector({
{overtimeStages.map((stage) => { const record = followups.find((followup) => followup.stageKey === stage.stageKey) + const deadlineStatus = getFollowupDeadlineStatus(record) const active = selectedStage.stageKey === stage.stageKey const isDirty = dirtyStageKeys.has(stage.stageKey) const isHistorical = !currentOvertimeStageKeys.has(stage.stageKey) @@ -1575,6 +1748,11 @@ function TimeoutFollowupInspector({ {isHistorical ? '历史记录' : isDirty ? '待保存' : record ? getTimeoutFollowupStatusLabel(record.status) : '待跟进'} + {deadlineStatus ? ( + + {getFollowupDeadlineLabel(deadlineStatus)} + + ) : null} ) })} @@ -1599,7 +1777,10 @@ function TimeoutFollowupInspector({ {isHistoricalStage ? (
-

该节点已不在当前超时阶段,以下为已保存的历史跟进内容。

+

+ 该阶段已离开当前节点,因此只作为历史超时保留,不会再触发钉钉待办。 + {selectedFollowup ? ' 以下为已保存的历史跟进内容。' : ' 该阶段尚未填写历史跟进记录。'} +

跟进状态
@@ -1611,7 +1792,12 @@ function TimeoutFollowupInspector({
预计完成日期
-
{formatCellText(selectedFollowup?.expectedResolveDate)}
+
+ {formatCellText(selectedFollowup?.expectedResolveDate)} + {selectedFollowupDeadlineStatus ? ( + {getFollowupDeadlineLabel(selectedFollowupDeadlineStatus)} + ) : null} +
@@ -1656,6 +1842,11 @@ function TimeoutFollowupInspector({ onChange={(value) => updateDraft((current) => ({ ...current, expectedResolveDate: value }))} />
+ {selectedFollowupDeadlineStatus ? ( +
+ {getFollowupDeadlineLabel(selectedFollowupDeadlineStatus)},请更新跟进状态或调整预计完成日期。 +
+ ) : null}