const Course = require('../models/Course'); const Score = require('../models/Score'); const Student = require('../models/Student'); class TeacherService { static async getCourses(teacherId) { return await Course.findByTeacherId(teacherId); } static async addScore(teacherId, scoreData) { const { studentId, courseId, score } = scoreData; // 验证学生 const student = await Student.findById(studentId); if (!student) { throw new Error('学生不存在'); } // 验证课程(可选:验证是否是该教师的课程) // const course = await Course.findById(courseId); // 检查重复 const existingScore = await Score.findByStudentAndCourse(studentId, courseId); if (existingScore) { throw new Error('该学生此课程成绩已存在'); } // 计算绩点和等级 const numericScore = parseFloat(score); let gradePoint = 0; let gradeLevel = 'F'; if (numericScore >= 90) { gradePoint = 4.0; gradeLevel = 'A'; } else if (numericScore >= 80) { gradePoint = 3.0; gradeLevel = 'B'; } else if (numericScore >= 70) { gradePoint = 2.0; gradeLevel = 'C'; } else if (numericScore >= 60) { gradePoint = 1.0; gradeLevel = 'D'; } const fullScoreData = { ...scoreData, teacherId, gradePoint, gradeLevel }; const gradeId = await Score.create(fullScoreData); return gradeId; } } module.exports = TeacherService;