From f2cb5e66b26d03f26b8f2987dfa6614295a84a8b Mon Sep 17 00:00:00 2001 From: freedakgmail Date: Mon, 17 Aug 2026 21:04:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=80=80=E4=BC=91=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=8C=89=E6=9C=80=E6=96=B0=E5=90=88=E5=90=8C?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=88=A4=E6=96=AD=EF=BC=8C=E6=8E=92=E9=99=A4?= =?UTF-8?q?=E5=8A=B3=E5=8A=A1=E5=8D=8F=E8=AE=AE=E5=91=98=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit detectRetirementRisks、updateEmployeesRetirementDays、日历退休日期查询 均改为 include 最新合同并在代码中判断 contractType,而非用 some 查询 (some 会匹配到历史 FIXED 合同导致劳务人员仍被检测) --- backend/src/services/retirement.service.ts | 21 +++++++++++++-------- backend/src/services/risk.service.ts | 14 ++++++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/backend/src/services/retirement.service.ts b/backend/src/services/retirement.service.ts index 8f941ad..581cb64 100644 --- a/backend/src/services/retirement.service.ts +++ b/backend/src/services/retirement.service.ts @@ -236,19 +236,24 @@ export async function confirmRetirementPolicy(orgId: string, policyId: string): // 批量更新员工距退休天数(基于个人出生日期 individually 计算) export async function updateEmployeesRetirementDays(orgId: string): Promise { - // 查询有正式劳动合同的员工 + // 查询所有在职员工(含合同信息),在代码中按最新合同判断 const employees = await prisma.employee.findMany({ - where: { - orgId, - contracts: { some: { contractType: { in: ['FIXED', 'UNFIXED'] } } }, - }, - select: { id: true, birthDate: true, gender: true, femaleWorkerType: true }, + where: { orgId, status: 'ACTIVE', birthDate: { not: null } }, + include: { contracts: { orderBy: { createdAt: 'desc' }, take: 1 } }, }) for (const emp of employees) { - if (!emp.birthDate) continue + const latestContractType = emp.contracts[0]?.contractType + if (!latestContractType || !['FIXED', 'UNFIXED'].includes(latestContractType)) { + // 非正式劳动合同:清除退休天数 + await prisma.employee.update({ + where: { id: emp.id }, + data: { retirementDaysLeft: null }, + }) + continue + } const gender = emp.gender || '男' - const daysLeft = calcRetirementDaysLeft(emp.birthDate, gender, emp.femaleWorkerType) + const daysLeft = calcRetirementDaysLeft(emp.birthDate!, gender, emp.femaleWorkerType) if (daysLeft !== null) { await prisma.employee.update({ where: { id: emp.id }, diff --git a/backend/src/services/risk.service.ts b/backend/src/services/risk.service.ts index 7a2ff0d..ea3b703 100644 --- a/backend/src/services/risk.service.ts +++ b/backend/src/services/risk.service.ts @@ -488,13 +488,16 @@ export async function detectMonthlyTasks(orgId: string) { */ export async function detectRetirementRisks(orgId: string) { const employees = await prisma.employee.findMany({ - where: { orgId, status: 'ACTIVE', birthDate: { not: null }, contracts: { some: { contractType: { in: ['FIXED', 'UNFIXED'] } } } }, - select: { id: true, name: true, gender: true, birthDate: true, femaleWorkerType: true }, + where: { orgId, status: 'ACTIVE', birthDate: { not: null } }, + include: { contracts: { orderBy: { createdAt: 'desc' }, take: 1 } }, }) const risks: { employeeId: string; type: RiskType; level: RiskLevel; title: string; description: string; actionUrl: string }[] = [] for (const emp of employees) { + // 只对最新合同为正式劳动合同的员工进行退休检测 + const latestContractType = emp.contracts[0]?.contractType + if (!latestContractType || !['FIXED', 'UNFIXED'].includes(latestContractType)) continue if (!emp.birthDate) continue const gender = emp.gender || '男' const fwt = emp.femaleWorkerType @@ -1204,16 +1207,19 @@ export async function getMonthlyCalendar(orgId: string, month: string) { }) } - // 6. 退休日期(仅正式劳动合同员工) + // 6. 退休日期(仅最新合同为正式劳动合同的员工) const retiringEmployees = await prisma.employee.findMany({ where: { orgId, status: 'ACTIVE', retirementDaysLeft: { gte: 0, lte: 365 }, - contracts: { some: { contractType: { in: ['FIXED', 'UNFIXED'] } } }, }, + include: { contracts: { orderBy: { createdAt: 'desc' }, take: 1 } }, }) for (const emp of retiringEmployees) { + // 只对最新合同为正式劳动合同的员工显示退休日期 + const latestContractType = emp.contracts[0]?.contractType + if (!latestContractType || !['FIXED', 'UNFIXED'].includes(latestContractType)) continue if (emp.retirementDaysLeft !== null) { const retireDate = new Date() retireDate.setDate(retireDate.getDate() + emp.retirementDaysLeft)