增加地区接口

This commit is contained in:
menxipeng
2025-08-23 14:25:31 +08:00
parent acfb370c73
commit f30ab86f2d
8 changed files with 552 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
package com.ruoyi.web.controller.back;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.common.utils.RegionDataImporter;
import com.ruoyi.system.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.IOException;
import java.util.List;
@RequestMapping("/back/region")
@RestController
public class RegionController {
@Autowired
private IRegionService regionService;
@RequestMapping("/add")
public AjaxResult add() throws IOException {
File file = new File("D:\\region_tree_data.json");
if (!file.exists()) {
System.err.println("文件不存在: " + file.getAbsolutePath());
}
// 读取JSON文件内容
String jsonContent = org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8");
System.out.println("JSON文件读取成功内容长度: " + jsonContent.length());
// 导入数据
List<Region> regionList = RegionDataImporter.importRegionData(jsonContent);
regionService.batchInsert(regionList);
return AjaxResult.success();
}
}

View File

@@ -0,0 +1,81 @@
package com.ruoyi.web.controller.client;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.system.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 地区信息接口
*/
@RequestMapping("/client/region")
@RestController
public class ClientRegionController {
@Autowired
private IRegionService regionService;
/**
* 获取省份列表
*/
@GetMapping("/provinces")
public AjaxResult getProvinces() {
Region region = new Region();
region.setLevel(1); // 省级
List<Region> provinces = regionService.selectRegionList(region);
return AjaxResult.success(provinces);
}
/**
* 根据父级编码获取子地区列表
*/
@GetMapping("/children/{parentCode}")
public AjaxResult getChildrenByParentCode(@PathVariable("parentCode") String parentCode) {
Region region = new Region();
region.setParentCode(parentCode);
List<Region> children = regionService.selectRegionList(region);
return AjaxResult.success(children);
}
/**
* 根据地区编码获取完整地址(省市区)
*/
@GetMapping("/address/{code}")
public AjaxResult getFullAddress(@PathVariable("code") String code) {
// 获取当前地区
Region currentRegion = regionService.selectRegionByCode(code);
if (currentRegion == null) {
return AjaxResult.error("地区编码不存在");
}
List<String> addressParts = new ArrayList<>();
addressParts.add(currentRegion.getName());
// 获取父级地区
String parentCode = currentRegion.getParentCode();
while (parentCode != null) {
Region parentRegion = regionService.selectRegionByCode(parentCode);
if (parentRegion == null) {
break;
}
addressParts.add(0, parentRegion.getName());
parentCode = parentRegion.getParentCode();
}
Map<String, Object> result = new HashMap<>();
result.put("address", String.join(" ", addressParts));
result.put("addressParts", addressParts);
return AjaxResult.success(result);
}
}

View File

@@ -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;
}

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,64 @@
package com.ruoyi.system.mapper;
import com.ruoyi.common.core.domain.entity.Region;
import java.util.List;
/**
* 地区信息Mapper接口
*
* @author ruoyi
* @date 2025-08-23
*/
public interface RegionMapper
{
/**
* 查询地区信息
*
* @param code 地区信息主键
* @return 地区信息
*/
public Region selectRegionByCode(String code);
/**
* 查询地区信息列表
*
* @param region 地区信息
* @return 地区信息集合
*/
public List<Region> selectRegionList(Region region);
/**
* 新增地区信息
*
* @param region 地区信息
* @return 结果
*/
public int insertRegion(Region region);
/**
* 修改地区信息
*
* @param region 地区信息
* @return 结果
*/
public int updateRegion(Region region);
/**
* 删除地区信息
*
* @param code 地区信息主键
* @return 结果
*/
public int deleteRegionByCode(String code);
/**
* 批量删除地区信息
*
* @param codes 需要删除的数据主键集合
* @return 结果
*/
public int deleteRegionByCodes(String[] codes);
int batchInsert(List<Region> regionList);
}

View File

@@ -0,0 +1,64 @@
package com.ruoyi.system.service;
import com.ruoyi.common.core.domain.entity.Region;
import java.util.List;
/**
* 地区信息Service接口
*
* @author ruoyi
* @date 2025-08-23
*/
public interface IRegionService
{
/**
* 查询地区信息
*
* @param code 地区信息主键
* @return 地区信息
*/
public Region selectRegionByCode(String code);
/**
* 查询地区信息列表
*
* @param region 地区信息
* @return 地区信息集合
*/
public List<Region> selectRegionList(Region region);
/**
* 新增地区信息
*
* @param region 地区信息
* @return 结果
*/
public int insertRegion(Region region);
/**
* 修改地区信息
*
* @param region 地区信息
* @return 结果
*/
public int updateRegion(Region region);
/**
* 批量删除地区信息
*
* @param codes 需要删除的地区信息主键集合
* @return 结果
*/
public int deleteRegionByCodes(String[] codes);
/**
* 删除地区信息信息
*
* @param code 地区信息主键
* @return 结果
*/
public int deleteRegionByCode(String code);
int batchInsert(List<Region> regionList);
}

View File

@@ -0,0 +1,99 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.system.mapper.RegionMapper;
import com.ruoyi.system.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 地区信息Service业务层处理
*
* @author ruoyi
* @date 2025-08-23
*/
@Service
public class RegionServiceImpl implements IRegionService
{
@Autowired
private RegionMapper regionMapper;
/**
* 查询地区信息
*
* @param code 地区信息主键
* @return 地区信息
*/
@Override
public Region selectRegionByCode(String code)
{
return regionMapper.selectRegionByCode(code);
}
/**
* 查询地区信息列表
*
* @param region 地区信息
* @return 地区信息
*/
@Override
public List<Region> selectRegionList(Region region)
{
return regionMapper.selectRegionList(region);
}
/**
* 新增地区信息
*
* @param region 地区信息
* @return 结果
*/
@Override
public int insertRegion(Region region)
{
return regionMapper.insertRegion(region);
}
/**
* 修改地区信息
*
* @param region 地区信息
* @return 结果
*/
@Override
public int updateRegion(Region region)
{
return regionMapper.updateRegion(region);
}
/**
* 批量删除地区信息
*
* @param codes 需要删除的地区信息主键
* @return 结果
*/
@Override
public int deleteRegionByCodes(String[] codes)
{
return regionMapper.deleteRegionByCodes(codes);
}
/**
* 删除地区信息信息
*
* @param code 地区信息主键
* @return 结果
*/
@Override
public int deleteRegionByCode(String code)
{
return regionMapper.deleteRegionByCode(code);
}
@Override
public int batchInsert(List<Region> regionList) {
return regionMapper.batchInsert(regionList);
}
}

View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.RegionMapper">
<resultMap type="Region" id="RegionResult">
<result property="code" column="code" />
<result property="name" column="name" />
<result property="parentCode" column="parent_code" />
<result property="level" column="level" />
</resultMap>
<sql id="selectRegionVo">
select code, name, parent_code, level from region
</sql>
<select id="selectRegionList" parameterType="Region" resultMap="RegionResult">
<include refid="selectRegionVo"/>
<where>
<if test="name != null and name != ''"> and name like concat('%', #{name}, '%')</if>
<if test="parentCode != null and parentCode != ''"> and parent_code = #{parentCode}</if>
<if test="level != null and level != ''"> and level = #{level}</if>
</where>
</select>
<select id="selectRegionByCode" parameterType="String" resultMap="RegionResult">
<include refid="selectRegionVo"/>
where code = #{code}
</select>
<insert id="insertRegion" parameterType="Region">
insert into region
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="code != null">code,</if>
<if test="name != null and name != ''">name,</if>
<if test="parentCode != null">parent_code,</if>
<if test="level != null">level,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="code != null">#{code},</if>
<if test="name != null and name != ''">#{name},</if>
<if test="parentCode != null">#{parentCode},</if>
<if test="level != null">#{level},</if>
</trim>
</insert>
<update id="updateRegion" parameterType="Region">
update region
<trim prefix="SET" suffixOverrides=",">
<if test="name != null and name != ''">name = #{name},</if>
<if test="parentCode != null">parent_code = #{parentCode},</if>
<if test="level != null">level = #{level},</if>
</trim>
where code = #{code}
</update>
<delete id="deleteRegionByCode" parameterType="String">
delete from region where code = #{code}
</delete>
<delete id="deleteRegionByCodes" parameterType="String">
delete from region where code in
<foreach item="code" collection="array" open="(" separator="," close=")">
#{code}
</foreach>
</delete>
<insert id="batchInsert" parameterType="java.util.List">
insert into region (code, name, parent_code, level) values
<foreach collection="list" item="item" separator=",">
(#{item.code}, #{item.name}, #{item.parentCode}, #{item.level})
</foreach>
</insert>
</mapper>