feat: 当前部门出现在最顶部
This commit is contained in:
parent
c667ab6f30
commit
a0b8e78c96
|
@ -143,10 +143,10 @@ public class BusinessTripApprovalController extends BaseController {
|
|||
}
|
||||
|
||||
|
||||
@GetMapping("/userList")
|
||||
/*@GetMapping("/userList")
|
||||
@ApiOperation("查询用户人员列表")
|
||||
public AjaxResult applyList() {
|
||||
/*List<SysUser> sysUsers = sysUserService.selectList();
|
||||
*//*List<SysUser> sysUsers = sysUserService.selectList();
|
||||
List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
|
||||
sysDepts = sysDepts.stream()
|
||||
.filter(dept -> dept.getDeptId() != 131000) // 过滤掉id为131000的部门
|
||||
|
@ -156,7 +156,7 @@ public class BusinessTripApprovalController extends BaseController {
|
|||
for (SysUser user : sysUsers) {
|
||||
deptUsersMap.computeIfAbsent(user.getDeptId(), k -> new ArrayList<>()).add(user);
|
||||
}
|
||||
return AjaxResult.success(buildTree(depts, deptUsersMap));*/
|
||||
return AjaxResult.success(buildTree(depts, deptUsersMap));*//*
|
||||
|
||||
// 获取当前用户ID
|
||||
// 获取当前用户ID
|
||||
|
@ -253,10 +253,164 @@ public class BusinessTripApprovalController extends BaseController {
|
|||
}
|
||||
}
|
||||
return null;
|
||||
}*/
|
||||
|
||||
@GetMapping("/userList")
|
||||
@ApiOperation("查询用户人员列表")
|
||||
public AjaxResult applyList() {
|
||||
// Long currentUserId = getCurrentUserId();
|
||||
Long currentUserId = getLoginUser().getUser().getUserId();
|
||||
List<SysUser> sysUsers = sysUserService.selectList();
|
||||
List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
|
||||
|
||||
// 过滤无效部门并复制原始列表(避免修改原始数据)
|
||||
List<SysDept> originalDepts = sysDepts.stream()
|
||||
.filter(dept -> dept.getDeptId() != 131000)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建原始部门树
|
||||
List<SysDept> deptTree = sysDeptService.buildDeptTree(originalDepts);
|
||||
|
||||
// 查找当前部门并从原始树中移除
|
||||
SysDept currentDept = removeCurrentDeptFromTree(deptTree, currentUserId, sysUsers);
|
||||
|
||||
// 构建用户映射
|
||||
Map<Long, List<SysUser>> deptUsersMap = sysUsers.stream()
|
||||
.collect(Collectors.groupingBy(SysUser::getDeptId));
|
||||
|
||||
// 构建结果:先添加当前部门(作为独立顶级节点),再添加原始树(已移除当前部门)
|
||||
List<TreeSelect> treeList = new ArrayList<>();
|
||||
if (currentDept != null) {
|
||||
treeList.add(buildCurrentDeptNode(currentDept, deptUsersMap));
|
||||
}
|
||||
treeList.addAll(buildTree(deptTree, deptUsersMap)); // 原始树已不含当前部门
|
||||
|
||||
return AjaxResult.success(treeList);
|
||||
}
|
||||
|
||||
// 从部门树中查找并移除当前部门
|
||||
private SysDept removeCurrentDeptFromTree(List<SysDept> deptTree, Long currentUserId, List<SysUser> sysUsers) {
|
||||
SysUser currentUser = sysUsers.stream()
|
||||
.filter(user -> user.getUserId().equals(currentUserId))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (currentUser == null) return null;
|
||||
|
||||
public List<TreeSelect> buildTree(List<SysDept> depts, Map<Long, List<SysUser>> deptUsersMap) {
|
||||
Long currentDeptId = currentUser.getDeptId();
|
||||
SysDept currentDept = findDeptInTree(deptTree, currentDeptId);
|
||||
|
||||
// 递归从原始树中移除当前部门(避免重复显示)
|
||||
if (currentDept != null) {
|
||||
removeDeptFromParent(deptTree, currentDeptId);
|
||||
}
|
||||
return currentDept;
|
||||
}
|
||||
private TreeSelect buildDeptNode(SysDept dept, Map<Long, List<SysUser>> deptUsersMap) {
|
||||
TreeSelect treeSelect = new TreeSelect();
|
||||
treeSelect.setId(dept.getDeptId());
|
||||
treeSelect.setLabel(dept.getDeptName());
|
||||
|
||||
// 添加部门下的用户
|
||||
List<TreeSelect> children = new ArrayList<>();
|
||||
List<SysUser> users = deptUsersMap.getOrDefault(dept.getDeptId(), Collections.emptyList());
|
||||
for (SysUser user : users) {
|
||||
TreeSelect userNode = new TreeSelect();
|
||||
userNode.setLabel(user.getNickName());
|
||||
userNode.setId(user.getUserId());
|
||||
userNode.setDictId(dept.getDeptId());
|
||||
userNode.setDictName(dept.getDeptName());
|
||||
children.add(userNode);
|
||||
}
|
||||
|
||||
// 递归添加所有子部门
|
||||
for (SysDept child : dept.getChildren()) {
|
||||
children.add(buildDeptNode(child, deptUsersMap));
|
||||
}
|
||||
|
||||
if (!children.isEmpty()) {
|
||||
treeSelect.setChildren(children);
|
||||
}
|
||||
|
||||
return treeSelect;
|
||||
}
|
||||
// 递归查找部门节点
|
||||
private SysDept findDeptInTree(List<SysDept> depts, Long deptId) {
|
||||
for (SysDept dept : depts) {
|
||||
if (dept.getDeptId().equals(deptId)) {
|
||||
return dept;
|
||||
}
|
||||
SysDept found = findDeptInTree(dept.getChildren(), deptId);
|
||||
if (found != null) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 递归从父节点中移除指定部门
|
||||
private void removeDeptFromParent(List<SysDept> depts, Long deptId) {
|
||||
depts.removeIf(dept -> dept.getDeptId().equals(deptId)); // 移除顶级节点
|
||||
depts.forEach(dept -> dept.getChildren().removeIf(child -> child.getDeptId().equals(deptId))); // 移除子节点
|
||||
depts.forEach(dept -> removeDeptFromParent(dept.getChildren(), deptId)); // 深层递归
|
||||
}
|
||||
|
||||
// 构建当前部门节点(仅显示一次在最外层)
|
||||
private TreeSelect buildCurrentDeptNode(SysDept dept, Map<Long, List<SysUser>> deptUsersMap) {
|
||||
TreeSelect node = new TreeSelect();
|
||||
node.setId(dept.getDeptId());
|
||||
node.setLabel(dept.getDeptName());
|
||||
|
||||
// 添加当前部门的用户和子部门(保持原有层级)
|
||||
List<TreeSelect> children = new ArrayList<>();
|
||||
deptUsersMap.getOrDefault(dept.getDeptId(), Collections.emptyList()).forEach(user -> {
|
||||
TreeSelect userNode = new TreeSelect();
|
||||
userNode.setLabel(user.getNickName());
|
||||
userNode.setId(user.getUserId());
|
||||
userNode.setDictId(dept.getDeptId());
|
||||
userNode.setDictName(dept.getDeptName());
|
||||
children.add(userNode);
|
||||
});
|
||||
dept.getChildren().forEach(child -> children.add(buildDeptNode(child, deptUsersMap))); // 递归子部门
|
||||
|
||||
if (!children.isEmpty()) {
|
||||
node.setChildren(children);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// 原始buildTree方法(仅处理原始树,不含当前部门)
|
||||
private List<TreeSelect> buildTree(List<SysDept> depts, Map<Long, List<SysUser>> deptUsersMap) {
|
||||
return depts.stream().map(dept -> {
|
||||
TreeSelect treeSelect = new TreeSelect();
|
||||
treeSelect.setId(dept.getDeptId());
|
||||
treeSelect.setLabel(dept.getDeptName());
|
||||
|
||||
// 处理当前部门以外的用户和子部门
|
||||
List<TreeSelect> children = deptUsersMap.getOrDefault(dept.getDeptId(), Collections.emptyList()).stream()
|
||||
.map(user -> {
|
||||
TreeSelect userNode = new TreeSelect();
|
||||
userNode.setLabel(user.getNickName());
|
||||
userNode.setId(user.getUserId());
|
||||
userNode.setDictId(dept.getDeptId());
|
||||
userNode.setDictName(dept.getDeptName());
|
||||
return userNode;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
children.addAll(buildTree(dept.getChildren(), deptUsersMap)); // 递归原始树的子部门
|
||||
|
||||
if (!children.isEmpty()) {
|
||||
treeSelect.setChildren(children);
|
||||
}
|
||||
return treeSelect;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// 获取当前用户ID(保持不变)
|
||||
|
||||
|
||||
|
||||
/*public List<TreeSelect> buildTree(List<SysDept> depts, Map<Long, List<SysUser>> deptUsersMap) {
|
||||
List<TreeSelect> treeList = new ArrayList<>();
|
||||
for (SysDept dept : depts) {
|
||||
TreeSelect treeSelect = new TreeSelect();
|
||||
|
@ -286,7 +440,7 @@ public class BusinessTripApprovalController extends BaseController {
|
|||
treeList.add(treeSelect);
|
||||
}
|
||||
return treeList;
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
@GetMapping("/addressList")
|
||||
|
|
Loading…
Reference in New Issue