From 7c9fdc8d6f1afeff55769abc717b854fcdb9dcaf Mon Sep 17 00:00:00 2001 From: chenqiang Date: Sat, 13 Sep 2025 09:08:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=89=E5=B7=B2=E5=AE=8C=E6=88=90=E8=80=83?= =?UTF-8?q?=E8=AF=95=E7=9A=84=E9=87=8D=E6=96=B0=E8=80=83=E8=AF=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- background/db/examing.js | 71 +++++++++++++++++++++++++++- background/preload.js | 3 ++ background/service/examingService.js | 34 ++++++++++++- src/views/user/ExamineeHomeView.vue | 37 ++++++++++++++- 4 files changed, 141 insertions(+), 4 deletions(-) diff --git a/background/db/examing.js b/background/db/examing.js index a4f21bc..46a5823 100644 --- a/background/db/examing.js +++ b/background/db/examing.js @@ -1399,6 +1399,74 @@ async function continueExam(paperId) { } } +/** + * 清理考生试卷及相关记录 + * @param {number} paperId - 试卷ID + * @returns {Promise} - 包含操作结果的对象 + */ +async function clearExamineePaper(paperId) { + try { + // 获取用户数据库连接 + const userDb = await getDbConnection(getUserDbPath()); + + // 开始事务 + await userDb.runAsync('BEGIN TRANSACTION'); + try { + // 获取paper_questions记录 + const paperQuestions = await userDb.allAsync( + 'SELECT id FROM paper_questions WHERE paper_id = ?', + paperId + ); + + // 遍历所有paper_questions,删除相关记录 + for (const question of paperQuestions) { + // 删除question_choices记录 + await userDb.runAsync( + 'DELETE FROM question_choices WHERE question_id = ?', + question.id + ); + + // 删除question_fill_blanks记录 + await userDb.runAsync( + 'DELETE FROM question_fill_blanks WHERE question_id = ?', + question.id + ); + + // 删除question_datasets记录 + await userDb.runAsync( + 'DELETE FROM question_datasets WHERE question_id = ?', + question.id + ); + + // 删除question_images记录 + await userDb.runAsync( + 'DELETE FROM question_images WHERE question_id = ?', + question.id + ); + } + + // 删除paper_questions记录 + await userDb.runAsync('DELETE FROM paper_questions WHERE paper_id = ?', paperId); + + // 删除examinee_papers记录 + await userDb.runAsync('DELETE FROM examinee_papers WHERE id = ?', paperId); + + // 提交事务 + await userDb.runAsync('COMMIT'); + console.log(`成功清理试卷ID: ${paperId}的所有相关记录`); + return { success: true }; + } catch (error) { + // 回滚事务 + await userDb.runAsync('ROLLBACK'); + console.error(`清理试卷记录失败: ${error.message}`); + throw new Error(`清理试卷记录失败: ${error.message}`); + } + } catch (error) { + console.error('获取数据库连接失败:', error); + throw new Error(`清理试卷记录失败: ${error.message}`); + } +} + exports = module.exports = { formatDateTime, batchInsert, @@ -1413,5 +1481,6 @@ exports = module.exports = { checkPaperAnswers, getPaper, getExamineePaper, - continueExam + continueExam, + clearExamineePaper }; \ No newline at end of file diff --git a/background/preload.js b/background/preload.js index b5b00bf..2c909bf 100644 --- a/background/preload.js +++ b/background/preload.js @@ -147,6 +147,9 @@ contextBridge.exposeInMainWorld("electronAPI", { // 新增:结束考试接口 examingEndPaper: ({ paperId }) => ipcRenderer.invoke("examing-end-paper", { paperId }), + // 新增:清理试卷数据接口 + examingClearPaper: ({ paperId }) => + ipcRenderer.invoke("examing-clear-paper", { paperId }), // 文件服务相关接口 // 保留一个fileGeneratePaperPdf定义,移除重复的 diff --git a/background/service/examingService.js b/background/service/examingService.js index e309958..ec643fb 100644 --- a/background/service/examingService.js +++ b/background/service/examingService.js @@ -11,7 +11,8 @@ const { checkPaperAnswers, getPaper, getExamineePaper, - continueExam, // 添加continueExam到导入列表中 + continueExam, + clearExamineePaper, } = require("../db/examing.js"); const { getDbConnection, closeAllConnections } = require("../db/index.js"); const { getUserDbPath } = require("../db/path.js"); @@ -75,6 +76,23 @@ async function getExamineePaperStatusService(examineeId) { } } +async function clearExamineePaperService(paperId) { + try { + const result = await clearExamineePaper(paperId); + return { + success: true, + message: "试卷数据清理成功", + data: result, + }; + } catch (error) { + console.error("清理试卷数据失败:", error); + return { + success: false, + message: `清理试卷数据失败: ${error.message}`, + }; + } +} + /** * 服务层:更新试卷状态 * @param {number} paperId - 试卷ID @@ -667,6 +685,19 @@ function initExamingIpc(ipcMain) { }; } }); + + // 找到initExamingIpc函数,在合适位置添加以下代码 + ipcMain.handle("examing-clear-paper", async (event, { paperId }) => { + try { + return await clearExamineePaperService(paperId); + } catch (error) { + console.error("清理试卷数据失败:", error); + return { + success: false, + message: `清理试卷数据失败: ${error.message}`, + }; + } + }); } // 导出使用CommonJS格式 @@ -686,5 +717,6 @@ module.exports = { getExamineePaperService, continueExamService, getPaperService, + clearExamineePaperService, initExamingIpc, }; diff --git a/src/views/user/ExamineeHomeView.vue b/src/views/user/ExamineeHomeView.vue index 9bf7ac1..d220987 100644 --- a/src/views/user/ExamineeHomeView.vue +++ b/src/views/user/ExamineeHomeView.vue @@ -125,7 +125,7 @@
你可以继续进行未完成的考试,或者重新开始,重新开始后将清除所有历史考试记录。
-
+
继续考试 重新开始
@@ -718,12 +718,45 @@ export default { } ).then(async () => { try { - // 清除未完成的试卷状态,然后调用开始考试方法 + this.isLoading = true + + // 首先获取要清除的试卷ID + let paperIdToClear = null + if (this.completedPaper && this.completedPaper.id) { + paperIdToClear = this.completedPaper.id + } else if (this.unfinishedPaper && this.unfinishedPaper.id) { + paperIdToClear = this.unfinishedPaper.id + } + + // 如果有试卷ID,则调用clear接口清除试卷数据 + if (paperIdToClear) { + console.log('开始清除试卷数据,试卷ID:', paperIdToClear) + const clearResult = await window.electronAPI.examingClearPaper({ + paperId: paperIdToClear + }) + + if (clearResult && clearResult.success) { + console.log('清除试卷数据成功') + } else { + console.warn('清除试卷数据失败:', clearResult) + // 不中断流程,继续尝试开始考试 + } + } + + // 清除本地的试卷状态 this.unfinishedPaper = null + this.completedPaper = null + + // 重新检查试卷状态,确保本地状态与实际数据同步 + await this.checkUnfinishedPaper() + + // 调用开始考试方法 await this.startExam() } catch (error) { console.error('重新开始考试失败:', error) this.$message.error('重新开始考试失败') + } finally { + this.isLoading = false } }).catch(() => { // 用户取消操作