@@ -3,6 +3,8 @@ package com.tuoheng.api.controller; | |||
import com.tuoheng.api.entity.domain.City; | |||
import com.tuoheng.api.entity.request.CityQuery; | |||
import com.tuoheng.api.entity.request.CityRequest; | |||
import com.tuoheng.api.entity.vo.CityInfoVo; | |||
import com.tuoheng.api.service.ICityService; | |||
import com.tuoheng.common.common.BaseController; | |||
import com.tuoheng.common.utils.JsonResult; | |||
@@ -48,4 +50,18 @@ public class CityController extends BaseController { | |||
return cityService.getCityList(); | |||
} | |||
/** | |||
* 查询身市区名及编码 | |||
* @return | |||
*/ | |||
@GetMapping("/cityNameAndCode") | |||
public JsonResult getCityNameAndCode(CityRequest request){ | |||
CityInfoVo vo = cityService.getCityNameAndCode(request); | |||
return JsonResult.success(vo); | |||
} | |||
} |
@@ -0,0 +1,21 @@ | |||
package com.tuoheng.api.entity.request; | |||
import lombok.Data; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2022/10/19 | |||
*/ | |||
@Data | |||
public class CityRequest { | |||
/** | |||
* 经度 | |||
*/ | |||
private String lng; | |||
/** | |||
*维度 | |||
*/ | |||
private String lat; | |||
} |
@@ -141,4 +141,20 @@ public class CityInfoVo { | |||
*/ | |||
private String villageName; | |||
/** | |||
* 省编码 | |||
*/ | |||
private String provinceCode; | |||
/** | |||
* 市编码 | |||
*/ | |||
private String cityCode; | |||
/** | |||
* 区县编码 | |||
*/ | |||
private String districtCode; | |||
} |
@@ -1,6 +1,7 @@ | |||
package com.tuoheng.api.service; | |||
import com.tuoheng.api.entity.domain.City; | |||
import com.tuoheng.api.entity.request.CityRequest; | |||
import com.tuoheng.api.entity.vo.CityInfoVo; | |||
import com.tuoheng.common.common.IBaseService; | |||
import com.tuoheng.common.utils.JsonResult; | |||
@@ -41,4 +42,11 @@ public interface ICityService extends IBaseService<City> { | |||
* @return 行政区列表 | |||
*/ | |||
JsonResult getCityList(); | |||
/** | |||
* 获取省市区及城市编码 | |||
* @param request | |||
* @return | |||
*/ | |||
CityInfoVo getCityNameAndCode(CityRequest request); | |||
} |
@@ -4,9 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
import com.tuoheng.api.entity.domain.City; | |||
import com.tuoheng.api.entity.request.CityQuery; | |||
import com.tuoheng.api.entity.request.CityRequest; | |||
import com.tuoheng.api.entity.vo.CityInfoVo; | |||
import com.tuoheng.api.mapper.CityMapper; | |||
import com.tuoheng.api.service.ICityService; | |||
import com.tuoheng.api.utils.GaodeUtil; | |||
import com.tuoheng.common.common.BaseQuery; | |||
import com.tuoheng.common.common.BaseServiceImpl; | |||
import com.tuoheng.common.utils.JsonResult; | |||
@@ -131,6 +133,13 @@ public class CityServiceImpl extends BaseServiceImpl<CityMapper, City> implement | |||
return super.edit(entity); | |||
} | |||
/** | |||
* 根据城市编码获取城市信息 | |||
* | |||
* @param cityCode 城市编码 | |||
* @param delimiter 分隔符号 | |||
* @return | |||
*/ | |||
@Override | |||
public String getCityNameByCode(String cityCode, String delimiter) { | |||
List<String> nameList = new ArrayList<>(); | |||
@@ -157,6 +166,13 @@ public class CityServiceImpl extends BaseServiceImpl<CityMapper, City> implement | |||
return org.apache.commons.lang3.StringUtils.join(nameList.toArray(), delimiter); | |||
} | |||
/** | |||
* 根据城市编码获取城市信息 | |||
* | |||
* @param cityCode 城市编码 | |||
* @param delimiter 分隔符号 | |||
* @return | |||
*/ | |||
@Override | |||
public CityInfoVo getCityInfoByCode(String cityCode, String delimiter) { | |||
CityInfoVo cityInfoVo = new CityInfoVo(); | |||
@@ -169,6 +185,7 @@ public class CityServiceImpl extends BaseServiceImpl<CityMapper, City> implement | |||
if (cityInfo == null) { | |||
return null; | |||
} | |||
Integer cityId = cityInfo.getId(); | |||
while (cityId > 0) { | |||
City cateInfo = cityMapper.selectById(cityId); | |||
@@ -201,4 +218,88 @@ public class CityServiceImpl extends BaseServiceImpl<CityMapper, City> implement | |||
return cityInfoVo; | |||
} | |||
/** | |||
* 获取城市名称及编码 | |||
* | |||
* @param request | |||
* @return | |||
*/ | |||
@Override | |||
public CityInfoVo getCityNameAndCode(CityRequest request) { | |||
//校验 | |||
if (StringUtils.isNull(request.getLat()) && StringUtils.isNull(request.getLng())) { | |||
JsonResult.error("经纬度坐标不存在"); | |||
} | |||
CityInfoVo cityInfoVo = new CityInfoVo(); | |||
List<String> nameList = new ArrayList<>(); | |||
String cityCode = GaodeUtil.getGaodeCode(request.getLng(), request.getLng()); | |||
if (StringUtils.isNull(cityCode)) { | |||
JsonResult.error("城市编码不存在"); | |||
} | |||
//根据编码查询对应城市信息 | |||
LambdaQueryWrapper<City> qw = new LambdaQueryWrapper<>(); | |||
qw.eq(StringUtils.isNotNull(cityCode), City::getCitycode, cityCode); | |||
qw.eq(City::getMark, 1); | |||
qw.last("limit 1"); | |||
City cityInfo = cityMapper.selectOne(qw); | |||
if (cityInfo == null) { | |||
return null; | |||
} | |||
Integer cityId = cityInfo.getId(); | |||
while (cityId > 0) { | |||
City cateInfo = cityMapper.selectById(cityId); | |||
if (cateInfo != null) { | |||
nameList.add(cateInfo.getName()); | |||
cityId = cateInfo.getPid(); | |||
} else { | |||
cityId = null; | |||
} | |||
} | |||
// 使用集合工具实现数组翻转 | |||
Collections.reverse(nameList); | |||
if (StringUtils.isNotEmpty(nameList) && nameList.size() > 0) { | |||
cityInfoVo.setProvinceName(nameList.get(0)); | |||
String provinceName = nameList.get(0); | |||
LambdaQueryWrapper<City> qw1 = new LambdaQueryWrapper<>(); | |||
qw1.eq(StringUtils.isNotEmpty(provinceName), City::getName, provinceName).eq(City::getMark, 1); | |||
qw1.last("limit 1"); | |||
City city = cityMapper.selectOne(qw1); | |||
if (StringUtils.isNull(city)) { | |||
return null; | |||
} | |||
String provinceCode = city.getCitycode(); | |||
cityInfoVo.setProvinceCode(provinceCode); | |||
} | |||
if (StringUtils.isNotEmpty(nameList) && nameList.size() > 1) { | |||
cityInfoVo.setCityName(nameList.get(1)); | |||
String cityName = nameList.get(1); | |||
LambdaQueryWrapper<City> qw2 = new LambdaQueryWrapper<>(); | |||
qw2.eq(StringUtils.isNotEmpty(cityName), City::getName, cityName).eq(City::getMark, 1); | |||
qw2.last("limit 1"); | |||
City city = cityMapper.selectOne(qw2); | |||
if (StringUtils.isNull(city)) { | |||
return null; | |||
} | |||
String cyCode = city.getCitycode(); | |||
cityInfoVo.setCityCode(cyCode); | |||
} | |||
if (StringUtils.isNotEmpty(nameList) && nameList.size() > 2) { | |||
cityInfoVo.setDistrictName(nameList.get(2)); | |||
String districtName = nameList.get(2); | |||
LambdaQueryWrapper<City> qw3 = new LambdaQueryWrapper<>(); | |||
qw3.eq(StringUtils.isNotEmpty(districtName), City::getName, districtName).eq(City::getMark, 1); | |||
qw3.last("limit 1"); | |||
City city = cityMapper.selectOne(qw3); | |||
if (StringUtils.isNull(city)) { | |||
return null; | |||
} | |||
String districtCode = city.getCitycode(); | |||
cityInfoVo.setDistrictCode(districtCode); | |||
} | |||
return cityInfoVo; | |||
} | |||
} |
@@ -0,0 +1,189 @@ | |||
package com.tuoheng.api.utils; | |||
import com.alibaba.fastjson.JSONObject; | |||
import com.tuoheng.common.config.CommonConfig; | |||
import lombok.extern.slf4j.Slf4j; | |||
import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.net.HttpURLConnection; | |||
import java.net.MalformedURLException; | |||
import java.net.URL; | |||
import java.text.DecimalFormat; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Map; | |||
@Slf4j | |||
public class GaodeUtil { | |||
/** | |||
* 逆地理编码 | |||
* | |||
* @param longitude 经度 | |||
* @param latitude 纬度 | |||
* @return | |||
*/ | |||
public static String getGaodeAddress(String longitude, String latitude) { | |||
String address = null; | |||
JSONObject json = new JSONObject(); | |||
try { | |||
json.put("key", CommonConfig.gaodeKey); | |||
json.put("location", longitude + "," + latitude); | |||
log.info("调用高德逆地理编码API请求参数:" + json); | |||
String result = doGet("https://restapi.amap.com/v3/geocode/regeo", json, null); | |||
log.info("调用高德逆地理编码API成功:" + result); | |||
JSONObject resultJson = JSONObject.parseObject(result.trim()); | |||
if("1".equals(resultJson.getString("status"))){ | |||
address = resultJson.getJSONObject("regeocode").getString("formatted_address"); | |||
} | |||
} catch (Exception e) { | |||
log.error("逆地理编码方法异常:", e); | |||
} | |||
return address; | |||
} | |||
/** | |||
* 逆地理编码 | |||
* | |||
* @param longitude 经度 | |||
* @param latitude 纬度 | |||
* @return | |||
*/ | |||
public static String getGaodeCode(String longitude, String latitude) { | |||
String cityCode = null; | |||
JSONObject json = new JSONObject(); | |||
try { | |||
json.put("key", CommonConfig.gaodeKey); | |||
json.put("location", longitude + "," + latitude); | |||
log.info("调用高德逆地理编码API请求参数:" + json); | |||
String result = doGet("https://restapi.amap.com/v3/geocode/regeo", json, null); | |||
log.info("调用高德逆地理编码API成功:" + result); | |||
JSONObject resultJson = JSONObject.parseObject(result.trim()); | |||
if("1".equals(resultJson.getString("status"))){ | |||
cityCode = resultJson.getJSONObject("regeocode").getJSONObject("addressComponent").getString("citycode"); | |||
} | |||
} catch (Exception e) { | |||
log.error("逆地理编码方法异常:", e); | |||
} | |||
return cityCode; | |||
} | |||
/** | |||
* 高德坐标转换 | |||
* | |||
* @param longitude 经度 | |||
* @param latitude 纬度 | |||
* @return | |||
*/ | |||
public static JSONObject getGaodeCoordinate(String longitude, String latitude) { | |||
JSONObject json = new JSONObject(); | |||
JSONObject param = new JSONObject(); | |||
try { | |||
//取六位小数 | |||
DecimalFormat df = new DecimalFormat("0.000000"); | |||
param.put("key", CommonConfig.gaodeKey); | |||
param.put("locations", df.format(Double.valueOf(longitude)) + "," + df.format(Double.valueOf(latitude))); | |||
param.put("coordsys", "gps"); | |||
log.info("调用高德坐标转换API请求参数:" + param); | |||
String result = doGet("https://restapi.amap.com/v3/assistant/coordinate/convert", param, null); | |||
log.info("调用高德坐标转换API成功:" + result); | |||
JSONObject resultJson = JSONObject.parseObject(result.trim()); | |||
if("1".equals(resultJson.getString("status"))){ | |||
String locations = JSONObject.parseObject(result).getString("locations"); | |||
String lonStr = locations.substring(0, locations.indexOf(",")); | |||
String latStr = locations.substring(lonStr.length()+1); | |||
json.put("longitude", lonStr); | |||
json.put("latitude", latStr); | |||
} | |||
} catch (Exception e) { | |||
log.error("高德坐标转换方法异常:", e); | |||
} | |||
return json; | |||
} | |||
public static String doGet(String url, JSONObject data, Map<String, String> properties) { | |||
HttpURLConnection connection = null; | |||
InputStream is = null; | |||
BufferedReader br = null; | |||
String result = null;// 返回结果字符串 | |||
StringBuffer urlNameString = new StringBuffer().append(url).append("?"); | |||
if (data != null) { | |||
for (String key : data.keySet()) { | |||
urlNameString.append(key).append("=").append(data.get(key)).append("&"); | |||
} | |||
url = urlNameString.substring(0, urlNameString.lastIndexOf("&")); | |||
} | |||
try { | |||
// 创建远程url连接对象 | |||
URL httpUrl = new URL(url); | |||
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类 | |||
connection = (HttpURLConnection) httpUrl.openConnection(); | |||
// 设置连接方式:get | |||
connection.setRequestMethod("GET"); | |||
// 设置连接主机服务器的超时时间:15000毫秒 | |||
connection.setConnectTimeout(15000); | |||
// 设置读取远程返回的数据时间:60000毫秒 | |||
connection.setReadTimeout(60000); | |||
if(null != properties && !properties.isEmpty()){ | |||
for (String key:properties.keySet()){ | |||
connection.setRequestProperty(key, properties.get(key)); | |||
} | |||
} | |||
// 发送请求 | |||
connection.connect(); | |||
// 通过connection连接,获取输入流 | |||
if (connection.getResponseCode() == 200) { | |||
is = connection.getInputStream(); | |||
// 封装输入流is,并指定字符集 | |||
br = new BufferedReader(new InputStreamReader(is, "UTF-8")); | |||
// 存放数据 | |||
StringBuffer sbf = new StringBuffer(); | |||
String temp = null; | |||
while ((temp = br.readLine()) != null) { | |||
sbf.append(temp); | |||
sbf.append("\r\n"); | |||
} | |||
result = sbf.toString(); | |||
} | |||
} catch (MalformedURLException e) { | |||
e.printStackTrace(); | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} finally { | |||
// 关闭资源 | |||
if (null != br) { | |||
try { | |||
br.close(); | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
if (null != is) { | |||
try { | |||
is.close(); | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
connection.disconnect();// 关闭远程连接 | |||
} | |||
return result; | |||
} | |||
} |