75 lines
2.2 KiB
JavaScript
75 lines
2.2 KiB
JavaScript
const Score = require('../models/Score');
|
||
const Student = require('../models/Student');
|
||
|
||
const Course = require('../models/Course');
|
||
|
||
class StudentService {
|
||
static async getStudentCourses(userId) {
|
||
const student = await Student.findById(userId);
|
||
if (!student) {
|
||
throw new Error('学生信息不存在');
|
||
}
|
||
|
||
const courses = await Course.findByStudentId(userId);
|
||
return courses;
|
||
}
|
||
|
||
static async getCourseDetails(courseId) {
|
||
const course = await Course.findDetailsById(courseId);
|
||
if (!course) {
|
||
throw new Error('课程不存在');
|
||
}
|
||
return course;
|
||
}
|
||
|
||
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; |