Compare commits
3 Commits
9a1355a379
...
2106afef92
Author | SHA1 | Date |
---|---|---|
|
2106afef92 | 2 years ago |
|
2452a39516 | 2 years ago |
|
15afa8326f | 2 years ago |
@ -0,0 +1,12 @@
|
|||||||
|
package com.internal.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class DicDistrict {
|
||||||
|
|
||||||
|
private String addressCode;
|
||||||
|
private String addressName;
|
||||||
|
private String parentAddressCode;
|
||||||
|
private Integer level;
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.taxi.servicemap.controller;
|
||||||
|
|
||||||
|
import com.internal.dto.ResponseResult;
|
||||||
|
import com.taxi.servicemap.service.DicDistrictService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class DicDistrictController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DicDistrictService dicDistrictService;
|
||||||
|
|
||||||
|
@GetMapping("/dic-district")
|
||||||
|
public ResponseResult initDicDistrict(String keywords){
|
||||||
|
return dicDistrictService.initDicDistrict(keywords);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.taxi.servicemap.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.internal.dto.DicDistrict;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface DicDistrictMapper extends BaseMapper<DicDistrict> {
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.taxi.servicemap.remote;
|
||||||
|
|
||||||
|
import com.internal.contant.AmapConfigConstant;
|
||||||
|
import com.internal.dto.ResponseResult;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class MapDicDistrictClient {
|
||||||
|
|
||||||
|
@Value("${amap.key}")
|
||||||
|
private String amapKey;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public String dicDistrict(String keywords){
|
||||||
|
//拼接请求的URL
|
||||||
|
StringBuilder url = new StringBuilder();
|
||||||
|
url.append(AmapConfigConstant.DISTRICT_URL);
|
||||||
|
url.append("?");
|
||||||
|
url.append("keywords="+keywords);
|
||||||
|
url.append("&");
|
||||||
|
url.append("subdistrict=3");
|
||||||
|
url.append("&");
|
||||||
|
url.append("key="+amapKey);
|
||||||
|
System.out.println(url.toString());
|
||||||
|
ResponseEntity<String> forEntity =
|
||||||
|
restTemplate.getForEntity(url.toString(),String.class);
|
||||||
|
|
||||||
|
return forEntity.getBody();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,116 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DicDistrictService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MapDicDistrictClient mapDicDistrictClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DicDistrictMapper districtMapper;
|
||||||
|
|
||||||
|
public ResponseResult initDicDistrict(String keywords) {
|
||||||
|
//拼接请求地图
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue