You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const pool = require('../db');
const ExcelJS = require('exceljs');
async function updateScore(req, res) {
const { student_id, points } = req.body;
let connection; // 在try块外部声明connection
try {
connection = await pool.getConnection();
// 更新学生的积分并增加点名次数
const [result] = await connection.query(
'UPDATE students SET score = score + ?, call_count = call_count + 1 WHERE student_id = ?',
[points, student_id]
);
if (result.affectedRows > 0) {
// 查询更新后的分数
const [rows] = await connection.query(
'SELECT score FROM students WHERE student_id = ?',
[student_id]
);
if (rows.length > 0) {
const updatedScore = rows[0].score;
return res.send(String(updatedScore));
} else {
return res.status(404).send('学生不存在');
}
} else {
return res.status(404).send('学生不存在');
}
} catch (error) {
console.error('数据库操作失败:', error);
return res.status(500).send('更新积分失败');
} finally {
if (connection) {
connection.release(); // 只有在connection已被赋值时才调用release
}
}
}
async function getStudentScore(req, res) {
const { student_id } = req.body;
let connection;
try {
connection = await pool.getConnection();
// 查询指定学生的分数
const [rows] = await connection.query(
'SELECT score FROM students WHERE student_id =?',
[student_id]
);
if (rows.length > 0) {
const score = rows[0].score;
// 修改这里构建包含student_id和score的对象并返回
return res.json({ student_id, score });
} else {
return res.status(404).json({ message: '学生不存在' });
}
} catch (error) {
console.error('数据库操作失败:', error);
return res.status(500).json({ error: '获取分数失败' });
} finally {
if (connection) {
connection.release();
}
}
}
async function exportScores(req, res) {
let connection;
try {
connection = await pool.getConnection();
// 查询学生的成绩
const [rows] = await connection.query('SELECT student_name, student_id, score FROM students');
// 创建一个工作簿和一个工作表
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Scores');
// 添加列标题
worksheet.columns = [
{ header: 'Student Name', key: 'student_name', width: 20 },
{ header: 'Student ID', key: 'student_id', width: 15 },
{ header: 'Score', key: 'score', width: 10 },
];
// 将数据添加到工作表中
rows.forEach(row => {
worksheet.addRow(row);
});
// 设置响应头,告知浏览器以附件形式下载文件
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', 'attachment; filename=scores.xlsx');
// 生成并发送 Excel 文件
await workbook.xlsx.write(res);
res.end();
} catch (error) {
console.error('导出成绩失败:', error);
res.status(500).send({ error: '无法导出成绩' });
} finally {
if (connection) connection.release();
}
}
module.exports = { updateScore, getStudentScore,exportScores };