wangwei 6 年 前
コミット
e517fe2e61

+ 90 - 0
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/provider/UserCloudServiceProvider.java

@@ -1,10 +1,21 @@
 package cn.com.qmth.examcloud.core.basic.api.provider;
 
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import javax.persistence.criteria.Subquery;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+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.jpa.domain.Specification;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -20,8 +31,10 @@ import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
 import cn.com.qmth.examcloud.core.basic.api.bean.RoleBean;
 import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
 import cn.com.qmth.examcloud.core.basic.api.request.AddUserReq;
+import cn.com.qmth.examcloud.core.basic.api.request.GetUserListReq;
 import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
 import cn.com.qmth.examcloud.core.basic.api.response.AddUserResp;
+import cn.com.qmth.examcloud.core.basic.api.response.GetUserListResp;
 import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
 import cn.com.qmth.examcloud.core.basic.base.constants.BasicConsts;
 import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
@@ -191,4 +204,81 @@ public class UserCloudServiceProvider extends ControllerSupport implements UserC
 		return roleList;
 	}
 
+	@ApiOperation(value = "获取用户集合")
+	@PostMapping("getUserList")
+	@Override
+	public GetUserListResp getUserList(@RequestBody GetUserListReq req) {
+		Long rootOrgId = req.getRootOrgId();
+		Long roleId = req.getRoleId();
+		String roleCode = req.getRoleCode();
+
+		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(), rootOrgId);
+			}
+			if (null == roleEntity) {
+				throw new StatusException("B-150002", "角色不存在");
+			}
+			roleId = roleEntity.getId();
+		} else {
+			throw new StatusException("B-150003", "roleId,roleCode不能都为空");
+		}
+		final Long finalRoleId = roleId;
+
+		final long start = null == req.getStart() ? 1 : req.getStart();
+
+		Pageable pageable = new PageRequest(0, 100, Sort.Direction.ASC, "id");
+
+		Specification<UserEntity> specification = (root, query, cb) -> {
+			List<Predicate> predicates = new ArrayList<>();
+			predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
+
+			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()]));
+		};
+
+		Page<UserEntity> page = userRepo.findAll(specification, pageable);
+
+		Iterator<UserEntity> iterator = page.iterator();
+
+		List<UserBean> list = Lists.newArrayList();
+		long next = start;
+		while (iterator.hasNext()) {
+			UserEntity userEntity = iterator.next();
+			UserBean userBean = new UserBean();
+			userBean.setUserId(userEntity.getId());
+			userBean.setName(userEntity.getName());
+			userBean.setLoginName(userEntity.getLoginName());
+			userBean.setDisplayName(userEntity.getLoginName() + " (" + userEntity.getName() + ")");
+			userBean.setOrgId(userEntity.getOrgId());
+			userBean.setRootOrgId(userEntity.getRootOrgId());
+
+			next = userEntity.getId();
+			list.add(userBean);
+		}
+
+		GetUserListResp resp = new GetUserListResp();
+		if (next != start) {
+			next++;
+		}
+		resp.setNext(next);
+		resp.setUserBeanList(list);
+
+		return resp;
+	}
+
 }