172 lines
4.7 KiB
JavaScript
172 lines
4.7 KiB
JavaScript
const {
|
||
getAllExamineePapers,
|
||
getExamineePaperById,
|
||
getExamineePapersByExamineeId,
|
||
getExamineePapersByStatus
|
||
} = require('../db/paper.js');
|
||
|
||
/**
|
||
* 服务层:查询所有考生试卷记录
|
||
* @returns {Promise<Object>} 包含状态、消息和数据的对象
|
||
*/
|
||
exports.getAllExamineePapersService = async function getAllExamineePapersService() {
|
||
try {
|
||
const papers = await getAllExamineePapers();
|
||
return {
|
||
success: true,
|
||
message: '查询成功',
|
||
data: papers
|
||
};
|
||
} catch (error) {
|
||
console.error('服务层: 查询所有考生试卷记录失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: []
|
||
};
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 服务层:根据试卷ID查询试卷详情
|
||
* @param {number} paperId - 试卷ID
|
||
* @returns {Promise<Object>} 包含状态、消息和数据的对象
|
||
*/
|
||
exports.getExamineePaperByIdService = async function getExamineePaperByIdService(paperId) {
|
||
try {
|
||
if (!paperId || paperId <= 0) {
|
||
throw new Error('试卷ID必须为正整数');
|
||
}
|
||
const paper = await getExamineePaperById(paperId);
|
||
if (!paper) {
|
||
return {
|
||
success: false,
|
||
message: '未找到该试卷记录',
|
||
data: null
|
||
};
|
||
}
|
||
return {
|
||
success: true,
|
||
message: '查询成功',
|
||
data: paper
|
||
};
|
||
} catch (error) {
|
||
console.error('服务层: 根据ID查询试卷详情失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: null
|
||
};
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 服务层:查询考生的试卷记录
|
||
* @param {number} examineeId - 考生ID
|
||
* @returns {Promise<Object>} 包含状态、消息和数据的对象
|
||
*/
|
||
exports.getExamineePapersByExamineeIdService = async function getExamineePapersByExamineeIdService(examineeId) {
|
||
try {
|
||
if (!examineeId || examineeId <= 0) {
|
||
throw new Error('考生ID必须为正整数');
|
||
}
|
||
const papers = await getExamineePapersByExamineeId(examineeId);
|
||
return {
|
||
success: true,
|
||
message: '查询成功',
|
||
data: papers
|
||
};
|
||
} catch (error) {
|
||
console.error('服务层: 查询考生试卷记录失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: []
|
||
};
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 服务层:根据状态查询试卷记录
|
||
* @param {number} status - 试卷状态
|
||
* @returns {Promise<Object>} 包含状态、消息和数据的对象
|
||
*/
|
||
exports.getExamineePapersByStatusService = async function getExamineePapersByStatusService(status) {
|
||
try {
|
||
const papers = await getExamineePapersByStatus(status);
|
||
return {
|
||
success: true,
|
||
message: '查询成功',
|
||
data: papers
|
||
};
|
||
} catch (error) {
|
||
console.error('服务层: 根据状态查询试卷记录失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: []
|
||
};
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 注册试卷管理相关的IPC处理程序
|
||
* @param {Electron.IpcMain} ipcMain - Electron的IpcMain实例
|
||
*/
|
||
exports.initPaperIpc = function initPaperIpc(ipcMain) {
|
||
// 查询所有考生试卷记录
|
||
ipcMain.handle('paper-get-all', async (event) => {
|
||
try {
|
||
return await exports.getAllExamineePapersService();
|
||
} catch (error) {
|
||
console.error('IPC处理程序: 查询所有考生试卷记录失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: []
|
||
};
|
||
}
|
||
});
|
||
|
||
// 根据试卷ID查询试卷详情
|
||
ipcMain.handle('paper-get-by-id', async (event, paperId) => {
|
||
try {
|
||
return await exports.getExamineePaperByIdService(paperId);
|
||
} catch (error) {
|
||
console.error('IPC处理程序: 根据ID查询试卷详情失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: null
|
||
};
|
||
}
|
||
});
|
||
|
||
// 查询考生的试卷记录
|
||
ipcMain.handle('paper-get-by-examinee-id', async (event, examineeId) => {
|
||
try {
|
||
return await exports.getExamineePapersByExamineeIdService(examineeId);
|
||
} catch (error) {
|
||
console.error('IPC处理程序: 查询考生试卷记录失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: []
|
||
};
|
||
}
|
||
});
|
||
|
||
// 根据状态查询试卷记录
|
||
ipcMain.handle('paper-get-by-status', async (event, status) => {
|
||
try {
|
||
return await exports.getExamineePapersByStatusService(status);
|
||
} catch (error) {
|
||
console.error('IPC处理程序: 根据状态查询试卷记录失败', error);
|
||
return {
|
||
success: false,
|
||
message: `查询失败: ${error.message}`,
|
||
data: []
|
||
};
|
||
}
|
||
});
|
||
} |