Explorar o código

Merge remote-tracking branch 'origin/master'

宋悦 %!s(int64=8) %!d(string=hai) anos
pai
achega
35486392bf

+ 91 - 86
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/StudentService.java

@@ -36,7 +36,7 @@ import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatc
  */
 @Service
 public class StudentService {
-	
+
     @Autowired
     StudentRepo studentRepo;
     @Autowired
@@ -45,29 +45,31 @@ public class StudentService {
     UserService userService;
     @Autowired
     OrgRepo orgRepo;
-    
+
     private static final String JPG = ".jpg";
 
     /**
      * 获取所有学生(分页)
+     *
      * @param studentCriteria
      * @param pageable
      * @return
      */
-    public Page<Student> getAllStudent(Student studentCriteria, Pageable pageable){
+    public Page<Student> getAllStudent(Student studentCriteria, Pageable pageable) {
         ExampleMatcher exampleMatcher = ExampleMatcher.matching()
-				.withMatcher("name",contains())
-				.withMatcher("studentCode",contains()).withIgnoreNullValues();
+                .withMatcher("name", contains())
+                .withMatcher("studentCode", contains()).withIgnoreNullValues();
         Example<Student> studentExample = Example.of(studentCriteria, exampleMatcher);
-        return studentRepo.findAll(studentExample,pageable);
+        return studentRepo.findAll(studentExample, pageable);
     }
 
     /**
      * 获取所有学生
+     *
      * @param studentCriteria
      * @return
      */
-    public List<Student> getAllStudent(Student studentCriteria){
+    public List<Student> getAllStudent(Student studentCriteria) {
         ExampleMatcher exampleMatcher = ExampleMatcher.matching();
         Example<Student> studentExample = Example.of(studentCriteria, exampleMatcher);
         return studentRepo.findAll(studentExample);
@@ -75,103 +77,106 @@ public class StudentService {
 
     /**
      * 绑定学生照片
+     *
      * @return
      */
-	public List<ErrorMsg> photopBundled(Long rootOrgId, File directory) {
-		List<ErrorMsg> list = new ArrayList<ErrorMsg>();
-		if(directory.isDirectory()){
-			for (File file : directory.listFiles()) {
-				if(!file.getName().endsWith(JPG)){
-					list.add(new ErrorMsg(file.getName()+"格式错误,文件名格式必须是jpg(小写)"));
-				}
-				String fileName = file.getName().split(JPG)[0];
-				//先查学号
-				Student student = studentRepo.findByUserRootOrgIdAndStudentCode(rootOrgId,fileName);
-				if(student == null){//学号不存在查身份证号
-					student = studentRepo.findByIdentityNumber(fileName);
-				}
-				if(student!=null ){//学生存在则绑定照片
-					student.setPhotoPath(student.getUser().getRootOrgId()+File.separator+"photo"+ File.separator+fileName);
-					studentRepo.save(student);
-				}
-			}
-		}else{
-			list.add(new ErrorMsg("文件路径不正确"));
-		}
-		return list;
-	}
+    public List<ErrorMsg> photopBundled(Long rootOrgId, File directory) {
+        List<ErrorMsg> list = new ArrayList<ErrorMsg>();
+        if (directory.isDirectory()) {
+            for (File file : directory.listFiles()) {
+                if (!file.getName().endsWith(JPG)) {
+                    list.add(new ErrorMsg(file.getName() + "格式错误,文件名格式必须是jpg(小写)"));
+                }
+                String fileName = file.getName().split(JPG)[0];
+                //先查学号
+                Student student = studentRepo.findByUserRootOrgIdAndStudentCode(rootOrgId, fileName);
+                if (student == null) {//学号不存在查身份证号
+                    student = studentRepo.findByIdentityNumber(fileName);
+                }
+                if (student != null) {//学生存在则绑定照片
+                    student.setPhotoPath(student.getUser().getRootOrgId() + File.separator + "photo" + File.separator + fileName);
+                    studentRepo.save(student);
+                }
+            }
+        } else {
+            list.add(new ErrorMsg("文件路径不正确"));
+        }
+        return list;
+    }
 
-	public Student save(Student student) {
-		if(student.getUser()==null || null==student.getUser().getId()){//判断是否有用户
-			//判断是否有该学生,
-			Student domain = studentRepo.findByUserRootOrgIdAndStudentCode(student.getUser().getRootOrgId(), student.getStudentCode());
-			if(domain!=null){//学号查找不为空,更新身份证号
-				domain.setIdentityNumber(student.getIdentityNumber());
-				domain.setUpdateTime(new Date());
-				return studentRepo.save(domain);
-			}
-			Student entity = studentRepo.findByIdentityNumber(student.getIdentityNumber());
-			if(entity!=null){//身份证查找不为空,更新学号
-				entity.setStudentCode(student.getStudentCode());
-				entity.setUpdateTime(new Date());
-				return studentRepo.save(entity);
-			}
-			//新建用户和学生
-			User user=new User(student.getName(), UserScope.ORG, student.getUser().getRootOrgId(), student.getUser().getOrgId(), UserType.STUDENT);
-			String password = null;
-			if(!StringUtils.isEmpty(student.getStudentCode())){//学号后6位
-				user.setLoginName(student.getStudentCode());
-				password = student.getStudentCode().substring(student.getStudentCode().length()-6, student.getStudentCode().length());
-			}else{//身份证号后6位
-				user.setLoginName(student.getIdentityNumber());
-				password = student.getIdentityNumber().substring(student.getIdentityNumber().length()-6,student.getIdentityNumber().length());
-			}
-			user.setPassword(password);
-			userRepo.save(user);
-			student.setUser(user);
-		}
-		return studentRepo.save(student);
-	}
+    public Student save(Student student) {
+        if (student.getUser() == null || null == student.getUser().getId()) {//判断是否有用户
+            //判断是否有该学生,
+            Student domain = studentRepo.findByUserRootOrgIdAndStudentCode(student.getUser().getRootOrgId(), student.getStudentCode());
+            if (domain != null) {//学号查找不为空,更新身份证号
+                domain.setIdentityNumber(student.getIdentityNumber());
+                domain.setUpdateTime(new Date());
+                return studentRepo.save(domain);
+            }
+            Student entity = studentRepo.findByIdentityNumber(student.getIdentityNumber());
+            if (entity != null) {//身份证查找不为空,更新学号
+                entity.setStudentCode(student.getStudentCode());
+                entity.setUpdateTime(new Date());
+                return studentRepo.save(entity);
+            }
+            //新建用户和学生
+            User user = new User(student.getName(), UserScope.ORG, student.getUser().getRootOrgId(), student.getUser().getOrgId(), UserType.STUDENT);
+            user.setEnable(student.getUser().getEnable() == null ? true : student.getUser().getEnable());
+            String password = null;
+            if (!StringUtils.isEmpty(student.getStudentCode())) {//学号后6位
+                user.setLoginName(student.getStudentCode());
+                password = student.getStudentCode().substring(student.getStudentCode().length() - 6, student.getStudentCode().length());
+            } else {//身份证号后6位
+                user.setLoginName(student.getIdentityNumber());
+                password = student.getIdentityNumber().substring(student.getIdentityNumber().length() - 6, student.getIdentityNumber().length());
+            }
+            user.setPassword(password);
+            userRepo.save(user);
+            student.setUser(user);
+        }
+        return studentRepo.save(student);
+    }
 
     /**
      * 二级登录
+     *
      * @param orgId
      * @param loginName
      * @param password
      * @return
      */
-    public ResponseEntity<?> login(String orgId,String loginName,
-                                String password,LoginType loginType){
-    	Org org = orgRepo.findByParentIdAndCode((long)0, orgId);
-    	if(org == null){
-    		return new ResponseEntity(new ErrorMsg("学校不存在"),HttpStatus.NOT_FOUND);
-    	}
-    	Student student = null;
-    	if(LoginType.STUDENT_CODE.equals(loginType)){
-    		student = studentRepo.findByUserRootOrgIdAndStudentCode(org.getId(), loginName);
-    	}
-    	if(LoginType.IDENTITY_NUMBER.equals(loginType)){
-    		student = studentRepo.findByIdentityNumber(loginName);
-    	}
-    	if(student != null){
-    		return this.loginProcess(student,password);
-    	}
+    public ResponseEntity<?> login(String orgId, String loginName,
+                                   String password, LoginType loginType) {
+        Org org = orgRepo.findByParentIdAndCode((long) 0, orgId);
+        if (org == null) {
+            return new ResponseEntity(new ErrorMsg("学校不存在"), HttpStatus.NOT_FOUND);
+        }
+        Student student = null;
+        if (LoginType.STUDENT_CODE.equals(loginType)) {
+            student = studentRepo.findByUserRootOrgIdAndStudentCode(org.getId(), loginName);
+        }
+        if (LoginType.IDENTITY_NUMBER.equals(loginType)) {
+            student = studentRepo.findByIdentityNumber(loginName);
+        }
+        if (student != null) {
+            return this.loginProcess(student, password);
+        }
         return userService.loginProcess(null, password);
     }
 
-	private ResponseEntity<?> loginProcess(Student student, String password) {
-		if(student.getUser() == null){
-            return new ResponseEntity(new ErrorMsg("该用户不存在"),HttpStatus.NOT_FOUND);
-        }else if(!student.getUser().getPassword().equals(password)){
-            return new ResponseEntity(new ErrorMsg("密码错误"),HttpStatus.EXPECTATION_FAILED);
-        }else{
+    private ResponseEntity<?> loginProcess(Student student, String password) {
+        if (student.getUser() == null) {
+            return new ResponseEntity(new ErrorMsg("该用户不存在"), HttpStatus.NOT_FOUND);
+        } else if (!student.getUser().getPassword().equals(password)) {
+            return new ResponseEntity(new ErrorMsg("密码错误"), HttpStatus.EXPECTATION_FAILED);
+        } else {
             String token = AccessCtrlUtil.buildToken();
             userService.createAccessUser(token, student.getUser(), student.getId());
-            UserInfo userInfo=userService.getUserInfo(student.getUser(),token);
+            UserInfo userInfo = userService.getUserInfo(student.getUser(), token);
             userInfo.setStudentId(student.getId());
             userInfo.setPhotoPath(student.getPhotoPath());
-            return new ResponseEntity(userInfo,HttpStatus.OK);
+            return new ResponseEntity(userInfo, HttpStatus.OK);
         }
-	}
+    }
 
 }