增加地区接口
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.common.core.domain.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class Region {
|
||||
private String code;
|
||||
private String name;
|
||||
private String parentCode;
|
||||
private int level;
|
||||
private List<Region> children;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.common.core.domain.entity.Region;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RegionDataImporter {
|
||||
|
||||
/**
|
||||
* 从JSON文件读取并处理地区数据
|
||||
*
|
||||
* @param jsonContent JSON字符串内容
|
||||
* @return 地区数据列表
|
||||
*/
|
||||
public static List<Region> importRegionData(String jsonContent) {
|
||||
List<Region> allRegions = new ArrayList<>();
|
||||
|
||||
try {
|
||||
JSONArray provinces = JSON.parseArray(jsonContent);
|
||||
processRegionData(provinces, null, 1, allRegions);
|
||||
System.out.println("成功解析地区数据,共 " + allRegions.size() + " 条记录");
|
||||
} catch (Exception e) {
|
||||
System.err.println("解析JSON数据时发生错误: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return allRegions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归处理地区数据
|
||||
*
|
||||
* @param regions JSON数组,包含当前层级的地区数据
|
||||
* @param parentCode 父级编码
|
||||
* @param level 当前层级
|
||||
* @param allRegions 存储所有地区数据的列表
|
||||
*/
|
||||
private static void processRegionData(JSONArray regions, String parentCode, int level, List<Region> allRegions) {
|
||||
if (regions == null || regions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < regions.size(); i++) {
|
||||
JSONObject regionJson = regions.getJSONObject(i);
|
||||
if (regionJson == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 创建地区对象
|
||||
Region region = new Region();
|
||||
region.setCode(regionJson.getString("code"));
|
||||
region.setName(regionJson.getString("text"));
|
||||
region.setParentCode(parentCode);
|
||||
region.setLevel(level);
|
||||
allRegions.add(region);
|
||||
|
||||
// 递归处理子地区
|
||||
JSONArray children = regionJson.getJSONArray("children");
|
||||
processRegionData(children, region.getCode(), level + 1, allRegions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件读取JSON内容
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return JSON字符串内容
|
||||
* @throws IOException 文件读取异常
|
||||
*/
|
||||
public static String readJsonFile(String filePath) throws IOException {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
throw new IOException("文件不存在: " + file.getAbsolutePath());
|
||||
}
|
||||
return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// 读取JSON文件内容
|
||||
String jsonContent = readJsonFile("D:\\region_tree_data.json");
|
||||
System.out.println("JSON文件读取成功,内容长度: " + jsonContent.length());
|
||||
|
||||
// 导入数据
|
||||
List<Region> regions = importRegionData(jsonContent);
|
||||
|
||||
// 输出部分数据进行验证
|
||||
if (!regions.isEmpty()) {
|
||||
System.out.println("数据示例:");
|
||||
for (int i = 0; i < Math.min(5, regions.size()); i++) {
|
||||
Region r = regions.get(i);
|
||||
System.out.printf("编码: %s, 名称: %s, 父级编码: %s, 层级: %d%n",
|
||||
r.getCode(), r.getName(), r.getParentCode(), r.getLevel());
|
||||
}
|
||||
|
||||
// 也可以输出最后几条数据
|
||||
System.out.println("最后几条数据示例:");
|
||||
for (int i = Math.max(0, regions.size() - 5); i < regions.size(); i++) {
|
||||
Region r = regions.get(i);
|
||||
System.out.printf("编码: %s, 名称: %s, 父级编码: %s, 层级: %d%n",
|
||||
r.getCode(), r.getName(), r.getParentCode(), r.getLevel());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("处理过程中发生错误: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user