题库导入功能完成,考试计划功能完成。

This commit is contained in:
2025-12-19 00:58:58 +08:00
parent ba252b2f56
commit 465d4d7b4a
27 changed files with 1851 additions and 177 deletions

View File

@@ -47,18 +47,43 @@ export class QuestionModel {
return this.findById(id) as Promise<Question>;
}
// 批量创建题目
// 批量创建题目 - 优化为使用事务批量插入
static async createMany(questions: CreateQuestionData[]): Promise<{ success: number; errors: string[] }> {
const errors: string[] = [];
let success = 0;
for (let i = 0; i < questions.length; i++) {
try {
await this.create(questions[i]);
success++;
} catch (error: any) {
errors.push(`${i + 1}题: ${error.message}`);
// 使用事务提高性能
try {
// 开始事务
await run('BEGIN TRANSACTION');
const sql = `
INSERT INTO questions (id, content, type, options, answer, score, category)
VALUES (?, ?, ?, ?, ?, ?, ?)
`;
for (let i = 0; i < questions.length; i++) {
try {
const question = questions[i];
const id = uuidv4();
const optionsStr = question.options ? JSON.stringify(question.options) : null;
const answerStr = Array.isArray(question.answer) ? JSON.stringify(question.answer) : question.answer;
const category = question.category && question.category.trim() ? question.category.trim() : '通用';
// 直接执行插入不调用单个create方法
await run(sql, [id, question.content, question.type, optionsStr, answerStr, question.score, category]);
success++;
} catch (error: any) {
errors.push(`${i + 1}题: ${error.message}`);
}
}
// 提交事务
await run('COMMIT');
} catch (error: any) {
// 回滚事务
await run('ROLLBACK');
errors.push(`事务错误: ${error.message}`);
}
return { success, errors };