263 lines
6.6 KiB
JavaScript
263 lines
6.6 KiB
JavaScript
const {
|
||
getAllExaminees,
|
||
getExamineeById,
|
||
getExamineeByIdCardAndAdmissionTicket,
|
||
createExaminee,
|
||
updateExaminee,
|
||
deleteExaminee,
|
||
getExamineeCount,
|
||
getValidExamineeCount,
|
||
getTestExamineeCount
|
||
} = require('../db/examinee.js');
|
||
|
||
/**
|
||
* 服务层:获取所有考生列表
|
||
* @returns {Promise<Array>} 考生列表
|
||
*/
|
||
async function fetchAllExamineesService() {
|
||
try {
|
||
return await getAllExaminees();
|
||
} catch (error) {
|
||
console.error('服务层: 获取所有考生列表失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据ID查询考生
|
||
* @param {number} id 考生ID
|
||
* @returns {Promise<Object|null>} 考生数据
|
||
*/
|
||
async function fetchExamineeByIdService(id) {
|
||
try {
|
||
return await getExamineeById(id);
|
||
} catch (error) {
|
||
console.error('服务层: 根据ID查询考生失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:添加考生
|
||
* @param {Object} examineeData 考生数据
|
||
* @returns {Promise<Object>} 添加的考生
|
||
*/
|
||
async function createExamineeService(examineeData) {
|
||
try {
|
||
// 数据验证
|
||
if (!examineeData.examinee_name || !examineeData.examinee_id_card) {
|
||
throw new Error('考生姓名和身份证号为必填项');
|
||
}
|
||
|
||
return await createExaminee(examineeData);
|
||
} catch (error) {
|
||
console.error('服务层: 添加考生失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新考生
|
||
* @param {number} id 考生ID
|
||
* @param {Object} examineeData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
async function updateExamineeService(id, examineeData) {
|
||
try {
|
||
if (!id) {
|
||
throw new Error('考生ID不能为空');
|
||
}
|
||
|
||
// 验证考生是否存在
|
||
const existingExaminee = await getExamineeById(id);
|
||
if (!existingExaminee) {
|
||
throw new Error('未找到指定考生');
|
||
}
|
||
|
||
// 数据验证
|
||
if (!examineeData.examinee_name || !examineeData.examinee_id_card) {
|
||
throw new Error('考生姓名和身份证号为必填项');
|
||
}
|
||
|
||
return await updateExaminee(id, examineeData);
|
||
} catch (error) {
|
||
console.error('服务层: 更新考生失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:删除考生
|
||
* @param {number} id 考生ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
async function deleteExamineeService(id) {
|
||
try {
|
||
return await deleteExaminee(id);
|
||
} catch (error) {
|
||
console.error('服务层: 删除考生失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:获取考生总数量
|
||
* @returns {Promise<number>} 考生总数量
|
||
*/
|
||
async function getExamineeCountService() {
|
||
try {
|
||
return await getExamineeCount();
|
||
} catch (error) {
|
||
console.error('服务层: 获取考生数量失败', error);
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:考生登录
|
||
* @param {string} idCard 身份证号
|
||
* @param {string} admissionTicket 准考证号
|
||
* @returns {Promise<Object|null>} 考生数据或null
|
||
*/
|
||
async function fetchExamineeByIdCardAndAdmissionTicketService(idCard, admissionTicket) {
|
||
try {
|
||
if (!idCard || !admissionTicket) {
|
||
throw new Error('身份证号和准考证号不能为空');
|
||
}
|
||
return await getExamineeByIdCardAndAdmissionTicket(idCard, admissionTicket);
|
||
} catch (error) {
|
||
console.error('服务层: 考生登录失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 初始化考生相关的IPC处理程序
|
||
* @param {Object} ipcMain Electron的ipcMain实例
|
||
*/
|
||
function initExamineeIpc(ipcMain) {
|
||
ipcMain.handle("examinee-fetch-all", async (event) => {
|
||
try {
|
||
return await fetchAllExamineesService();
|
||
} catch (error) {
|
||
console.error("Failed to fetch all examinees:", error);
|
||
return [];
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("user-login", async (event, {idCard, admissionTicket}) => {
|
||
try {
|
||
return await fetchExamineeByIdCardAndAdmissionTicketService(idCard, admissionTicket);
|
||
} catch (error) {
|
||
console.error("Failed to login examinee:", error);
|
||
return null;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("examinee-fetch-by-id", async (event, id) => {
|
||
try {
|
||
return await fetchExamineeByIdService(id);
|
||
} catch (error) {
|
||
console.error("Failed to fetch examinee by id:", error);
|
||
return null;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("examinee-create", async (event, examineeData) => {
|
||
try {
|
||
return await createExamineeService(examineeData);
|
||
} catch (error) {
|
||
console.error("Failed to create examinee:", error);
|
||
return null;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("examinee-update", async (event, {id, examineeData}) => {
|
||
try {
|
||
return await updateExamineeService(id, examineeData);
|
||
} catch (error) {
|
||
console.error("Failed to update examinee:", error);
|
||
return null;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("examinee-delete", async (event, id) => {
|
||
try {
|
||
return await deleteExamineeService(id);
|
||
} catch (error) {
|
||
console.error("Failed to delete examinee:", error);
|
||
return null;
|
||
}
|
||
});
|
||
|
||
// 添加获取考生数量的IPC处理程序
|
||
ipcMain.handle("examinee-get-count", async (event) => {
|
||
try {
|
||
return await getExamineeCountService();
|
||
} catch (error) {
|
||
console.error("Failed to get examinee count:", error);
|
||
return 0;
|
||
}
|
||
});
|
||
|
||
// 在initExamineeIpc函数中添加新的IPC处理程序
|
||
ipcMain.handle("examinee-get-valid-count", async () => {
|
||
try {
|
||
const count = await getValidExamineeCountService();
|
||
return count;
|
||
} catch (error) {
|
||
console.error("获取有效考生数量失败:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("examinee-get-test-count", async () => {
|
||
try {
|
||
const count = await getTestExamineeCountService();
|
||
return count;
|
||
} catch (error) {
|
||
console.error("获取测试考生数量失败:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 服务层:获取有效考生数量
|
||
* @returns {Promise<number>} 有效考生数量
|
||
*/
|
||
async function getValidExamineeCountService() {
|
||
try {
|
||
return await getValidExamineeCount();
|
||
} catch (error) {
|
||
console.error("服务层: 获取有效考生数量失败", error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:获取测试考生数量
|
||
* @returns {Promise<number>} 测试考生数量
|
||
*/
|
||
async function getTestExamineeCountService() {
|
||
try {
|
||
return await getTestExamineeCount();
|
||
} catch (error) {
|
||
console.error("服务层: 获取测试考生数量失败", error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 导出使用CommonJS格式
|
||
module.exports = {
|
||
fetchAllExamineesService,
|
||
fetchExamineeByIdService,
|
||
createExamineeService,
|
||
updateExamineeService,
|
||
deleteExamineeService,
|
||
getExamineeCountService,
|
||
getValidExamineeCountService,
|
||
getTestExamineeCountService,
|
||
fetchExamineeByIdCardAndAdmissionTicketService,
|
||
initExamineeIpc
|
||
}; |