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)