|
@@ -1,7 +1,5 @@
|
|
|
package cn.com.qmth.examcloud.service.examwork.service;
|
|
|
|
|
|
-import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
|
|
|
-
|
|
|
import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -9,22 +7,29 @@ import java.util.List;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.domain.Example;
|
|
|
-import org.springframework.data.domain.ExampleMatcher;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import cn.com.qmth.examcloud.common.dto.core.Course;
|
|
|
+import cn.com.qmth.examcloud.common.dto.core.Org;
|
|
|
+import cn.com.qmth.examcloud.common.dto.core.Student;
|
|
|
+import cn.com.qmth.examcloud.common.dto.core.User;
|
|
|
import cn.com.qmth.examcloud.common.util.ErrorMsg;
|
|
|
import cn.com.qmth.examcloud.common.util.excel.ExcelError;
|
|
|
import cn.com.qmth.examcloud.common.util.excel.ExcelReader;
|
|
|
import cn.com.qmth.examcloud.common.util.excel.ExcelReaderHandle;
|
|
|
+import cn.com.qmth.examcloud.service.examwork.assembler.ExamStudentAssembler;
|
|
|
import cn.com.qmth.examcloud.service.examwork.dao.ExamRepo;
|
|
|
import cn.com.qmth.examcloud.service.examwork.dao.ExamStudentRepo;
|
|
|
+import cn.com.qmth.examcloud.service.examwork.dto.ExamStudentDTO;
|
|
|
import cn.com.qmth.examcloud.service.examwork.entity.Exam;
|
|
|
import cn.com.qmth.examcloud.service.examwork.entity.ExamStudent;
|
|
|
+import cn.com.qmth.examcloud.service.examwork.service.rpc.CourseService;
|
|
|
+import cn.com.qmth.examcloud.service.examwork.service.rpc.OrgService;
|
|
|
+import cn.com.qmth.examcloud.service.examwork.service.rpc.StudentService;
|
|
|
|
|
|
/**
|
|
|
* 考试学生服务类
|
|
@@ -36,6 +41,14 @@ public class ExamStudentService {
|
|
|
ExamStudentRepo examStudentRepo;
|
|
|
@Autowired
|
|
|
ExamRepo examRepo;
|
|
|
+ @Autowired
|
|
|
+ ExamStudentAssembler examStudentAssembler;
|
|
|
+ @Autowired
|
|
|
+ StudentService studentService;
|
|
|
+ @Autowired
|
|
|
+ CourseService courseService;
|
|
|
+ @Autowired
|
|
|
+ OrgService orgService;
|
|
|
|
|
|
// @Value("${app.em.photo.path}")
|
|
|
// private String PHOTO_PATH ;
|
|
@@ -51,12 +64,12 @@ public class ExamStudentService {
|
|
|
* @return
|
|
|
* @return
|
|
|
*/
|
|
|
- public Page<ExamStudent> getAllExamStudent(ExamStudent examCriteria, Pageable pageable){
|
|
|
+ public Page<ExamStudent> getAllExamStudent(ExamStudentDTO examCriteria, Pageable pageable){
|
|
|
// ExampleMatcher exampleMatcher = ExampleMatcher.matching()
|
|
|
// .withMatcher("name",startsWith());
|
|
|
// Example<ExamStudent> examExamStudentple = Example.of(examCriteria, exampleMatcher);
|
|
|
// return examStudentRepo.findAll(examExamStudentple,pageable);
|
|
|
- Specification<ExamStudent> specification = getStudentSpecification(examCriteria);
|
|
|
+ Specification<ExamStudent> specification = getSpecification(examCriteria);
|
|
|
return examStudentRepo.findAll(specification,pageable);
|
|
|
}
|
|
|
|
|
@@ -66,8 +79,8 @@ public class ExamStudentService {
|
|
|
* @return
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<ExamStudent> getAllExamStudent(ExamStudent examCriteria){
|
|
|
- Specification<ExamStudent> specification = getStudentSpecification(examCriteria);
|
|
|
+ public List<ExamStudent> getAllExamStudent(ExamStudentDTO examCriteria){
|
|
|
+ Specification<ExamStudent> specification = getSpecification(examCriteria);
|
|
|
return examStudentRepo.findAll(specification);
|
|
|
}
|
|
|
|
|
@@ -122,20 +135,34 @@ public class ExamStudentService {
|
|
|
*/
|
|
|
public List<ExcelError> importExamStudent(Long examId, InputStream inputStream) {
|
|
|
Exam exam = examRepo.findOne(examId);
|
|
|
+ if(exam == null){
|
|
|
+ new RuntimeException("请先创建考试");
|
|
|
+ }
|
|
|
List<ExamStudent> examStudents = new ArrayList<ExamStudent>();
|
|
|
- ExcelReader excelReader = new ExcelReader(ExamStudent.class);
|
|
|
+ ExcelReader excelReader = new ExcelReader(ExamStudentDTO.class);
|
|
|
List<ExcelError> excelErrors = excelReader.reader(inputStream, new ExcelReaderHandle() {
|
|
|
@Override
|
|
|
public ExcelError handle(Object obj) {
|
|
|
- ExamStudent dto = (ExamStudent) obj;
|
|
|
+ ExamStudentDTO dto = (ExamStudentDTO) obj;
|
|
|
dto.setRootOrgId(exam.getOrgId());
|
|
|
dto.setExam(exam);
|
|
|
ExcelError error = importCheck(dto);
|
|
|
if (error == null) {
|
|
|
- examStudents.add(dto);
|
|
|
+ examStudents.add(examStudentAssembler.toDomain(dto));
|
|
|
}
|
|
|
if (examStudents.size() % 1000 == 0) {
|
|
|
examStudentRepo.save(examStudents);
|
|
|
+ for (ExamStudent examStudent : examStudents) {
|
|
|
+ Student student = new Student();
|
|
|
+ student.setIdentityNumber(examStudent.getIdentityNumber());
|
|
|
+ student.setName(examStudent.getName());
|
|
|
+ student.setStudentCode(examStudent.getStudentCode());
|
|
|
+ User user = new User();
|
|
|
+ user.setOrgId(examStudent.getOrgId());
|
|
|
+ user.setRootOrgId(examStudent.getRootOrgId());
|
|
|
+ student.setUser(user);
|
|
|
+ studentService.addStudent(student);
|
|
|
+ }
|
|
|
examStudents.removeAll(examStudents);
|
|
|
}
|
|
|
return error;
|
|
@@ -151,8 +178,30 @@ public class ExamStudentService {
|
|
|
* @param stu
|
|
|
* @return
|
|
|
*/
|
|
|
- public ExcelError importCheck(ExamStudent dto){
|
|
|
-
|
|
|
+ public ExcelError importCheck(ExamStudentDTO dto){
|
|
|
+ if(dto.getName()==null){
|
|
|
+ return new ExcelError("姓名不能为空");
|
|
|
+ }
|
|
|
+ if(dto.getStudentCode()==null||dto.getIdentityNumber()==null){
|
|
|
+ return new ExcelError("学号或身份证号不能为空");
|
|
|
+ }
|
|
|
+ if(dto.getCourseCode()==null){
|
|
|
+ return new ExcelError("课程代码不能为空");
|
|
|
+ }
|
|
|
+ if(dto.getOrgCode()==null){
|
|
|
+ return new ExcelError("学习中心代码不能为空");
|
|
|
+ }
|
|
|
+ Course course = courseService.findByOrgIdAndCode(dto.getRootOrgId(), dto.getCourseCode());
|
|
|
+ if(course == null || !course.getEnable()){
|
|
|
+ return new ExcelError("课程不存在/被禁用");
|
|
|
+ }
|
|
|
+ dto.setCourseLevel(course.getLevel().toString());
|
|
|
+ dto.setCourseName(course.getName());
|
|
|
+ Org org = orgService.findByParentIdAndCode(dto.getRootOrgId(), dto.getOrgCode());
|
|
|
+ if(org == null || !org.getEnable()){
|
|
|
+ return new ExcelError("学习中心不存在/禁用");
|
|
|
+ }
|
|
|
+ dto.setOrgId(org.getId());
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -168,8 +217,13 @@ public class ExamStudentService {
|
|
|
ErrorMsg errorMsg = null;
|
|
|
if(!StringUtils.isEmpty(examStudent.getStudentCode())){
|
|
|
|
|
|
+ errorMsg = new ErrorMsg(examStudent.getStudentCode());
|
|
|
}else if(!StringUtils.isEmpty(examStudent.getIdentityNumber())){
|
|
|
|
|
|
+ errorMsg = new ErrorMsg(examStudent.getIdentityNumber());
|
|
|
+ }
|
|
|
+ if(errorMsg != null){
|
|
|
+ msgs.add(errorMsg);
|
|
|
}
|
|
|
}
|
|
|
return msgs;
|
|
@@ -180,7 +234,7 @@ public class ExamStudentService {
|
|
|
* @param examCriteria
|
|
|
* @return
|
|
|
*/
|
|
|
- private Specification<ExamStudent> getStudentSpecification(ExamStudent examCriteria) {
|
|
|
+ private Specification<ExamStudent> getSpecification(ExamStudentDTO examCriteria) {
|
|
|
Specification<ExamStudent> specification = (root, query, cb) -> {
|
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
|
predicates.add(cb.equal(root.get("rootOrgId"),examCriteria.getRootOrgId()));
|
|
@@ -196,7 +250,9 @@ public class ExamStudentService {
|
|
|
if(null!=examCriteria.getStudentCode()){
|
|
|
predicates.add(cb.like(root.get("studentCode"),examCriteria.getStudentCode()));
|
|
|
}
|
|
|
- predicates.add(cb.equal(root.get("finished"),examCriteria.isFinished()));
|
|
|
+ if(null!=examCriteria.getFinished()){
|
|
|
+ predicates.add(cb.equal(root.get("finished"),examCriteria.getFinished()));
|
|
|
+ }
|
|
|
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
};
|
|
|
return specification;
|