/** * 批量补齐无年度标准的账户:从旧 SocialInsuranceConfig / HousingFundConfig 继承数据 */ import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() async function main() { const accounts = await prisma.socialAccount.findMany() let created = 0 let skipped = 0 for (const account of accounts) { // 检查是否已有标准 const existing = await prisma.socialYearStandard.findFirst({ where: { accountId: account.id, isCurrent: true }, }) if (existing) { skipped++ continue } // 查旧配置 let oldConfig: any = null if (account.type === 'HOUSING') { oldConfig = await prisma.housingFundConfig.findFirst({ where: { orgId: account.orgId, city: account.city }, orderBy: { effectiveFrom: 'desc' }, }) } else { oldConfig = await prisma.socialInsuranceConfig.findFirst({ where: { orgId: account.orgId, city: account.city }, orderBy: { effectiveFrom: 'desc' }, }) } if (!oldConfig) { console.log(`[跳过] ${account.type} ${account.name} (${account.city}) — 无旧配置可继承`) continue } // 检查是否已有同 effectiveFrom 的标准(含历史标准) const dupCheck = await prisma.socialYearStandard.findFirst({ where: { accountId: account.id, effectiveFrom: oldConfig.effectiveFrom }, }) if (dupCheck) { // 把已有的设为 current await prisma.socialYearStandard.updateMany({ where: { accountId: account.id, effectiveFrom: oldConfig.effectiveFrom }, data: { isCurrent: true }, }) console.log(`[修复] ${account.type} ${account.name} (${account.city}) — 已有标准设为 current`) created++ continue } // 创建年度标准 const std = await prisma.socialYearStandard.create({ data: { orgId: account.orgId, accountId: account.id, // 社保比例 pensionOrg: oldConfig.pensionOrg || 16, pensionEmp: oldConfig.pensionEmp || 8, medicalOrg: oldConfig.medicalOrg || 9.8, medicalEmp: oldConfig.medicalEmp || 2, medicalOrgExtra: (oldConfig as any).medicalOrgExtra || 0, medicalEmpExtra: (oldConfig as any).medicalEmpExtra || 0, unemploymentOrg: oldConfig.unemploymentOrg || 0.5, unemploymentEmp: oldConfig.unemploymentEmp || 0.5, injuryOrg: oldConfig.injuryOrg || 0.2, maternityOrg: oldConfig.maternityOrg || 0.8, baseMin: oldConfig.baseMin || 6326, baseMax: oldConfig.baseMax || 33891, medicalBaseMin: oldConfig.medicalBaseMin || 0, medicalBaseMax: oldConfig.medicalBaseMax || 0, extraInsurances: oldConfig.extraInsurances || null, // 公积金比例 housingOrg: (oldConfig as any).housingOrg || 12, housingEmp: (oldConfig as any).housingEmp || 12, housingBaseMin: 0, housingBaseMax: 0, // 最低工资 minWage: (oldConfig as any).minWage || 0, // 生效信息 effectiveFrom: oldConfig.effectiveFrom, effectiveTo: null, isCurrent: true, adjustmentDone: false, createdBy: account.createdBy, }, }) console.log(`[创建] ${account.type} ${account.name} (${account.city}) ← ${oldConfig.effectiveFrom}`) created++ } console.log(`\n完成:创建 ${created} 个标准,跳过 ${skipped} 个已有标准`) } main().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1) })