feat(UserManagementRelationxxx.java): 改造带人关系树的构造代码。
feat(UserController.java): 新增/list-by-dept-id接口,根据部门ID获取该部门和下属部门的用户精简信息列表。 fix(AdminUserServiceImpl.java): 修复删除某用户(含批量删除)后,带人关系树构造错乱、加载不出来的问题。
This commit is contained in:
@@ -57,6 +57,7 @@ public interface ErrorCodeConstants {
|
||||
// ========== 用户带人关系模块 1-002-003-100 ==========
|
||||
ErrorCode USER_MANAGEMENT_RELATION_NOT_FOUND = new ErrorCode(1_002_003_100, "用户带人关系不存在");
|
||||
ErrorCode USER_MANAGEMENT_RELATION_MANAGER_EXISTS = new ErrorCode(1_002_003_101, "该用户已有直属上级,不能重复添加");
|
||||
ErrorCode USER_MANAGEMENT_RELATION_EXISTS = new ErrorCode(1_002_003_102, "该用户在带人关系中还在使用,不可删除!");
|
||||
|
||||
// ========== 部门模块 1-002-004-000 ==========
|
||||
ErrorCode DEPT_NAME_DUPLICATE = new ErrorCode(1_002_004_000, "已经存在该名字的部门");
|
||||
|
||||
@@ -141,6 +141,15 @@ public class UserController {
|
||||
return success(UserConvert.INSTANCE.convertSimpleList(list, deptMap));
|
||||
}
|
||||
|
||||
@GetMapping("/list-by-dept-id")
|
||||
@Operation(summary = "根据部门ID获取该部门和下属部门的用户精简信息列表")
|
||||
@Parameter(name = "deptId", description = "部门ID", required = true)
|
||||
public CommonResult<List<UserSimpleRespVO>> getUserListByDeptId(@RequestParam("deptId") Long deptId) {
|
||||
List<AdminUserDO> list = userService.getAllUserByDeptId(deptId);
|
||||
Map<Long, DeptDO> deptMap = deptService.getDeptMap(convertList(list, AdminUserDO::getDeptId));
|
||||
return success(UserConvert.INSTANCE.convertSimpleList(list, deptMap));
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得用户详情")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
|
||||
@@ -163,8 +163,8 @@ public class UserManagementRelationController {
|
||||
@GetMapping("/tree")
|
||||
@Operation(summary = "获取用户带人关系树形结构", description = "用于前端树形控件展示,包含用户的上下级层级关系")
|
||||
@PreAuthorize("@ss.hasPermission('system:user-management-relation:query')")
|
||||
public CommonResult<List<UserManagementRelationTreeRespVO>> getUserManagementRelationTree() {
|
||||
return success(userManagementRelationService.getRelationTree());
|
||||
public CommonResult<List<UserManagementRelationTreeRespVO>> getUserManagementRelationTree(@Validated UserManagementRelationQueryReqVO reqVO) {
|
||||
return success(userManagementRelationService.getRelationTree(reqVO));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -13,4 +13,9 @@ public class UserManagementRelationQueryReqVO {
|
||||
@Schema(description = "被管理用户ID", example = "2")
|
||||
private Long subordinateUserId;
|
||||
|
||||
@Schema(description = "访问是否来自user/index组件", example = "true/false")
|
||||
private Boolean fromUserIndex;
|
||||
|
||||
@Schema(description = "所选中的部门id", example = "100(灿能电力的部门id)")
|
||||
private Long deptId;
|
||||
}
|
||||
|
||||
@@ -235,4 +235,11 @@ public interface AdminUserService {
|
||||
*/
|
||||
boolean isUserAvailable(AdminUserDO user);
|
||||
|
||||
/**
|
||||
* 通过部门ID查询该部门及以下部门的所有用户
|
||||
*
|
||||
* @param deptId 部门ID
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<AdminUserDO> getAllUserByDeptId(Long deptId);
|
||||
}
|
||||
|
||||
@@ -39,36 +39,13 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static com.njcn.rdms.framework.common.util.collection.CollectionUtils.singleton;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_EMAIL_EXISTS;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_IMPORT_INIT_PASSWORD;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_IS_DISABLE;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_IS_RESIGNED;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_MOBILE_EXISTS;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_PASSWORD_FAILED;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_REGISTER_DISABLED;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.USER_USERNAME_EXISTS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_CREATE_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_CREATE_SUCCESS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_DELETE_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_DELETE_SUCCESS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_UPDATE_PASSWORD_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_UPDATE_PASSWORD_SUCCESS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_UPDATE_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_USER_UPDATE_SUCCESS;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.*;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.*;
|
||||
|
||||
/**
|
||||
* 后台用户 Service 实现类
|
||||
@@ -97,6 +74,8 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
private OAuth2TokenService oauth2TokenService;
|
||||
@Resource
|
||||
private ConfigService configService;
|
||||
@Resource
|
||||
private UserManagementRelationService userManagementRelationService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@@ -218,6 +197,12 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
// 1. 校验用户存在
|
||||
AdminUserDO user = validateUserExists(id);
|
||||
// 2. 删除用户及其关联数据
|
||||
// 2.1 删除前判断带人关系表是否还在用该用户
|
||||
Boolean res = userManagementRelationService.hasRelation(id);
|
||||
if (res) {
|
||||
throw exception(USER_MANAGEMENT_RELATION_EXISTS);
|
||||
}
|
||||
// 2.2 确认用户关系表中没使用该用户可继续往下执行
|
||||
userMapper.deleteById(id);
|
||||
permissionService.processUserDeleted(id);
|
||||
oauth2TokenService.removeAccessToken(id, UserTypeEnum.ADMIN.getValue());
|
||||
@@ -228,6 +213,13 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteUserList(List<Long> ids) {
|
||||
//批量删除前查看是否带人关系表还在使用该用户
|
||||
for (Long id : ids){
|
||||
Boolean res = userManagementRelationService.hasRelation(id);
|
||||
if (res) {
|
||||
throw exception(USER_MANAGEMENT_RELATION_EXISTS);
|
||||
}
|
||||
}
|
||||
// 1. 批量删除用户
|
||||
userMapper.deleteByIds(ids);
|
||||
// 2. 批量删除用户关联数据
|
||||
@@ -504,6 +496,12 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
&& !isUserResigned(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getAllUserByDeptId(Long deptId) {
|
||||
Set<Long> deptCondition = getDeptCondition(deptId);
|
||||
return getUserListByDeptIds(deptCondition);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对密码进行加密
|
||||
*
|
||||
|
||||
@@ -97,7 +97,7 @@ public interface UserManagementRelationService {
|
||||
*
|
||||
* @return 用户带人关系树形列表
|
||||
*/
|
||||
List<UserManagementRelationTreeRespVO> getRelationTree();
|
||||
List<UserManagementRelationTreeRespVO> getRelationTree(UserManagementRelationQueryReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得用户带人关系 Map
|
||||
@@ -112,4 +112,14 @@ public interface UserManagementRelationService {
|
||||
return CollectionUtils.convertMap(getRelationList(ids), UserManagementRelationDO::getId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过某个用户的id,判断带人关系表中是否有该用户的记录
|
||||
* 判断原则:
|
||||
* --管理者ID或被管理者ID有一个字段的值等于用户id,且该记录没有被逻辑删除
|
||||
* --则认为带人关系表中还在使用该用户
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return true:有该用户的记录,false:没有该用户的记录
|
||||
*/
|
||||
Boolean hasRelation(Long userId);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.njcn.rdms.framework.common.exception.ServiceException;
|
||||
import com.njcn.rdms.framework.common.util.object.BeanUtils;
|
||||
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import com.njcn.rdms.module.system.controller.admin.user.vo.userManagementRelation.UserManagementRelationQueryReqVO;
|
||||
import com.njcn.rdms.module.system.controller.admin.user.vo.userManagementRelation.UserManagementRelationSaveReqVO;
|
||||
import com.njcn.rdms.module.system.controller.admin.user.vo.userManagementRelation.UserManagementRelationTreeRespVO;
|
||||
@@ -46,6 +47,9 @@ public class UserManagementRelationServiceImpl implements UserManagementRelation
|
||||
@Resource
|
||||
private AdminUserMapper adminUserMapper;
|
||||
|
||||
@Resource
|
||||
private AdminUserService adminUserService;
|
||||
|
||||
/**
|
||||
* 树形结构构建上下文
|
||||
* 包含构建树形结构所需的所有基础数据
|
||||
@@ -232,7 +236,7 @@ public class UserManagementRelationServiceImpl implements UserManagementRelation
|
||||
*/
|
||||
@Override
|
||||
public List<UserManagementRelationTreeRespVO> getRelationQuery(UserManagementRelationQueryReqVO reqVO) {
|
||||
TreeBuildContext context = buildTreeContext();
|
||||
TreeBuildContext context = buildTreeContext(reqVO);
|
||||
if (context == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -284,26 +288,60 @@ public class UserManagementRelationServiceImpl implements UserManagementRelation
|
||||
* @return 用户带人关系树形结构列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserManagementRelationTreeRespVO> getRelationTree() {
|
||||
TreeBuildContext context = buildTreeContext();
|
||||
public List<UserManagementRelationTreeRespVO> getRelationTree(UserManagementRelationQueryReqVO reqVO) {
|
||||
TreeBuildContext context = buildTreeContext(reqVO);
|
||||
if (context == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return buildFullTree(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过某个用户的id,判断带人关系表中是否有该用户的记录
|
||||
* 判断原则:
|
||||
* --管理者ID或被管理者ID有一个字段的值等于用户id,且该记录没有被逻辑删除
|
||||
* --则认为带人关系表中还在使用该用户
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return true:有该用户的记录,false:没有该用户的记录
|
||||
*/
|
||||
@Override
|
||||
public Boolean hasRelation(Long userId) {
|
||||
if (userId == null) {
|
||||
return false;
|
||||
}
|
||||
LambdaQueryWrapperX<UserManagementRelationDO> wrapper = new LambdaQueryWrapperX<>();
|
||||
wrapper.eq(UserManagementRelationDO::getManagerUserId, userId)
|
||||
.or()
|
||||
.eq(UserManagementRelationDO::getSubordinateUserId, userId);
|
||||
return userManagementRelationMapper.selectCount(wrapper) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建树形结构上下文
|
||||
* 查询并组装构建树形结构所需的所有基础数据
|
||||
*
|
||||
* @param reqVO 查询条件VO,包含fromUserIndex和deptId
|
||||
* @return 树形结构上下文,如果没有数据则返回null
|
||||
*/
|
||||
private TreeBuildContext buildTreeContext() {
|
||||
private TreeBuildContext buildTreeContext(UserManagementRelationQueryReqVO reqVO) {
|
||||
List<UserManagementRelationDO> allRelations = userManagementRelationMapper.selectList(new UserManagementRelationQueryReqVO());
|
||||
if (CollUtil.isEmpty(allRelations)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE.equals(reqVO.getFromUserIndex()) && reqVO.getDeptId() != null && reqVO.getDeptId() != 100L) {
|
||||
List<AdminUserDO> deptUsers = adminUserService.getAllUserByDeptId(reqVO.getDeptId());
|
||||
Set<Long> deptUserIds = deptUsers.stream().map(AdminUserDO::getId).collect(Collectors.toSet());
|
||||
allRelations = allRelations.stream()
|
||||
.filter(relation -> deptUserIds.contains(relation.getManagerUserId())
|
||||
&& deptUserIds.contains(relation.getSubordinateUserId()))
|
||||
.collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(allRelations)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Set<Long> allUserIds = new HashSet<>();
|
||||
for (UserManagementRelationDO relation : allRelations) {
|
||||
allUserIds.add(relation.getManagerUserId());
|
||||
|
||||
Reference in New Issue
Block a user