WANG 6 жил өмнө
parent
commit
85bef6e619

+ 123 - 0
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/UserController.java

@@ -20,6 +20,7 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort;
+import org.springframework.data.domain.Sort.Direction;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.DeleteMapping;
@@ -217,6 +218,128 @@ public class UserController extends ControllerSupport {
 		return ret;
 	}
 
+	@ApiOperation(value = "模糊查询用户", notes = "")
+	@GetMapping("query")
+	public List<UserDomain> query(@RequestParam(required = false) Long rootOrgId,
+			@RequestParam String loginName, @RequestParam String name,
+			@RequestParam(required = false) Boolean enable,
+			@RequestParam(required = false) Long roleId, @RequestParam(required = false) Long orgId,
+			@RequestParam(required = false) String roleCode) {
+
+		User accessUser = getAccessUser();
+
+		if (null == rootOrgId) {
+			rootOrgId = accessUser.getRootOrgId();
+		} else {
+			validateRootOrgIsolation(rootOrgId);
+		}
+
+		final Long finalRootOrgId = rootOrgId;
+
+		OrgEntity rootOrg = orgRepo.findOne(rootOrgId);
+		if (null == rootOrg) {
+			throw new StatusException("B-150003", "机构不存在");
+		}
+		if (null != rootOrg.getParentId()) {
+			throw new StatusException("B-150004", "机构错误");
+		}
+
+		if (null != roleId) {
+			RoleEntity roleEntity = roleRepo.findOne(roleId);
+			if (null == roleEntity) {
+				throw new StatusException("B-150002", "角色不存在");
+			}
+		} else if (StringUtils.isNotBlank(roleCode)) {
+			RoleEntity roleEntity = roleRepo.findByCodeAndRootOrgIdIsNull(roleCode.trim());
+			if (null == roleEntity) {
+				roleEntity = roleRepo.findByCodeAndRootOrgId(roleCode.trim(),
+						accessUser.getRootOrgId());
+			}
+			if (null == roleEntity) {
+				throw new StatusException("B-150002", "角色不存在");
+			}
+			roleId = roleEntity.getId();
+		}
+
+		final Long finalRoleId = roleId;
+
+		Specification<UserEntity> specification = (root, query, cb) -> {
+			List<Predicate> predicates = new ArrayList<>();
+			predicates.add(cb.equal(root.get("rootOrgId"), finalRootOrgId));
+
+			if (StringUtils.isNotBlank(loginName)) {
+				predicates.add(cb.like(root.get("loginName"), toSqlSearchPattern(loginName)));
+			}
+			if (StringUtils.isNotBlank(name)) {
+				predicates.add(cb.like(root.get("name"), toSqlSearchPattern(name)));
+			}
+			if (null != orgId) {
+				predicates.add(cb.equal(root.get("orgId"), orgId));
+			}
+			if (null != finalRoleId) {
+				Subquery<UserRoleRelationEntity> subquery = query
+						.subquery(UserRoleRelationEntity.class);
+				Root<UserRoleRelationEntity> subRoot = subquery.from(UserRoleRelationEntity.class);
+				subquery.select(subRoot.get("userId"));
+				Predicate p1 = cb.equal(subRoot.get("roleId"), finalRoleId);
+				Predicate p2 = cb.equal(subRoot.get("userId"), root.get("id"));
+				subquery.where(cb.and(p1, p2));
+				predicates.add(cb.exists(subquery));
+			}
+			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+		};
+
+		PageRequest pageRequest = new PageRequest(0, 50, new Sort(Direction.DESC, "updateTime"));
+
+		Page<UserEntity> userList = userRepo.findAll(specification, pageRequest);
+
+		Iterator<UserEntity> iterator = userList.iterator();
+
+		List<UserDomain> fullUserInfoList = Lists.newArrayList();
+		while (iterator.hasNext()) {
+			UserEntity next = iterator.next();
+			UserDomain bean = new UserDomain();
+			bean.setId(next.getId());
+			bean.setLoginName(next.getLoginName());
+			bean.setName(next.getName());
+			bean.setRootOrgId(next.getRootOrgId());
+			bean.setUpdateTime(next.getUpdateTime());
+			bean.setCreationTime(next.getCreationTime());
+			bean.setOrgId(next.getOrgId());
+			if (null != bean.getOrgId()) {
+				OrgEntity org = orgRepo.findOne(Long.valueOf(bean.getOrgId()));
+				if (null != org) {
+					bean.setOrgName(org.getName());
+					bean.setOrgCode(org.getCode());
+				}
+			}
+			bean.setRootOrgName(rootOrg.getName());
+			bean.setEnable(next.getEnable());
+
+			List<UserRoleRelationEntity> relationList = userRoleRelationRepo
+					.findAllByUserId(next.getId());
+			List<String> roleNameList = Lists.newArrayList();
+			List<Long> roleIdList = Lists.newArrayList();
+			List<String> roleCodeList = Lists.newArrayList();
+			for (UserRoleRelationEntity cur : relationList) {
+				RoleEntity curRoleEntity = roleRepo.findOne(cur.getRoleId());
+				if (null == curRoleEntity) {
+					throw new StatusException("B-150002", "角色错误");
+				}
+				roleNameList.add(curRoleEntity.getName());
+				roleIdList.add(curRoleEntity.getId());
+				roleCodeList.add(curRoleEntity.getCode());
+			}
+			bean.setRoleNamesStr(StringUtils.join(roleNameList, ","));
+			bean.setRoleIds(roleIdList);
+			bean.setRoleCodes(roleCodeList);
+
+			fullUserInfoList.add(bean);
+		}
+
+		return fullUserInfoList;
+	}
+
 	/**
 	 * 方法注释
 	 *