public class TreeJson implements Serializable { private static final long serialVersionUID = 7317390555555174059L; private String id; private String pid; private String name; private String title; private List<TreeJson> children;
/** * 推荐关系树状图 * @param userId * @return */ @Override public List<TreeJson> showChildNodes(String userId) { List<TreeJson> singleTreeNodes=memberMapper.showChildNodes(userId); if(singleTreeNodes == null || singleTreeNodes.isEmpty()) { return null; } List<TreeJson> nodeTrees = new ArrayList<TreeJson>(); Map<String,TreeJson> treeMap = new LinkedHashMap<String,TreeJson>(); for(TreeJson node : singleTreeNodes){ TreeJson treeNode = new TreeJson(); treeNode.setId(node.getId()); treeNode.setTitle(node.getTitle()); treeNode.setName(node.getId()); treeNode.setPid(node.getPid()); treeMap.put(node.getId(), treeNode); } for(String nodeId : treeMap.keySet()){ TreeJson treeNode = treeMap.get(nodeId); String pid = treeNode.getPid(); if(pid==null || pid.length()==0 || !treeMap.containsKey(pid)){ treeNode.setPid(""); nodeTrees.add(treeNode); }else{ TreeJson parentTreeNode = treeMap.get(pid); if(parentTreeNode.getChildren()==null){ parentTreeNode.setChildren(new ArrayList<TreeJson>()); } parentTreeNode.getChildren().add(treeNode); } } return nodeTrees; }