递归添加子部门
/**
* 获取部门树
*
* @return EbookDepartmentListVo
*/
@Override
@InvokeLog
public EbookDepartmentListVo getDepartmentTree() {
// 通过递归DepartmentBaseVO集合,转化为自定义DepartmentDetailVo集合
List<DepartmentDetailVo> transformRes = recursiveTrans(null, hrDmApiClient.departmentTree());
EbookDepartmentListVo departmentInfoList = new EbookDepartmentListVo();
departmentInfoList.setDepartmentItems(transformRes);
return departmentInfoList;
}
/**
* 递归转化
*
* @param parentVo DepartmentDetailVo 集合收集
* @param departmentTree JSONArray 部门树
* @return List<DepartmentDetailVo>
*/
private List<DepartmentDetailVo> recursiveTrans(DepartmentDetailVo parentVo, JSONArray departmentTree) {
List<DepartmentDetailVo> transformRes = new ArrayList<>();
departmentTree
.forEach(item -> {
DepartmentDetailVo departmentDetailVo = new DepartmentDetailVo();
departmentDetailVo.setParentId(((JSONObject) item).getInteger("parentId"));
departmentDetailVo.setLabel(((JSONObject) item).getString("name"));
departmentDetailVo.setValue(((JSONObject) item).getInteger("id"));
transformRes.add(departmentDetailVo);
if (parentVo != null) {
parentVo.setChildren(transformRes);
}
if (StringUtils.isNotEmpty(((JSONObject) item).getString("children"))) {
recursiveTrans(departmentDetailVo, ((JSONObject) item).getJSONArray("children"));
}
});
return transformRes;
}
/**
* 递归转换
*
* @param parent DepartmentDetailVo
* @param departmentBaseVOList List<? extends DepartmentBaseVO> 部门树集合
* @return List<DepartmentDetailVo>
*/
private List<DepartmentDetailVo> recursiveTrans(DepartmentDetailVo parent, List<? extends DepartmentBaseVO> departmentBaseVOList) {
List<DepartmentDetailVo> transformRes = new ArrayList<>();
for (int i = 0; i < departmentBaseVOList.size(); i++) {
DepartmentDetailVo departmentDetailVo = new DepartmentDetailVo();
departmentDetailVo.setParentId(departmentBaseVOList.get(i).getParentId());
departmentDetailVo.setLabel(departmentBaseVOList.get(i).getName());
departmentDetailVo.setValue(departmentBaseVOList.get(i).getId());
transformRes.add(departmentDetailVo);
if (parent != null) {
parent.setChildren(transformRes);
}
if (CollectionUtils.isNotEmpty(departmentBaseVOList.get(i).getChildren())) {
recursiveTrans(departmentDetailVo, departmentBaseVOList.get(i).getChildren());
}
}
return transformRes;
}
存储路径问题
问题:文章路径的获取首先是通过文章路径nodeId,在表e_toc_node找到导航路径uuid,然后在uuid这一行获取path字段,再从相同手册中找到uuid,这样往复循环知道,找到path为0的记录,最后拿到从根到最底层子节点的uuid的集合就是文章路径集合,也就是我想获取的。
原来我是通过循环Map<uuid,path>来查,通过一开始获取到的uuid获取对应的path,然后用这个path再去Map查,但是收集到的结果却总会漏掉一些信息。
解决办法:通过递归的方式来解决,最后通过"0".equals(waitAddUuid)这个判断,结束递归。
/**
* 获取文章路径
*
* @param nodeId Integer 文章路径id
* @param ebookId Long 电子手册id
* @return List<String> 文章路径集合
*/
private List<String> getNodeIdPath(Integer nodeId, Long ebookId) {
// 根据文章路径id查询对应uuid、path
ETocNodeEntity eTocNodeEntity = eTocNodeDao.getById(nodeId);
String uuid = eTocNodeEntity.getUuid();
Validator.notNull(uuid, "参数错误:uuid不能为空");
String path = eTocNodeEntity.getPath();
Validator.notNull(path, "参数错误:path不能为空");
// 获取同一tocId的Map其中key:uuid value:path
LambdaQueryWrapper<ETocNodeEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ETocNodeEntity::getTocId, ebookId);
Map<String, String> pathAndUuidMap = eTocNodeDao.listByConds(queryWrapper)
.stream()
.collect(Collectors.toMap(ETocNodeEntity::getUuid, ETocNodeEntity::getPath));
// 获取文章路径id
List<String> nodeIdPath = new ArrayList<>();
nodeIdPath.add(uuid);
// 递归循环查找
recursiveFind(nodeIdPath, pathAndUuidMap, uuid, 0);
// 翻转
Collections.reverse(nodeIdPath);
return nodeIdPath;
}
private void recursiveFind(List<String> nodeIdPath, Map<String, String> pathAndUuidMaps, String uuid, Integer cycleTimes) {
if (cycleTimes > 20) {
return;
}
String waitAddUuid = pathAndUuidMaps.get(uuid);
if (StringUtils.isEmpty(waitAddUuid) || "0".equals(waitAddUuid)) {
return;
}
nodeIdPath.add(waitAddUuid);
// 递归查找
recursiveFind(nodeIdPath, pathAndUuidMaps, waitAddUuid, cycleTimes + 1);
}