递归查询

select id,name from sys_dept where pids like '%1067246875800000065%' and del_flag = 0

select id,name from sys_dept where pid ='1067246875800000065' and del_flag = 0

 


/**
* getChildSubDeptIdList
* @return
*/
@GetMapping("getChildSubDeptIdList")
@ApiOperation("getChildSubDeptIdList")
public Result<List<Long>> getChildSubDeptIdList(){
List<Long> idList =new ArrayList<>();
List<Long> data = sysDeptService.getChildSubDeptIdList(1067246875800000065L,idList);
return new Result<List<Long>>().ok(data);
}



/**
*
* @param id 部门ID
* @return
*/
@Override
public List<Long> getChildSubDeptIdList(Long id,List<Long> idList) {
List<Long> deptIdList = baseDao.getChildSubDeptIdList(id);
for (int i=0;i<deptIdList.size();i++){
Long deptId= deptIdList.get(i);
idList.add(deptId);
getChildSubDeptIdList(deptId,idList);
}
deptIdList.add(id);
return idList;
}

/**
* 根据部门ID,获取所有子部门ID列表
* @param id 部门ID
*/
List<Long> getChildSubDeptIdList(Long id);


<select id="getChildSubDeptIdList" resultType="long">
select id from sys_dept where pid = #{id} and del_flag = 0
</select>


子查询父级
/**
* getParentPidById
* @return
*/
@GetMapping("getParentPidById")
@ApiOperation("getParentPidById")
public Result<List<Long>> getParentPidById(){
List<Long> idList =new ArrayList<>();
List<Long> data = sysDeptService.getParentPidById(1425390890702680066L,idList);
return new Result<List<Long>>().ok(data);
}

/**
* 根据部门ID,获取本部门及父部门ID列
* @param id
* @param idList
* @return
*/
List<Long> getParentPidById(Long id,List<Long> idList);


/**
* getParentPidById
* @param id
* @param idList
* @return
*/
@Override
public List<Long> getParentPidById(Long id,List<Long> idList) {
Long deptId = baseDao.getParentPidById(id);
if (deptId!=null && deptId!=0L){
idList.add(deptId);
getParentPidById(deptId,idList);
}
return idList;
}

/**
* 根据部门ID,获取所有父部门ID列表
* @param id
* @return
*/
Long getParentPidById(Long id);

<select id="getParentPidById" resultType="long">
select pid from sys_dept where id = #{id} and del_flag = 0
</select>






/**
* 获取所有上级部门ID
* @param pid 上级ID
*/
private String getPidList(Long pid){
//顶级部门,无上级部门
if(Constant.DEPT_ROOT.equals(pid)){
return Constant.DEPT_ROOT + "";
}

//所有部门的id、pid列表
List<SysDeptEntity> deptList = baseDao.getIdAndPidList();

//list转map
Map<Long, SysDeptEntity> map = new HashMap<>(deptList.size());
for(SysDeptEntity entity : deptList){
map.put(entity.getId(), entity);
}

//递归查询所有上级部门ID列表
List<Long> pidList = new ArrayList<>();
getPidTree(pid, map, pidList);

return StringUtils.join(pidList, ",");
}


/**
* 递归查询
* @param pid
* @param map
* @param pidList
*/
private void getPidTree(Long pid, Map<Long, SysDeptEntity> map, List<Long> pidList) {
//顶级部门,无上级部门
if(Constant.DEPT_ROOT.equals(pid)){
return ;
}

//上级部门存在
SysDeptEntity parent = map.get(pid);
if(parent != null){
getPidTree(parent.getPid(), map, pidList);
}

pidList.add(pid);
}
posted @ 2021-08-12 13:53  小蚊子大人KN  阅读(69)  评论(0编辑  收藏  举报