Commit a839cf2c by 刘宇峰

Merge branch 'v1.5.0' into 'master'

V1.5.0

See merge request !1
2 parents d235712b 9460fa6a
......@@ -10,11 +10,11 @@
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<artifactId>glsx-ne-shield-usercenter</artifactId>
<artifactId>ne-shield-usercenter</artifactId>
<properties>
<kaptcha.version>0.0.9</kaptcha.version>
<shiro.version>1.6.0</shiro.version>
<shiro.version>1.8.0</shiro.version>
</properties>
<dependencies>
......@@ -44,8 +44,8 @@
<dependency>
<groupId>com.glsx</groupId>
<artifactId>glsx-ne-shield-auth-client</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>ne-shield-auth-client</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
......
......@@ -2,7 +2,10 @@ package cn.com.glsx.shield.modules.api;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.MenuPermission;
import cn.com.glsx.auth.model.SimpleUser;
import cn.com.glsx.auth.model.SyntheticUser;
import cn.com.glsx.shield.common.exception.UserCenterException;
import cn.com.glsx.shield.modules.entity.User;
import cn.com.glsx.shield.modules.service.UserService;
import com.glsx.plat.core.web.R;
import com.glsx.plat.jwt.base.ComJwtUser;
......@@ -29,6 +32,34 @@ public class AuthApiController implements AuthFeignClient {
private JwtUtils<ComJwtUser> jwtUtils;
@Override
public R<SimpleUser> login(String account, String password) {
User user = userService.findByAccount(account);
userService.verify(user, password);
SimpleUser simpleUser = new SimpleUser();
simpleUser.setId(user.getId());
simpleUser.setAccount(user.getAccount());
simpleUser.setUsername(user.getUsername());
return R.ok().data(simpleUser);
}
@Override
public R<SimpleUser> getSimpleUserById(Long userId) {
User user = userService.getById(userId);
if (user == null) {
throw new UserCenterException("用户不存在");
}
SimpleUser simpleUser = new SimpleUser();
simpleUser.setId(user.getId());
simpleUser.setAccount(user.getAccount());
simpleUser.setUsername(user.getUsername());
return R.ok().data(simpleUser);
}
@Override
public R<Boolean> sniff() {
String token = SessionUtils.request().getHeader(HttpHeaders.AUTHORIZATION);
boolean verifyFlag = jwtUtils.verifyToken(token);
......@@ -37,28 +68,34 @@ public class AuthApiController implements AuthFeignClient {
@Override
public R<SyntheticUser> getAuthUser() {
SyntheticUser authUser = userService.getSyntheticUser();
SyntheticUser authUser = userService.getSyntheticUserByToken();
return R.ok().data(authUser);
}
@Override
public R<SyntheticUser> getAuthUserById(Long userId) {
SyntheticUser authUser = userService.getSyntheticUserById(userId);
return R.ok().data(authUser);
}
@Override
public R<Set<Long>> getAuthDeptIds() {
// TODO: 2021/6/10 单独查询
SyntheticUser authUser = userService.getSyntheticUser();
SyntheticUser authUser = userService.getSyntheticUserByToken();
return R.ok().data(authUser.getVisibleDeptIds());
}
@Override
public R<Set<Long>> getAuthUserIds() {
// TODO: 2021/6/10 单独查询
SyntheticUser authUser = userService.getSyntheticUser();
SyntheticUser authUser = userService.getSyntheticUserByToken();
return R.ok().data(authUser.getVisibleCreatorIds());
}
@Override
public R<List<MenuPermission>> getPermMenus() {
// TODO: 2021/6/10 单独查询
SyntheticUser authUser = userService.getSyntheticUser();
SyntheticUser authUser = userService.getSyntheticUserByToken();
List<MenuPermission> list = userService.getPermissionMenus(authUser);
return R.ok().data(list);
}
......
......@@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* feign组织机构服务接口
......@@ -149,6 +150,12 @@ public class FeignOrganizationController implements cn.com.glsx.usercenter.api.f
}
@Override
public R<Set<Long>> getAllSubOrgIds() {
Set<Long> levelDeptList = organizationService.getAllSubOrgIds();
return R.ok().data(levelDeptList);
}
@Override
public R orgTree(@RequestBody FindOrgRequest request) {
OrgTreeSearch search = new OrgTreeSearch();
search.setOrgName(request.getOrgName());
......
......@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* feign用户服务接口
......@@ -53,25 +54,32 @@ public class FeignUserController implements cn.com.glsx.usercenter.api.feign.Use
}
@Override
@GetMapping(value = "/getSuperiorUserById")
public R<User> getSuperiorUserById(@RequestParam("id") Long id) {
cn.com.glsx.shield.modules.entity.User u = userService.getSuperiorUserById(id);
@GetMapping(value = "/getSuperiorById")
public R<User> getSuperiorById(@RequestParam("id") Long userId) {
cn.com.glsx.shield.modules.entity.User u = userService.getSuperiorUserById(userId);
User user = null;
if (u != null) {
user = UserConverter.INSTANCE.do2foo(u);
user.setMid(id);
user.setMid(userId);
}
return R.ok().data(user);
}
@Override
@PostMapping(value = "/getSuperiorUserByIds")
public R<List<User>> getSuperiorUserByIds(@RequestBody Collection<Long> userIds) {
@PostMapping(value = "/getSuperiorByIds")
public R<List<User>> getSuperiorByIds(@RequestBody Collection<Long> userIds) {
List<User> userList = userService.getSuperiorUserByIds(userIds);
return R.ok().data(userList);
}
@Override
@GetMapping(value = "/getSubordinateById")
public R<Set<Long>> getSubordinateById(@RequestParam("id") Long userId) {
Set<Long> userIdSet = userService.getSubordinateById(userId);
return R.ok().data(userIdSet);
}
@Override
@ApiOperation(value = "系统用户列表查询")
@GetMapping("/findByUser")
public R<List<User>> findByUser(@RequestBody FindUserRequest request) {
......
......@@ -24,6 +24,8 @@ public interface UserMapper extends CommonBaseMapper<User> {
List<User> selectByTenantId(@Param("tenantId") Long tenantId);
List<User> selectByTenantIds(@Param("tenantIds") Collection<Long> tenantIds);
List<User> selectByDepartmentId(@Param("departmentId") Long departmentId);
List<OrgModel> selectUserOrgModelById(@Param("id") Long id);
......
......@@ -20,7 +20,7 @@ public class UserBO {
private Long id;
@Size(min = 5, max = 50)
@Size(min = 3, max = 30)
@NotBlank
private String account;
......
......@@ -207,6 +207,12 @@ public class DepartmentService {
List<Long> subUserIdList = userPathList.stream().map(UserPath::getSubId).collect(Collectors.toList());
return userMapper.selectByIds(subUserIdList);
} else if (assignTenants.getCode().equals(role.getRolePermissionType())) {
List<RoleDataPermission> assignTenants = roleDataPermissionMapper.selectByRoleId(roleId);
List<Long> assignTenantIdList = assignTenants.stream().map(RoleDataPermission::getRefId).collect(Collectors.toList());
return userMapper.selectByTenantIds(assignTenantIdList);
}
return userList;
}
......
......@@ -20,6 +20,7 @@ import cn.com.glsx.shield.modules.model.view.DepartmentDTO;
import cn.com.glsx.shield.modules.service.permissionStrategy.PermissionStrategy;
import cn.com.glsx.usercenter.api.dto.tree.OrgModel;
import cn.com.glsx.usercenter.api.dto.tree.OrgTreeModel;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.model.TreeModel;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.common.utils.TreeModelUtil;
......@@ -250,6 +251,8 @@ public class OrganizationService {
}
public List orgTree(OrgTreeSearch search) {
log.info("OrgTree OrgTreeSearch:{}", JSONUtil.toJsonStr(search));
List<? extends TreeModel> treeModels = this.getPermissionStrategy().orgTree(search);
treeModels.sort(Comparator.comparing(TreeModel::getOrder));
......@@ -305,4 +308,12 @@ public class OrganizationService {
return levelOrgList;
}
public Set<Long> getAllSubOrgIds() {
List<Organization> subOrgList = organizationMapper.selectAllSubBySuperiorId(ShieldContextHolder.getDepartmentId());
Set<Long> subOrgIdList = subOrgList.stream().map(Organization::getSubId).collect(Collectors.toSet());
return subOrgIdList;
}
}
......@@ -31,6 +31,7 @@ import com.glsx.plat.jwt.base.ComJwtUser;
import com.glsx.plat.jwt.util.JwtUtils;
import com.glsx.plat.web.utils.SessionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
......@@ -701,13 +702,31 @@ public class UserService {
}
/**
* 获取综合用户信息
* 获取综合用户信息by token
*
* @return
*/
public SyntheticUser getSyntheticUser() {
public SyntheticUser getSyntheticUserByToken() {
User user = this.getByToken();
return getSyntheticUser(user);
}
/**
* 获取综合用户信息by userId
*
* @return
*/
public SyntheticUser getSyntheticUserById(Long userId) {
User user = userMapper.selectById(userId);
return getSyntheticUser(user);
}
/**
* 获取综合用户信息
*
* @return
*/
public SyntheticUser getSyntheticUser(User user) {
boolean isSupperAdmin = UserGlobalType.SUPER_ADMIN.getType().equals(user.getGlobalType());
SyntheticUser authUser = new SyntheticUser();
......@@ -718,7 +737,6 @@ public class UserService {
// TODO: 2020/9/24 缓存数据
//租户
Tenant tenant = tenantMapper.selectById(user.getTenantId());
authUser.setTenant(AuthTenantConverter.INSTANCE.toAuthTenant(tenant));
......@@ -737,58 +755,63 @@ public class UserService {
authUser.setRoles(list);
}
//可见租户
Set<Long> tenantIds = this.getRelationAuthTenantIds(authUser);
Set<Long> tenantIds = this.getRelationAuthTenantIds(user.getId(), roleList.get(0).getId());
//可见根部门(租户)
authUser.setVisibleTenantIds(tenantIds);
//可见部门
Set<Long> deptIds = this.getRelationAuthDeptIds(user.getId(), roleList.get(0).getId());
//可见组织(部门)
authUser.setVisibleDeptIds(deptIds);
//超级管理员无需查询更详细的部门、用户等信息
if (!isSupperAdmin) {
//可见部门
Set<Long> deptIds = this.getRelationAuthDeptIds(authUser);
//可见用户
Set<Long> userIds = this.getRelationAuthUserIds(authUser);
Set<Long> userIds = this.getRelationAuthUserIds(user.getId(), roleList.get(0).getId());
//可见用户
authUser.setVisibleCreatorIds(userIds);
}
return authUser;
}
public Set<Long> getRelationAuthTenantIds(SyntheticUser authUser) {
List<Tenant> deptList = departmentService.getUserRoleTenants(authUser.getUserId(),
authUser.getRoles().get(0).getRoleId());
Set<Long> tenantIds = deptList.stream().map(Tenant::getId).collect(Collectors.toSet());
/**
* 获取用户根据数据权限范围内的可见租户id
*
* @param userId
* @param roleId
* @return
*/
public Set<Long> getRelationAuthTenantIds(Long userId, Long roleId) {
//可见根部门(租户)
authUser.setVisibleTenantIds(tenantIds);
List<Tenant> deptList = departmentService.getUserRoleTenants(userId, roleId);
return tenantIds;
return deptList.stream().map(Tenant::getId).collect(Collectors.toSet());
}
public Set<Long> getRelationAuthDeptIds(SyntheticUser authUser) {
List<Department> deptList = departmentService.getUserRoleDepartments(authUser.getUserId(),
authUser.getRoles().get(0).getRoleId());
Set<Long> deptIds = deptList.stream().map(Department::getId).collect(Collectors.toSet());
/**
* 获取用户根据数据权限范围内的可见部门id
*
* @param userId
* @param roleId
* @return
*/
public Set<Long> getRelationAuthDeptIds(Long userId, Long roleId) {
//可见组织(部门)
authUser.setVisibleDeptIds(deptIds);
List<Department> deptList = departmentService.getUserRoleDepartments(userId, roleId);
return deptIds;
return deptList.stream().map(Department::getId).collect(Collectors.toSet());
}
/**
* 获取用户根据数据权限范围内的可见用户id
*
* @param authUser
* @param userId
* @param roleId
* @return
*/
public Set<Long> getRelationAuthUserIds(SyntheticUser authUser) {
List<User> userList = departmentService.getUserRoleUsers(authUser.getUserId(), authUser.getRoles().get(0).getRoleId());
Set<Long> userIds = userList.stream().map(User::getId).collect(Collectors.toSet());
public Set<Long> getRelationAuthUserIds(Long userId, Long roleId) {
List<User> userList = departmentService.getUserRoleUsers(userId, roleId);
//可见用户
authUser.setVisibleCreatorIds(userIds);
return userIds;
return userList.stream().map(User::getId).collect(Collectors.toSet());
}
/**
......@@ -983,6 +1006,16 @@ public class UserService {
return userMapper.selectSuperiorUserById(id);
}
public Set<Long> getSubordinateById(Long userId) {
List<Role> roleList = roleService.getUserRoleList(userId);
if (CollectionUtils.isEmpty(roleList)) {
return Sets.newHashSet();
}
return this.getRelationAuthUserIds(userId, roleList.get(0).getId());
}
public List<cn.com.glsx.usercenter.api.dto.response.User> getSuperiorUserByIds(Collection<Long> userIds) {
return userMapper.selectSuperiorUserByIds(userIds);
}
......
......@@ -15,10 +15,12 @@ import com.glsx.plat.common.model.TreeModel;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.common.utils.TreeModelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import javax.annotation.Resource;
import java.util.Collection;
......@@ -124,17 +126,11 @@ public class AllStrategy extends PermissionStrategy {
if (CollectionUtils.isNotEmpty(search.getSubOrgIds())) {
Map<Long, Set<Long>> subOrgIdListMap = getSubIdsMap(search.getSubOrgIds());
Collection<Long> finalOrgIds = orgIds;
//选中的节点及下级节点
Set<Long> selectSubOrgIds = Sets.newHashSet();
for (Map.Entry<Long, Set<Long>> entry : subOrgIdListMap.entrySet()) {
Set<Long> subOrgIds = entry.getValue().stream().filter(finalOrgIds::contains).collect(Collectors.toSet());
if (orgIds.contains(entry.getKey())) {
subOrgIds.add(entry.getKey());
}
selectSubOrgIds.addAll(subOrgIds);
selectSubOrgIds.addAll(entry.getValue());
}
orgIds = selectSubOrgIds;
......@@ -145,19 +141,43 @@ public class AllStrategy extends PermissionStrategy {
orgIds = this.getUserOrgSuperiorIds(search.getUserIds());
}
//如果有查询条件,如果没有过滤到需要查询的组织链,返回空数据
if ((StringUtils.isNotEmpty(search.getOrgName())
|| CollectionUtils.isNotEmpty(search.getOrgIds())
|| CollectionUtils.isNotEmpty(search.getSupOrgIds())
|| CollectionUtils.isNotEmpty(search.getSubOrgIds())
|| CollectionUtils.isNotEmpty(search.getUserIds())) && CollectionUtils.isEmpty(orgIds)) {
return Lists.newArrayList();
}
search.setOrgIds(orgIds);
StopWatch sw = new StopWatch();
sw.start("task1");
List<OrgModel> modelList = organizationMapper.selectOrgList(search);
List<Long> departmentIdList = modelList.stream().map(OrgModel::getOrgId).collect(Collectors.toList());
sw.stop();
sw.start("task2");
//计算用户数
Map<Long, Integer> recursiveDepartmentUserMap = departmentService.countRecursiveDepartmentUser(departmentIdList);
List<OrgTreeModel> orgTreeModelList = modelList.stream().map(OrgTreeModel::new).collect(Collectors.toList());
List<OrgTreeModel> allTreeModelList = Lists.newArrayList(orgTreeModelList);
sw.stop();
//查询需要的用户,然后放到对应的组织节点的child,如果需要返回用户子节点的话
Map<Long, List<OrgModel>> groupByOrgIdUserMap = Maps.newHashMap();
if (search.getHasUserLeaf() && CollectionUtils.isNotEmpty(departmentIdList)) {
List<OrgModel> allUserList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentIds(departmentIdList).setUserIds(search.getUserIds()));
groupByOrgIdUserMap = allUserList.stream().collect(Collectors.groupingBy(OrgModel::getParentId));
}
sw.start("task3");
// final Set<Long> subRootOrgIds = subOrgIdListMap.keySet();
Map<Long, List<OrgModel>> finalGroupByOrgIdUserMap = groupByOrgIdUserMap;
orgTreeModelList.forEach(otm -> {
Long orgId = Long.valueOf(otm.getId());
Integer userNumber = recursiveDepartmentUserMap.get(orgId);
......@@ -169,16 +189,21 @@ public class AllStrategy extends PermissionStrategy {
// }
//用户子树
if (search.getHasUserLeaf() && otm.getUserNumber() > 0) {
List<OrgModel> userList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentId(orgId).setUserIds(search.getUserIds()));
List<OrgModel> userList = finalGroupByOrgIdUserMap.getOrDefault(orgId, Lists.newArrayList());
List<OrgTreeModel> userTreeList = userList.stream().map(OrgTreeModel::new).collect(Collectors.toList());
allTreeModelList.addAll(userTreeList);
}
});
sw.stop();
sw.start("task4");
if (search.getReturnTrees()) {
List<? extends TreeModel> orgTree = TreeModelUtil.fastConvertByRootMark(allTreeModelList, 1);
return orgTree;
}
sw.stop();
log.info("任务耗时概览:{},总耗时:{}秒", sw.prettyPrint(), sw.getTotalTimeSeconds());
return allTreeModelList;
}
......
......@@ -17,6 +17,7 @@ import com.glsx.plat.common.model.TreeModel;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.common.utils.TreeModelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
......@@ -127,17 +128,11 @@ public class AssignTenantStrategy extends PermissionStrategy {
if (CollectionUtils.isNotEmpty(search.getSubOrgIds())) {
Map<Long, Set<Long>> subOrgIdListMap = getSubIdsMap(search.getSubOrgIds());
Collection<Long> finalOrgIds = orgIds;
//选中的节点及下级节点
Set<Long> selectSubOrgIds = Sets.newHashSet();
for (Map.Entry<Long, Set<Long>> entry : subOrgIdListMap.entrySet()) {
Set<Long> subOrgIds = entry.getValue().stream().filter(finalOrgIds::contains).collect(Collectors.toSet());
if (orgIds.contains(entry.getKey())) {
subOrgIds.add(entry.getKey());
}
selectSubOrgIds.addAll(subOrgIds);
selectSubOrgIds.addAll(entry.getValue());
}
orgIds = selectSubOrgIds;
......@@ -152,16 +147,26 @@ public class AssignTenantStrategy extends PermissionStrategy {
List<Tenant> tenantList = departmentService.getRolePermissionTenants();
Set<Long> tenantIds = tenantList.stream().map(Tenant::getId).collect(Collectors.toSet());
//如果指定租户没有,返回空数据
if (CollectionUtils.isEmpty(tenantIds)) {
return Lists.newArrayList();
}
//如果有查询条件,如果没有过滤到需要查询的组织链,返回空数据
if ((StringUtils.isNotEmpty(search.getOrgName())
|| CollectionUtils.isNotEmpty(search.getOrgIds())
|| CollectionUtils.isNotEmpty(search.getSupOrgIds())
|| CollectionUtils.isNotEmpty(search.getSubOrgIds())
|| CollectionUtils.isNotEmpty(search.getUserIds())) && CollectionUtils.isEmpty(orgIds)) {
return Lists.newArrayList();
}
//指定数据权限租户
search.setTenantIds(tenantIds);
//过滤出的组织
search.setOrgIds(orgIds);
//如果没有过滤到需要查询的组织链,返回空数据
if (CollectionUtils.isEmpty(orgIds) && CollectionUtils.isEmpty(tenantIds)) {
return Lists.newArrayList();
}
List<OrgModel> modelList = organizationMapper.selectOrgList(search);
List<Long> departmentIdList = modelList.stream().map(OrgModel::getOrgId).collect(Collectors.toList());
......@@ -172,13 +177,21 @@ public class AssignTenantStrategy extends PermissionStrategy {
List<OrgTreeModel> allTreeModelList = Lists.newArrayList(orgTreeModelList);
//查询需要的用户,然后放到对应的组织节点的child,如果需要返回用户子节点的话
Map<Long, List<OrgModel>> groupByOrgIdUserMap = Maps.newHashMap();
if (search.getHasUserLeaf() && CollectionUtils.isNotEmpty(departmentIdList)) {
List<OrgModel> allUserList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentIds(departmentIdList).setUserIds(search.getUserIds()));
groupByOrgIdUserMap = allUserList.stream().collect(Collectors.groupingBy(OrgModel::getParentId));
}
Map<Long, List<OrgModel>> finalGroupByOrgIdUserMap = groupByOrgIdUserMap;
orgTreeModelList.forEach(otm -> {
Long orgId = Long.valueOf(otm.getId());
Integer userNumber = recursiveDepartmentUserMap.get(orgId);
otm.setUserNumber(userNumber == null ? 0 : userNumber);
//用户子树
if (search.getHasUserLeaf() && otm.getUserNumber() > 0) {
List<OrgModel> userList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentId(orgId).setUserIds(search.getUserIds()));
List<OrgModel> userList = finalGroupByOrgIdUserMap.getOrDefault(orgId, Lists.newArrayList());
List<OrgTreeModel> userTreeList = userList.stream().map(OrgTreeModel::new).collect(Collectors.toList());
allTreeModelList.addAll(userTreeList);
}
......
......@@ -171,6 +171,10 @@ public abstract class PermissionStrategy {
Set<Long> departmentIdSet = userList.stream().map(User::getDepartmentId).collect(Collectors.toSet());
if (CollectionUtils.isEmpty(departmentIdSet)) {
return Lists.newArrayList();
}
List<OrgSuperiorModel> superiorModelList = organizationMapper.selectSuperiorIdsByOrg(new OrgTreeSearch().setOrgIds(departmentIdSet));
Collection<Long> superiorIdSet = this.getSuperiorIds(superiorModelList);
......
......@@ -15,6 +15,7 @@ import com.glsx.plat.common.model.TreeModel;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.common.utils.TreeModelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
......@@ -136,17 +137,11 @@ public class SelfDepartmentStrategy extends PermissionStrategy {
if (CollectionUtils.isNotEmpty(search.getSubOrgIds())) {
Map<Long, Set<Long>> subOrgIdListMap = getSubIdsMap(search.getSubOrgIds());
Collection<Long> finalOrgIds = orgIds;
//选中的节点及下级节点
Set<Long> selectSubOrgIds = Sets.newHashSet();
for (Map.Entry<Long, Set<Long>> entry : subOrgIdListMap.entrySet()) {
Set<Long> subOrgIds = entry.getValue().stream().filter(finalOrgIds::contains).collect(Collectors.toSet());
if (orgIds.contains(entry.getKey())) {
subOrgIds.add(entry.getKey());
}
selectSubOrgIds.addAll(subOrgIds);
selectSubOrgIds.addAll(entry.getValue());
}
orgIds = selectSubOrgIds;
......@@ -157,8 +152,12 @@ public class SelfDepartmentStrategy extends PermissionStrategy {
orgIds = this.getUserOrgSuperiorIds(search.getUserIds());
}
//如果没有过滤到需要查询的组织链,返回空数据
if (CollectionUtils.isEmpty(orgIds)) {
//如果有查询条件,如果没有过滤到需要查询的组织链,返回空数据
if ((StringUtils.isNotEmpty(search.getOrgName())
|| CollectionUtils.isNotEmpty(search.getOrgIds())
|| CollectionUtils.isNotEmpty(search.getSupOrgIds())
|| CollectionUtils.isNotEmpty(search.getSubOrgIds())
|| CollectionUtils.isNotEmpty(search.getUserIds())) && CollectionUtils.isEmpty(orgIds)) {
return Lists.newArrayList();
}
......@@ -174,13 +173,21 @@ public class SelfDepartmentStrategy extends PermissionStrategy {
List<OrgTreeModel> allTreeModelList = Lists.newArrayList(orgTreeModelList);
//查询需要的用户,然后放到对应的组织节点的child,如果需要返回用户子节点的话
Map<Long, List<OrgModel>> groupByOrgIdUserMap = Maps.newHashMap();
if (search.getHasUserLeaf() && CollectionUtils.isNotEmpty(departmentIdList)) {
List<OrgModel> allUserList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentIds(departmentIdList).setUserIds(search.getUserIds()));
groupByOrgIdUserMap = allUserList.stream().collect(Collectors.groupingBy(OrgModel::getParentId));
}
Map<Long, List<OrgModel>> finalGroupByOrgIdUserMap = groupByOrgIdUserMap;
orgTreeModelList.stream().filter(otm -> Long.valueOf(otm.getId()).equals(deptId)).forEach(otm -> {
Long orgId = Long.valueOf(otm.getId());
Integer userNumber = recursiveDepartmentUserMap.get(orgId);
otm.setUserNumber(userNumber == null ? 0 : userNumber);
//用户子树
if (search.getHasUserLeaf() && otm.getUserNumber() > 0) {
List<OrgModel> userList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentId(orgId).setUserIds(search.getUserIds()));
List<OrgModel> userList = finalGroupByOrgIdUserMap.getOrDefault(orgId, Lists.newArrayList());
List<OrgTreeModel> userTreeList = userList.stream().map(OrgTreeModel::new).collect(Collectors.toList());
allTreeModelList.addAll(userTreeList);
}
......
......@@ -14,6 +14,7 @@ import com.glsx.plat.common.model.TreeModel;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.common.utils.TreeModelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
......@@ -192,17 +193,11 @@ public class SubDepartmentStrategy extends PermissionStrategy {
if (CollectionUtils.isNotEmpty(search.getSubOrgIds())) {
Map<Long, Set<Long>> subOrgIdListMap = getSubIdsMap(search.getSubOrgIds());
Collection<Long> finalOrgIds = orgIds;
//选中的节点及下级节点
Set<Long> selectSubOrgIds = Sets.newHashSet();
for (Map.Entry<Long, Set<Long>> entry : subOrgIdListMap.entrySet()) {
Set<Long> subOrgIds = entry.getValue().stream().filter(finalOrgIds::contains).collect(Collectors.toSet());
if (orgIds.contains(entry.getKey())) {
subOrgIds.add(entry.getKey());
}
selectSubOrgIds.addAll(subOrgIds);
selectSubOrgIds.addAll(entry.getValue());
}
orgIds = selectSubOrgIds;
......@@ -213,8 +208,12 @@ public class SubDepartmentStrategy extends PermissionStrategy {
orgIds = this.getUserOrgSuperiorIds(search.getUserIds());
}
//如果没有过滤到需要查询的组织链,返回空数据
if (CollectionUtils.isEmpty(orgIds)) {
//如果有查询条件,如果没有过滤到需要查询的组织链,返回空数据
if ((StringUtils.isNotEmpty(search.getOrgName())
|| CollectionUtils.isNotEmpty(search.getOrgIds())
|| CollectionUtils.isNotEmpty(search.getSupOrgIds())
|| CollectionUtils.isNotEmpty(search.getSubOrgIds())
|| CollectionUtils.isNotEmpty(search.getUserIds())) && CollectionUtils.isEmpty(orgIds)) {
return Lists.newArrayList();
}
......@@ -230,6 +229,14 @@ public class SubDepartmentStrategy extends PermissionStrategy {
List<OrgTreeModel> allTreeModelList = Lists.newArrayList(orgTreeModelList);
//查询需要的用户,然后放到对应的组织节点的child,如果需要返回用户子节点的话
Map<Long, List<OrgModel>> groupByOrgIdUserMap = Maps.newHashMap();
if (search.getHasUserLeaf() && CollectionUtils.isNotEmpty(departmentIdList)) {
List<OrgModel> allUserList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentIds(departmentIdList).setUserIds(search.getUserIds()));
groupByOrgIdUserMap = allUserList.stream().collect(Collectors.groupingBy(OrgModel::getParentId));
}
Map<Long, List<OrgModel>> finalGroupByOrgIdUserMap = groupByOrgIdUserMap;
orgTreeModelList.forEach(otm -> {
Long orgId = Long.valueOf(otm.getId());
//这里控制从用户自己部门开始才有用户
......@@ -239,7 +246,7 @@ public class SubDepartmentStrategy extends PermissionStrategy {
//用户子树
if (search.getHasUserLeaf() && otm.getUserNumber() > 0) {
List<OrgModel> userList = userMapper.selectUserOrgModels(new UserSearch().setDepartmentId(orgId).setUserIds(search.getUserIds()));
List<OrgModel> userList = finalGroupByOrgIdUserMap.getOrDefault(orgId, Lists.newArrayList());
List<OrgTreeModel> userTreeList = userList.stream().map(OrgTreeModel::new).collect(Collectors.toList());
allTreeModelList.addAll(userTreeList);
}
......
......@@ -20,7 +20,6 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
......@@ -181,6 +180,11 @@ public class SubOrdinateStrategy extends PermissionStrategy {
} else {
matchedDepartmentIdList = subDepartmentIdList;
}
//如果没找到匹配部门,返回为空(指定授权跨部门情况容易发生)
if (CollectionUtils.isEmpty(matchedDepartmentIdList)) {
return Lists.newArrayList();
}
//全部上级
List<Organization> superiorOrgList = organizationMapper.selectList(new OrganizationBO().setSubIdList(matchedDepartmentIdList));
//所有部门id
......
......@@ -58,10 +58,22 @@
</select>
<select id="selectByTenantId" resultMap="BaseResultMap">
select *
select
<include refid="Base_Column_List"/>
from t_user
where tenant_id = #{tenantId}
and del_flag = 0
where del_flag = 0
and tenant_id = #{tenantId}
</select>
<select id="selectByTenantIds" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_user
where del_flag = 0
and tenant_id in
<foreach collection="tenantIds" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</select>
<select id="selectByDepartmentId" resultMap="BaseResultMap">
......@@ -96,7 +108,10 @@
tu.id orderNum
from t_user tu
where del_flag = 0
and department_id = #{departmentId}
and department_id in
<foreach collection="departmentIds" item="departmentId" open="(" close=")" separator=",">
#{departmentId}
</foreach>
<if test="userIds != null and userIds.size() > 0">
and tu.id in
<foreach collection="userIds" item="item" open="(" close=")" separator=",">
......
......@@ -188,7 +188,7 @@ public class UserApplicationTests {
@Test
public void testUser() {
SyntheticUser user = userService.getSyntheticUser();
SyntheticUser user = userService.getSyntheticUserById(1L);
System.out.println(JSON.toJSONString(user));
userService.addUser(new UserBO().setAccount("rrew").setUsername("桃桃桃").setDepartmentId(1L).setRoleId(1L).setPassword("123456"));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!