233 lines
6.4 KiB
JavaScript
233 lines
6.4 KiB
JavaScript
// 修复导入方式
|
||
const { getDbConnection } = require('./index.js');
|
||
const { getSystemDbPath } = require('./path.js'); // 从 path.js 导入
|
||
const { executeWithRetry } = require('./utils.js'); // 从 utils.js 导入
|
||
|
||
/**
|
||
* 查询所有考生列表
|
||
* @returns {Promise<Array>} 考生列表
|
||
*/
|
||
async function getAllExaminees() {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT * FROM examinee ORDER BY created_at DESC';
|
||
return await db.allAsync(sql);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 根据ID查询考生
|
||
* @param {number} id 考生ID
|
||
* @returns {Promise<Object|null>} 考生数据或null
|
||
*/
|
||
async function getExamineeById(id) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT * FROM examinee WHERE id = ?';
|
||
return await db.getAsync(sql, [id]);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 根据身份证号和准考证号查询考生
|
||
* @param {string} idCard 身份证号
|
||
* @param {string} admissionTicket 准考证号
|
||
* @returns {Promise<Object|null>} 考生数据或null
|
||
*/
|
||
async function getExamineeByIdCardAndAdmissionTicket(idCard, admissionTicket) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT * FROM examinee WHERE examinee_id_card = ? AND examinee_admission_ticket = ?';
|
||
return await db.getAsync(sql, [idCard, admissionTicket]);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 添加考生
|
||
* @param {Object} examineeData 考生数据
|
||
* @returns {Promise<Object>} 添加的考生
|
||
*/
|
||
async function createExaminee(examineeData) {
|
||
const {
|
||
examinee_name,
|
||
examinee_gender,
|
||
examinee_unit,
|
||
written_exam_room,
|
||
written_exam_seat,
|
||
computer_exam_room,
|
||
computer_exam_seat,
|
||
examinee_id_card,
|
||
examinee_admission_ticket
|
||
} = examineeData;
|
||
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = `INSERT INTO examinee (
|
||
examinee_name,
|
||
examinee_gender,
|
||
examinee_unit,
|
||
written_exam_room,
|
||
written_exam_seat,
|
||
computer_exam_room,
|
||
computer_exam_seat,
|
||
examinee_id_card,
|
||
examinee_admission_ticket
|
||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
||
|
||
const result = await db.runAsync(sql, [
|
||
examinee_name,
|
||
examinee_gender || '',
|
||
examinee_unit || '',
|
||
written_exam_room || '',
|
||
written_exam_seat || '',
|
||
computer_exam_room || '',
|
||
computer_exam_seat || '',
|
||
examinee_id_card || '',
|
||
examinee_admission_ticket || ''
|
||
]);
|
||
|
||
// 检查result是否存在,如果不存在则获取最后插入的ID
|
||
let lastId;
|
||
if (result && result.lastID) {
|
||
lastId = result.lastID;
|
||
} else {
|
||
// 使用另一种方式获取最后插入的ID
|
||
const idResult = await db.getAsync('SELECT last_insert_rowid() as id');
|
||
lastId = idResult ? idResult.id : null;
|
||
}
|
||
|
||
if (!lastId) {
|
||
throw new Error('无法获取插入的考生ID');
|
||
}
|
||
|
||
return { id: lastId, ...examineeData };
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 更新考生
|
||
* @param {number} id 考生ID
|
||
* @param {Object} examineeData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
async function updateExaminee(id, examineeData) {
|
||
const {
|
||
examinee_name,
|
||
examinee_gender,
|
||
examinee_unit,
|
||
written_exam_room,
|
||
written_exam_seat,
|
||
computer_exam_room,
|
||
computer_exam_seat,
|
||
examinee_id_card,
|
||
examinee_admission_ticket
|
||
} = examineeData;
|
||
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = `UPDATE examinee SET
|
||
examinee_name = ?,
|
||
examinee_gender = ?,
|
||
examinee_unit = ?,
|
||
written_exam_room = ?,
|
||
written_exam_seat = ?,
|
||
computer_exam_room = ?,
|
||
computer_exam_seat = ?,
|
||
examinee_id_card = ?,
|
||
examinee_admission_ticket = ?,
|
||
updated_at = CURRENT_TIMESTAMP
|
||
WHERE id = ?`;
|
||
|
||
const result = await db.runAsync(sql, [
|
||
examinee_name,
|
||
examinee_gender || '',
|
||
examinee_unit || '',
|
||
written_exam_room || '',
|
||
written_exam_seat || '',
|
||
computer_exam_room || '',
|
||
computer_exam_seat || '',
|
||
examinee_id_card || '',
|
||
examinee_admission_ticket || '',
|
||
id
|
||
]);
|
||
|
||
// 检查result是否存在以及是否有changes属性
|
||
if (!result || result.changes === undefined) {
|
||
return false;
|
||
}
|
||
|
||
return result.changes > 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 删除考生
|
||
* @param {number} id 考生ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
async function deleteExaminee(id) {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'DELETE FROM examinee WHERE id = ?';
|
||
const result = await db.runAsync(sql, [id]);
|
||
|
||
// 检查result是否存在以及是否有changes属性
|
||
if (!result || result.changes === undefined) {
|
||
return false;
|
||
}
|
||
|
||
return result.changes > 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 获取考生总数量
|
||
* @returns {Promise<number>} 考生总数量
|
||
*/
|
||
async function getExamineeCount() {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT COUNT(*) as count FROM examinee';
|
||
const result = await db.getAsync(sql);
|
||
return result ? result.count : 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 获取有效考生数量(身份证号为18位的考生)
|
||
* @returns {Promise<number>} 有效考生数量
|
||
*/
|
||
async function getValidExamineeCount() {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT COUNT(*) as count FROM examinee WHERE LENGTH(examinee_id_card) = 18';
|
||
const result = await db.getAsync(sql);
|
||
return result ? result.count : 0;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 获取测试考生数量(身份证号不足18位的考生)
|
||
* @returns {Promise<number>} 测试考生数量
|
||
*/
|
||
async function getTestExamineeCount() {
|
||
const db = await getDbConnection(getSystemDbPath());
|
||
return executeWithRetry(db, async () => {
|
||
const sql = 'SELECT COUNT(*) as count FROM examinee WHERE LENGTH(examinee_id_card) < 18';
|
||
const result = await db.getAsync(sql);
|
||
return result ? result.count : 0;
|
||
});
|
||
}
|
||
|
||
// 导出使用CommonJS格式
|
||
module.exports = {
|
||
getAllExaminees,
|
||
getExamineeById,
|
||
getExamineeByIdCardAndAdmissionTicket,
|
||
createExaminee,
|
||
updateExaminee,
|
||
deleteExaminee,
|
||
getExamineeCount,
|
||
getValidExamineeCount,
|
||
getTestExamineeCount
|
||
}; |