181 lines
4.8 KiB
JavaScript
181 lines
4.8 KiB
JavaScript
import { getSystemDbPath } from './path.js';
|
||
import { executeWithRetry } from './utils.js';
|
||
import { getDbConnection } from './index.js';
|
||
|
||
/**
|
||
* 查询所有考生列表
|
||
* @returns {Promise<Array>} 考生列表
|
||
*/
|
||
export 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
|
||
*/
|
||
export 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
|
||
*/
|
||
export 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>} 添加的考生
|
||
*/
|
||
export 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>} 是否更新成功
|
||
*/
|
||
export 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>} 是否删除成功
|
||
*/
|
||
export 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;
|
||
});
|
||
}
|