Files
WebWork/backend/services/studentService.js
祀梦 b9a975004b feat: 实现成绩管理系统核心功能
添加响应工具、错误处理中间件和数据库模型
创建用户、学生、课程和成绩相关服务
实现管理员、教师和学生控制器的基本功能
重构路由处理并优化数据库查询
2025-12-21 22:10:48 +08:00

55 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const Score = require('../models/Score');
const Student = require('../models/Student');
class StudentService {
static async getStudentGrades(userId) {
// 先通过 userId 获取 studentId
// 假设 users.id = students.id或者通过 user_id 关联
// 根据之前的 authService我们假设 users.id 就是 students.id
// 确认学生是否存在
const student = await Student.findById(userId);
if (!student) {
throw new Error('学生信息不存在');
}
const grades = await Score.findByStudentId(userId);
// 计算统计信息
let totalCredits = 0;
let totalGradePoints = 0;
const totalCourses = grades.length;
grades.forEach(grade => {
const credit = parseFloat(grade.credit);
totalCredits += credit;
if (grade.grade_point) {
totalGradePoints += parseFloat(grade.grade_point) * credit;
}
});
const gpa = totalCredits > 0 ? (totalGradePoints / totalCredits).toFixed(2) : 0;
const averageScore = totalCourses > 0 ?
(grades.reduce((sum, g) => sum + parseFloat(g.score || 0), 0) / totalCourses).toFixed(1) : 0;
return {
grades,
statistics: {
totalCourses,
totalCredits,
gpa,
averageScore
}
};
}
static async getGradeDetails(scoreId, userId) {
const grade = await Score.findDetailsById(scoreId, userId);
if (!grade) {
throw new Error('成绩不存在');
}
return grade;
}
}
module.exports = StudentService;