165 lines
4.7 KiB
JavaScript
165 lines
4.7 KiB
JavaScript
// 导入数据库操作函数
|
||
import { generateExamineePaper } from '../db/examing.js';
|
||
import { getDbConnection, closeAllConnections } from '../db/index.js';
|
||
import { getUserDbPath } from '../db/path.js';
|
||
|
||
/**
|
||
* 服务层:生成考生试卷
|
||
* @param {Object} examineeData - 考生数据
|
||
* @param {number} examDuration - 考试时长(分钟)
|
||
* @returns {Promise<Object>} - 包含试卷ID和状态的对象
|
||
*/
|
||
// 变更函数入参和验证逻辑
|
||
export async function generateExamineePaperService(examineeData, examData) {
|
||
try {
|
||
// 数据验证
|
||
if (!examineeData || !examineeData.id || !examineeData.examinee_name) {
|
||
throw new Error('考生数据不完整,必须包含ID和姓名');
|
||
}
|
||
|
||
if (!examData || !examData.exam_minutes || examData.exam_minutes <= 0) {
|
||
throw new Error('考试时长必须为正数');
|
||
}
|
||
|
||
if (examData.exam_minutes_min === undefined || examData.exam_minutes_min < 0) {
|
||
throw new Error('最短考试时长必须为非负数');
|
||
}
|
||
|
||
const result = await generateExamineePaper(examineeData, examData);
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 生成考生试卷失败', error);
|
||
return {
|
||
success: false,
|
||
message: `生成试卷失败: ${error.message}`
|
||
};
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:获取考生试卷状态
|
||
* @param {number} examineeId - 考生ID
|
||
* @returns {Promise<Object|null>} - 试卷状态信息
|
||
*/
|
||
export async function getExamineePaperStatusService(examineeId) {
|
||
try {
|
||
if (!examineeId || examineeId <= 0) {
|
||
throw new Error('考生ID必须为正数');
|
||
}
|
||
|
||
const userDb = await getDbConnection(getUserDbPath());
|
||
const paperStatus = await userDb.getAsync(
|
||
'SELECT * FROM examinee_papers WHERE examinee_id = ?',
|
||
[examineeId]
|
||
);
|
||
|
||
return paperStatus;
|
||
} catch (error) {
|
||
console.error('服务层: 获取考生试卷状态失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新试卷状态
|
||
* @param {number} paperId - 试卷ID
|
||
* @param {Object} statusData - 状态数据
|
||
* @returns {Promise<boolean>} - 是否更新成功
|
||
*/
|
||
export async function updatePaperStatusService(paperId, statusData) {
|
||
try {
|
||
if (!paperId || paperId <= 0) {
|
||
throw new Error('试卷ID必须为正数');
|
||
}
|
||
|
||
const userDb = await getDbConnection(getUserDbPath());
|
||
|
||
// 构建更新字段
|
||
const fields = [];
|
||
const values = [];
|
||
|
||
if (statusData.paper_start_time !== undefined) {
|
||
fields.push('paper_start_time = ?');
|
||
values.push(statusData.paper_start_time);
|
||
}
|
||
|
||
if (statusData.paper_last_time !== undefined) {
|
||
fields.push('paper_last_time = ?');
|
||
values.push(statusData.paper_last_time);
|
||
}
|
||
|
||
if (statusData.paper_submit_time !== undefined) {
|
||
fields.push('paper_submit_time = ?');
|
||
values.push(statusData.paper_submit_time);
|
||
}
|
||
|
||
if (statusData.paper_end_time !== undefined) {
|
||
fields.push('paper_end_time = ?');
|
||
values.push(statusData.paper_end_time);
|
||
}
|
||
|
||
if (statusData.paper_status !== undefined) {
|
||
fields.push('paper_status = ?');
|
||
values.push(statusData.paper_status);
|
||
}
|
||
|
||
if (statusData.paper_score_real !== undefined) {
|
||
fields.push('paper_score_real = ?');
|
||
values.push(statusData.paper_score_real);
|
||
}
|
||
|
||
if (fields.length === 0) {
|
||
return true; // 没有需要更新的字段
|
||
}
|
||
|
||
// 添加WHERE条件的值
|
||
values.push(paperId);
|
||
|
||
const sql = `UPDATE examinee_papers SET ${fields.join(', ')} WHERE id = ?`;
|
||
await userDb.runAsync(sql, values);
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('服务层: 更新试卷状态失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 初始化考试相关的IPC处理程序
|
||
* @param {import('electron').IpcMain} ipcMain - IPC主进程实例
|
||
*/
|
||
export function initExamingIpc(ipcMain) {
|
||
// 生成考生试卷
|
||
ipcMain.handle('examing-generate-paper', async (event, { examineeData, examData }) => {
|
||
try {
|
||
return await generateExamineePaperService(examineeData, examData);
|
||
} catch (error) {
|
||
console.error('生成考生试卷失败:', error);
|
||
return {
|
||
success: false,
|
||
message: `生成试卷失败: ${error.message}`
|
||
};
|
||
}
|
||
});
|
||
|
||
// 获取考生试卷状态
|
||
ipcMain.handle('examing-get-paper-status', async (event, examineeId) => {
|
||
try {
|
||
return await getExamineePaperStatusService(examineeId);
|
||
} catch (error) {
|
||
console.error('获取考生试卷状态失败:', error);
|
||
return null;
|
||
}
|
||
});
|
||
|
||
// 更新试卷状态
|
||
ipcMain.handle('examing-update-paper-status', async (event, { paperId, statusData }) => {
|
||
try {
|
||
return await updatePaperStatusService(paperId, statusData);
|
||
} catch (error) {
|
||
console.error('更新试卷状态失败:', error);
|
||
return false;
|
||
}
|
||
});
|
||
} |