electron-vue-exam-single/electron/service/examineeService.js

172 lines
4.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 在文件开头的导入语句中添加新函数
import {
getAllExaminees,
getExamineeById,
createExaminee,
updateExaminee,
deleteExaminee,
getExamineeByIdCardAndAdmissionTicket
} from '../db/examinee.js';
/**
* 服务层:获取所有考生列表
* @returns {Promise<Array>} 考生列表
*/
export async function fetchAllExamineesService() {
try {
return await getAllExaminees();
} catch (error) {
console.error('服务层: 获取所有考生列表失败', error);
throw error;
}
}
/**
* 服务层根据ID查询考生
* @param {number} id 考生ID
* @returns {Promise<Object|null>} 考生数据
*/
export async function fetchExamineeByIdService(id) {
try {
return await getExamineeById(id);
} catch (error) {
console.error('服务层: 根据ID查询考生失败', error);
throw error;
}
}
/**
* 服务层:添加考生
* @param {Object} examineeData 考生数据
* @returns {Promise<Object>} 添加的考生
*/
export 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>} 是否更新成功
*/
export async function updateExamineeService(id, examineeData) {
console.log(id);
console.log(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>} 是否删除成功
*/
export async function deleteExamineeService(id) {
try {
return await deleteExaminee(id);
} catch (error) {
console.error('服务层: 删除考生失败', error);
throw error;
}
}
/**
* 服务层:考生登录
* @param {string} idCard 身份证号
* @param {string} admissionTicket 准考证号
* @returns {Promise<Object|null>} 考生数据或null
*/
export async function fetchExamineeByIdCardAndAdmissionTicketService(idCard, admissionTicket) {
try {
if (!idCard || !admissionTicket) {
throw new Error('身份证号和准考证号不能为空');
}
return await getExamineeByIdCardAndAdmissionTicket(idCard, admissionTicket);
} catch (error) {
console.error('服务层: 考生登录失败', error);
throw error;
}
}
export async function initExamineeIpc(ipcMain) {
ipcMain.handle("examinee-fetch-all", async () => {
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;
}
});
}