diff --git a/src/main/java/com/xkrs/controller/FirePointController.java b/src/main/java/com/xkrs/controller/FirePointController.java index 74b69a7..80491c0 100644 --- a/src/main/java/com/xkrs/controller/FirePointController.java +++ b/src/main/java/com/xkrs/controller/FirePointController.java @@ -209,10 +209,14 @@ public class FirePointController { * @return */ @GetMapping("/selectFirePointByMonth") - public String selectFirePointByMonth() { + @PreAuthorize("hasAnyAuthority('auth_general_user')") + public String selectFirePointByMonth(@RequestHeader(value="Authorization") String token) { // 获取区域信息 Locale locale = LocaleContextHolder.getLocale(); - List firePointEntities = firePointService.selectFirePointByMonth(); + // 验证token + String tokenUserName = TokenUtil.getTokenUserName(token); + SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); + List firePointEntities = firePointService.selectFirePointByMonth(sysUserEntity.getCountyCode()); if(firePointEntities == null || firePointEntities.size() == 0){ return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时没有火点数据",locale); } diff --git a/src/main/java/com/xkrs/dao/CityDao.java b/src/main/java/com/xkrs/dao/CityDao.java index 9f27699..6343ba0 100644 --- a/src/main/java/com/xkrs/dao/CityDao.java +++ b/src/main/java/com/xkrs/dao/CityDao.java @@ -30,4 +30,18 @@ public interface CityDao extends JpaRepository, JpaSpecificatio */ @Query(value = "select city_name cityname,city_code citycode from city where pro_code = :code",nativeQuery = true) List> selectCityName(String code); + + /** + * 根据市编码查询信息 + * @param cityCode + * @return + */ + List findByCityCode(String cityCode); + + /** + * 根据省编码查询信息 + * @param proCode + * @return + */ + List findByProCode(String proCode); } diff --git a/src/main/java/com/xkrs/dao/FirePointDao.java b/src/main/java/com/xkrs/dao/FirePointDao.java index 0e5b00b..e9a537a 100644 --- a/src/main/java/com/xkrs/dao/FirePointDao.java +++ b/src/main/java/com/xkrs/dao/FirePointDao.java @@ -19,11 +19,11 @@ public interface FirePointDao extends JpaRepository, JpaSp /** * 查询今天的火点信息 * @param addTime - * @param countyCode + * @param address * @return */ - @Query(value = "select * from fire_point where add_time like CONCAT('%',:addTime,'%') and county_code like CONCAT('%',:countyCode,'%')",nativeQuery = true) - List selectTodayFirePoint(String addTime,String countyCode); + @Query(value = "select * from fire_point where add_time like CONCAT('%',:addTime,'%') and fire_point_address like CONCAT('%',:address,'%')",nativeQuery = true) + List selectTodayFirePoint(String addTime,String address); /** * 区县条件查询今天火点信息 diff --git a/src/main/java/com/xkrs/model/qo/FirePointQo.java b/src/main/java/com/xkrs/model/qo/FirePointQo.java index 806d3db..3160315 100644 --- a/src/main/java/com/xkrs/model/qo/FirePointQo.java +++ b/src/main/java/com/xkrs/model/qo/FirePointQo.java @@ -44,7 +44,7 @@ public class FirePointQo { /** * 植被类型 */ - private String landType; + private String landtype; /** * 置信度 @@ -54,7 +54,7 @@ public class FirePointQo { public FirePointQo() { } - public FirePointQo(String fireCode, Integer countyCode, String countyName, Integer satelliteTimeTs, Double longitude, Double latitude, String satelliteType, String landType, String confidence) { + public FirePointQo(String fireCode, Integer countyCode, String countyName, Integer satelliteTimeTs, Double longitude, Double latitude, String satelliteType, String landtype, String confidence) { this.fireCode = fireCode; this.countyCode = countyCode; this.countyName = countyName; @@ -62,7 +62,7 @@ public class FirePointQo { this.longitude = longitude; this.latitude = latitude; this.satelliteType = satelliteType; - this.landType = landType; + this.landtype = landtype; this.confidence = confidence; } @@ -122,12 +122,12 @@ public class FirePointQo { this.satelliteType = satelliteType; } - public String getLandType() { - return landType; + public String getLandtype() { + return landtype; } - public void setLandType(String landType) { - this.landType = landType; + public void setLandtype(String landtype) { + this.landtype = landtype; } public String getConfidence() { @@ -148,7 +148,7 @@ public class FirePointQo { ", longitude=" + longitude + ", latitude=" + latitude + ", satelliteType='" + satelliteType + '\'' + - ", landType='" + landType + '\'' + + ", landType='" + landtype + '\'' + ", confidence='" + confidence + '\'' + '}'; } diff --git a/src/main/java/com/xkrs/service/FirePointService.java b/src/main/java/com/xkrs/service/FirePointService.java index c2387b2..f6f2294 100644 --- a/src/main/java/com/xkrs/service/FirePointService.java +++ b/src/main/java/com/xkrs/service/FirePointService.java @@ -102,7 +102,7 @@ public interface FirePointService { * 查询近一个月的火点信息 * @return */ - List selectFirePointByMonth(); + List selectFirePointByMonth(String countyCode); /** * 查询近一个月的火点数量 diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index 89be695..7a014af 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -80,9 +80,9 @@ public class FirePointServiceImpl implements FirePointService { firePointEntity.setSatelliteTime(DateTimeUtil.timeMillisToString(firePointQo.getSatelliteTimeTs().longValue())); firePointEntity.setLongitude(firePointQo.getLongitude()); firePointEntity.setLatitude(firePointQo.getLatitude()); - firePointEntity.setFirePointAddress(AddressUtils.getLatAndLng(firePointQo.getLatitude().toString(),firePointQo.getLongitude().toString())); + firePointEntity.setFirePointAddress(AddressUtils.getLocal(firePointQo.getLatitude().toString(),firePointQo.getLongitude().toString())); firePointEntity.setSatelliteType(firePointQo.getSatelliteType()); - firePointEntity.setLandType(firePointQo.getLandType()); + firePointEntity.setLandType(firePointQo.getLandtype()); firePointEntity.setConfidence(firePointQo.getConfidence()); firePointEntity.setAddTime(DateTimeUtil.dateTimeToString(LocalDateTime.now())); firePointEntity.setFireType("0"); @@ -116,9 +116,13 @@ public class FirePointServiceImpl implements FirePointService { public List selectTodayFirePoint(String countyCode) { String addTime = DateTimeUtil.dateToString(LocalDate.now()); if(countyCode.substring(2).equals("0000")){ - return firePointDao.selectTodayFirePoint(addTime,countyCode.substring(0,2)); + List byProCode = cityDao.findByProCode(countyCode); + String proName = byProCode.get(0).getProName(); + return firePointDao.selectTodayFirePoint(addTime,proName); }else if(countyCode.substring(4).equals("00") && !countyCode.substring(2).equals("0000")){ - return firePointDao.selectTodayFirePoint(addTime,countyCode.substring(0,4)); + List byCityCode = cityDao.findByCityCode(countyCode); + String cityName = byCityCode.get(0).getCityName(); + return firePointDao.selectTodayFirePoint(addTime,cityName); }else { return firePointDao.selectTodayFirePointOne(addTime,countyCode); } @@ -135,7 +139,9 @@ public class FirePointServiceImpl implements FirePointService { */ @Override public List selectFirePoint(String proCode,String cityCode, String satelliteType, String landType, String startTime, String endTime) { - return query.selectFirePointByPro(proCode,cityCode, satelliteType, landType, startTime, endTime); + List byProCode = cityDao.findByProCode(proCode); + String proName = byProCode.get(0).getProName(); + return query.selectFirePointByPro(proName,cityCode, satelliteType, landType, startTime, endTime); } /** @@ -150,7 +156,9 @@ public class FirePointServiceImpl implements FirePointService { */ @Override public List selectFirePointByCity(String cityCodeOne, String cityCode, String satelliteType, String landType, String startTime, String endTime) { - return query.selectFirePointByCity(cityCodeOne,cityCode,satelliteType,landType,startTime,endTime); + List byCityCode = cityDao.findByCityCode(cityCodeOne); + String cityName = byCityCode.get(0).getCityName(); + return query.selectFirePointByCity(cityName,cityCode,satelliteType,landType,startTime,endTime); } /** @@ -304,13 +312,24 @@ public class FirePointServiceImpl implements FirePointService { * @return */ @Override - public List selectFirePointByMonth() { + public List selectFirePointByMonth(String countyCode) { LocalDateTime localDateTime = LocalDateTime.now(); String endTime = DateTimeUtil.dateTimeToString(localDateTime); // 当前时间减7 - LocalDateTime localDateTime1 = localDateTime.minusDays(30); + LocalDateTime localDateTime1 = localDateTime.minusDays(31); String startTime = DateTimeUtil.dateTimeToString(localDateTime1); - return query.selectFirePointByMonth(startTime,endTime); + if(countyCode.substring(2).equals("0000")){ + List byProCode = cityDao.findByProCode(countyCode); + String proName = byProCode.get(0).getProName(); + return query.selectFirePointByMonth(proName,startTime,endTime); + }else if(countyCode.substring(4).equals("00") && !countyCode.substring(2).equals("0000")){ + List byCityCode = cityDao.findByCityCode(countyCode); + String cityName = byCityCode.get(0).getCityName(); + return query.selectFirePointByMonthCity(cityName,startTime,endTime); + }else { + return query.selectFirePointByMonthCounty(countyCode,startTime,endTime); + } + } /** diff --git a/src/main/java/com/xkrs/utils/AddressUtils.java b/src/main/java/com/xkrs/utils/AddressUtils.java index 918a1eb..5140dc5 100644 --- a/src/main/java/com/xkrs/utils/AddressUtils.java +++ b/src/main/java/com/xkrs/utils/AddressUtils.java @@ -1,13 +1,33 @@ package com.xkrs.utils; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import net.sf.json.JSONObject; import io.micrometer.core.instrument.util.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.net.ssl.*; import java.io.*; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.HashMap; +import java.util.Map; /** * 根据经纬度获取地址:省 市 区 位置名称 @@ -15,7 +35,7 @@ import java.security.cert.X509Certificate; */ public class AddressUtils { - public static String getLatAndLng(String lat, String lng) { + /*public static String getLatAndLng(String lat, String lng) { String key = "O7QBZ-ZYDKI-EMKGN-53UHG-5XSJF-AAFBP"; try { String hsUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key + "&get_poi=1"; @@ -96,10 +116,180 @@ public class AddressUtils { e.printStackTrace(); } return null; + }*/ + + private static final Logger log = LoggerFactory.getLogger(AddressUtils.class); + private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"; + + /** + * 获取指定经纬度的地理位置 + * @param latitude 纬度 + * @param longitude 经度 + * @return + */ + public static String getLocal(String latitude, String longitude) { + String ok = "ok"; + String msg = "msg"; + String info = getAdd(latitude, longitude); + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = null; + try { + node = mapper.readTree(info); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + assert node != null; + String province = null; + String city = null; + String county = null; + String road = null; + String address = null; + if (ok.equals(node.path(msg).asText())) { + province = node.path("result").path("addressComponent").path("province").asText(); + city = node.path("result").path("addressComponent").path("city").asText(); + county = node.path("result").path("addressComponent").path("county").asText(); + road = node.path("result").path("addressComponent").path("road").asText(); + address = node.path("result").path("addressComponent").path("address").asText(); + } + String fireAddress = province + city + county + road + address; + return fireAddress; + } + /** + * 根据经纬度获取位置信息 + * @param latitude 纬度 + * @param longitude 经度 + * @return + */ + public static String getAdd(String latitude, String longitude) { + // 读取成功标志 + boolean isSuccess = false; + // 重复次数 + int count = 10; + ObjectMapper mapper = new ObjectMapper(); + Map paramMap = new HashMap<>(3); + paramMap.put("lon", longitude); + paramMap.put("lat", latitude); + paramMap.put("ver", "1"); + String paramStr = null; + try { + paramStr = mapper.writeValueAsString(paramMap); + } catch (JsonProcessingException e) { + log.error("转json失败,{}", (Object) e.getStackTrace()); + } + String url = String.format("http://api.tianditu.gov.cn/geocoder?type=geocode&tk=5a1d34815475f88e6d8802da6be832ae&postStr=%s", + paramStr); + // 创建http对象 + RequestConfig defaultRequestConfig = RequestConfig.custom() + .setSocketTimeout(60000).setConnectTimeout(60000) + .setConnectionRequestTimeout(60000) + .build(); + CloseableHttpClient client = HttpClients.custom() + .setDefaultRequestConfig(defaultRequestConfig).build(); + + // 创建并设置URI + URIBuilder uri = null; + // 创建Get请求 + HttpGet get = null; + + try { + URL url1 = new URL(url); + URI uri1 = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null); + uri = new URIBuilder(uri1); + get = new HttpGet(uri.build()); + // 设置请求头 + setGet(get); + } catch (URISyntaxException | MalformedURLException e) { + log.info("错误{}", (Object) e.getStackTrace()); + } + //发送请求 + HttpEntity entity = null; + InputStream is = null; + BufferedReader br = null; + // 创建响应对象 + CloseableHttpResponse response = null; + String line; + String sLine = null; + + String json = null; + while (!isSuccess && count > 0) { + try { + response = client.execute(get); + // 获取请求结果 + if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + log.info("实时定位数据未请求成功"); + count--; + //close(is, br, response, client); + continue; + } + entity = response.getEntity(); + is = entity.getContent(); + br = new BufferedReader( + new InputStreamReader(is, StandardCharsets.UTF_8) + ); + StringBuilder sb = new StringBuilder(); + while ((line = br.readLine()) != null) { + sb.append(line); + } + sLine = sb.toString(); + sLine = sLine.substring(sLine.indexOf("{")); + //使用ObjectMapper对象对 User对象进行转换 + try { + json = mapper.writeValueAsString(sLine); + } catch (JsonProcessingException e) { + log.info("json字符串转化异常{}", (Object) e.getStackTrace()); + } + isSuccess = true; + + } catch (ClientProtocolException e) { + log.info("请求超时等问题:{}", (Object) e.getStackTrace()); + } catch (IOException e) { + log.info("I/O问题:{}", (Object) e.getStackTrace()); + } finally { + close(is, br, response, client); + } + } + return sLine; + } + + private static void close(InputStream is, BufferedReader br, CloseableHttpResponse response, CloseableHttpClient client){ + try { + if (null != is){ + is.close(); + } + if (null != br){ + br.close(); + } + if (null != response){ + response.close(); + } + if (null != client){ + client.close(); + } + } catch (IOException e) { + log.info("IO错误{}", (Object) e.getStackTrace()); + } + } + + private static HttpGet setGet(HttpGet get) { + get.setHeader("Accept" + , "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + get.setHeader("Accept-Encoding" + , "gzip, deflate"); + get.setHeader("Accept-Language" + , "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6"); + get.setHeader("Connection" + , "keep-alive"); + get.setHeader("Cache-Control" + , "no-cache"); + get.setHeader("Upgrade-Insecure-Requests" + , "1"); + get.setHeader("User-Agent", USER_AGENT); + return get; } public static void main(String[] args) { - String latAndLng = getLatAndLng("36.677841", "119.12423"); + String latAndLng = getLocal("34.68", "117.74"); System.out.println(latAndLng); } } diff --git a/src/main/java/com/xkrs/utils/Query.java b/src/main/java/com/xkrs/utils/Query.java index e858bfb..a24772a 100644 --- a/src/main/java/com/xkrs/utils/Query.java +++ b/src/main/java/com/xkrs/utils/Query.java @@ -28,14 +28,13 @@ public class Query { * @param * @return */ - public List selectFirePointByPro(String proCode, String cityCode, String satelliteType, String landType, String startTime, String endTime) { + public List selectFirePointByPro(String address, String cityCode, String satelliteType, String landType, String startTime, String endTime) { Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { List list = new ArrayList<>(); - if(proCode != null && !"".equals(proCode)){ - String substring = proCode.substring(0, 2); - list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + substring + "%")); + if(address != null && !"".equals(address)){ + list.add(criteriaBuilder.like(root.get("firePointAddress").as(String.class), "%" + address + "%")); } if (cityCode != null && !"".equals(cityCode)) { list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + cityCode + "%")); @@ -64,14 +63,13 @@ public class Query { * @param * @return */ - public List selectFirePointByCity(String cityCodeOne, String cityCode,String satelliteType,String landType,String startTime,String endTime) { + public List selectFirePointByCity(String address, String cityCode,String satelliteType,String landType,String startTime,String endTime) { Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { List list = new ArrayList<>(); - if(cityCodeOne != null && !"".equals(cityCodeOne)){ - String substring = cityCodeOne.substring(0, 4); - list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + substring + "%")); + if(address != null && !"".equals(address)){ + list.add(criteriaBuilder.like(root.get("firePointAddress").as(String.class), "%" + address + "%")); } if (cityCode != null && !"".equals(cityCode)) { list.add(criteriaBuilder.equal(root.get("countyCode").as(String.class),cityCode)); @@ -157,11 +155,70 @@ public class Query { * @param endTime * @return */ - public List selectFirePointByMonth(String startTime,String endTime) { + public List selectFirePointByMonth(String address,String startTime,String endTime) { Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { List list = new ArrayList<>(); + if(address != null && !"".equals(address)){ + list.add(criteriaBuilder.like(root.get("firePointAddress").as(String.class), "%" + address + "%")); + } + if(startTime != null && !"".equals(startTime)){ + list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(String.class), startTime)); + } + if(endTime != null && !"".equals(endTime)){ + list.add(criteriaBuilder.lessThanOrEqualTo(root.get("addTime").as(String.class), endTime)); + } + Predicate[] predicates = new Predicate[list.size()]; + return criteriaBuilder.and(list.toArray(predicates)); + } + }; + return firePointDao.findAll(specification); + } + + /** + * 查询近一个月的火点信息(市) + * @param address + * @param startTime + * @param endTime + * @return + */ + public List selectFirePointByMonthCity(String address,String startTime,String endTime) { + Specification specification = new Specification() { + @Override + public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { + List list = new ArrayList<>(); + if(address != null && !"".equals(address)){ + list.add(criteriaBuilder.like(root.get("firePointAddress").as(String.class), "%" + address + "%")); + } + if(startTime != null && !"".equals(startTime)){ + list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(String.class), startTime)); + } + if(endTime != null && !"".equals(endTime)){ + list.add(criteriaBuilder.lessThanOrEqualTo(root.get("addTime").as(String.class), endTime)); + } + Predicate[] predicates = new Predicate[list.size()]; + return criteriaBuilder.and(list.toArray(predicates)); + } + }; + return firePointDao.findAll(specification); + } + + /** + * 查询近一个月的火点信息(县) + * @param countyCode + * @param startTime + * @param endTime + * @return + */ + public List selectFirePointByMonthCounty(String countyCode,String startTime,String endTime) { + Specification specification = new Specification() { + @Override + public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { + List list = new ArrayList<>(); + if(countyCode != null && !"".equals(countyCode)){ + list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), countyCode)); + } if(startTime != null && !"".equals(startTime)){ list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(String.class), startTime)); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6f83a38..8ecbbfb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -38,7 +38,7 @@ spring.cache.type = redis spring.redis.database = 8 spring.redis.host = localhost spring.redis.port = 6379 -spring.redis.password=sdust2020 +#spring.redis.password=sdust2020 spring.redis.timeout = 10000 spring.redis.lettuce.pool.max-active = 100 spring.redis.lettuce.pool.max-wait = 10000