import { createExam, getAllExams, getExamById, updateExam, deleteExam, getLastExam, // 添加这一行 } from "../db/exam.js"; /** * 服务层:创建考试 * @param {Object} examData 考试数据 * @returns {Promise} 创建的考试 */ export async function createNewExam(examData) { try { // 数据验证 - 修改为exam_minutes和exam_minutes_min必填 if (!examData.exam_minutes || !examData.exam_minutes_min) { throw new Error("考试时长和最少考试时间为必填项"); } // 移除默认值设置,因为现在是必填项 if (typeof examData.exam_minutes_min !== "number") { throw new Error("最少考试时间必须是数字"); } // 确保最少考试时间不大于考试时长 if (examData.exam_minutes_min > examData.exam_minutes) { throw new Error("最少考试时间不能大于考试时长"); } return await createExam(examData); } catch (error) { console.error("服务层: 创建考试失败", error); throw error; } } /** * 服务层:查询所有考试 * @returns {Promise} 考试列表 */ export async function fetchAllExams() { try { return await getAllExams(); } catch (error) { console.error("服务层: 查询所有考试失败", error); throw error; } } /** * 服务层:根据ID查询考试 * @param {number} id 考试ID * @returns {Promise} 考试数据 */ export async function fetchExamById(id) { try { if (!id) { throw new Error("考试ID不能为空"); } const exam = await getExamById(id); if (!exam) { throw new Error("未找到指定考试"); } return exam; } catch (error) { console.error("服务层: 根据ID查询考试失败", error); throw error; } } /** * 服务层:更新考试 * @param {number} id 考试ID * @param {Object} examData 更新的数据 * @returns {Promise} 是否更新成功 */ export async function modifyExam(id, examData) { try { if (!id) { throw new Error("考试ID不能为空"); } // 验证考试是否存在 const existingExam = await getExamById(id); if (!existingExam) { throw new Error("未找到指定考试"); } // 数据验证 - 修改为exam_minutes和exam_minutes_min必填 if (!examData.exam_minutes || !examData.exam_minutes_min) { throw new Error("考试时长和最少考试时间为必填项"); } // 移除默认值设置,因为现在是必填项 if (typeof examData.exam_minutes_min !== "number") { throw new Error("最少考试时间必须是数字"); } // 确保最少考试时间不大于考试时长 if (examData.exam_minutes_min > examData.exam_minutes) { throw new Error("最少考试时间不能大于考试时长"); } return await updateExam(id, examData); } catch (error) { console.error("服务层: 更新考试失败", error); throw error; } } /** * 服务层:删除考试 * @param {number} id 考试ID * @returns {Promise} 是否删除成功 */ export async function removeExam(id) { try { if (!id) { throw new Error("考试ID不能为空"); } // 验证考试是否存在 const existingExam = await getExamById(id); if (!existingExam) { throw new Error("未找到指定考试"); } return await deleteExam(id); } catch (error) { console.error("服务层: 删除考试失败", error); throw error; } } /** * 服务层:查询ID最大的考试记录 * @returns {Promise} 考试数据 */ export async function fetchLastExam() { try { return await getLastExam(); } catch (error) { console.error("服务层: 查询ID最大的考试失败", error); throw error; } } export async function initExamIpc(ipcMain) { // 考试管理相关IPC ipcMain.handle("exam-create", async (event, examData) => { try { // 确保exam_notice是序列化的JSON字符串 if (examData.exam_notice && typeof examData.exam_notice === "object") { examData.exam_notice = JSON.stringify(examData.exam_notice); } return await createNewExam(examData); } catch (error) { console.error("Failed to create exam:", error); throw error; } }); ipcMain.handle("exam-update", async (event, { id, examData }) => { try { // 确保exam_notice是序列化的JSON字符串 if (examData.exam_notice && typeof examData.exam_notice === "object") { examData.exam_notice = JSON.stringify(examData.exam_notice); } return await modifyExam(id, examData); } catch (error) { console.error("Failed to update exam:", error); throw error; } }); ipcMain.handle("exam-fetch-last", async () => { try { const exam = await fetchLastExam(); // 将exam_notice字符串解析为数组 if (exam && exam.exam_notice) { try { exam.exam_notice = JSON.parse(exam.exam_notice); } catch (e) { console.error("解析考试须知失败:", e); exam.exam_notice = []; } } return exam; } catch (error) { console.error("Failed to fetch last exam:", error); throw error; } }); ipcMain.handle("exam-fetch-all", async () => { try { return { success: true, data: await fetchAllExams() }; } catch (error) { console.error("Failed to fetch all exams:", error); return { success: false, error: error.message }; } }); ipcMain.handle("exam-fetch-last", async () => { try { return { success: true, data: await fetchLastExam() }; } catch (error) { console.error("Failed to fetch last exam:", error); return { success: false, error: error.message }; } }); ipcMain.handle("exam-fetch-by-id", async (event, id) => { try { return { success: true, data: await fetchExamById(id) }; } catch (error) { console.error(`Failed to fetch exam by id ${id}:`, error); return { success: false, error: error.message }; } }); ipcMain.handle("exam-update", async (event, { id, examData }) => { try { const result = await modifyExam(id, examData); return { success: result, data: { id, ...examData } }; } catch (error) { console.error(`Failed to update exam ${id}:`, error); return { success: false, error: error.message }; } }); ipcMain.handle("exam-delete", async (event, id) => { try { const result = await removeExam(id); return { success: result, data: { id } }; } catch (error) { console.error(`Failed to delete exam ${id}:`, error); return { success: false, error: error.message }; } }); }