增加了几个Repository, StaticsService构建完成
This commit is contained in:
parent
ac05b505cc
commit
31e45ec6ad
@ -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;
|
||||||
|
|
||||||
|
}
|
@ -7,6 +7,7 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@ -20,6 +21,10 @@ public class Notification {
|
|||||||
|
|
||||||
String title;
|
String title;
|
||||||
|
|
||||||
|
Date creationDate = new Date();
|
||||||
|
|
||||||
|
Date announcementDate;
|
||||||
|
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
List<File> files;
|
List<File> files;
|
||||||
|
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
package com.codesdream.ase.model.parent;
|
package com.codesdream.ase.model.parent;
|
||||||
|
|
||||||
import com.codesdream.ase.model.file.Image;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table
|
@Table
|
||||||
@Data
|
@Data
|
||||||
public class Excercise {
|
public class Exercise {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
int id;
|
int id;
|
||||||
|
|
||||||
|
int studentId;
|
||||||
|
|
||||||
// 开始时间
|
// 开始时间
|
||||||
Date start;
|
Date start;
|
||||||
|
|
@ -12,9 +12,9 @@ public class StudentCourse {
|
|||||||
@Id
|
@Id
|
||||||
int id;
|
int id;
|
||||||
|
|
||||||
String studentId;
|
int studentId;
|
||||||
|
|
||||||
String courseId;
|
int courseId;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
float score;
|
float score;
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -9,5 +9,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface HonorRepository extends JpaRepository<Honor, Integer> {
|
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);
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,14 @@ import com.codesdream.ase.model.message.Notification;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface NotificationRepository extends JpaRepository<Notification, Integer> {
|
public interface NotificationRepository extends JpaRepository<Notification, Integer> {
|
||||||
Optional<Notification> findByTitle(String title);
|
Optional<Notification> findByTitle(String title);
|
||||||
|
|
||||||
|
List<Notification> findAllByOrOrderByAnnouncementDateDesc();
|
||||||
|
|
||||||
|
List<Notification> findAllByOrOrderByCreationDateDesc();
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
161
src/main/java/com/codesdream/ase/service/StaticsService.java
Normal file
161
src/main/java/com/codesdream/ase/service/StaticsService.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@ import com.codesdream.ase.validator.GeneralValidator;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -117,9 +118,11 @@ public class StudentService {
|
|||||||
Honor honor = optionalHonor.get();
|
Honor honor = optionalHonor.get();
|
||||||
if(!description.isEmpty()){
|
if(!description.isEmpty()){
|
||||||
honor.setDescription(description);
|
honor.setDescription(description);
|
||||||
|
honor.setLastModification(new Date());
|
||||||
}
|
}
|
||||||
if(!images.isEmpty()){
|
if(!images.isEmpty()){
|
||||||
honor.setImages(images);
|
honor.setImages(images);
|
||||||
|
honor.setLastModification(new Date());
|
||||||
}
|
}
|
||||||
return honorRepository.save(honor);
|
return honorRepository.save(honor);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user