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("三沙市"));
|
}
|
|
}
|