import { DatabaseSync } from 'node:sqlite'; const databasePath = process.argv[2]; const candidateNumber = process.argv[3]; if (!databasePath || !candidateNumber) throw new Error('缺少 SQLite 路径或报名号'); const database = new DatabaseSync(databasePath); const registration = database.prepare(` SELECT registration.id, registration.exam_id FROM registrations registration JOIN users user ON user.id = registration.user_id WHERE user.candidate_number = ? AND registration.status = 'approved' ORDER BY registration.created_at, registration.id LIMIT 1 `).get(candidateNumber); if (!registration) throw new Error('未找到已审核报名'); const numberRule = database.prepare('SELECT id FROM admission_number_rules ORDER BY created_at, id LIMIT 1').get(); const center = database.prepare("SELECT id, code, name, province_name, city_name, district_name, address FROM test_centers WHERE status = 'active' ORDER BY id LIMIT 1").get(); const room = center ? database.prepare("SELECT id, code, name, building, floor FROM test_rooms WHERE center_id = ? AND status = 'active' ORDER BY code, id LIMIT 1").get(center.id) : null; if (!numberRule || !center || !room) throw new Error('准考证冒烟数据缺少编号规则、考点或考场'); const now = '2026-07-22T00:00:00.000Z'; database.exec('BEGIN IMMEDIATE'); try { database.prepare('UPDATE exams SET admit_download_start = ?, admit_download_end = ? WHERE id = ?') .run('2000-01-01T00:00:00.000Z', '2099-12-31T23:59:59.999Z', registration.exam_id); database.prepare(` INSERT OR IGNORE INTO exam_arrangement_plans ( id, exam_id, number_rule_id, mixing_scope, random_seed, candidate_count, center_count, subject_assignment_count, subject_combination_count, same_school_center_rate, warnings_json, generated_by, generated_at ) VALUES (?, ?, ?, 'district', 'migration-smoke', 1, 1, 1, 1, 1, '[]', NULL, ?) `).run('plan_migration_smoke', registration.exam_id, numberRule.id, now); const plan = database.prepare('SELECT id FROM exam_arrangement_plans WHERE exam_id = ?').get(registration.exam_id); const address = [center.province_name, center.city_name, center.district_name, center.address].filter(Boolean).join(' '); database.prepare(` INSERT OR REPLACE INTO admit_cards ( registration_id, plan_id, card_number, center_id, test_center, center_code, center_address, generated_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) `).run(registration.id, plan.id, 'MIGRATION-SMOKE-001', center.id, center.name, center.code, address, now); const subjects = database.prepare('SELECT subject_id FROM registration_subjects WHERE registration_id = ? ORDER BY subject_id').all(registration.id); const insertAssignment = database.prepare(` INSERT OR REPLACE INTO admit_card_subjects ( registration_id, subject_id, room_id, room, room_code, exam_room_code, building, floor, seat, subject_signature ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `); subjects.forEach((subject, index) => insertAssignment.run( registration.id, subject.subject_id, room.id, room.name, room.code, '001', room.building, room.floor || '', '01', `migration-smoke-${index + 1}` )); database.exec('COMMIT'); console.log(registration.id); } catch (error) { database.exec('ROLLBACK'); throw error; } finally { database.close(); }