Compare commits
No commits in common. 'main' and '后端' have entirely different histories.
@ -1,65 +0,0 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
|
||||||
const xlsx = require('xlsx');
|
|
||||||
const pool = require('../db');
|
|
||||||
|
|
||||||
const classManager = {
|
|
||||||
addClass: async (req, res) => {
|
|
||||||
// 创建students表的SQL语句
|
|
||||||
const createTableQuery = `
|
|
||||||
CREATE TABLE IF NOT EXISTS students (
|
|
||||||
student_id INT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
student_name VARCHAR(255) NOT NULL,
|
|
||||||
score DECIMAL(5, 2),
|
|
||||||
call_count INT DEFAULT 0
|
|
||||||
);
|
|
||||||
`;
|
|
||||||
try {
|
|
||||||
// 先创建表
|
|
||||||
await pool.query(createTableQuery);
|
|
||||||
|
|
||||||
const filePath = req.file.path;
|
|
||||||
|
|
||||||
// 读取并解析Excel文件
|
|
||||||
const workbook = xlsx.readFile(filePath);
|
|
||||||
const sheetName = workbook.SheetNames[0];
|
|
||||||
const worksheet = workbook.Sheets[sheetName];
|
|
||||||
|
|
||||||
const students = xlsx.utils.sheet_to_json(worksheet);
|
|
||||||
|
|
||||||
// 定义所需的字段
|
|
||||||
const requiredFields = ['student_id','student_name'];
|
|
||||||
let hasError = false;
|
|
||||||
|
|
||||||
students.forEach(student => {
|
|
||||||
// 数据验证
|
|
||||||
const missingFields = requiredFields.filter(field =>!(field in student));
|
|
||||||
if (missingFields.length > 0) {
|
|
||||||
console.error(`Student data is missing fields: ${missingFields.join(', ')}`);
|
|
||||||
hasError = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将学生数据插入数据库
|
|
||||||
const query = 'INSERT INTO students (student_id, student_name, score, call_count) VALUES (?,?, 0, 0)';
|
|
||||||
pool.query(query, [student.student_id, student.student_name], (error) => {
|
|
||||||
if (error) {
|
|
||||||
console.error(`Error inserting student: ${student.name}`, error);
|
|
||||||
hasError = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (hasError) {
|
|
||||||
res.status(500).json({ message: 'Error adding class. Some students were not added successfully.' });
|
|
||||||
} else {
|
|
||||||
res.status(200).json({ message: 'Class added successfully!' });
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error creating students table:', err);
|
|
||||||
res.status(500).json({ message: 'Error creating table during class addition.' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = classManager;
|
|
@ -1,24 +0,0 @@
|
|||||||
const mysql = require('mysql2/promise');
|
|
||||||
|
|
||||||
// 创建连接池
|
|
||||||
const pool = mysql.createPool({
|
|
||||||
host: 'localhost', // 数据库地址
|
|
||||||
user: 'root', // 数据库用户名
|
|
||||||
password: '123456', // 数据库密码
|
|
||||||
database: 'class_k' ,// 使用的数据库
|
|
||||||
waitForConnections: true,
|
|
||||||
connectionLimit: 10,
|
|
||||||
queueLimit: 0
|
|
||||||
});
|
|
||||||
|
|
||||||
// 测试数据库连接
|
|
||||||
pool.getConnection((err, connection) => {
|
|
||||||
if (err) {
|
|
||||||
console.error('Error connecting to the database:', err);
|
|
||||||
} else {
|
|
||||||
console.log('Database connected successfully!');
|
|
||||||
connection.release(); // 释放连接
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = pool;
|
|
@ -1,22 +0,0 @@
|
|||||||
const pool = require('../db');
|
|
||||||
|
|
||||||
async function deleteTable(req, res) {
|
|
||||||
const { tableName } = req.body; // 从请求体中获取表名
|
|
||||||
let connection;
|
|
||||||
|
|
||||||
try {
|
|
||||||
connection = await pool.getConnection();
|
|
||||||
await connection.query(`DROP TABLE IF EXISTS ??`, [tableName]); // 删除表
|
|
||||||
connection.release();
|
|
||||||
res.json({ message: `表 ${tableName} 已被成功删除` });
|
|
||||||
} catch (error) {
|
|
||||||
console.error('删除表失败:', error);
|
|
||||||
res.status(500).json({ error: '删除表失败' });
|
|
||||||
} finally {
|
|
||||||
if (connection) {
|
|
||||||
connection.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { deleteTable };
|
|
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"dependencies": {
|
|
||||||
"body-parser": "^1.20.3",
|
|
||||||
"express": "^4.21.0",
|
|
||||||
"multer": "^1.4.5-lts.1",
|
|
||||||
"mysql2": "^3.11.3",
|
|
||||||
"xlsx": "^0.18.5"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
const pool = require('../db');
|
|
||||||
|
|
||||||
async function selectStudent(req, res) {
|
|
||||||
try {
|
|
||||||
const connection = await pool.getConnection();
|
|
||||||
|
|
||||||
// 从数据库查询学生
|
|
||||||
const [students] = await connection.query('SELECT * FROM students');
|
|
||||||
|
|
||||||
// 计算权重的总和(使用 softmax 权重计算)
|
|
||||||
const totalWeight = students.reduce((acc, student) => acc + Math.exp(-student.score), 0);
|
|
||||||
|
|
||||||
// 将每个学生的权重归一化
|
|
||||||
const weightedStudents = students.map(student => ({
|
|
||||||
student,
|
|
||||||
weight: Math.exp(-student.score) / totalWeight,
|
|
||||||
}));
|
|
||||||
|
|
||||||
// 生成随机数
|
|
||||||
const random = Math.random(); // 介于 0 和 1 之间
|
|
||||||
let sum = 0; // 用于累加权重
|
|
||||||
let selectedStudent = null;
|
|
||||||
|
|
||||||
// 遍历加权后的学生,累加权重,并判断随机数落在哪个学生的区间
|
|
||||||
for (let i = 0; i < weightedStudents.length; i++) {
|
|
||||||
sum += weightedStudents[i].weight; // 累加当前学生的权重
|
|
||||||
if (random <= sum) { // 如果随机数小于或等于当前的累积权重
|
|
||||||
selectedStudent = weightedStudents[i].student; // 选中该学生
|
|
||||||
break; // 找到后立即退出循环
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedStudent) {
|
|
||||||
res.send({ student_name: selectedStudent.student_name,
|
|
||||||
student_id: selectedStudent.student_id
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.status(404).send({ message: '无学生数据' });
|
|
||||||
}
|
|
||||||
|
|
||||||
connection.release();
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).send({ error: '随机选择学生失败' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { selectStudent };
|
|
@ -1,25 +0,0 @@
|
|||||||
const pool = require('../db');
|
|
||||||
|
|
||||||
async function get_descend_Ranking(req, res) {
|
|
||||||
try {
|
|
||||||
const connection = await pool.getConnection();
|
|
||||||
const [ranking] = await connection.query('SELECT student_name, student_id, score FROM students ORDER BY score DESC');
|
|
||||||
connection.release();
|
|
||||||
res.json(ranking);
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).send({ error: '无法获取排名' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function get_ascend_Ranking(req, res) {
|
|
||||||
try {
|
|
||||||
const connection = await pool.getConnection();
|
|
||||||
const [ranking] = await connection.query('SELECT student_name, student_id, score FROM students ORDER BY score ASC');
|
|
||||||
connection.release();
|
|
||||||
res.json(ranking);
|
|
||||||
} catch (error) {
|
|
||||||
res.status(500).send({ error: '无法获取排名' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { get_ascend_Ranking, get_descend_Ranking };
|
|
@ -1,50 +0,0 @@
|
|||||||
const express = require('express');
|
|
||||||
const router = express.Router();
|
|
||||||
const multer = require('multer');
|
|
||||||
const randomSelect = require('./services/randomSelect');
|
|
||||||
const ranking = require('./services/ranking');
|
|
||||||
const classManager = require('./services/classManager');
|
|
||||||
const scoreManager = require('./services/scoreManager');
|
|
||||||
const TableManager = require('./services/delete-table');
|
|
||||||
const pool = require('./db');
|
|
||||||
// 文件上传配置
|
|
||||||
const upload = multer({ dest: 'uploads/' });
|
|
||||||
|
|
||||||
// 路由 - 添加班级
|
|
||||||
router.post('/upload', upload.single('file'), classManager.addClass);
|
|
||||||
|
|
||||||
// 路由 - 随机点名
|
|
||||||
router.get('/random-call', randomSelect.selectStudent);
|
|
||||||
|
|
||||||
// 路由 - 获取排名
|
|
||||||
router.get('/ascend_ranking', ranking.get_ascend_Ranking);
|
|
||||||
router.get('/descend_ranking', ranking.get_descend_Ranking);
|
|
||||||
|
|
||||||
// 路由 - 更新分数
|
|
||||||
router.post('/update-score', scoreManager.updateScore);
|
|
||||||
|
|
||||||
//路由 - 获取某个id的分数
|
|
||||||
router.post('/get-score', scoreManager.getStudentScore);
|
|
||||||
|
|
||||||
//路由 - 删除班级
|
|
||||||
router.post('/delete-table', TableManager.deleteTable);
|
|
||||||
|
|
||||||
|
|
||||||
// 获取全部学生名单
|
|
||||||
router.get('/get-students', async (req, res) => {
|
|
||||||
let connection; // 声明连接变量
|
|
||||||
try {
|
|
||||||
connection = await pool.getConnection(); // 从连接池获取连接
|
|
||||||
const [students] = await connection.query('SELECT student_name FROM students'); // 执行查询
|
|
||||||
const studentNames = students.map(student => ({ student_name: student.student_name })); // 只保留 student_name 字段
|
|
||||||
res.json(studentNames); // 返回只包含 student_name 的结果
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching students:', error); // 记录错误信息
|
|
||||||
res.status(500).send({ error: 'Failed to fetch students' }); // 返回500错误响应
|
|
||||||
} finally {
|
|
||||||
if (connection) connection.release(); // 确保连接被释放
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
|
@ -1,26 +0,0 @@
|
|||||||
const express = require('express');
|
|
||||||
const path = require('path');
|
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const router = require('./router');
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
// 中间件
|
|
||||||
app.use(bodyParser.json());
|
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
|
||||||
|
|
||||||
// API 路由
|
|
||||||
app.use('/api', router);
|
|
||||||
|
|
||||||
// 静态文件
|
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
|
||||||
|
|
||||||
// 处理 SPA 路由
|
|
||||||
app.get('*', (req, res) => {
|
|
||||||
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
||||||
});
|
|
||||||
|
|
||||||
// 启动服务器
|
|
||||||
const PORT = process.env.PORT || 3000;
|
|
||||||
app.listen(PORT, () => {
|
|
||||||
console.log(`Server running on port ${PORT}`);
|
|
||||||
});
|
|
Loading…
Reference in new issue