From c3cb493a1b2f470ce2ff55618943e8d42320742e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=9E=97=E9=93=96?= Date: Fri, 28 Aug 2026 09:34:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8C=89=E6=97=B6=E4=BA=A4?= =?UTF-8?q?=E4=BB=98=E7=BB=9F=E8=AE=A1=E5=B9=B6=E5=A2=9E=E5=8A=A0=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E7=AD=9B=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DESIGN.md | 1 + PRODUCT.md | 1 + .../department-dashboard/CoreMetricDetail.tsx | 101 +-------- .../OnTimeDeliveryInsights.test.tsx | 109 ++++++++++ .../OnTimeDeliveryInsights.tsx | 200 ++++++++++++++++++ lib/__tests__/ding-workflow-sync.test.ts | 10 + lib/ding-workflow-sync.ts | 30 +++ 7 files changed, 352 insertions(+), 100 deletions(-) create mode 100644 components/department-dashboard/OnTimeDeliveryInsights.test.tsx create mode 100644 components/department-dashboard/OnTimeDeliveryInsights.tsx diff --git a/DESIGN.md b/DESIGN.md index f923daa9..51d01c23 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -133,6 +133,7 @@ components: - 人员架构在 Department Shell 内直接复用真实组织工作面,并将组织树根节点限定为“品牌中心”。 - 素材资产库按权威组织归属选择外壳:品牌中心成员保留 Department Shell,外部普通用户只显示 64px 中性顶栏、素材工作面与退出入口。 - 进行中项目明细采用可键盘聚焦的横向滚动数据表:桌面固定“审批单标题”首列并保留 14 项业务字段,移动端转换为同字段的语义化定义列表;节点时间在数值下方标注对应流程节点名称。 +- 按时交付明细使用紧凑的三项流程筛选条;当前项用浅蓝底和蓝字表达,未选项悬停为灰底,切换时同步更新公式、数量和横向明细表。 ## Colors diff --git a/PRODUCT.md b/PRODUCT.md index 443b4bfe..b2f1a98c 100644 --- a/PRODUCT.md +++ b/PRODUCT.md @@ -32,6 +32,7 @@ web - 美术设计申请流程的 `ding_WorkflowTask.activityId` 按已确认流程定义映射为精确中文节点名并写入本地快照;未知节点继续使用同步字段推断或明确回退,不改写上游只读数据。 - 钉钉流程快照每小时从只读报表库的 `ding_Workflow`、`ding_WorkflowTask`、`ding_WorkflowScheme` 联合增量同步,只接收已批准的美术设计申请和内容视频流程;三表复合游标写入现有同步状态并继续复用同一批生产快照数据。 - “本月上新”只读取表单字段“需求类型”为“新品”的六品牌美术设计流程,不以流程标题关键词判断,也不计“优化”等其他需求类型;分子为本月整条流程已完结数量,分母为本月已完结数量与当前进行中数量之和;其中进行中流程直接复用“进行中项目”的美术设计流程状态、时间与当前品牌中心处理人条件。 +- “按时交付”只统计本月审批状态为已完结且审批结论未拒绝的美术设计与内容视频流程;详情页可在全部流程、美术设计流程和内容视频流程之间筛选,公式、数量与明细必须共用当前筛选口径。 - 除素材资产库、自动化工作流、人员架构、数据口径和已接入的中心指标外,其余板块现阶段只展示可理解、可扩展的模块框架,不虚构业务数据。 - 沿用项目现有 Next.js、NextAuth、Tailwind CSS 和权限体系。 diff --git a/components/department-dashboard/CoreMetricDetail.tsx b/components/department-dashboard/CoreMetricDetail.tsx index ad5e5b93..ce434be6 100644 --- a/components/department-dashboard/CoreMetricDetail.tsx +++ b/components/department-dashboard/CoreMetricDetail.tsx @@ -21,6 +21,7 @@ import type { import DepartmentShell from './DepartmentShell' import MonthlyLaunchesInsights from './MonthlyLaunchesInsights' import OngoingProjectsInsights from './OngoingProjectsInsights' +import OnTimeDeliveryInsightsView from './OnTimeDeliveryInsights' import OnTimeDeliveryRate from './OnTimeDeliveryRate' import type { CoreMetric } from './core-metrics' @@ -251,106 +252,6 @@ function PendingDataSource({ metric }: { metric: CoreMetric }) { ) } -function formatDuration(value: number | null, unit: 'day' | 'hour') { - if (value === null) return '—' - const amount = Number.isInteger(value) ? value.toFixed(0) : value.toFixed(2) - return `${amount} ${unit === 'day' ? '天' : '小时'}` -} - -function DeliveryStatusBadge({ status }: { status: 'on_time' | 'overtime' | 'pending' }) { - const styles = status === 'on_time' - ? 'bg-emerald-50 text-emerald-800' - : status === 'overtime' - ? 'bg-red-50 text-red-700' - : 'bg-amber-50 text-amber-800' - const label = status === 'on_time' ? '未超时' : status === 'overtime' ? '已超时' : '待计算' - return {label} -} - -function OnTimeDeliveryInsightsView({ insights }: { insights: OnTimeDeliveryInsights }) { - const { items, summary } = insights - - return ( -
-
-

统计公式

-

- 本月按时交付率 = 本月未超时完结流程数 ÷ 本月全部完结流程数 × 100% -

-

- 当前口径:{summary.onTime} ÷ {summary.total} = {formatRate(summary.rate)} · 超时 {summary.overtime} · 待计算 {summary.pending} -

-
- -
-
-

本月完结流程时效明细

-

- 美术按“品牌策划主管分配任务”至“品牌策划主管审核”计算;内容按规则计至“编导审核”或“内容主管审核”。 -

-
-

共 {summary.total} 条

-
- - {items.length === 0 ? ( -
-
- ) : ( -
- - - - - - - - - - - - - {items.map((item) => ( - - - - - - - - - ))} - -
流程发起人 / 部门计时节点标准时效实际用时判定
-

{item.title}

-

{item.businessId || item.id}

-

{item.projectType}

-
-

{item.initiatorName}

-

{item.initiatorDepartment}

-
-

{item.assessment.startNodeLabel}

-

{formatDate(item.assessment.startedAt, true)}

-

{item.assessment.endNodeLabel}

-

{formatDate(item.assessment.finishedAt, true)}

-
- {formatDuration(item.assessment.standardDuration, item.assessment.durationUnit)} - - {formatDuration(item.assessment.actualDuration, item.assessment.durationUnit)} - - - {item.assessment.status === 'pending' && ( -

缺少标准配置或关键节点时间

- )} -
-
- )} -
- ) -} - export default function CoreMetricDetail({ metric, query, projectInsights, monthlyLaunches, onTimeDelivery, firstPassPage }: CoreMetricDetailProps) { const hasRealProjectSource = metric.key === 'ongoing-projects' && projectInsights const hasMonthlyLaunchSource = metric.key === 'monthly-launches' && monthlyLaunches diff --git a/components/department-dashboard/OnTimeDeliveryInsights.test.tsx b/components/department-dashboard/OnTimeDeliveryInsights.test.tsx new file mode 100644 index 00000000..42bbcf7d --- /dev/null +++ b/components/department-dashboard/OnTimeDeliveryInsights.test.tsx @@ -0,0 +1,109 @@ +import { fireEvent, render, screen } from '@testing-library/react' +import type { OnTimeDeliveryWorkflowItem } from '@/lib/ding-workflow-sync' +import OnTimeDeliveryInsightsView from './OnTimeDeliveryInsights' + +const ART_PROCESS_CODE = 'PROC-3571C915-0506-4490-A91A-8AA7EA33F838' +const CONTENT_PROCESS_CODE = 'PROC-AE9FF3E1-A442-4E6A-85CF-0F2B86409673' + +function buildItem( + id: string, + processCode: string, + assessmentStatus: 'on_time' | 'overtime' | 'pending', +): OnTimeDeliveryWorkflowItem { + const isContent = processCode === CONTENT_PROCESS_CODE + return { + id, + businessId: `BIZ-${id}`, + title: isContent ? `内容流程-${id}` : `美术流程-${id}`, + status: 'COMPLETED', + originatorUserId: `user-${id}`, + originatorDeptName: '品牌中心', + sourceCreatedAt: '2026-08-01T00:00:00.000Z', + sourceUpdatedAt: '2026-08-03T00:00:00.000Z', + activeTaskCount: 0, + taskCount: 2, + schemeFieldCount: 3, + latestTaskStatus: 'COMPLETED', + processCode, + projectType: isContent ? '内容视频和图片拍摄' : '美术设计流程', + brand: '亿家达', + requesterName: '测试需求人', + requesterDepartment: '品牌中心', + initiatorName: '测试发起人', + initiatorDepartment: '运营中心A', + formFields: { + platform: null, + brand: null, + category: null, + spu: null, + demandType: null, + standardOutputDeadline: null, + sourceType: null, + taskType: null, + installVideo: null, + quantity: null, + cameraQuantity: null, + planningTaskStats: null, + graphicTaskStats: null, + imageTaskStats: null, + }, + assignmentNodeLabel: isContent ? '内容主管分配任务' : '品牌策划主管分配任务', + assignmentStartedAt: '2026-08-01T00:00:00.000Z', + reviewNodeLabel: isContent ? '编导审核' : '品牌策划主管审核', + reviewFinishedAt: '2026-08-03T00:00:00.000Z', + activeTasks: [], + result: 'AGREE', + sourceFinishedAt: '2026-08-03T00:00:00.000Z', + assessment: { + status: assessmentStatus, + standardDuration: 3, + durationUnit: isContent ? 'hour' : 'day', + actualDuration: assessmentStatus === 'pending' ? null : 2, + startedAt: '2026-08-01T00:00:00.000Z', + finishedAt: '2026-08-03T00:00:00.000Z', + startNodeLabel: isContent ? '内容主管分配任务' : '品牌策划主管分配任务', + endNodeLabel: isContent ? '编导审核' : '品牌策划主管审核', + }, + } +} + +describe('OnTimeDeliveryInsightsView', () => { + it('按全部、美术和内容流程切换公式、数量与明细', () => { + render( + , + ) + + expect(screen.getByText('当前口径:2 ÷ 3 = 66.7% · 超时 1 · 待计算 0')).toBeInTheDocument() + expect(screen.getByRole('tab', { name: '筛选全部流程,共3条' })).toHaveAttribute('aria-selected', 'true') + + fireEvent.click(screen.getByRole('tab', { name: '筛选内容视频流程,共1条' })) + expect(screen.getByText('当前口径:1 ÷ 1 = 100% · 超时 0 · 待计算 0')).toBeInTheDocument() + expect(screen.getByRole('tab', { name: '筛选内容视频流程,共1条' })).toHaveClass('bg-[#e7f3ff]') + expect(screen.getByText('内容流程-content-on-time')).toBeInTheDocument() + expect(screen.queryByText('美术流程-art-on-time')).not.toBeInTheDocument() + + fireEvent.click(screen.getByRole('tab', { name: '筛选美术设计流程,共2条' })) + expect(screen.getByText('当前口径:1 ÷ 2 = 50% · 超时 1 · 待计算 0')).toBeInTheDocument() + expect(screen.getByText('美术流程-art-overtime')).toBeInTheDocument() + expect(screen.queryByText('内容流程-content-on-time')).not.toBeInTheDocument() + }) +}) diff --git a/components/department-dashboard/OnTimeDeliveryInsights.tsx b/components/department-dashboard/OnTimeDeliveryInsights.tsx new file mode 100644 index 00000000..0d342017 --- /dev/null +++ b/components/department-dashboard/OnTimeDeliveryInsights.tsx @@ -0,0 +1,200 @@ +'use client' + +import { useMemo, useState } from 'react' +import { Database } from 'lucide-react' +import type { + OnTimeDeliveryInsights, + OnTimeDeliveryWorkflowItem, +} from '@/lib/ding-workflow-sync' + +const ART_PROCESS_CODE = 'PROC-3571C915-0506-4490-A91A-8AA7EA33F838' +const CONTENT_PROCESS_CODE = 'PROC-AE9FF3E1-A442-4E6A-85CF-0F2B86409673' + +type ProcessFilter = 'all' | 'art' | 'content' + +const PROCESS_FILTERS: Array<{ key: ProcessFilter; label: string }> = [ + { key: 'all', label: '全部流程' }, + { key: 'art', label: '美术设计流程' }, + { key: 'content', label: '内容视频流程' }, +] + +function formatDate(value: string | null) { + if (!value) return '—' + return new Intl.DateTimeFormat('zh-CN', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + hour12: false, + timeZone: 'Asia/Shanghai', + }).format(new Date(value)) +} + +function formatRate(rate: number | null) { + if (rate === null) return '—' + return `${Number.isInteger(rate) ? rate.toFixed(0) : rate.toFixed(1)}%` +} + +function formatDuration(value: number | null, unit: 'day' | 'hour') { + if (value === null) return '—' + const amount = Number.isInteger(value) ? value.toFixed(0) : value.toFixed(2) + return `${amount} ${unit === 'day' ? '天' : '小时'}` +} + +function matchesProcessFilter(item: OnTimeDeliveryWorkflowItem, filter: ProcessFilter) { + if (filter === 'art') return item.processCode === ART_PROCESS_CODE + if (filter === 'content') return item.processCode === CONTENT_PROCESS_CODE + return true +} + +function summarize(items: OnTimeDeliveryWorkflowItem[]) { + const onTime = items.filter((item) => item.assessment.status === 'on_time').length + const overtime = items.filter((item) => item.assessment.status === 'overtime').length + const pending = items.filter((item) => item.assessment.status === 'pending').length + const total = items.length + return { + onTime, + overtime, + pending, + total, + rate: total > 0 ? Math.round((onTime / total) * 1_000) / 10 : null, + } +} + +function DeliveryStatusBadge({ status }: { status: 'on_time' | 'overtime' | 'pending' }) { + const styles = status === 'on_time' + ? 'bg-emerald-50 text-emerald-800' + : status === 'overtime' + ? 'bg-red-50 text-red-700' + : 'bg-amber-50 text-amber-800' + const label = status === 'on_time' ? '未超时' : status === 'overtime' ? '已超时' : '待计算' + return {label} +} + +export default function OnTimeDeliveryInsightsView({ insights }: { insights: OnTimeDeliveryInsights }) { + const [activeFilter, setActiveFilter] = useState('all') + const { items, summary } = insights + const counts = useMemo(() => ({ + all: summary.total, + art: items.filter((item) => matchesProcessFilter(item, 'art')).length, + content: items.filter((item) => matchesProcessFilter(item, 'content')).length, + }), [items, summary.total]) + const filteredItems = useMemo( + () => items.filter((item) => matchesProcessFilter(item, activeFilter)), + [activeFilter, items], + ) + const filteredSummary = activeFilter === 'all' ? summary : summarize(filteredItems) + const activeLabel = PROCESS_FILTERS.find((filter) => filter.key === activeFilter)?.label || '全部流程' + + return ( +
+
+
+
+

统计公式

+

+ 本月按时交付率 = 本月未超时完结流程数 ÷ 本月全部完结流程数 × 100% +

+
+
+ {PROCESS_FILTERS.map((filter) => { + const selected = filter.key === activeFilter + return ( + + ) + })} +
+
+

+ 当前口径:{filteredSummary.onTime} ÷ {filteredSummary.total} = {formatRate(filteredSummary.rate)} · 超时 {filteredSummary.overtime} · 待计算 {filteredSummary.pending} +

+
+ +
+
+

本月完结流程时效明细

+

+ 仅统计审批状态为已完结且未被拒绝的流程。美术按“品牌策划主管分配任务”至“品牌策划主管审核”计算;内容按规则计至“编导审核”或“内容主管审核”。 +

+
+

{activeLabel} · 共 {filteredSummary.total} 条

+
+ +
+ {filteredItems.length === 0 ? ( +
+
+ ) : ( +
+ + + + + + + + + + + + + {filteredItems.map((item) => ( + + + + + + + + + ))} + +
流程发起人 / 部门计时节点标准时效实际用时判定
+

{item.title}

+

{item.businessId || item.id}

+

{item.processCode === CONTENT_PROCESS_CODE ? '内容视频流程' : item.projectType}

+
+

{item.initiatorName}

+

{item.initiatorDepartment}

+
+

{item.assessment.startNodeLabel}

+

{formatDate(item.assessment.startedAt)}

+

{item.assessment.endNodeLabel}

+

{formatDate(item.assessment.finishedAt)}

+
+ {formatDuration(item.assessment.standardDuration, item.assessment.durationUnit)} + + {formatDuration(item.assessment.actualDuration, item.assessment.durationUnit)} + + + {item.assessment.status === 'pending' && ( +

缺少标准配置或关键节点时间

+ )} +
+
+ )} +
+
+ ) +} diff --git a/lib/__tests__/ding-workflow-sync.test.ts b/lib/__tests__/ding-workflow-sync.test.ts index bd33e9fc..0b040c60 100644 --- a/lib/__tests__/ding-workflow-sync.test.ts +++ b/lib/__tests__/ding-workflow-sync.test.ts @@ -14,6 +14,7 @@ import { isDingWorkflowStartedOnOrAfterCutoff, isDingWorkflowOngoing, isDingWorkflowCompletedInRange, + isDingWorkflowApprovedCompletion, normalizeDingWorkflowBrand, normalizeDingWorkflowDepartment, normalizeDingWorkflowDetailValue, @@ -71,6 +72,15 @@ describe('ding workflow snapshot sync semantics', () => { expect(isDingWorkflowCompletedInRange('COMPLETED', null, range)).toBe(false) }) + it('only treats completed workflows without a rejected decision as approved completions', () => { + expect(isDingWorkflowApprovedCompletion('COMPLETED', 'AGREE', null)).toBe(true) + expect(isDingWorkflowApprovedCompletion('completed', null, 'NONE')).toBe(true) + expect(isDingWorkflowApprovedCompletion('COMPLETED', 'REFUSE', null)).toBe(false) + expect(isDingWorkflowApprovedCompletion('COMPLETED', '已拒绝', null)).toBe(false) + expect(isDingWorkflowApprovedCompletion('COMPLETED', null, 'reject')).toBe(false) + expect(isDingWorkflowApprovedCompletion('TERMINATED', 'AGREE', null)).toBe(false) + }) + it('only accepts the two approved process types', () => { expect(DING_WORKFLOW_ALLOWED_PROCESS_CODES).toEqual([ 'PROC-3571C915-0506-4490-A91A-8AA7EA33F838', diff --git a/lib/ding-workflow-sync.ts b/lib/ding-workflow-sync.ts index e7f2123f..947ae12f 100644 --- a/lib/ding-workflow-sync.ts +++ b/lib/ding-workflow-sync.ts @@ -12,6 +12,18 @@ import { prisma } from '@/lib/prisma' const SYNC_STATE_ID = 'ding-workflow-hourly' const TERMINAL_WORKFLOW_STATUSES = new Set(['COMPLETED', 'TERMINATED', 'CANCELED']) +const REJECTED_WORKFLOW_DECISIONS = new Set([ + 'REFUSE', + 'REFUSED', + 'REJECT', + 'REJECTED', + 'DISAGREE', + 'DENY', + 'DENIED', + '拒绝', + '已拒绝', + '不同意', +]) const LEASE_DURATION_MS = 20 * 60 * 1000 const DEFAULT_SYNC_INTERVAL_MS = 60 * 60 * 1000 const INITIAL_SYNC_DELAY_MS = 5_000 @@ -684,6 +696,22 @@ export function isDingWorkflowCompletedInRange( return finishedAtMs >= range.start.getTime() && finishedAtMs < range.end.getTime() } +/** + * 钉钉会把审批结束状态和审批结论分开保存:被拒绝的流程也可能处于 COMPLETED。 + * 按时交付只统计已完结且没有明确拒绝结论的流程,避免拒绝单进入分母。 + */ +export function isDingWorkflowApprovedCompletion( + status: unknown, + result: unknown, + bizAction: unknown, +) { + if (String(status || '').trim().toUpperCase() !== 'COMPLETED') return false + return ![result, bizAction].some((value) => { + const decision = String(value || '').trim().toUpperCase() + return decision ? REJECTED_WORKFLOW_DECISIONS.has(decision) : false + }) +} + export function isDingWorkflowStartedOnOrAfterCutoff(value: unknown): boolean { const startedAt = sourceWorkflowTimestampToIso(value) return Boolean(startedAt && startedAt >= DING_WORKFLOW_STARTED_AT_CUTOFF_ISO) @@ -1489,6 +1517,7 @@ const ONGOING_WORKFLOW_SELECT = { const MONTHLY_LAUNCH_WORKFLOW_SELECT = { ...ONGOING_WORKFLOW_SELECT, result: true, + bizAction: true, sourceFinishedAtRaw: true, } as const @@ -1803,6 +1832,7 @@ async function readOnTimeDeliveryInsightsWithoutAccessCheck(now = new Date()): P ]) const rows = candidates.filter((row) => ( isDingWorkflowCompletedInRange(row.status, row.sourceFinishedAtRaw, range) + && isDingWorkflowApprovedCompletion(row.status, row.result, row.bizAction) )) const initiatorDirectory = await readInitiatorDirectory(rows) const items = rows.map((row): OnTimeDeliveryWorkflowItem => {