Files
WebWork/backend/services/studentService.js
祀梦 59501e514f feat(学生): 添加学生课程管理功能
- 新增我的课程页面及路由
- 实现课程列表和详情查看功能
- 更新导航链接指向新页面
- 添加成绩详情模态框
- 完善相关后端接口和服务
2025-12-22 19:48:38 +08:00

75 lines
2.2 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');
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;