代码优化,避免【菜单、部门】移动节点时出现PID数据环形问题

close https://github.com/elunez/eladmin/issues/803
pull/805/head
Zheng Jie 2023-07-07 17:49:10 +08:00
parent 484785c222
commit 00f7f25062
1 changed files with 9 additions and 6 deletions

View File

@ -67,20 +67,23 @@ public class DeptController {
@ApiOperation("查询部门:根据ID获取同级与上级数据") @ApiOperation("查询部门:根据ID获取同级与上级数据")
@PostMapping("/superior") @PostMapping("/superior")
@PreAuthorize("@el.check('user:list','dept:list')") @PreAuthorize("@el.check('user:list','dept:list')")
public ResponseEntity<Object> getDeptSuperior(@RequestBody List<Long> ids) { public ResponseEntity<Object> getDeptSuperior(@RequestBody List<Long> ids,
@RequestParam(defaultValue = "false") Boolean exclude) {
Set<DeptDto> deptSet = new LinkedHashSet<>(); Set<DeptDto> deptSet = new LinkedHashSet<>();
for (Long id : ids) { for (Long id : ids) {
DeptDto deptDto = deptService.findById(id); DeptDto deptDto = deptService.findById(id);
List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>()); List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>());
for (DeptDto dept : depts) { if(exclude){
if(dept.getId().equals(deptDto.getPid())) { for (DeptDto dept : depts) {
dept.setSubCount(dept.getSubCount() - 1); if(dept.getId().equals(deptDto.getPid())) {
dept.setSubCount(dept.getSubCount() - 1);
}
} }
// 编辑部门时不显示自己以及自己下级的数据避免出现PID数据环形问题
depts = depts.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toList());
} }
deptSet.addAll(depts); deptSet.addAll(depts);
} }
// 编辑部门时不显示自己以及自己下级的数据避免出现PID数据环形问题
deptSet = deptSet.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toSet());
return new ResponseEntity<>(deptService.buildTree(new ArrayList<>(deptSet)),HttpStatus.OK); return new ResponseEntity<>(deptService.buildTree(new ArrayList<>(deptSet)),HttpStatus.OK);
} }