|
|
|
@ -1,7 +1,13 @@
|
|
|
|
|
package com.taxi.servicemap.service;
|
|
|
|
|
|
|
|
|
|
import com.internal.contant.AmapConfigConstant;
|
|
|
|
|
import com.internal.contant.CommonStatusEnum;
|
|
|
|
|
import com.internal.dto.DicDistrict;
|
|
|
|
|
import com.internal.dto.ResponseResult;
|
|
|
|
|
import com.taxi.servicemap.mapper.DicDistrictMapper;
|
|
|
|
|
import com.taxi.servicemap.remote.MapDicDistrictClient;
|
|
|
|
|
import net.sf.json.JSONArray;
|
|
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
@ -11,13 +17,100 @@ public class DicDistrictService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private MapDicDistrictClient mapDicDistrictClient;
|
|
|
|
|
|
|
|
|
|
public ResponseResult initDicDistrict(String keywords){
|
|
|
|
|
@Autowired
|
|
|
|
|
private DicDistrictMapper districtMapper;
|
|
|
|
|
|
|
|
|
|
public ResponseResult initDicDistrict(String keywords) {
|
|
|
|
|
//拼接请求地图
|
|
|
|
|
String dicDistrictObj = mapDicDistrictClient.dicDistrict(keywords);
|
|
|
|
|
System.out.println(dicDistrictObj);
|
|
|
|
|
String dicDistrictStr = mapDicDistrictClient.dicDistrict(keywords);
|
|
|
|
|
System.out.println(dicDistrictStr);
|
|
|
|
|
//解析结果
|
|
|
|
|
JSONObject dicDistrictObj = JSONObject.fromObject(dicDistrictStr);
|
|
|
|
|
int status = dicDistrictObj.getInt(AmapConfigConstant.STATUS);
|
|
|
|
|
if (status != 1) {
|
|
|
|
|
return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
JSONArray countryJsonArray = dicDistrictObj.getJSONArray(AmapConfigConstant.DISTRICTS);
|
|
|
|
|
for (int country = 0; country < countryJsonArray.size(); country++) {
|
|
|
|
|
JSONObject countryJsonObject = countryJsonArray.getJSONObject(country);
|
|
|
|
|
String countryAddressAdcode = countryJsonObject.getString(AmapConfigConstant.ADCODE);
|
|
|
|
|
String countryAddressName = countryJsonObject.getString(AmapConfigConstant.NAME);
|
|
|
|
|
String countryParentAddressCode = "0";
|
|
|
|
|
String countryLevel = countryJsonObject.getString(AmapConfigConstant.LEVEL);
|
|
|
|
|
insertDicDistrict(countryAddressAdcode, countryAddressName, countryParentAddressCode, countryLevel);
|
|
|
|
|
|
|
|
|
|
JSONArray provinceJsonArray = countryJsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
|
|
|
|
|
|
|
|
|
|
for (int province = 0; province < provinceJsonArray.size(); province++) {
|
|
|
|
|
JSONObject provideJsonObject = provinceJsonArray.getJSONObject(province);
|
|
|
|
|
String provinceAddressAdcode = provideJsonObject.getString(AmapConfigConstant.ADCODE);
|
|
|
|
|
String provinceAddressName = provideJsonObject.getString(AmapConfigConstant.NAME);
|
|
|
|
|
String provinceParentAddressCode = countryAddressAdcode;
|
|
|
|
|
String provinceLevel = provideJsonObject.getString(AmapConfigConstant.LEVEL);
|
|
|
|
|
|
|
|
|
|
insertDicDistrict(provinceAddressAdcode, provinceAddressName, provinceParentAddressCode, provinceLevel);
|
|
|
|
|
|
|
|
|
|
JSONArray cityJsonArray = provideJsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
|
|
|
|
|
|
|
|
|
|
for (int city = 0; city < cityJsonArray.size(); city++) {
|
|
|
|
|
JSONObject cityJsonObject = cityJsonArray.getJSONObject(city);
|
|
|
|
|
String cityAddressAdcode = cityJsonObject.getString(AmapConfigConstant.ADCODE);
|
|
|
|
|
String cityAddressName = cityJsonObject.getString(AmapConfigConstant.NAME);
|
|
|
|
|
String cityParentAddressCode = provinceAddressAdcode;
|
|
|
|
|
String cityLevel = cityJsonObject.getString(AmapConfigConstant.LEVEL);
|
|
|
|
|
insertDicDistrict(cityAddressAdcode, cityAddressName, cityParentAddressCode, cityLevel);
|
|
|
|
|
|
|
|
|
|
JSONArray districtJsonArray = cityJsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
|
|
|
|
|
for (int district = 0; district < districtJsonArray.size(); district++) {
|
|
|
|
|
JSONObject districtJsonObject = districtJsonArray.getJSONObject(district);
|
|
|
|
|
String districtAddressAdcode = districtJsonObject.getString(AmapConfigConstant.ADCODE);
|
|
|
|
|
String districtAddressName = districtJsonObject.getString(AmapConfigConstant.NAME);
|
|
|
|
|
String districtParentAddressCode = cityAddressAdcode;
|
|
|
|
|
String districtLevel = districtJsonObject.getString(AmapConfigConstant.LEVEL);
|
|
|
|
|
|
|
|
|
|
if(AmapConfigConstant.STREET.equals(districtLevel.trim())){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
insertDicDistrict(districtAddressAdcode, districtAddressName, districtParentAddressCode, districtLevel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//插入数据库
|
|
|
|
|
return ResponseResult.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ctrl+Alt+M快捷键将一段代码提取成方法
|
|
|
|
|
*
|
|
|
|
|
* @param addressAdcode
|
|
|
|
|
* @param addressName
|
|
|
|
|
* @param parentAddressCode
|
|
|
|
|
* @param level
|
|
|
|
|
*/
|
|
|
|
|
private void insertDicDistrict(String addressAdcode, String addressName, String parentAddressCode, String level) {
|
|
|
|
|
//数据库对象
|
|
|
|
|
DicDistrict dicDistrict = new DicDistrict();
|
|
|
|
|
dicDistrict.setAddressCode(addressAdcode);
|
|
|
|
|
dicDistrict.setAddressName(addressName);
|
|
|
|
|
dicDistrict.setParentAddressCode(parentAddressCode);
|
|
|
|
|
dicDistrict.setLevel(generateLevel(level));
|
|
|
|
|
//插入数据库
|
|
|
|
|
districtMapper.insert(dicDistrict);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int generateLevel(String level) {
|
|
|
|
|
int levelInt = 0;
|
|
|
|
|
if (level.trim().equals("country")) {
|
|
|
|
|
levelInt = 0;
|
|
|
|
|
} else if ("province".equals(level.trim())) {
|
|
|
|
|
levelInt = 1;
|
|
|
|
|
} else if ("city".equals(level.trim())) {
|
|
|
|
|
levelInt = 2;
|
|
|
|
|
} else if ("district".equals(level.trim())) {
|
|
|
|
|
levelInt = 3;
|
|
|
|
|
}
|
|
|
|
|
return levelInt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|