diff --git a/backend/src/services/risk.service.ts b/backend/src/services/risk.service.ts index 19ffe25..2308ede 100644 --- a/backend/src/services/risk.service.ts +++ b/backend/src/services/risk.service.ts @@ -546,7 +546,7 @@ export async function runRiskDetection(orgId: string) { // 按 employeeId:type 归并,不依赖 actionUrl(actionUrl 可能因天数变化而不同) const existingRisks = await prisma.riskItem.findMany({ where: { orgId, status: { in: ['PENDING', 'RESOLVED', 'IGNORED'] } }, - select: { id: true, employeeId: true, type: true, title: true, status: true }, + select: { id: true, employeeId: true, type: true, title: true, status: true, actionUrl: true }, }) const existingKeys = new Set(existingRisks.map((r: typeof existingRisks[number]) => `${r.employeeId}:${r.type}`)) // PENDING 风险的 employeeId:type → record 映射,用于更新标题 @@ -592,18 +592,19 @@ export async function runRiskDetection(orgId: string) { ...monthlyTasks.filter((r) => !monthlyKeys.has(`${r.employeeId}:${r.title}`)), ] - // 更新已存在的 PENDING 风险:标题/量化信息可能因天数变化而变化 - const toUpdate: { id: string; title: string; description: string; estimatedLoss: number; lossRange: [number, number]; deadline?: Date }[] = [] + // 更新已存在的 PENDING 风险:标题/量化信息/actionUrl 可能因天数变化或代码更新而变化 + const toUpdate: { id: string; title: string; description: string; actionUrl: string; estimatedLoss: number; lossRange: [number, number]; deadline?: Date }[] = [] for (const r of nonMonthlyRisks) { const key = `${r.employeeId}:${r.type}` const existing = pendingRiskMap.get(key) - if (existing && existing.title !== r.title) { + if (existing && (existing.title !== r.title || existing.actionUrl !== r.actionUrl)) { const emp = r.employeeId ? empMap.get(r.employeeId) : null const cost = estimateRiskCost(r, emp) toUpdate.push({ id: existing.id, title: r.title, description: r.description, + actionUrl: r.actionUrl, estimatedLoss: cost.estimatedLoss, lossRange: cost.lossRange, deadline: cost.deadline, @@ -616,6 +617,7 @@ export async function runRiskDetection(orgId: string) { data: { title: u.title, description: u.description, + actionUrl: u.actionUrl, estimatedLoss: u.estimatedLoss, lossRange: u.lossRange, deadline: u.deadline, @@ -623,6 +625,19 @@ export async function runRiskDetection(orgId: string) { }) } + // 更新 MONTHLY 类型 PENDING 记录的 actionUrl(标题不变但 actionUrl 可能因代码更新而变化) + const monthlyPending = existingRisks.filter((r: typeof existingRisks[number]) => r.status === 'PENDING' && r.type === 'MONTHLY') + const monthlyActionUrlMap = new Map(monthlyTasks.map((r) => [r.title, r.actionUrl])) + for (const r of monthlyPending) { + const newUrl = monthlyActionUrlMap.get(r.title) + if (newUrl && r.actionUrl !== newUrl) { + await prisma.riskItem.update({ + where: { id: r.id }, + data: { actionUrl: newUrl }, + }) + } + } + if (toCreate.length > 0) { // 为每个风险计算量化信息 const createData = toCreate.map((r) => {