rain
2024-07-31 587e31924f7bae43f8f31cb4fc9bf5f33593b50f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.dji.sample.patches.utils;
 
import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 行政区划编码工具类
 *
 * @author ht
 */
public class DistrictCodeUtils {
 
    private static final Logger log = LoggerFactory.getLogger(DistrictCodeUtils.class);
 
    /**
     * 行政区划编码数据
     */
    private static Map DistrictCode = new HashMap();
 
    static {
        try {
            log.info("初始化行政区划编码数据!!!");
            String result = ResourceUtil.readUtf8Str("classpath:districtCode.json");
            DistrictCode = JSONObject.parseObject(result, Map.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public static String nameToCode(String name) {
        boolean bs = isChinese(name);
        if (!bs) {
            return codeToName(name);
        }
        Map<String, Object> jsonObject = new HashMap<>(DistrictCode);
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            if (entry.getValue().equals(name)) {
                return entry.getKey();
            }
        }
        return null;
    }
 
    /**
     * 地区编码转名称
     *
     * @param code
     * @return
     */
    public static String codeToName(String code) {
        if (!DistrictCode.containsKey(code)) {
            return null;
        }
        return DistrictCode.get(code).toString();
    }
 
    public static Object nameToCodes(String name) {
        if (!DistrictCode.containsValue(name)) {
            return null;
        }
        return DistrictCode.get(name);
    }
 
    public static boolean isChinese(String str) {
        return str.matches("[\\u4E00-\\u9FFF]+");
    }
 
 
    public static void main(String[] args) {
        System.out.println(nameToCode("222400"));
        System.out.println(nameToCode("三沙市"));
    }
 
}