Skip to content

Commit

Permalink
代码优化,避免【菜单、部门】移动节点时出现PID数据环形问题
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Jul 7, 2023
1 parent 3aab156 commit 484785c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author Zheng Jie
Expand Down Expand Up @@ -71,8 +72,15 @@ public ResponseEntity<Object> getDeptSuperior(@RequestBody List<Long> ids) {
for (Long id : ids) {
DeptDto deptDto = deptService.findById(id);
List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>());
for (DeptDto dept : depts) {
if(dept.getId().equals(deptDto.getPid())) {
dept.setSubCount(dept.getSubCount() - 1);
}
}
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.vo.MenuVo;
import me.zhengjie.modules.system.service.MenuService;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.MenuDto;
import me.zhengjie.modules.system.service.dto.MenuQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.MenuMapper;
Expand All @@ -43,7 +44,6 @@
* @author Zheng Jie
* @date 2018-12-03
*/

@RestController
@RequiredArgsConstructor
@Api(tags = "系统:菜单管理")
Expand Down Expand Up @@ -104,8 +104,16 @@ public ResponseEntity<List<MenuDto>> getMenuSuperior(@RequestBody List<Long> ids
if(CollectionUtil.isNotEmpty(ids)){
for (Long id : ids) {
MenuDto menuDto = menuService.findById(id);
menuDtos.addAll(menuService.getSuperior(menuDto, new ArrayList<>()));
List<MenuDto> menuDtoList = menuService.getSuperior(menuDto, new ArrayList<>());
for (MenuDto menu : menuDtoList) {
if(menu.getId().equals(menuDto.getPid())) {
menu.setSubCount(menu.getSubCount() - 1);
}
}
menuDtos.addAll(menuDtoList);
}
// 编辑菜单时不显示自己以及自己下级的数据,避免出现PID数据环形问题
menuDtos = menuDtos.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toSet());
return new ResponseEntity<>(menuService.buildTree(new ArrayList<>(menuDtos)),HttpStatus.OK);
}
return new ResponseEntity<>(menuService.getMenus(null),HttpStatus.OK);
Expand Down

0 comments on commit 484785c

Please sign in to comment.