/** * 迁移脚本:为现有员工设置默认班次(长期排班=工作日班次) * 每个员工的默认班次设为该组织的第一个班次 */ import prisma from '../src/lib/prisma' async function main() { // 查找所有没有默认班次的在职员工 const employees = await prisma.employee.findMany({ where: { status: 'ACTIVE', defaultShiftId: null, }, select: { id: true, orgId: true, name: true }, }) console.log(`找到 ${employees.length} 个员工未设置默认班次`) // 按组织分组 const orgShifts = new Map() let updated = 0 for (const emp of employees) { // 获取该组织的第一个班次(缓存) let shiftId = orgShifts.get(emp.orgId) if (!shiftId) { const shift = await prisma.shift.findFirst({ where: { orgId: emp.orgId }, orderBy: { createdAt: 'asc' }, }) if (shift) { shiftId = shift.id orgShifts.set(emp.orgId, shiftId) } } if (shiftId) { await prisma.employee.update({ where: { id: emp.id }, data: { defaultShiftId: shiftId }, }) console.log(` ✓ 员工 ${emp.name} → 默认班次已设置`) updated++ } else { console.log(` ✗ 员工 ${emp.name} → 该组织无班次配置,跳过`) } } console.log(`\n完成:共更新 ${updated} 个员工`) } main() .catch(console.error) .finally(() => prisma.$disconnect())