省市区三级数据来源
图
在应用中很容易使用到国家省份、城市、区县等数据,通常都是使用区域选择,那么区域的数据如何来?其实是根据国家行政区划代码来设计的,以此为核心来开展地区的选择等业务,在工作中或许会用到

数据来源

数据来源

转换代码

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.Assert;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * 区域处理
 */
public class AreaUtil {

    public static JSONArray getAllAreaList() {
        // http://www.mca.gov.cn/article/sj/xzqh/2020/20201201.html
        // 把最新的区域数据复制到某个文件中,用代码按行读取并解析
        String path = "area.txt";
        JSONArray oneList = new JSONArray();
        try {
            // 文件路径
            System.out.println("当前路径:" + new File(".").getCanonicalPath());
            // 构造一个BufferedReader类来读取文件
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
            ArrayList<String> list = new ArrayList<>();
            String s;
            // 使用readLine方法,一次读一行
            while ((s = br.readLine()) != null) {
                list.add(s.replace(" ", "").replace("\t", ""));
            }
            JSONArray twoList = new JSONArray();
            JSONArray threeList = new JSONArray();
            JSONObject oneItem = new JSONObject();
            JSONObject twoItem = new JSONObject();
            for (String str : list) {
                Assert.isTrue(str.length() > 6, "地区 " + str + " 没有行政编码,请补全");
                int code = Integer.parseInt(str.substring(0, 6));
                String name = str.substring((6));
                if (code % 10000 == 0) {
                    // 三级容器有数据
                    if (threeList.size() > 0) {
                        twoItem.put("child", threeList);
                        threeList = new JSONArray();
                    }
                    // 二级对象有数据
                    if (twoItem.size() > 0) {
                        twoList.add(twoItem);
                        twoItem = new JSONObject();
                    }
                    // 二级容器不为空
                    if (twoList.size() > 0) {
                        oneItem.put("child", twoList);
                        twoList = new JSONArray();
                    }
                    // 一级对象有数据
                    if (oneItem.size() > 0) {
                        oneList.add(oneItem);
                        oneItem = new JSONObject();
                    }
                    // 一级
                    oneItem.put("code", code);
                    oneItem.put("name", name);
                } else {
                    // 4大直辖市 北京(110000)、天津(120000)、上海(310000)、重庆(500000)
                    if ((code > 110000 && code < 120000)
                            || (code > 120000 && code < 130000)
                            || (code > 310000 && code < 320000)
                            || (code > 500000 && code < 510000)) {
                        twoItem.put("code", code);
                        twoItem.put("name", name);
                        twoList.add(twoItem);
                        twoItem = new JSONObject();
                    } else {
                        // 非直辖市
                        if (code % 100 == 0) {
                            // 三级容器有数据
                            if (threeList.size() > 0) {
                                twoItem.put("child", threeList);
                                threeList = new JSONArray();
                            }
                            // 二级对象有数据
                            if (twoItem.size() > 0) {
                                twoList.add(twoItem);
                                twoItem = new JSONObject();
                            }
                            // 二级
                            twoItem.put("code", code);
                            twoItem.put("name", name);
                        } else {
                            // 三级
                            JSONObject threeItem = new JSONObject();
                            threeItem.put("code", code);
                            threeItem.put("name", name);
                            threeList.add(threeItem);
                        }
                    }
                }
            }
            // 三级容器有数据
            if (threeList.size() > 0) {
                twoItem.put("child", threeList);
            }
            // 二级容器不为空
            if (twoList.size() > 0) {
                oneItem.put("child", twoList);
            }
            // 一级对象有数据
            if (oneItem.get("code") != null) {
                oneList.add(oneItem);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oneList;
    }

}

城市编码管辖范围工具类

/**
 * 城市编码工具类
 */
public class CityCodeUtil {

    /**
     * 获取城市编码能管理的最大范围
     */
    public static int maxCityCode(int cityCode) {
        // 省级
        if (cityCode % 10000 == 0) {
            return cityCode + 9999;
        }
        // 市级
        if (cityCode % 100 == 0) {
            return cityCode + 99;
        }
        // 区县
        return cityCode;
    }

}

查找所在省份/城市

// 省份
(520115 / 10000) * 10000
// 城市
(520115 / 100) * 100

注意JS计算用以下方案,否则无法取整
parseInt(520100 / 10000) * 10000