Bladeren bron

增加用户名验证

ting.yin 8 jaren geleden
bovenliggende
commit
18bfed84a3

+ 10 - 2
core-api/src/main/java/cn/com/qmth/examcloud/service/core/api/UserApi.java

@@ -95,14 +95,22 @@ public class UserApi {
         user.setScope(UserScope.ORG);
         user.setType(UserType.NOT_STUDENT);
         user.setCreateTime(new Date());
-        return new ResponseEntity(userRepo.save(user), HttpStatus.CREATED);
+        try {
+			return new ResponseEntity(userService.save(user), HttpStatus.CREATED);
+		} catch (Exception e) {
+			return new ResponseEntity(new ErrorMsg(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
+		}
     }
 
     @ApiOperation(value="更新用户",notes="更新")
     @PutMapping
     public ResponseEntity updateUser(@RequestBody User user){
         user.setUpdateTime(new Date());
-        return new ResponseEntity(userRepo.save(user), HttpStatus.OK);
+        try {
+			return new ResponseEntity(userService.save(user), HttpStatus.OK);
+		} catch (Exception e) {
+			return new ResponseEntity(new ErrorMsg(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
+		}
     }
 
     @ApiOperation(value="重置用户密码",notes="重置密码")

+ 39 - 18
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/CourseService.java

@@ -1,21 +1,20 @@
 package cn.com.qmth.examcloud.service.core.service;
 
-import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
-
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import javax.persistence.criteria.Predicate;
 import javax.transaction.Transactional;
 
 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.util.excel.ExcelError;
 import cn.com.qmth.examcloud.common.util.excel.ExcelReader;
@@ -69,22 +68,44 @@ public class CourseService {
 		return null;
 	}
 	
-	public Page<Course> findAll(Course orgCriteria, Pageable pageable) {
-		ExampleMatcher exampleMatcher = ExampleMatcher.matching()
-				.withMatcher("name", startsWith())
-				.withMatcher("code", startsWith()).withIgnoreNullValues();
-		Example<Course> examExamStudentple = Example.of(orgCriteria,
-				exampleMatcher);
-		return courseRepo.findAll(examExamStudentple, pageable);
+	public Page<Course> findAll(Course courseCriteria, Pageable pageable) {
+//		ExampleMatcher exampleMatcher = ExampleMatcher.matching()
+//				.withMatcher("name", startsWith())
+//				.withMatcher("code", startsWith()).withIgnoreNullValues();
+//		Example<Course> examExamStudentple = Example.of(orgCriteria,
+//				exampleMatcher);
+//		return courseRepo.findAll(examExamStudentple, pageable);
+		Specification<Course> specification = getSpecification(courseCriteria);
+		Page<Course> examStudents = courseRepo.findAll(specification,pageable);
+		return examStudents;
+	}
+
+	private Specification<Course> getSpecification(Course courseCriteria) {
+		Specification<Course> specification = (root, query, cb) -> {
+		    List<Predicate> predicates = new ArrayList<>();
+		    if(!StringUtils.isEmpty(courseCriteria.getOrgId())){
+		    	predicates.add(cb.equal(root.get("orgId"),courseCriteria.getOrgId()));
+		    }
+		    if(!StringUtils.isEmpty(courseCriteria.getName())){
+		    	predicates.add(cb.like(root.get("name"),"%"+courseCriteria.getName()+"%"));
+		    }
+		    if(!StringUtils.isEmpty(courseCriteria.getCode())){
+		    	predicates.add(cb.like(root.get("code"),"%"+courseCriteria.getCode()+"%"));
+		    }
+		    return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+		};
+		return specification;
 	}
 
-	public List<Course> findAll(Course orgCriteria) {
-		ExampleMatcher exampleMatcher = ExampleMatcher.matching()
-				.withMatcher("name", startsWith())
-				.withMatcher("code", startsWith()).withIgnoreNullValues();
-        Example<Course> examExample = Example.of(orgCriteria,exampleMatcher);
-		List<Course> courseList = courseRepo.findAll(examExample);
-        return courseList;
+	public List<Course> findAll(Course courseCriteria) {
+//		ExampleMatcher exampleMatcher = ExampleMatcher.matching()
+//				.withMatcher("name", startsWith())
+//				.withMatcher("code", startsWith()).withIgnoreNullValues();
+//        Example<Course> examExample = Example.of(orgCriteria,exampleMatcher);
+//		List<Course> courseList = courseRepo.findAll(examExample);
+//        return courseList;
+		Specification<Course> specification = getSpecification(courseCriteria);
+        return courseRepo.findAll(specification);
 	}
 
 	public Course save(Course course) throws Exception{

+ 9 - 3
core-api/src/main/java/cn/com/qmth/examcloud/service/core/service/UserService.java

@@ -9,8 +9,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
-import cn.com.qmth.examcloud.common.uac.enums.RoleMeta;
-import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Example;
@@ -21,6 +19,7 @@ import org.springframework.stereotype.Service;
 
 import cn.com.qmth.examcloud.common.uac.AccessCtrlUtil;
 import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
+import cn.com.qmth.examcloud.common.uac.enums.RoleMeta;
 import cn.com.qmth.examcloud.common.util.RedisUtil;
 import cn.com.qmth.examcloud.service.core.dto.UserInfo;
 import cn.com.qmth.examcloud.service.core.entity.Org;
@@ -33,7 +32,6 @@ import cn.com.qmth.examcloud.service.core.repo.OrgRepo;
 import cn.com.qmth.examcloud.service.core.repo.StudentRepo;
 import cn.com.qmth.examcloud.service.core.repo.UserRepo;
 import cn.com.qmth.examcloud.service.core.repo.UserRoleRepo;
-import org.springframework.util.CollectionUtils;
 
 /**
  * 用户服务类
@@ -200,4 +198,12 @@ public class UserService {
 		return userRepo.findMarkerByRootOrgId(rootOrgId);
 	}
 
+	public User save(User user) throws Exception{
+		User old = userRepo.findByRootOrgIdAndLoginName(user.getRootOrgId(), user.getLoginName());
+		if(old!=null){
+			throw new RuntimeException("用户名已存在");
+		}
+		return userRepo.save(user);
+	}
+
 }

+ 2 - 1
core-domain/src/main/java/cn/com/qmth/examcloud/service/core/repo/CourseRepo.java

@@ -3,11 +3,12 @@ package cn.com.qmth.examcloud.service.core.repo;
 import java.util.List;
 
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.repository.query.QueryByExampleExecutor;
 
 import cn.com.qmth.examcloud.service.core.entity.Course;
 
-public interface CourseRepo extends JpaRepository<Course, Long>,QueryByExampleExecutor<Course> {
+public interface CourseRepo extends JpaRepository<Course, Long>,QueryByExampleExecutor<Course>, JpaSpecificationExecutor<Course> {
 
 	List<Course> findByOrgId(Long orgId);