From 9ac1cd73c9a14b7ed5104bd6f89616d216fbdd5d Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 12 Apr 2023 17:16:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=81=AB=E7=82=B9=E9=87=8D=E6=9E=84-=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E9=98=B2=E7=81=AB=E5=AD=A3=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xkrs/helper/GeoCodeHelper.java | 104 ------------ .../java/com/xkrs/model/bean/AddressBean.java | 153 ++++++++++++++++++ .../impl/DispatchFirePointServiceImpl.java | 60 +++---- .../xkrs/utilsnew/FirePointAddressUtils.java | 88 ++++++++++ 4 files changed, 266 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/com/xkrs/helper/GeoCodeHelper.java create mode 100644 src/main/java/com/xkrs/model/bean/AddressBean.java create mode 100644 src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java diff --git a/src/main/java/com/xkrs/helper/GeoCodeHelper.java b/src/main/java/com/xkrs/helper/GeoCodeHelper.java deleted file mode 100644 index b4a6de6..0000000 --- a/src/main/java/com/xkrs/helper/GeoCodeHelper.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.xkrs.helper; - -import com.xkrs.model.vo.GaoDeIgGeocodeVo; -import com.xkrs.service.impl.DispatchFirePointServiceImpl; -import com.xkrs.utilsold.GaoDeApiUtil; -import org.apache.hc.core5.util.TextUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.List; - -public class GeoCodeHelper { - - public static Logger log = LoggerFactory.getLogger(DispatchFirePointServiceImpl.class); - private GaoDeIgGeocodeVo geocode; - - public void doGeoCode(Double longitude, Double latitude) { - try { - List locationList = new ArrayList<>(); - locationList.add(longitude + "," + latitude); - this.geocode = GaoDeApiUtil.geocode(locationList); - } catch (Exception e) { - e.printStackTrace(); - this.geocode = null; - } - } - - public String getCalCountyCode() { - try { - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); - return addressComponent.getAdcode(); - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - - public String getCalCountyName() { - try { - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); - return addressComponent.getDistrict(); - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - - public String getCalTownCode() { - try { - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); - String townCode = addressComponent.getTowncode(); - return townCode.length() > 9 ? townCode.substring(0, 9) : townCode; - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - - public String getCalTownName() { - try { - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); - return addressComponent.getTownship(); - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - - public String getCalFirePointAddress() { - try { - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - return reGeoCode.getFormatted_address(); - } catch (Exception e) { - e.printStackTrace(); - } - return ""; - } - - public boolean checkGeoCodeResult(String receivedCountyCode, String receivedCountyName, String receivedTownCode) { - String calCountyCode = getCalCountyCode(); - String calCountyName = getCalCountyName(); - String calTownCode = getCalTownCode(); - if (!calCountyCode.equals(receivedCountyCode)) { - log.info("-------逆地理编码校验失败:接收到的countyCode=" + receivedCountyCode + ",计算得到的countyCode=" + calCountyCode); - return false; - } - if (!calCountyName.equals(receivedCountyName)) { - log.info("-------逆地理编码校验失败:接收到的countyName=" + receivedCountyName + ",计算得到的countyName=" + calCountyName); - return false; - } - if (!TextUtils.isEmpty(receivedTownCode)) { - if (!calTownCode.equals(receivedTownCode)) { - log.info("-------逆地理编码校验失败:接收到的townCode=" + receivedTownCode + ",计算得到的townCode=" + calTownCode); - return false; - } - } - return true; - } -} diff --git a/src/main/java/com/xkrs/model/bean/AddressBean.java b/src/main/java/com/xkrs/model/bean/AddressBean.java new file mode 100644 index 0000000..1f7d161 --- /dev/null +++ b/src/main/java/com/xkrs/model/bean/AddressBean.java @@ -0,0 +1,153 @@ +package com.xkrs.model.bean; + +public class AddressBean { + + /** + * 解析结果 + */ + private boolean success; + + /** + * 省的编码 + */ + private String proCode; + + /** + * 省的名称 + */ + private String proName; + + /** + * 市的编码 + */ + private String cityCode; + + /** + * 市的名称 + */ + private String cityName; + + /** + * 区县的编码 + */ + private String countyCode; + + /** + * 区县的名称 + */ + private String countyName; + + /** + * 乡镇街道的编码 + */ + private String townCode; + + /** + * 乡镇街道的名称 + */ + private String townName; + + /** + * 详细地址 + */ + private String firePointAddress; + + public AddressBean() { + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getProCode() { + return proCode; + } + + public void setProCode(String proCode) { + this.proCode = proCode; + } + + public String getProName() { + return proName; + } + + public void setProName(String proName) { + this.proName = proName; + } + + public String getCityCode() { + return cityCode; + } + + public void setCityCode(String cityCode) { + this.cityCode = cityCode; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getCountyCode() { + return countyCode; + } + + public void setCountyCode(String countyCode) { + this.countyCode = countyCode; + } + + public String getCountyName() { + return countyName; + } + + public void setCountyName(String countyName) { + this.countyName = countyName; + } + + public String getTownCode() { + return townCode; + } + + public void setTownCode(String townCode) { + this.townCode = townCode; + } + + public String getTownName() { + return townName; + } + + public void setTownName(String townName) { + this.townName = townName; + } + + public String getFirePointAddress() { + return firePointAddress; + } + + public void setFirePointAddress(String firePointAddress) { + this.firePointAddress = firePointAddress; + } + + @Override + public String toString() { + return "AddressBean{" + + "success=" + success + + ", proCode='" + proCode + '\'' + + ", proName='" + proName + '\'' + + ", cityCode='" + cityCode + '\'' + + ", cityName='" + cityName + '\'' + + ", countyCode='" + countyCode + '\'' + + ", countyName='" + countyName + '\'' + + ", townCode='" + townCode + '\'' + + ", townName='" + townName + '\'' + + ", firePointAddress='" + firePointAddress + '\'' + + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/impl/DispatchFirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/DispatchFirePointServiceImpl.java index c5719c9..087d4e0 100644 --- a/src/main/java/com/xkrs/service/impl/DispatchFirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/DispatchFirePointServiceImpl.java @@ -6,7 +6,7 @@ import com.xkrs.dao.StreetDao; import com.xkrs.dao.SysUserDao; import com.xkrs.helper.FirePointPushManager; import com.xkrs.helper.FirePointQueryManager; -import com.xkrs.helper.GeoCodeHelper; +import com.xkrs.model.bean.AddressBean; import com.xkrs.model.bean.DataWrapper2; import com.xkrs.model.entity.FirePointOrdinaryEntity; import com.xkrs.model.entity.StreetEntity; @@ -15,6 +15,7 @@ import com.xkrs.model.qo.UpdateFirePointQo; import com.xkrs.service.DispatchFirePointService; import com.xkrs.utilsnew.DateTimeUtils; import com.xkrs.utilsnew.ErrorInfoLogUtils; +import com.xkrs.utilsnew.FirePointAddressUtils; import com.xkrs.utilsnew.FirePointFilterUtils; import org.apache.hc.core5.util.TextUtils; import org.slf4j.Logger; @@ -68,9 +69,8 @@ public class DispatchFirePointServiceImpl implements DispatchFirePointService { if (!checkSatelliteTypeWrapper.getData1()) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, checkSatelliteTypeWrapper.getData2(), locale); } - log.info("insertFirePoint 接收到火点信息:" + firePointQo.toString()); - + //解构接收的数据 String fireCode = firePointQo.getFireCode(); Long satelliteTimeTs = firePointQo.getSatelliteTimeTs(); Double longitude = firePointQo.getLongitude(); @@ -83,11 +83,11 @@ public class DispatchFirePointServiceImpl implements DispatchFirePointService { String countyCode = String.valueOf(firePointQo.getCountyCode()); String countyName = firePointQo.getCountyName(); String townCode = firePointQo.getTowncode(); - + //格式化卫星时间和添加时间 LocalDateTime satelliteLocalDateTime = LocalDateTime.ofEpochSecond(satelliteTimeTs, 0, ZoneOffset.ofHours(8)); String satelliteTime = DateTimeUtils.localDateTimeToString(satelliteLocalDateTime);//卫星时间 String addTime = DateTimeUtils.localDateTimeToString(LocalDateTime.now());//添加时间 - + //生成实体类 FirePointOrdinaryEntity firePointOrdinaryEntity = new FirePointOrdinaryEntity(); firePointOrdinaryEntity.setFireCode(fireCode); firePointOrdinaryEntity.setSatelliteTime(satelliteTime); @@ -99,9 +99,29 @@ public class DispatchFirePointServiceImpl implements DispatchFirePointService { firePointOrdinaryEntity.setFireImage(fireImage); firePointOrdinaryEntity.setSatelliteImage(satelliteImage); firePointOrdinaryEntity.setAddTime(addTime); + firePointOrdinaryEntity.setRemark(""); + //过滤重复数据 if (FirePointFilterUtils.checkDuplicated(firePointOrdinaryDao, firePointOrdinaryEntity)) { return outputEncapsulationObject(PromptMessageEnum.DATA_EXIT, "该火点和历史火点重复", locale); } + //使用网络方式解析地址信息 + AddressBean addressBean = FirePointAddressUtils.analysisWithNetwork(firePointOrdinaryEntity.getLongitude(), firePointOrdinaryEntity.getLatitude()); + if (addressBean.isSuccess()) { + //使用网络方式解析地址信息成功,就对实体类对象赋值入库。 + firePointOrdinaryEntity.setProCode(addressBean.getProCode()); + firePointOrdinaryEntity.setProName(addressBean.getProName()); + firePointOrdinaryEntity.setCityCode(addressBean.getCityCode()); + firePointOrdinaryEntity.setCityName(addressBean.getCityName()); + firePointOrdinaryEntity.setCountyCode(addressBean.getCountyCode()); + firePointOrdinaryEntity.setCountyName(addressBean.getCountyName()); + firePointOrdinaryEntity.setTownCode(addressBean.getTownCode()); + firePointOrdinaryEntity.setTownName(addressBean.getTownName()); + firePointOrdinaryEntity.setFirePointAddress(addressBean.getFirePointAddress()); + firePointOrdinaryDao.save(firePointOrdinaryEntity); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功", locale); + } + ErrorInfoLogUtils.log("高德逆地理编码异常,出现问题的火点详情:" + firePointOrdinaryEntity.toString()); + //使用网络方式解析地址信息失败,就使用接收到的数据结合字典表对实体类对象赋值入库。 StreetEntity streetEntity = obtainInfoByCountyCode(countyCode); if (streetEntity != null) { firePointOrdinaryEntity.setProCode(streetEntity.getProCode()); @@ -112,7 +132,6 @@ public class DispatchFirePointServiceImpl implements DispatchFirePointService { firePointOrdinaryEntity.setCountyCode(countyCode); firePointOrdinaryEntity.setCountyName(countyName); firePointOrdinaryEntity.setTownCode(townCode); - bindFirePointAddress(firePointOrdinaryEntity); firePointOrdinaryDao.save(firePointOrdinaryEntity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功", locale); } @@ -126,35 +145,6 @@ public class DispatchFirePointServiceImpl implements DispatchFirePointService { return null; } - /** - * 绑定火点位置 - * 如果能接收到TownCode(山东省内),会判断CountyCode、CountyName、TownCode, - * 如果不能接收到TownCode(其他省份),会判断CountyCode、CountyName, - *

- * 如果根据经纬度反算出来的变量都一致,就更新TownCode、TownName、FirePointAddress,无备注。 - * 如果不一致,就只更新FirePointAddress,备注存储差异信息 - */ - private void bindFirePointAddress(FirePointOrdinaryEntity firePointOrdinaryEntity) { - String countyCode = firePointOrdinaryEntity.getCountyCode(); - String countyName = firePointOrdinaryEntity.getCountyName(); - String townCode = firePointOrdinaryEntity.getTownCode(); - GeoCodeHelper geoCodeHelper = new GeoCodeHelper(); - geoCodeHelper.doGeoCode(firePointOrdinaryEntity.getLongitude(), firePointOrdinaryEntity.getLatitude()); - boolean geoCodeResult = geoCodeHelper.checkGeoCodeResult(countyCode, countyName, townCode); - if (geoCodeResult) { - firePointOrdinaryEntity.setTownCode(geoCodeHelper.getCalTownCode()); - firePointOrdinaryEntity.setTownName(geoCodeHelper.getCalTownName()); - firePointOrdinaryEntity.setFirePointAddress(geoCodeHelper.getCalFirePointAddress()); - firePointOrdinaryEntity.setRemark(""); - } else { - firePointOrdinaryEntity.setTownCode(geoCodeHelper.getCalTownCode()); - firePointOrdinaryEntity.setTownName(geoCodeHelper.getCalTownName()); - firePointOrdinaryEntity.setFirePointAddress(geoCodeHelper.getCalFirePointAddress()); - String remark = "接收到的countyCode=" + countyCode + ",计算得到的countyCode=" + geoCodeHelper.getCalCountyCode() + "。接收到的countyName=" + countyName + ",计算得到的countyName=" + geoCodeHelper.getCalCountyName() + "。接收到的townCode=" + townCode + ",计算得到的townCode=" + geoCodeHelper.getCalTownCode() + "。"; - firePointOrdinaryEntity.setRemark(remark); - } - } - /** * 更新火点状态 */ diff --git a/src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java b/src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java new file mode 100644 index 0000000..4a95661 --- /dev/null +++ b/src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java @@ -0,0 +1,88 @@ +package com.xkrs.utilsnew; + +import com.xkrs.model.bean.AddressBean; +import com.xkrs.model.vo.GaoDeIgGeocodeVo; +import com.xkrs.utilsold.GaoDeApiUtil; +import org.apache.hc.core5.util.TextUtils; + +import java.util.ArrayList; +import java.util.List; + +public class FirePointAddressUtils { + + private FirePointAddressUtils() { + } + + /** + * 使用网络方式解析地址信息 + */ + public static AddressBean analysisWithNetwork(double longitude, double latitude) { + try { + List locationList = new ArrayList<>(); + locationList.add(longitude + "," + latitude); + GaoDeIgGeocodeVo geocode = GaoDeApiUtil.geocode(locationList); + GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); + GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); + String townCode = addressComponent.getTowncode(); + //填充AddressBean数据 + AddressBean addressBean = new AddressBean(); + addressBean.setSuccess(true); + addressBean.setProCode(townCode.substring(0, 2) + "0000"); + addressBean.setProName(addressComponent.getProvince()); + addressBean.setCityCode(townCode.substring(0, 4) + "00"); + addressBean.setCityName(addressComponent.getCity()); + addressBean.setCountyCode(townCode.substring(0, 6)); + addressBean.setCountyName(addressComponent.getDistrict()); + addressBean.setTownCode(townCode.substring(0, 9)); + addressBean.setTownName(addressComponent.getTownship()); + addressBean.setFirePointAddress(reGeoCode.getFormatted_address()); + //检查AddressBean的合法性 + checkValidAddress(addressBean); + return addressBean; + } catch (Exception e) { + e.printStackTrace(); + } + AddressBean addressBean = new AddressBean(); + addressBean.setSuccess(false); + return addressBean; + } + + /** + * 检查AddressBean的合法性 + */ + private static void checkValidAddress(AddressBean addressBean) { + if (addressBean.getProCode().length() != 6) { + throw new RuntimeException("ProCode Error"); + } + if (TextUtils.isEmpty(addressBean.getProName())) { + throw new RuntimeException("ProName Error"); + } + if (addressBean.getCityCode().length() != 6) { + throw new RuntimeException("CityCode Error"); + } + if (TextUtils.isEmpty(addressBean.getCityName())) { + throw new RuntimeException("CityName Error"); + } + if (addressBean.getCountyCode().length() != 6) { + throw new RuntimeException("CountyCode Error"); + } + if (TextUtils.isEmpty(addressBean.getCountyName())) { + throw new RuntimeException("CountyName Error"); + } + if (addressBean.getTownCode().length() != 9) { + throw new RuntimeException("TownCode Error"); + } + if (TextUtils.isEmpty(addressBean.getTownName())) { + throw new RuntimeException("TownName Error"); + } + if (TextUtils.isEmpty(addressBean.getFirePointAddress())) { + throw new RuntimeException("FirePointAddress Error"); + } + } + + public static void main(String[] args) { + + AddressBean addressBean = analysisWithNetwork(120, 36); + System.out.println(addressBean.toString()); + } +}