From 31e45ec6ad2b7858986003cb099c720e4dd7b877 Mon Sep 17 00:00:00 2001 From: chuyan <1047381936@qq.com> Date: Fri, 11 Sep 2020 16:37:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=87=A0=E4=B8=AARe?= =?UTF-8?q?pository,=20StaticsService=E6=9E=84=E5=BB=BA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ase/component/student/SubjectScore.java | 20 +++ .../ase/model/message/Notification.java | 5 + .../parent/{Excercise.java => Exercise.java} | 6 +- .../ase/model/student/StudentCourse.java | 4 +- .../repository/parent/ExerciseRepository.java | 12 ++ .../repository/student/HonorRepository.java | 4 +- .../student/NotificationRepository.java | 5 + .../student/StudentCourseRepository.java | 14 ++ .../ase/service/StaticsService.java | 161 ++++++++++++++++++ .../ase/service/StudentService.java | 5 +- 10 files changed, 229 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/component/student/SubjectScore.java rename src/main/java/com/codesdream/ase/model/parent/{Excercise.java => Exercise.java} (78%) create mode 100644 src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java create mode 100644 src/main/java/com/codesdream/ase/repository/student/StudentCourseRepository.java create mode 100644 src/main/java/com/codesdream/ase/service/StaticsService.java diff --git a/src/main/java/com/codesdream/ase/component/student/SubjectScore.java b/src/main/java/com/codesdream/ase/component/student/SubjectScore.java new file mode 100644 index 0000000..10e80e7 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/student/SubjectScore.java @@ -0,0 +1,20 @@ +package com.codesdream.ase.component.student; + +import lombok.Data; + +import java.util.Date; + +@Data +public class SubjectScore { + + private int studentId; + + private String subject; + + private float credit; + + private float score; + + private Date finishedDate; + +} diff --git a/src/main/java/com/codesdream/ase/model/message/Notification.java b/src/main/java/com/codesdream/ase/model/message/Notification.java index 33c2322..81c1d15 100644 --- a/src/main/java/com/codesdream/ase/model/message/Notification.java +++ b/src/main/java/com/codesdream/ase/model/message/Notification.java @@ -7,6 +7,7 @@ import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; +import java.util.Date; import java.util.List; @Entity @@ -20,6 +21,10 @@ public class Notification { String title; + Date creationDate = new Date(); + + Date announcementDate; + @ManyToMany List files; diff --git a/src/main/java/com/codesdream/ase/model/parent/Excercise.java b/src/main/java/com/codesdream/ase/model/parent/Exercise.java similarity index 78% rename from src/main/java/com/codesdream/ase/model/parent/Excercise.java rename to src/main/java/com/codesdream/ase/model/parent/Exercise.java index ce76faa..065f2e3 100644 --- a/src/main/java/com/codesdream/ase/model/parent/Excercise.java +++ b/src/main/java/com/codesdream/ase/model/parent/Exercise.java @@ -1,22 +1,22 @@ package com.codesdream.ase.model.parent; -import com.codesdream.ase.model.file.Image; import lombok.Data; import javax.persistence.*; import java.sql.Time; import java.util.Date; -import java.util.List; @Entity @Table @Data -public class Excercise { +public class Exercise { @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; + int studentId; + // 开始时间 Date start; diff --git a/src/main/java/com/codesdream/ase/model/student/StudentCourse.java b/src/main/java/com/codesdream/ase/model/student/StudentCourse.java index c82d6d1..826a017 100644 --- a/src/main/java/com/codesdream/ase/model/student/StudentCourse.java +++ b/src/main/java/com/codesdream/ase/model/student/StudentCourse.java @@ -12,9 +12,9 @@ public class StudentCourse { @Id int id; - String studentId; + int studentId; - String courseId; + int courseId; @Column(nullable = false) float score; diff --git a/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java b/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java new file mode 100644 index 0000000..bd1ef7f --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.repository.parent; + +import com.codesdream.ase.model.parent.Exercise; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ExerciseRepository extends JpaRepository { + List findByStudentId(int studentId); +} diff --git a/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java b/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java index 68dfa79..ae08c8b 100644 --- a/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java +++ b/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java @@ -9,5 +9,7 @@ import java.util.List; @Repository public interface HonorRepository extends JpaRepository { - List findByStudentId(String studentId, Sort sort); + + List findByStudentId(int studentId); + List findByStudentId(int studentId, Sort sort); } diff --git a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java b/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java index b8220b2..e0851fd 100644 --- a/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java +++ b/src/main/java/com/codesdream/ase/repository/student/NotificationRepository.java @@ -4,9 +4,14 @@ import com.codesdream.ase.model.message.Notification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @Repository public interface NotificationRepository extends JpaRepository { Optional findByTitle(String title); + + List findAllByOrOrderByAnnouncementDateDesc(); + + List findAllByOrOrderByCreationDateDesc(); } diff --git a/src/main/java/com/codesdream/ase/repository/student/StudentCourseRepository.java b/src/main/java/com/codesdream/ase/repository/student/StudentCourseRepository.java new file mode 100644 index 0000000..ad804f8 --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/student/StudentCourseRepository.java @@ -0,0 +1,14 @@ +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 StudentCourseRepository extends JpaRepository { + List findByStudentId(int studentId); + List findByStudentId(int studentId, Sort sort); +} diff --git a/src/main/java/com/codesdream/ase/service/StaticsService.java b/src/main/java/com/codesdream/ase/service/StaticsService.java new file mode 100644 index 0000000..f41fade --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/StaticsService.java @@ -0,0 +1,161 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.component.student.SubjectScore; +import com.codesdream.ase.exception.notfound.NotFoundException; +import com.codesdream.ase.model.message.Notification; +import com.codesdream.ase.model.parent.Exercise; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.model.permission.UserDetail; +import com.codesdream.ase.model.student.Course; +import com.codesdream.ase.model.student.Honor; +import com.codesdream.ase.model.student.Student; +import com.codesdream.ase.model.student.StudentCourse; +import com.codesdream.ase.repository.parent.ExerciseRepository; +import com.codesdream.ase.repository.permission.UserRepository; +import com.codesdream.ase.repository.student.*; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Service +public class StaticsService { + + @Resource + StudentCourseRepository scRepo; + + @Resource + UserRepository userRepository; + + @Resource + CourseRepository courseRepository; + + @Resource + StudentRepository studentRepository; + + @Resource + HonorRepository honorRepository; + + @Resource + NotificationRepository notificationRepository; + + @Resource + ExerciseRepository exerciseRepository; + + + /** + * 根据给定排序方式查询某学生的成绩情况 + * @see SubjectScore + * @see StudentCourse + * @param studentId 学生id + * @param flag 指定排序方式,0表示按学期排序,1表示按分数排序,2表示按照学分排序,否则表示不排序 + * @return + */ + public List displayScore (int studentId, int flag){ + + Sort sort; + switch (flag){ + case 0:{ + sort = Sort.by(Sort.Direction.ASC, "term"); + break; + } + case 1:{ + sort = Sort.by(Sort.Direction.ASC, "score"); + break; + } + case 2:{ + sort = Sort.by(Sort.Direction.ASC, "credit"); + break; + } + default: + sort = null; + } + List studentCourseList = new ArrayList<>(); + if(sort == null){ + studentCourseList = scRepo.findByStudentId(studentId); + }else{ + studentCourseList = scRepo.findByStudentId(studentId, sort); + } + if(studentCourseList.isEmpty()){ + return null; + } + List subjectScores = new ArrayList<>(); + for(StudentCourse studentCourse : studentCourseList){ + + Course course = courseRepository.findById(studentCourse.getCourseId()).get(); + SubjectScore sc = new SubjectScore(); + + sc.setCredit(course.getCredit()); + sc.setFinishedDate(studentCourse.getFinishedDate()); + sc.setScore(studentCourse.getScore()); + sc.setStudentId(studentCourse.getStudentId()); + + subjectScores.add(sc); + } + return subjectScores; + } + + /** + * 查询学生荣誉信息,按照荣誉创建时间排序 + * @param studentId 学生id + * @return 荣誉列表 + */ + public List displayHonor(int studentId){ + if(!checkStudentExistence(studentId)){ + throw new NotFoundException("No such student."); + } + return honorRepository.findByStudentId(studentId, Sort.by(Sort.Direction.DESC, "creationDate")); + } + + /** + * 显示所有公告,默认按照公告发布时间排序 + * @return 公告列表 + */ + public List displayNotification(){ + return notificationRepository.findAllByOrOrderByAnnouncementDateDesc(); + } + + /** + * 显示学生锻炼情况 + * @exception NotFoundException 如果学生id不存在则抛出此异常 + * @param studentId 学生id + * @return 锻炼情况列表 + */ + public List displayExercise(int studentId){ + if(!checkStudentExistence(studentId)){ + throw new NotFoundException("No such student."); + } + return exerciseRepository.findByStudentId(studentId); + } + + /** + * 显示学生详细信息 + * @exception NotFoundException 如果学生id不存在则抛出此异常 + * @param studentId 学生id + * @return 学生详细信息 + */ + public UserDetail displayStudentInfo(int studentId){ + if(!checkStudentExistence(studentId)){ + throw new NotFoundException("No such student."); + } + User user = userRepository.findById(studentId).get(); + return user.getUserDetail(); + + } + + /** + * 私有方法查询学生id是否存在 + * @param studentId 学生id + * @return id存在返回true,否则返回false + */ + private boolean checkStudentExistence(int studentId){ + Optional optionalStudent = studentRepository.findById(studentId); + if(!optionalStudent.isPresent()){ + return false; + } + return true; + } +} \ No newline at end of file diff --git a/src/main/java/com/codesdream/ase/service/StudentService.java b/src/main/java/com/codesdream/ase/service/StudentService.java index 44837ff..bf380bb 100644 --- a/src/main/java/com/codesdream/ase/service/StudentService.java +++ b/src/main/java/com/codesdream/ase/service/StudentService.java @@ -17,6 +17,7 @@ import com.codesdream.ase.validator.GeneralValidator; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Optional; @@ -117,9 +118,11 @@ public class StudentService { Honor honor = optionalHonor.get(); if(!description.isEmpty()){ honor.setDescription(description); + honor.setLastModification(new Date()); } if(!images.isEmpty()){ - honor.setImages(images); + honor.setImages(images); + honor.setLastModification(new Date()); } return honorRepository.save(honor); }