|
@@ -4,7 +4,7 @@ import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.text.csv.CsvRow;
|
|
|
import cn.hutool.core.text.csv.CsvUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.util.ObjUtil;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
|
|
import cn.iocoder.yudao.framework.ip.core.Area;
|
|
|
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
|
|
@@ -18,6 +18,7 @@ import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst;
|
|
|
|
|
|
/**
|
|
|
* 区域工具类
|
|
@@ -75,6 +76,26 @@ public class AreaUtils {
|
|
|
return areas.get(id);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获得指定区域对应的编号
|
|
|
+ *
|
|
|
+ * @param path 区域编号
|
|
|
+ * @return 编号
|
|
|
+ */
|
|
|
+ public static Area getArea(String path) {
|
|
|
+ String[] paths = path.split("/");
|
|
|
+ Area area = null;
|
|
|
+ for (int i = 0; i < paths.length; i++) {
|
|
|
+ final int finalI = i;
|
|
|
+ if (area == null) {
|
|
|
+ area = findFirst(convertList(areas.values(), a -> a), item -> ObjUtil.equal(paths[finalI], item.getName()));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ area = findFirst(area.getChildren(), item -> ObjUtil.equal(paths[finalI], item.getName()));
|
|
|
+ }
|
|
|
+ return area;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取所有节点的全路径名称如:河南省/石家庄市/新华区
|
|
|
*
|
|
@@ -82,23 +103,29 @@ public class AreaUtils {
|
|
|
* @return 所有节点的全路径名称
|
|
|
*/
|
|
|
public static List<String> getAllAreaNodePaths(List<Area> areas) {
|
|
|
- return convertList(areas, AreaUtils::buildTreePath);
|
|
|
+ List<String> paths = new ArrayList<>();
|
|
|
+ areas.forEach(area -> traverse(area, "", paths));
|
|
|
+ return paths;
|
|
|
}
|
|
|
|
|
|
- // TODO @puhui999: 展开树再构建全路径
|
|
|
- private static String buildTreePath(Area node) {
|
|
|
- if (node.getParent() == null || Area.ID_CHINA.equals(node.getParent().getId())) { // 忽略中国
|
|
|
- // 已经是根节点,直接返回节点名称
|
|
|
- return node.getName();
|
|
|
- } else {
|
|
|
- // 递归拼接上级节点的名称
|
|
|
- Area parent = getArea(node.getParent().getId());
|
|
|
- if (parent != null) {
|
|
|
- String parentPath = buildTreePath(parent);
|
|
|
- return parentPath + "/" + node.getName();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 构建一棵树的所有节点的全路径名称,并将其存储为 "祖先/父级/子级" 的形式
|
|
|
+ *
|
|
|
+ * @param node 父节点
|
|
|
+ * @param path 全路径名称
|
|
|
+ * @param paths 全路径名称列表
|
|
|
+ */
|
|
|
+ private static void traverse(Area node, String path, List<String> paths) {
|
|
|
+ if (node == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 构建当前节点的路径
|
|
|
+ String currentPath = path.isEmpty() ? node.getName() : path + "/" + node.getName();
|
|
|
+ paths.add(currentPath);
|
|
|
+ // 递归遍历子节点
|
|
|
+ for (Area child : node.getChildren()) {
|
|
|
+ traverse(child, currentPath, paths);
|
|
|
}
|
|
|
- return StrUtil.EMPTY;
|
|
|
}
|
|
|
|
|
|
/**
|