第一步:获取结果集
/**
* 获取表单关系
*
* @param formId
*/
@Override
public Result getReltionList(String formId) {
if(StringUtils.isBlank(formId)){
return Result.error(ResponseMessage.PARAMERROR);
}
LambdaQueryWrapper<FormReltionship> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(FormReltionship::getDeleteIf,true);
wrapper.eq(FormReltionship::getFormId,formId);
List<FormReltionship> shipList = formReltionshipMapper.selectList(wrapper);
// 以树型结构返回
List<FormReltionship> treeList = getTree("0",shipList);
return Result.success(treeList);
}
工具类:
public class FormUtils {
/** 表单关系获取树形结构-根据父id
* */
public static List<FormReltionship> getTree(String parentId,List<FormReltionship> shipList){
List<FormReltionship> formReltionshipList = new ArrayList<>();
// 获取父节点
List<FormReltionship> mainNodeList = getMainNode(parentId,shipList);
mainNodeList.stream().forEach(x->{
// 根据父节点下的子节点
x=childShipList(x,shipList);
formReltionshipList.add(x);
});
return formReltionshipList;
}
/** 递归 获取 子节点
* */
public static FormReltionship childShipList(FormReltionship ship,List<FormReltionship> shipList){
List<FormReltionship> childList = new ArrayList<>();
shipList.stream().forEach(x->{
// 通过parentId 进行比较
if(ship.getId().equals(x.getParentId())){
// 如果为子节点,则判断子节点下是否存在节点
childList.add(childShipList(x,shipList));
}
});
ship.setChildrenList(childList);
return ship;
}
/**获取相同formId下 parentid 的父节点
* */
public static List<FormReltionship> getMainNode(String parentId,List<FormReltionship> shipList){
List<FormReltionship> reltionshipList = new ArrayList<>();
shipList.stream().forEach(x->{
if(parentId.equals(x.getParentId())){
reltionshipList.add(x);
}
});
return reltionshipList;
}
}