|
|
@ -1,8 +1,13 @@
|
|
|
|
package com.mashibing.servicemap.service;
|
|
|
|
package com.mashibing.servicemap.service;
|
|
|
|
|
|
|
|
|
|
|
|
import com.mashibing.internalcommon.constant.AmapConfigConstants;
|
|
|
|
import com.mashibing.internalcommon.constant.AmapConfigConstants;
|
|
|
|
|
|
|
|
import com.mashibing.internalcommon.constant.CommonStatusEnum;
|
|
|
|
|
|
|
|
import com.mashibing.internalcommon.dto.DicDistrict;
|
|
|
|
import com.mashibing.internalcommon.dto.ResponseResult;
|
|
|
|
import com.mashibing.internalcommon.dto.ResponseResult;
|
|
|
|
|
|
|
|
import com.mashibing.servicemap.mapper.DicDistrictMapper;
|
|
|
|
import com.mashibing.servicemap.remote.MapDicDistrictClient;
|
|
|
|
import com.mashibing.servicemap.remote.MapDicDistrictClient;
|
|
|
|
|
|
|
|
import net.sf.json.JSONArray;
|
|
|
|
|
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@ -14,15 +19,118 @@ public class DicDistrictService {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private MapDicDistrictClient mapDicDistrictClient;
|
|
|
|
private MapDicDistrictClient mapDicDistrictClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private DicDistrictMapper dicDistrictMapper;
|
|
|
|
|
|
|
|
|
|
|
|
public ResponseResult initDicDistrict(String keywords){
|
|
|
|
public ResponseResult initDicDistrict(String keywords){
|
|
|
|
|
|
|
|
|
|
|
|
// 请求第三方接口,拉取区域编号信息
|
|
|
|
// 请求第三方接口,拉取区域编号信息
|
|
|
|
String dicDistrict = mapDicDistrictClient.dicDistrict(keywords);
|
|
|
|
String dicDistrictResultStr = mapDicDistrictClient.dicDistrict(keywords);
|
|
|
|
System.out.println("\n\ndicDistrict = " + dicDistrict);
|
|
|
|
System.out.println("dicDistrictResultStr = " + dicDistrictResultStr);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析 结果
|
|
|
|
// 解析 结果
|
|
|
|
|
|
|
|
JSONObject resultJobj = JSONObject.fromObject(dicDistrictResultStr);
|
|
|
|
|
|
|
|
int status = resultJobj.getInt(AmapConfigConstants.STATUS);
|
|
|
|
|
|
|
|
// 错误响应 返回失败
|
|
|
|
|
|
|
|
if(status != 1){
|
|
|
|
|
|
|
|
ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 正确响应 则进行详细解析
|
|
|
|
|
|
|
|
JSONArray resultJobjArr = resultJobj.getJSONArray(AmapConfigConstants.DISTRICTS);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历 解析 所有节点信息
|
|
|
|
|
|
|
|
for (int i = 0; i < resultJobjArr.size(); i++) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 0 国家节点下的信息
|
|
|
|
|
|
|
|
JSONObject contryJobj = resultJobjArr.getJSONObject(i);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String contryAddresscode = contryJobj.getString(AmapConfigConstants.ADCODE) ;
|
|
|
|
|
|
|
|
String contryAddressName = contryJobj.getString(AmapConfigConstants.NAME) ;
|
|
|
|
|
|
|
|
String contryParentAddressCode = "0" ;
|
|
|
|
|
|
|
|
String contryLevel = contryJobj.getString(AmapConfigConstants.LEVEL) ;
|
|
|
|
|
|
|
|
// 持久化 国家节点信息
|
|
|
|
|
|
|
|
saveDicDistrictObj(contryAddresscode,contryAddressName,contryParentAddressCode,contryLevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析 省
|
|
|
|
|
|
|
|
JSONArray provinceJobjArr = contryJobj.getJSONArray(AmapConfigConstants.DISTRICTS);
|
|
|
|
|
|
|
|
for (int p = 0; p < provinceJobjArr.size(); p++) {
|
|
|
|
|
|
|
|
JSONObject provinceJobj = provinceJobjArr.getJSONObject(p);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 持久化 省 节点信息
|
|
|
|
|
|
|
|
String provinceAddresscode = provinceJobj.getString(AmapConfigConstants.ADCODE) ;
|
|
|
|
|
|
|
|
String provinceAddressName = provinceJobj.getString(AmapConfigConstants.NAME) ;
|
|
|
|
|
|
|
|
String provinceParentAddressCode = contryAddresscode;
|
|
|
|
|
|
|
|
String provinceLevel = provinceJobj.getString(AmapConfigConstants.LEVEL) ;
|
|
|
|
|
|
|
|
saveDicDistrictObj(provinceAddresscode,provinceAddressName,provinceParentAddressCode,provinceLevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析 市
|
|
|
|
|
|
|
|
JSONArray cityJobjArr = provinceJobj.getJSONArray(AmapConfigConstants.DISTRICTS);
|
|
|
|
|
|
|
|
for (int c = 0;c< cityJobjArr.size();c++) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 持久化 市 节点信息
|
|
|
|
|
|
|
|
JSONObject cityJobj = cityJobjArr.getJSONObject(c);
|
|
|
|
|
|
|
|
String cityAddressCode = cityJobj.getString(AmapConfigConstants.ADCODE);
|
|
|
|
|
|
|
|
String cityAddressName = cityJobj.getString(AmapConfigConstants.NAME);
|
|
|
|
|
|
|
|
String cityParentAddressCode = provinceAddresscode;
|
|
|
|
|
|
|
|
String cityLevel = cityJobj.getString(AmapConfigConstants.LEVEL);
|
|
|
|
|
|
|
|
saveDicDistrictObj(cityAddressCode, cityAddressName, cityParentAddressCode, cityLevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析 区
|
|
|
|
|
|
|
|
JSONArray districtArray = cityJobj.getJSONArray(AmapConfigConstants.DISTRICTS);
|
|
|
|
|
|
|
|
for (int d = 0; d < districtArray.size(); d++) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 持久化 区 节点信息
|
|
|
|
|
|
|
|
JSONObject districtJobj = districtArray.getJSONObject(d);
|
|
|
|
|
|
|
|
String districtAddressCode = districtJobj.getString(AmapConfigConstants.ADCODE);
|
|
|
|
|
|
|
|
String districtAddressName = districtJobj.getString(AmapConfigConstants.NAME);
|
|
|
|
|
|
|
|
String districtLevelParentAddressCode = cityAddressCode;
|
|
|
|
|
|
|
|
String districtLevelLevel = districtJobj.getString(AmapConfigConstants.LEVEL);
|
|
|
|
|
|
|
|
// 暂不解析 街道
|
|
|
|
|
|
|
|
if(districtLevelLevel.equals(AmapConfigConstants.STREET)){
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
saveDicDistrictObj(districtAddressCode, districtAddressName, districtLevelParentAddressCode, districtLevelLevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseResult.success();
|
|
|
|
return ResponseResult.success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void saveDicDistrictObj(String addresscode,String addressName,String parentAddressCode,String levelStr){
|
|
|
|
|
|
|
|
DicDistrict district = new DicDistrict();
|
|
|
|
|
|
|
|
district.setAddressCode(addresscode);
|
|
|
|
|
|
|
|
district.setAddressName(addressName);
|
|
|
|
|
|
|
|
Integer level = generatelevel(levelStr);
|
|
|
|
|
|
|
|
district.setLevel(level) ;
|
|
|
|
|
|
|
|
district.setParentAddressCode(parentAddressCode);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dicDistrictMapper.insert(district);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int generatelevel(String level) {
|
|
|
|
|
|
|
|
int levelInt = 0;
|
|
|
|
|
|
|
|
if (level.trim().equals("country")) {
|
|
|
|
|
|
|
|
levelInt = 0;
|
|
|
|
|
|
|
|
} else if (level.trim().equals("province")) {
|
|
|
|
|
|
|
|
levelInt = 1;
|
|
|
|
|
|
|
|
} else if (level.trim().equals("city")) {
|
|
|
|
|
|
|
|
levelInt = 2;
|
|
|
|
|
|
|
|
} else if (level.trim().equals("district")) {
|
|
|
|
|
|
|
|
levelInt = 3;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return levelInt;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|