增加了几个Repository, StaticsService构建完成

This commit is contained in:
chuyan 2020-09-11 16:37:27 +08:00
parent ac05b505cc
commit 31e45ec6ad
10 changed files with 229 additions and 7 deletions

View File

@ -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;
}

View File

@ -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<File> files;

View File

@ -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;

View File

@ -12,9 +12,9 @@ public class StudentCourse {
@Id
int id;
String studentId;
int studentId;
String courseId;
int courseId;
@Column(nullable = false)
float score;

View File

@ -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<Exercise, Integer> {
List<Exercise> findByStudentId(int studentId);
}

View File

@ -9,5 +9,7 @@ import java.util.List;
@Repository
public interface HonorRepository extends JpaRepository<Honor, Integer> {
List<Honor> findByStudentId(String studentId, Sort sort);
List<Honor> findByStudentId(int studentId);
List<Honor> findByStudentId(int studentId, Sort sort);
}

View File

@ -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<Notification, Integer> {
Optional<Notification> findByTitle(String title);
List<Notification> findAllByOrOrderByAnnouncementDateDesc();
List<Notification> findAllByOrOrderByCreationDateDesc();
}

View File

@ -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<StudentCourse, Integer> {
List<StudentCourse> findByStudentId(int studentId);
List<StudentCourse> findByStudentId(int studentId, Sort sort);
}

View File

@ -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<SubjectScore> 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<StudentCourse> studentCourseList = new ArrayList<>();
if(sort == null){
studentCourseList = scRepo.findByStudentId(studentId);
}else{
studentCourseList = scRepo.findByStudentId(studentId, sort);
}
if(studentCourseList.isEmpty()){
return null;
}
List<SubjectScore> 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<Honor> 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<Notification> displayNotification(){
return notificationRepository.findAllByOrOrderByAnnouncementDateDesc();
}
/**
* 显示学生锻炼情况
* @exception NotFoundException 如果学生id不存在则抛出此异常
* @param studentId 学生id
* @return 锻炼情况列表
*/
public List<Exercise> 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<Student> optionalStudent = studentRepository.findById(studentId);
if(!optionalStudent.isPresent()){
return false;
}
return true;
}
}

View File

@ -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);
}