From 17aace8fa69c48c1b987a4ddf0f01d99ddee0fd9 Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Fri, 11 Sep 2020 20:35:05 +0800 Subject: [PATCH] =?UTF-8?q?CalculationService=E6=9E=84=E5=BB=BA=EF=BC=8C?= =?UTF-8?q?=E6=9C=89=E5=BE=85=E5=AE=B6=E9=95=BF=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/student/StudentController.java | 11 +++ .../ase/repository/student/SCRepository.java | 15 ---- .../ase/service/ActivityService.java | 2 +- .../ase/service/CalculationService.java | 84 +++++++++++++++++++ 4 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/controller/student/StudentController.java delete mode 100644 src/main/java/com/codesdream/ase/repository/student/SCRepository.java create mode 100644 src/main/java/com/codesdream/ase/service/CalculationService.java diff --git a/src/main/java/com/codesdream/ase/controller/student/StudentController.java b/src/main/java/com/codesdream/ase/controller/student/StudentController.java new file mode 100644 index 0000000..c3c61a5 --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/student/StudentController.java @@ -0,0 +1,11 @@ +package com.codesdream.ase.controller.student; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("student") +public class StudentController { + + public +} diff --git a/src/main/java/com/codesdream/ase/repository/student/SCRepository.java b/src/main/java/com/codesdream/ase/repository/student/SCRepository.java deleted file mode 100644 index 4b170a4..0000000 --- a/src/main/java/com/codesdream/ase/repository/student/SCRepository.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.codesdream.ase.repository.student; - -import com.codesdream.ase.model.student.StudentCourse; -import org.springframework.data.domain.Sort; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface SCRepository extends JpaRepository { - - List findByStudentId(String studentId); - List findByStudentId(String studentId, Sort sort); -} diff --git a/src/main/java/com/codesdream/ase/service/ActivityService.java b/src/main/java/com/codesdream/ase/service/ActivityService.java index 17dcf0e..73a0c00 100644 --- a/src/main/java/com/codesdream/ase/service/ActivityService.java +++ b/src/main/java/com/codesdream/ase/service/ActivityService.java @@ -171,7 +171,7 @@ public class ActivityService { * @exception NotFoundException 对应activityId在数据库中无记录 * @see NotFoundException * @param activityId - * @return + * @return 对应活动实例 */ private Activity getActivity(int activityId){ Optional optionalActivity = activityRepository.findById(activityId); diff --git a/src/main/java/com/codesdream/ase/service/CalculationService.java b/src/main/java/com/codesdream/ase/service/CalculationService.java new file mode 100644 index 0000000..72c0ed1 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/CalculationService.java @@ -0,0 +1,84 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.exception.notfound.NotFoundException; +import com.codesdream.ase.model.student.Course; +import com.codesdream.ase.model.student.Student; +import com.codesdream.ase.model.student.StudentCourse; +import com.codesdream.ase.repository.student.CourseRepository; +import com.codesdream.ase.repository.student.StudentCourseRepository; +import com.codesdream.ase.repository.student.StudentRepository; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Optional; + +public class CalculationService { + @Resource + StudentRepository studentRepository; + + @Resource + StudentCourseRepository studentCourseRepository; + + @Resource + CourseRepository courseRepository; + + /** + * 用于计算指定学生的学分绩 + * @exception NotFoundException 课程或学生id无效 + * @param studentId 学生id + * @return 计算好的学分绩 + */ + public float calculateGPA(int studentId){ + if(checkStudentExistence(studentId)){ + throw new NotFoundException("No such student."); + } + List studentCourses = studentCourseRepository.findByStudentId(studentId); + + float sum = 0f; + float credits = 0f; + for (StudentCourse studentCourse : studentCourses){ + + Optional optionalCourse = courseRepository.findById(studentCourse.getCourseId()); + if(!optionalCourse.isPresent()){ + throw new NotFoundException("No such course."); + } + Course course = optionalCourse.get(); + sum += studentCourse.getScore() * course.getCredit(); + credits += course.getCredit(); + } + return sum / credits; + } + + public float calculateAttendanceRate(int studentId, int term){ + //Todo + return -1; + } + + public float sumUpStudentAbsence (int studentId, int term){ + //Todo + return -1; + } + + public float calculateConcernRate(int parentId){ + //Todo + return -1; + } + + public float calculateGradeChange(int studentId){ + //Todo + return -1; + } + + /** + * 私有方法用于判断学生id是否有效 + * @param studentId 学生id + * @return 是否有效 + */ + private boolean checkStudentExistence(int studentId){ + Optional optionalStudent = studentRepository.findById(studentId); + if(!optionalStudent.isPresent()){ + return false; + } + return true; + } +}