TreeUtil.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package thyyxxk.webserver.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import thyyxxk.webserver.entity.ResultVo;
  4. import thyyxxk.webserver.entity.settings.permissions.MenuItem;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. /**
  10. * @description: 层级菜单工具
  11. * @author: DingJie
  12. * @create: 2021/8/1016:09
  13. */
  14. public class TreeUtil {
  15. public static ResultVo<List<MenuItem>> getMenuTree(List<MenuItem> tempList) {
  16. List<MenuItem> resultList = new ArrayList<>();
  17. Map<Integer, MenuItem> treeMap = new HashMap<>(tempList.size());
  18. for (MenuItem item : tempList) {
  19. JSONObject meta = new JSONObject();
  20. if (StringUtil.notBlank(item.getIcon())) {
  21. meta.put("icon", "iconfont " + item.getIcon());
  22. }
  23. meta.put("title", item.getName());
  24. item.setMeta(meta);
  25. treeMap.put(item.getCode(), item);
  26. if (0 == item.getParent()) {
  27. resultList.add(item);
  28. }
  29. }
  30. for (MenuItem item : tempList) {
  31. MenuItem template = treeMap.get(item.getParent());
  32. if (null != template) {
  33. if (null == template.getChildren()) {
  34. template.setChildren(new ArrayList<>());
  35. }
  36. template.getChildren().add(item);
  37. }
  38. }
  39. return ResultVoUtil.success(resultList);
  40. }
  41. }