parent
d272070c75
commit
77f39dc63d
@ -1,48 +1,48 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const xlsx = require('xlsx');
|
const xlsx = require('xlsx');
|
||||||
const pool = require('../db');
|
const pool = require('../db');
|
||||||
|
|
||||||
const classManager = {
|
const classManager = {
|
||||||
addClass: (req, res) => {
|
addClass: (req, res) => {
|
||||||
const filePath = req.file.path;
|
const filePath = req.file.path;
|
||||||
|
|
||||||
// 读取并解析Excel文件
|
// 读取并解析Excel文件
|
||||||
const workbook = xlsx.readFile(filePath);
|
const workbook = xlsx.readFile(filePath);
|
||||||
const sheetName = workbook.SheetNames[0];
|
const sheetName = workbook.SheetNames[0];
|
||||||
const worksheet = workbook.Sheets[sheetName];
|
const worksheet = workbook.Sheets[sheetName];
|
||||||
|
|
||||||
const students = xlsx.utils.sheet_to_json(worksheet);
|
const students = xlsx.utils.sheet_to_json(worksheet);
|
||||||
|
|
||||||
// 定义所需的字段
|
// 定义所需的字段
|
||||||
const requiredFields = [ 'student_id', 'student_name'];
|
const requiredFields = [ 'student_id', 'student_name'];
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
|
|
||||||
students.forEach(student => {
|
students.forEach(student => {
|
||||||
// 数据验证
|
// 数据验证
|
||||||
const missingFields = requiredFields.filter(field =>!(field in student));
|
const missingFields = requiredFields.filter(field =>!(field in student));
|
||||||
if (missingFields.length > 0) {
|
if (missingFields.length > 0) {
|
||||||
console.error(`Student data is missing fields: ${missingFields.join(', ')}`);
|
console.error(`Student data is missing fields: ${missingFields.join(', ')}`);
|
||||||
hasError = true;
|
hasError = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将学生数据插入数据库
|
// 将学生数据插入数据库
|
||||||
const query = 'INSERT INTO students (student_id, student_name, score, call_count) VALUES (?,?, 0, 0)';
|
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) => {
|
pool.query(query, [student.student_id, student.student_name], (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Error inserting student: ${student.name}`, error);
|
console.error(`Error inserting student: ${student.name}`, error);
|
||||||
hasError = true;
|
hasError = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
res.status(500).json({ message: 'Error adding class. Some students were not added successfully.' });
|
res.status(500).json({ message: 'Error adding class. Some students were not added successfully.' });
|
||||||
} else {
|
} else {
|
||||||
res.status(200).json({ message: 'Class added successfully!' });
|
res.status(200).json({ message: 'Class added successfully!' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = classManager;
|
module.exports = classManager;
|
||||||
|
Loading…
Reference in new issue