Selaa lähdekoodia

修改考生相关bug

宋悦 8 vuotta sitten
vanhempi
commit
4408806c2f

+ 21 - 9
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/api/ExamStudentApi.java

@@ -104,21 +104,33 @@ public class ExamStudentApi {
     @PostMapping()
     public ResponseEntity addExamStudent(HttpServletRequest request,
                                          @RequestBody ExamStudent examStudent) {
-        AccessUser accessUser = (AccessUser) request.getAttribute("accessUser");
-        if(accessUser != null){
-            examStudent.setRootOrgId(accessUser.getRootOrgId());
+        try {
+            AccessUser accessUser = (AccessUser) request.getAttribute("accessUser");
+            if (accessUser != null) {
+                examStudent.setRootOrgId(accessUser.getRootOrgId());
+            }
+            Exam exam = examRepo.findOne(examStudent.getExam().getId());
+            examStudent.setExam(exam);
+            ExamStudent saveExamStu = examStudentService.saveExamStudent(examStudent);
+            return new ResponseEntity(saveExamStu, HttpStatus.OK);
+        } catch (Exception e){
+            e.printStackTrace();
+            return new ResponseEntity(new ErrorMsg(e.getMessage()),HttpStatus.INTERNAL_SERVER_ERROR);
         }
-        Exam exam = examRepo.findOne(examStudent.getExam().getId());
-        examStudent.setExam(exam);
-        return new ResponseEntity<ExamStudent>(examStudentService.saveExamStudent(examStudent), HttpStatus.OK);
     }
 
     @ApiOperation(value = "更新考试学生", notes = "更新")
     @PutMapping()
     public ResponseEntity updateExamStudent(@RequestBody ExamStudent examStudent) {
-        Exam exam = examRepo.findOne(examStudent.getExam().getId());
-        examStudent.setExam(exam);
-        return new ResponseEntity(examStudentService.saveExamStudent(examStudent), HttpStatus.OK);
+        try {
+            Exam exam = examRepo.findOne(examStudent.getExam().getId());
+            examStudent.setExam(exam);
+            ExamStudent saveExamStu = examStudentService.saveExamStudent(examStudent);
+            return new ResponseEntity(saveExamStu, HttpStatus.OK);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ResponseEntity(new ErrorMsg(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
+        }
     }
 
     @ApiOperation(value = "更新考试学生缺考状态", notes = "更新缺考")

+ 4 - 2
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/service/ExamService.java

@@ -95,13 +95,15 @@ public class ExamService {
 
     public boolean checkExamName(Exam exam){
         if(exam.getId()!=null){
-            if(examRepo.countByNameAndIdNot(exam.getName(),exam.getId()) > 0){
+            if(examRepo.countByNameAndRootOrgIdAndIdNot(exam.getName(),
+                                                        exam.getRootOrgId(),
+                                                        exam.getId()) > 0){
                 return false;
             }else{
                 return true;
             }
         }else{
-            if(examRepo.countByName(exam.getName()) > 0){
+            if(examRepo.countByNameAndRootOrgId(exam.getName(),exam.getRootOrgId()) > 0){
                 return false;
             }else{
                 return true;

+ 17 - 2
exam-work-api/src/main/java/cn/com/qmth/examcloud/service/examwork/service/ExamStudentService.java

@@ -119,7 +119,8 @@ public class ExamStudentService {
      * @param examStudent
      * @return
      */
-    public ExamStudent saveExamStudent(ExamStudent examStudent){
+    public ExamStudent saveExamStudent(ExamStudent examStudent)throws Exception{
+    	checkExamStudent(examStudent);
         return saveStudent(examStudent);
     }
 
@@ -189,12 +190,14 @@ public class ExamStudentService {
 		examStudents.removeAll(examStudents);
 	}
 
-	private ExamStudent saveStudent(ExamStudent examStudent) {
+	private ExamStudent saveStudent(ExamStudent examStudent){
 		if(examStudent.getStudentId() == null){
 			Student student = new Student();
 			student.setIdentityNumber(examStudent.getIdentityNumber());
 			student.setName(examStudent.getName());
 			student.setStudentCode(examStudent.getStudentCode());
+			student.setRootOrgId(examStudent.getRootOrgId());
+			student.setOrgId(examStudent.getOrgId());
 			User user = new User();
 			user.setOrgId(examStudent.getOrgId());
 			user.setEnable(true);
@@ -210,6 +213,18 @@ public class ExamStudentService {
 		examStudent.setRepair(false);
 		return examStudentRepo.save(examStudent);
 	}
+
+	public void checkExamStudent(ExamStudent examStudent){
+		if(examStudent.getId() != null){
+			if(examStudentRepo.checkExamStuById(examStudent.getRootOrgId(),examStudent.getIdentityNumber(),examStudent.getId()) > 0){
+				throw new RuntimeException("该考生已存在");
+			}
+		}else{
+			if(examStudentRepo.checkExamStu(examStudent.getRootOrgId(),examStudent.getIdentityNumber()) > 0){
+				throw new RuntimeException("该考生已存在");
+			}
+		}
+	}
 	
 	/**
 	 * 考生导入验证

+ 4 - 0
exam-work-domain/src/main/java/cn/com/qmth/examcloud/service/examwork/dao/ExamRepo.java

@@ -17,5 +17,9 @@ public interface ExamRepo extends JpaRepository<Exam, Long>,QueryByExampleExecut
 
 	Long countByName(String name);
 
+	Long countByNameAndRootOrgId(String name,Long rootOrgId);
+
 	Long countByNameAndIdNot(String name,Long id);
+
+	Long countByNameAndRootOrgIdAndIdNot(String name,Long rootOrgId,Long id);
 }

+ 6 - 0
exam-work-domain/src/main/java/cn/com/qmth/examcloud/service/examwork/dao/ExamStudentRepo.java

@@ -32,4 +32,10 @@ public interface ExamStudentRepo extends JpaRepository<ExamStudent, Long>, Query
 
     @Query("select s from ExamStudent s where s.exam.id = ?1 and s.courseCode = ?2 and (s.studentCode = ?3 or s.identityNumber = ?3)")
     ExamStudent findFirstByExamId(Long examId,String courseCode,String stuNumber);
+
+    @Query("select count(s) from ExamStudent s where s.rootOrgId=?1 and s.identityNumber = ?2 and s.id != ?3")
+    int checkExamStuById(Long rootOrgId,String identityNumber,Long id);
+
+    @Query("select count(s) from ExamStudent s where s.rootOrgId=?1 and s.identityNumber = ?2")
+    int checkExamStu(Long rootOrgId,String identityNumber);
 }