按发起人展示进行中项目维度

This commit is contained in:
江林铖 2026-08-25 18:45:45 +08:00
parent dcaa11ce35
commit 586d4cd147
4 changed files with 203 additions and 27 deletions

View File

@ -24,6 +24,8 @@ describe('CoreMetricDetail', () => {
brand: '亿家达',
requesterName: '林需求',
requesterDepartment: '品牌中心',
initiatorName: '荀瑶',
initiatorDepartment: '运营中心A-事业四部-品类九部-淘系四组',
originatorUserId: 'ding-1',
originatorDeptName: '品牌中心',
sourceCreatedAt: '2026-08-10T00:00:00.000Z',
@ -58,6 +60,16 @@ describe('CoreMetricDetail', () => {
fireEvent.click(screen.getByRole('tab', { name: /项目负责人/ }))
expect(screen.getByText('负责人按当前未完结节点的处理人统计;并行处理会分别计入对应人员。')).toBeInTheDocument()
expect(screen.getAllByText('陈负责人').length).toBeGreaterThan(0)
fireEvent.click(screen.getByRole('tab', { name: /发起人维度/ }))
expect(screen.getByText('发起人进行中数量')).toBeInTheDocument()
expect(screen.getByText('发起人按钉钉唯一标识匹配人员架构;所在部门以 HR 花名册为准。')).toBeInTheDocument()
expect(screen.getByRole('columnheader', { name: '排名' })).toBeInTheDocument()
expect(screen.getByRole('columnheader', { name: '发起人' })).toBeInTheDocument()
expect(screen.getByRole('columnheader', { name: '所在部门' })).toBeInTheDocument()
expect(screen.getByRole('columnheader', { name: '进行中数量' })).toBeInTheDocument()
expect(screen.getAllByText('荀瑶').length).toBeGreaterThan(0)
expect(screen.getAllByText('运营中心A-事业四部-品类九部-淘系四组').length).toBeGreaterThan(0)
})
it('未接入的指标只展示诚实的明细框架', () => {

View File

@ -3,7 +3,6 @@
import { useMemo, useState } from 'react'
import {
BriefcaseBusiness,
Building2,
ChevronDown,
Layers3,
Search,
@ -16,7 +15,7 @@ import type {
OngoingDingWorkflowListItem,
} from '@/lib/ding-workflow-sync'
type Perspective = 'brand' | 'owner' | 'requester'
type Perspective = 'brand' | 'owner' | 'initiator'
type ProjectGroup = {
key: string
@ -36,7 +35,7 @@ const PERSPECTIVES: Array<{
}> = [
{ key: 'brand', label: '品牌维度', description: '按品牌查看进行中数量、任务类型与当前节点', icon: Layers3 },
{ key: 'owner', label: '项目负责人', description: '项目负责人按当前节点处理人认定', icon: UsersRound },
{ key: 'requester', label: '需求方维度', description: '按需求部门查看进行中项目类型与明细', icon: Building2 },
{ key: 'initiator', label: '发起人维度', description: '按发起人及人员架构所在部门查看项目', icon: UserRound },
]
function formatDate(value: string | null) {
@ -80,6 +79,8 @@ function projectMatchesQuery(project: OngoingDingWorkflowListItem, query: string
project.projectType,
project.requesterName,
project.requesterDepartment,
project.initiatorName,
project.initiatorDepartment,
...project.activeTasks.flatMap((task) => [task.assigneeName, task.nodeName, task.taskId]),
].some((value) => String(value || '').toLocaleLowerCase('zh-CN').includes(normalized))
}
@ -147,19 +148,23 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
}))
.sort((a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN'))
}
const requesters = new Map<string, Set<string>>()
const initiators = new Map<string, { label: string; department: string; projectIds: Set<string> }>()
insights.items.forEach((project) => {
const department = project.requesterDepartment || '未记录部门'
const projects = requesters.get(department) || new Set<string>()
projects.add(project.id)
requesters.set(department, projects)
const key = project.originatorUserId?.trim() || `name:${project.initiatorName}`
const current = initiators.get(key) || {
label: project.initiatorName || '未记录',
department: project.initiatorDepartment || '未记录部门',
projectIds: new Set<string>(),
}
current.projectIds.add(project.id)
initiators.set(key, current)
})
return Array.from(requesters.entries())
.map(([department, projectIds]) => ({
key: department,
label: department,
description: '需求部门',
count: projectIds.size,
return Array.from(initiators.entries())
.map(([key, initiator]) => ({
key,
label: initiator.label,
description: initiator.department,
count: initiator.projectIds.size,
}))
.sort((a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN'))
}, [insights.items, perspective])
@ -175,7 +180,9 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
humanOwnerTasks(project).some((task) => ownerKey(task) === selectedGroup.key)
))
}
return insights.items.filter((project) => project.requesterDepartment === selectedGroup.key)
return insights.items.filter((project) => (
(project.originatorUserId?.trim() || `name:${project.initiatorName}`) === selectedGroup.key
))
}, [insights.items, perspective, selectedGroup])
const tasksForSelectedOwner = (project: OngoingDingWorkflowListItem) => (
@ -243,7 +250,7 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
<div>
<h2 id="project-insights-title" className="text-xl font-bold tracking-[-0.02em] sm:text-2xl">进行中项目分析</h2>
<p className="mt-1 max-w-3xl text-sm leading-6 text-slate-600">
从品牌、当前节点处理人和需求部门三个维度下钻;所有数字均来自每小时同步、且发起时间不早于 2026 年 4 月 1 日的未完结流程快照。
从品牌、当前节点处理人和发起人三个维度下钻;所有数字均来自每小时同步、且发起时间不早于 2026 年 4 月 1 日的未完结流程快照。
</p>
</div>
<p className="text-sm font-semibold tabular-nums text-slate-600">共 {insights.total} 个进行中项目</p>
@ -275,15 +282,81 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
<div className="mt-6 overflow-hidden rounded-xl border border-slate-200 bg-white">
<div className="border-b border-slate-200 px-5 py-4 sm:px-6">
<h3 className="font-bold text-slate-950">
{perspective === 'brand' ? '品牌进行中数量' : perspective === 'owner' ? '项目负责人(当前处理人)' : '需求部门进行中数量'}
{perspective === 'brand' ? '品牌进行中数量' : perspective === 'owner' ? '项目负责人(当前处理人)' : '发起人进行中数量'}
</h3>
<p className="mt-1 text-sm text-slate-500">
{perspective === 'owner' ? '负责人按当前未完结节点的处理人统计;并行处理会分别计入对应人员。' : '选择一项查看任务类型、当前节点与项目明细。'}
{perspective === 'owner'
? '负责人按当前未完结节点的处理人统计;并行处理会分别计入对应人员。'
: perspective === 'initiator'
? '发起人按钉钉唯一标识匹配人员架构;所在部门以 HR 花名册为准。'
: '选择一项查看任务类型、当前节点与项目明细。'}
</p>
</div>
{groups.length === 0 ? (
<div className="px-5 py-12 text-center text-sm text-slate-500">当前没有可匹配的品牌中心节点处理人。</div>
<div className="px-5 py-12 text-center text-sm text-slate-500">当前没有可匹配的数据。</div>
) : perspective === 'initiator' ? (
<>
<div className="max-h-[520px] overflow-y-auto md:hidden">
<ol className="divide-y divide-slate-200" aria-label="发起人进行中项目排名">
{groups.map((group, index) => {
const selected = selectedGroup?.key === group.key
return (
<li key={group.key}>
<button
type="button"
aria-pressed={selected}
className={`grid min-h-20 w-full grid-cols-[2rem_1fr_auto] items-center gap-3 px-5 py-3 text-left transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[#5a9ce6] ${selected ? 'bg-[#e6f4ff]' : 'hover:bg-[#eef2f6]'}`}
onClick={() => selectGroup(group.key)}
>
<span className="text-sm font-bold tabular-nums text-slate-500">{index + 1}</span>
<span className="min-w-0">
<span className={`block font-bold ${selected ? 'text-[#0050cb]' : 'text-slate-950'}`}>{group.label}</span>
<span className="mt-1 block text-xs leading-5 text-slate-500">{group.description}</span>
</span>
<span className={`text-2xl font-bold tabular-nums ${selected ? 'text-[#0050cb]' : 'text-slate-950'}`}>{group.count}</span>
</button>
</li>
)
})}
</ol>
</div>
<div className="hidden max-h-[520px] overflow-y-auto md:block">
<table className="w-full text-left text-sm">
<caption className="sr-only">发起人进行中项目排名</caption>
<thead className="sticky top-0 z-10 bg-slate-50 text-xs font-bold text-slate-500">
<tr>
<th scope="col" className="w-20 px-6 py-3.5">排名</th>
<th scope="col" className="w-60 px-4 py-3.5">发起人</th>
<th scope="col" className="px-4 py-3.5">所在部门</th>
<th scope="col" className="w-32 px-6 py-3.5 text-right">进行中数量</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-200">
{groups.map((group, index) => {
const selected = selectedGroup?.key === group.key
return (
<tr key={group.key} className={selected ? 'bg-[#e6f4ff]' : 'hover:bg-[#eef2f6]'}>
<td className="px-6 py-3.5 font-bold tabular-nums text-slate-500">{index + 1}</td>
<td className="px-4 py-3.5">
<button
type="button"
aria-pressed={selected}
className={`min-h-11 rounded-lg px-3 text-left font-bold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${selected ? 'text-[#0050cb]' : 'text-slate-950 hover:text-[#0050cb]'}`}
onClick={() => selectGroup(group.key)}
>
{group.label}
</button>
</td>
<td className="px-4 py-3.5 leading-6 text-slate-700">{group.description}</td>
<td className={`px-6 py-3.5 text-right text-xl font-bold tabular-nums ${selected ? 'text-[#0050cb]' : 'text-slate-950'}`}>{group.count}</td>
</tr>
)
})}
</tbody>
</table>
</div>
</>
) : (
<div className="max-h-[430px] overflow-y-auto p-3 sm:p-4">
<div className="grid gap-2 sm:grid-cols-2 xl:grid-cols-3">
@ -487,7 +560,7 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
<th scope="col" className="px-5 py-3.5">项目 / 任务编号</th>
<th scope="col" className="px-4 py-3.5">品牌</th>
<th scope="col" className="px-4 py-3.5">任务类型</th>
<th scope="col" className="px-4 py-3.5">发起人 / 需求部门</th>
<th scope="col" className="px-4 py-3.5">发起人 / 所在部门</th>
<th scope="col" className="px-4 py-3.5">当前节点 / 处理人</th>
<th scope="col" className="px-5 py-3.5">发起 / 更新</th>
</tr>
@ -502,8 +575,8 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
<td className="px-4 py-4 font-bold text-slate-800">{project.brand}</td>
<td className="px-4 py-4 text-slate-700">{project.projectType}</td>
<td className="max-w-[220px] px-4 py-4">
<p className="font-bold text-slate-800">{project.requesterName}</p>
<p className="mt-1 text-xs leading-5 text-slate-500">{project.requesterDepartment}</p>
<p className="font-bold text-slate-800">{project.initiatorName}</p>
<p className="mt-1 text-xs leading-5 text-slate-500">{project.initiatorDepartment}</p>
</td>
<td className="max-w-[240px] px-4 py-4"><CurrentTaskSummary project={project} tasks={tasksForSelectedOwner(project)} /></td>
<td className="px-5 py-4 text-xs leading-5 tabular-nums text-slate-600">
@ -528,8 +601,8 @@ export default function OngoingProjectsInsights({ insights }: { insights: Ongoin
</div>
<dl className="mt-4 grid gap-4 text-sm sm:grid-cols-2 lg:grid-cols-3">
<div><dt className="text-xs text-slate-500">任务类型</dt><dd className="mt-1 font-bold text-slate-800">{project.projectType}</dd></div>
<div><dt className="text-xs text-slate-500">发起人</dt><dd className="mt-1 font-bold text-slate-800">{project.requesterName}</dd></div>
<div><dt className="text-xs text-slate-500">需求部门</dt><dd className="mt-1 font-bold text-slate-800">{project.requesterDepartment}</dd></div>
<div><dt className="text-xs text-slate-500">发起人</dt><dd className="mt-1 font-bold text-slate-800">{project.initiatorName}</dd></div>
<div><dt className="text-xs text-slate-500">所在部门</dt><dd className="mt-1 font-bold text-slate-800">{project.initiatorDepartment}</dd></div>
<div><dt className="text-xs text-slate-500">当前节点 / 处理人</dt><dd className="mt-1"><CurrentTaskSummary project={project} tasks={tasksForSelectedOwner(project)} /></dd></div>
<div><dt className="text-xs text-slate-500">发起时间</dt><dd className="mt-1 tabular-nums text-slate-700">{formatDate(project.sourceCreatedAt)}</dd></div>
<div><dt className="text-xs text-slate-500">最近更新</dt><dd className="mt-1 tabular-nums text-slate-700">{formatDate(project.sourceUpdatedAt)}</dd></div>

View File

@ -9,6 +9,7 @@ import {
normalizeDingWorkflowBrand,
normalizeDingWorkflowDepartment,
normalizeDingWorkflowProcessCode,
resolveDingWorkflowInitiator,
sourceWorkflowTimestampToIso,
} from '@/lib/ding-workflow-sync'
@ -94,4 +95,28 @@ describe('ding workflow snapshot sync semantics', () => {
expect(normalizeDingWorkflowDepartment('综合支持部')).toBe('综合支持部')
expect(normalizeDingWorkflowDepartment(null)).toBeNull()
})
it('matches initiators by DingTalk roster profile and formats the HR organization path', () => {
expect(resolveDingWorkflowInitiator({
displayName: '荀瑶',
departmentName: '品类九部',
departmentPath: '江苏简尚品牌管理有限公司/运营中心A/事业四部/品类九部/淘系四组',
}, {
name: '旧需求人',
department: '旧需求部门',
})).toEqual({
name: '荀瑶',
department: '运营中心A-事业四部-品类九部-淘系四组',
})
})
it('uses the workflow snapshot when the initiator is absent from the HR roster', () => {
expect(resolveDingWorkflowInitiator(null, {
name: '林发起',
department: '[{"name":"品牌中心-品牌创意A部"}]',
})).toEqual({
name: '林发起',
department: '品牌中心-品牌创意A部',
})
})
})

View File

@ -162,6 +162,8 @@ export type OngoingDingWorkflowListItem = {
brand: string
requesterName: string
requesterDepartment: string
initiatorName: string
initiatorDepartment: string
activeTasks: OngoingDingWorkflowActiveTask[]
}
@ -980,6 +982,56 @@ const ONGOING_WORKFLOW_SELECT = {
latestTaskStatus: true,
} as const
type DingWorkflowInitiatorProfile = {
displayName: string | null
departmentName: string | null
departmentPath: string | null
}
const COMPANY_ORGANIZATION_ROOT = '江苏简尚品牌管理有限公司'
function formatInitiatorDepartmentPath(value: string | null | undefined) {
const parts = String(value || '')
.split('/')
.map((part) => part.trim())
.filter(Boolean)
if (parts[0] === COMPANY_ORGANIZATION_ROOT) parts.shift()
return parts.join('-')
}
export function resolveDingWorkflowInitiator(
profile: DingWorkflowInitiatorProfile | null | undefined,
fallback: { name: string | null | undefined; department: string | null | undefined },
) {
return {
name: profile?.displayName?.trim() || fallback.name?.trim() || '未记录',
department: formatInitiatorDepartmentPath(profile?.departmentPath)
|| profile?.departmentName?.trim()
|| normalizeDingWorkflowDepartment(fallback.department)
|| '未记录部门',
}
}
async function readInitiatorDirectory(rows: Array<{ originatorUserId: string | null }>) {
const dingtalkUserIds = Array.from(new Set(
rows.map((row) => row.originatorUserId?.trim()).filter((value): value is string => Boolean(value)),
))
if (dingtalkUserIds.length === 0) return new Map<string, DingWorkflowInitiatorProfile>()
const profiles = await prisma.performanceSurveyEmployeeProfile.findMany({
where: { dingtalkUserId: { in: dingtalkUserIds } },
select: {
dingtalkUserId: true,
displayName: true,
departmentName: true,
departmentPath: true,
},
})
return new Map(profiles.flatMap((profile) => (
profile.dingtalkUserId ? [[profile.dingtalkUserId, profile] as const] : []
)))
}
function mapSnapshotListItem(row: {
processInstanceId: string
businessId: string | null
@ -999,8 +1051,12 @@ function mapSnapshotListItem(row: {
taskCount: number
schemeFieldCount: number
latestTaskStatus: string | null
}): OngoingDingWorkflowListItem {
}, initiatorProfile?: DingWorkflowInitiatorProfile | null): OngoingDingWorkflowListItem {
const processCode = normalizeDingWorkflowProcessCode(row.processCode) || 'UNKNOWN'
const initiator = resolveDingWorkflowInitiator(initiatorProfile, {
name: row.requesterName,
department: row.originatorDeptName || row.requesterDepartment,
})
return {
id: row.processInstanceId,
businessId: row.businessId,
@ -1013,6 +1069,8 @@ function mapSnapshotListItem(row: {
requesterDepartment: normalizeDingWorkflowDepartment(row.requesterDepartment)
|| normalizeDingWorkflowDepartment(row.originatorDeptName)
|| '未记录部门',
initiatorName: initiator.name,
initiatorDepartment: initiator.department,
activeTasks: normalizeActiveTasks(row.activeTasks),
originatorUserId: row.originatorUserId,
originatorDeptName: row.originatorDeptName,
@ -1039,8 +1097,12 @@ export async function getOngoingDingWorkflowInsights(session: Session | null): P
}),
readSummaryWithoutAccessCheck(),
])
const initiatorDirectory = await readInitiatorDirectory(rows)
return {
items: rows.map(mapSnapshotListItem),
items: rows.map((row) => mapSnapshotListItem(
row,
row.originatorUserId ? initiatorDirectory.get(row.originatorUserId.trim()) : null,
)),
total: rows.length,
sync,
}
@ -1081,9 +1143,13 @@ export async function getOngoingDingWorkflowProjects(
}),
readSummaryWithoutAccessCheck(),
])
const initiatorDirectory = await readInitiatorDirectory(rows)
return {
items: rows.map(mapSnapshotListItem),
items: rows.map((row) => mapSnapshotListItem(
row,
row.originatorUserId ? initiatorDirectory.get(row.originatorUserId.trim()) : null,
)),
pagination: {
page,
pageSize,