新增文本题库导入功能,题目新增“解析”字段
This commit is contained in:
@@ -8,6 +8,7 @@ export interface Question {
|
||||
category: string;
|
||||
options?: string[];
|
||||
answer: string | string[];
|
||||
analysis: string;
|
||||
score: number;
|
||||
createdAt: string;
|
||||
}
|
||||
@@ -18,6 +19,7 @@ export interface CreateQuestionData {
|
||||
category?: string;
|
||||
options?: string[];
|
||||
answer: string | string[];
|
||||
analysis?: string;
|
||||
score: number;
|
||||
}
|
||||
|
||||
@@ -26,6 +28,7 @@ export interface ExcelQuestionData {
|
||||
type: string;
|
||||
category?: string;
|
||||
answer: string;
|
||||
analysis?: string;
|
||||
score: number;
|
||||
options?: string[];
|
||||
}
|
||||
@@ -37,13 +40,14 @@ export class QuestionModel {
|
||||
const optionsStr = data.options ? JSON.stringify(data.options) : null;
|
||||
const answerStr = Array.isArray(data.answer) ? JSON.stringify(data.answer) : data.answer;
|
||||
const category = data.category && data.category.trim() ? data.category.trim() : '通用';
|
||||
const analysis = String(data.analysis ?? '').trim().slice(0, 255);
|
||||
|
||||
const sql = `
|
||||
INSERT INTO questions (id, content, type, options, answer, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO questions (id, content, type, options, answer, analysis, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
await run(sql, [id, data.content, data.type, optionsStr, answerStr, data.score, category]);
|
||||
await run(sql, [id, data.content, data.type, optionsStr, answerStr, analysis, data.score, category]);
|
||||
return this.findById(id) as Promise<Question>;
|
||||
}
|
||||
|
||||
@@ -58,8 +62,8 @@ export class QuestionModel {
|
||||
await run('BEGIN TRANSACTION');
|
||||
|
||||
const sql = `
|
||||
INSERT INTO questions (id, content, type, options, answer, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO questions (id, content, type, options, answer, analysis, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
@@ -69,9 +73,10 @@ export class QuestionModel {
|
||||
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() : '通用';
|
||||
const analysis = String(question.analysis ?? '').trim().slice(0, 255);
|
||||
|
||||
// 直接执行插入,不调用单个create方法
|
||||
await run(sql, [id, question.content, question.type, optionsStr, answerStr, question.score, category]);
|
||||
await run(sql, [id, question.content, question.type, optionsStr, answerStr, analysis, question.score, category]);
|
||||
success++;
|
||||
} catch (error: any) {
|
||||
errors.push(`第${i + 1}题: ${error.message}`);
|
||||
@@ -89,6 +94,77 @@ export class QuestionModel {
|
||||
return { success, errors };
|
||||
}
|
||||
|
||||
static async importFromText(
|
||||
mode: 'overwrite' | 'incremental',
|
||||
questions: CreateQuestionData[],
|
||||
): Promise<{
|
||||
inserted: number;
|
||||
updated: number;
|
||||
errors: string[];
|
||||
cleared?: { questions: number; quizRecords: number; quizAnswers: number };
|
||||
}> {
|
||||
const errors: string[] = [];
|
||||
let inserted = 0;
|
||||
let updated = 0;
|
||||
let cleared: { questions: number; quizRecords: number; quizAnswers: number } | undefined;
|
||||
|
||||
const insertSql = `
|
||||
INSERT INTO questions (id, content, type, options, answer, analysis, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
await run('BEGIN TRANSACTION');
|
||||
try {
|
||||
if (mode === 'overwrite') {
|
||||
const [qCount, rCount, aCount] = await Promise.all([
|
||||
get(`SELECT COUNT(*) as total FROM questions`),
|
||||
get(`SELECT COUNT(*) as total FROM quiz_records`),
|
||||
get(`SELECT COUNT(*) as total FROM quiz_answers`),
|
||||
]);
|
||||
cleared = { questions: qCount.total, quizRecords: rCount.total, quizAnswers: aCount.total };
|
||||
|
||||
await run(`DELETE FROM quiz_answers`);
|
||||
await run(`DELETE FROM quiz_records`);
|
||||
await run(`DELETE FROM questions`);
|
||||
}
|
||||
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
const question = questions[i];
|
||||
try {
|
||||
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() : '通用';
|
||||
const analysis = String(question.analysis ?? '').trim().slice(0, 255);
|
||||
|
||||
if (mode === 'incremental') {
|
||||
const existing = await get(`SELECT id FROM questions WHERE content = ?`, [question.content]);
|
||||
if (existing?.id) {
|
||||
await run(
|
||||
`UPDATE questions SET content = ?, type = ?, options = ?, answer = ?, analysis = ?, score = ?, category = ? WHERE id = ?`,
|
||||
[question.content, question.type, optionsStr, answerStr, analysis, question.score, category, existing.id],
|
||||
);
|
||||
updated++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const id = uuidv4();
|
||||
await run(insertSql, [id, question.content, question.type, optionsStr, answerStr, analysis, question.score, category]);
|
||||
inserted++;
|
||||
} catch (error: any) {
|
||||
errors.push(`第${i + 1}题: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
await run('COMMIT');
|
||||
} catch (error) {
|
||||
await run('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
|
||||
return { inserted, updated, errors, cleared };
|
||||
}
|
||||
|
||||
// 根据ID查找题目
|
||||
static async findById(id: string): Promise<Question | null> {
|
||||
const sql = `SELECT * FROM questions WHERE id = ?`;
|
||||
@@ -219,6 +295,11 @@ export class QuestionModel {
|
||||
fields.push('answer = ?');
|
||||
values.push(answerStr);
|
||||
}
|
||||
|
||||
if (data.analysis !== undefined) {
|
||||
fields.push('analysis = ?');
|
||||
values.push(String(data.analysis ?? '').trim().slice(0, 255));
|
||||
}
|
||||
|
||||
if (data.score !== undefined) {
|
||||
fields.push('score = ?');
|
||||
@@ -257,6 +338,7 @@ export class QuestionModel {
|
||||
category: row.category || '通用',
|
||||
options: row.options ? JSON.parse(row.options) : undefined,
|
||||
answer: this.parseAnswer(row.answer, row.type),
|
||||
analysis: String(row.analysis ?? ''),
|
||||
score: row.score,
|
||||
createdAt: row.created_at
|
||||
};
|
||||
@@ -309,6 +391,10 @@ export class QuestionModel {
|
||||
if (data.category !== undefined && data.category.trim().length === 0) {
|
||||
errors.push('题目类别不能为空');
|
||||
}
|
||||
|
||||
if (data.analysis !== undefined && String(data.analysis).length > 255) {
|
||||
errors.push('解析长度不能超过255个字符');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user