1、构建treeData
/**
* 查询所有分类
* @return
*/
@ApiOperation(value = "查询所有分类")
@GetMapping("/allCategoryList")
public R indexAllCategory() {
List<ProjectCategory> list = projectCategoryService.indexAllCategory();
return R.data(ResultCode.SUCCESS.getCode(), list, "查询所有分类成功");
}
@Override
public List<ProjectCategory> indexAllCategory() {
List<ProjectCategory> resultList = new ArrayList<>();
Long rootId = null;//根节点
ProjectCategory rootCategory = projectCategoryMapper.selectOne(new QueryWrapper<ProjectCategory>().eq("pid", 0).eq("is_deleted", 0));
if(rootCategory != null){
rootId = rootCategory.getId();
}
if(rootId == null){
throw new SinomaException("根节点不能为空,请核实!");
}
//获取所有分类数据
List<ProjectCategory> allCategoryList = projectCategoryMapper.selectList(new QueryWrapper<ProjectCategory>().eq("is_deleted",0));
resultList = build(rootId, allCategoryList);
return resultList;
}
private static List<ProjectCategory> build(Long rootId, List<ProjectCategory> categoryList) {
List<ProjectCategory> trees = new ArrayList<>();
if (CollectionUtils.isNotEmpty(categoryList)) {
for (ProjectCategory category : categoryList) {
if (rootId.equals(category.getId())) {
category.setLevel(1);
trees.add(findChildren(category, categoryList));
}
}
}
return trees;
}
private static ProjectCategory findChildren(ProjectCategory treeNode, List<ProjectCategory> treeNodes) {
treeNode.setChildren(new ArrayList<ProjectCategory>());
for (ProjectCategory it : treeNodes) {
if (treeNode.getId().equals(it.getPid())) {
int level = treeNode.getLevel() + 1;
it.setLevel(level);
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<>());
}
treeNode.getChildren().add(findChildren(it, treeNodes));
}
}
List<ProjectCategory> children = treeNode.getChildren();
treeNode.setChildren(children);
return treeNode;
}
2、递归删除分类
@Override
public void removeChildById(Long id) {
List<Long> idList = new ArrayList<>();
this.selectChildListById(id, idList);
idList.add(id);
baseMapper.deleteBatchIds(idList);
}
/**
* 递归获取子节点
* @param id
* @param idList
*/
private void selectChildListById(Long id, List<Long> idList) {
List<ProjectCategory> childList = baseMapper.selectList(new QueryWrapper<ProjectCategory>().eq("pid", id).select("id"));
childList.stream().forEach(item -> {
idList.add(item.getId());
this.selectChildListById(item.getId(), idList);
});
}
3、向上递归获取code
//根据parent_id获取机构信息,递归获取所有上级机构编码
String institutionCode = entity.getInstitutionCode();
if (StringUtils.isNotBlank(institutionCode)) {
projectInstitutionCodes.add(institutionCode);
InstitutionDO institutionDO = institutionService.getOne(new QueryWrapper<InstitutionDO>().eq("institution_code", institutionCode).eq("is_deleted", 0));
if (institutionDO != null) {
projectInstitutionCodes = getProjectCodeAll(projectInstitutionCodes, institutionDO);
}
}
if (CollectionUtils.isNotEmpty(projectInstitutionCodes)) {
entity.setMainInstitutionAll(String.join(",", projectInstitutionCodes));
} else {
entity.setMainInstitutionAll("");
}
private List<String> getProjectCodeAll(List<String> mainInstitutionCodes, InstitutionDO entity) {
InstitutionDO institutionDO = institutionService.getById(entity.getParentId());
if (institutionDO != null) {
mainInstitutionCodes.add(institutionDO.getInstitutionCode());
getProjectCodeAll(mainInstitutionCodes, institutionDO);
}
return mainInstitutionCodes;
}
4、向下递归获取code
List<ProjectVolumeVO> volumeProjectTree = projectService.projectVolumeUp();
List<String> projectCodes = new ArrayList<>();
for (ProjectVolumeVO volumeVO : volumeProjectTree) {
projectCodes = drilldownProjectCode(projectCodes,volumeVO);
}
private static List<String> drilldownProjectCode(List<String> projectCodes,ProjectVolumeVO entity) {
projectCodes.add(entity.getProjectCode());
if (entity.getChild() != null && entity.getChild().size() != 0) {
for (ProjectVolumeVO childEntity : entity.getChild()) {
drilldownProjectCode(projectCodes,childEntity);
}
}
return projectCodes;
}