elunez / eladmin

eladmin jpa 版本:项目基于 Spring Boot 2.6.4、 Jpa、 Spring Security、Redis、Vue的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,支持一键生成前后端代码,支持动态路由
https://eladmin.vip/demo
Apache License 2.0
21.04k stars 7.33k forks source link

BUG:查询部门:根据ID获取同级与上级数据 #842

Open NM1024 opened 2 months ago

NM1024 commented 2 months ago

system/dept.js

export function getDeptSuperior(ids, exclude) {
  exclude = exclude !== undefined ? exclude : false

  // 修改后
  const data = Array.isArray(ids) || ids.length === 0 ? ids : Array.of(ids)
  // 原来的,再某些情况下参数无法以数组的形式传递给后端
  // const data = ids.length || ids.length === 0 ? ids : Array.of(ids)
  debugger
  return request({
    url: 'api/dept/superior?exclude=' + exclude,
    method: 'post',
    data
  })
}

对应的后端APIsystem/rest/DeptController

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