From fe880979dd19794e6f6ace2a2f0b288f5ac2f392 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 7 Jun 2022 21:14:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=9A=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/controller/FirePointController.java | 46 ++--- .../com/xkrs/service/FirePointService.java | 51 ------ .../service/impl/FirePointServiceImpl.java | 88 ---------- .../java/com/xkrs/utils/DateTimeUtils.java | 10 +- .../com/xkrs/utils/FirePointQueryHelper.java | 16 +- src/main/java/com/xkrs/utils/Query.java | 161 ------------------ 6 files changed, 36 insertions(+), 336 deletions(-) diff --git a/src/main/java/com/xkrs/controller/FirePointController.java b/src/main/java/com/xkrs/controller/FirePointController.java index 1639019..059eff0 100644 --- a/src/main/java/com/xkrs/controller/FirePointController.java +++ b/src/main/java/com/xkrs/controller/FirePointController.java @@ -14,6 +14,8 @@ import com.xkrs.model.qo.ShanDongFirePointVerifyStateQo; import com.xkrs.model.vo.AppTaskBodyVo; import com.xkrs.service.FirePointService; import com.xkrs.service.StreetService; +import com.xkrs.utils.FirePointQueryHelper; +import org.apache.hc.core5.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; @@ -55,6 +57,9 @@ public class FirePointController { @Resource private StreetService streetService; + @Resource + private FirePointQueryHelper firePointQueryHelper; + /** * 添加火点数据 * @@ -220,45 +225,24 @@ public class FirePointController { Locale locale = LocaleContextHolder.getLocale(); // 区县编码 String cityCode = (String) map.get("cityCode"); - // 卫星类型 - String satelliteType = (String) map.get("satelliteType"); - // 植被类型 - String landType = (String) map.get("landType"); // 开始时间 String startTime = (String) map.get("startTime"); // 结束时间 String endTime = (String) map.get("endTime"); + // 卫星类型 + String satelliteType = (String) map.get("satelliteType"); + // 植被类型 + String landType = (String) map.get("landType"); // 验证token String tokenUserName = TokenUtil.getTokenUserName(token); SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName); - if (9 == sysUserEntity.getCountyCode().length()) {// 查询县 - List firePointEntities = firePointService.selectFirePointByStreet(sysUserEntity.getCountyCode(), satelliteType, landType, startTime, endTime); - if (firePointEntities == null || firePointEntities.size() == 0) { - return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有火点数据", locale); - } - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointEntities, locale); - } else if (sysUserEntity.getCountyCode().substring(2).equals("0000")) { - // 查询省 - List firePointEntities = firePointService.selectFirePoint(sysUserEntity.getCountyCode(), cityCode, satelliteType, landType, startTime, endTime); - if (firePointEntities == null || firePointEntities.size() == 0) { - return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有火点数据", locale); - } - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointEntities, locale); - } else if (sysUserEntity.getCountyCode().substring(4).equals("00") && !sysUserEntity.getCountyCode().substring(2).equals("0000")) { - // 查询市 - List firePointEntities = firePointService.selectFirePointByCity(sysUserEntity.getCountyCode(), cityCode, satelliteType, landType, startTime, endTime); - if (firePointEntities == null || firePointEntities.size() == 0) { - return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有火点数据", locale); - } - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointEntities, locale); - } else { - // 查询县 - List firePointEntities = firePointService.selectFirePointByCounty(sysUserEntity.getCountyCode(), cityCode, satelliteType, landType, startTime, endTime); - if (firePointEntities == null || firePointEntities.size() == 0) { - return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有火点数据", locale); - } - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointEntities, locale); + // 用于查询的区划编码,可能是省市区县街道任意级别 + String queryCode = TextUtils.isEmpty(cityCode) ? sysUserEntity.getCountyCode() : cityCode; + List firePointList = firePointQueryHelper.queryFirePoint(queryCode, startTime, endTime, satelliteType, landType); + if (firePointList == null || firePointList.size() == 0) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有火点数据", locale); } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointList, locale); } /** diff --git a/src/main/java/com/xkrs/service/FirePointService.java b/src/main/java/com/xkrs/service/FirePointService.java index 3b75759..d9e3fd5 100644 --- a/src/main/java/com/xkrs/service/FirePointService.java +++ b/src/main/java/com/xkrs/service/FirePointService.java @@ -41,57 +41,6 @@ public interface FirePointService { */ List selectTodayFirePoint(String countyCode); - /** - * 动态多条件查询火点信息(省) - * - * @param proCode - * @param cityCode - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - List selectFirePoint(String proCode, String cityCode, String satelliteType, String landType, String startTime, String endTime); - - /** - * 动态多条件查询火点信息(市) - * - * @param cityCode 市编码 - * @param countyCode 县编码 - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - List selectFirePointByCity(String cityCode, String countyCode, String satelliteType, String landType, String startTime, String endTime); - - /** - * 动态多条件查询火点信息(县) - * - * @param countyCode 县编码 - * @param streetCode 街道编码 - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - List selectFirePointByCounty(String countyCode, String streetCode, String satelliteType, String landType, String startTime, String endTime); - - /** - * 动态多条件查询火点信息(街道) - * - * @param streetCode - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - List selectFirePointByStreet(String streetCode, String satelliteType, String landType, String startTime, String endTime); - /** * 根据火点编码修改火点的状态(预警) * diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index 6c06655..bcd2155 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -525,94 +525,6 @@ public class FirePointServiceImpl implements FirePointService { } } - /** - * 动态多条件查询火点信息(省) - * - * @param cityCode - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") - @Override - public List selectFirePoint(String proCode, String cityCode, String satelliteType, String landType, String startTime, String endTime) { - List byProCode = cityDao.findByProCode(proCode); - String proName = byProCode.get(0).getProName(); - if (!"".equals(startTime) && !"".equals(endTime)) { - LocalDate localDate = DateTimeUtil.stringToDate(endTime); -// DateTimeUtils. - // 结束时间 - LocalDate localDate1 = localDate.plusDays(1); - String dateToString = DateTimeUtil.dateToString(localDate1); - return query.selectFirePointByPro(proName, cityCode, satelliteType, landType, startTime, dateToString); - } - return query.selectFirePointByPro(proName, cityCode, satelliteType, landType, startTime, endTime); - } - - /** - * 动态多条件查询火点信息(市) - * - * @param cityCode 市编码 - * @param countyCode 县编码 - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") - @Override - public List selectFirePointByCity(String cityCode, String countyCode, String satelliteType, String landType, String startTime, String endTime) { - List byCityCode = countyDao.findByCityCode(cityCode); - String cityName = byCityCode.get(0).getCityName(); - if (!"".equals(startTime) && !"".equals(endTime)) { - LocalDate localDate = DateTimeUtil.stringToDate(endTime); - // 结束时间 - LocalDate localDate1 = localDate.plusDays(1); - String dateToString = DateTimeUtil.dateToString(localDate1); - return query.selectFirePointByCity(cityName, countyCode, satelliteType, landType, startTime, dateToString); - } - return query.selectFirePointByCity(cityName, countyCode, satelliteType, landType, startTime, endTime); - } - - /** - * 动态多条件查询火点信息(县) - * - * @param countyCode 县编码 - * @param streetCode 街道编码 - * @param satelliteType - * @param landType - * @param startTime - * @param endTime - * @return - */ - @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") - @Override - public List selectFirePointByCounty(String countyCode, String streetCode, String satelliteType, String landType, String startTime, String endTime) { - if (!"".equals(startTime) && !"".equals(endTime)) { - LocalDate localDate = DateTimeUtil.stringToDate(endTime); - // 结束时间 - LocalDate localDate1 = localDate.plusDays(1); - String dateToString = DateTimeUtil.dateToString(localDate1); - return query.selectFirePointByCounty(countyCode, streetCode, satelliteType, landType, startTime, dateToString); - } - return query.selectFirePointByCounty(countyCode, streetCode, satelliteType, landType, startTime, endTime); - } - - @Override - public List selectFirePointByStreet(String streetCode, String satelliteType, String landType, String startTime, String endTime) { - if (!"".equals(startTime) && !"".equals(endTime)) { - LocalDate localDate = DateTimeUtil.stringToDate(endTime); - // 结束时间 - LocalDate localDate1 = localDate.plusDays(1); - String dateToString = DateTimeUtil.dateToString(localDate1); - return query.selectFirePointByStreet(streetCode, satelliteType, landType, startTime, dateToString); - } - return query.selectFirePointByStreet(streetCode, satelliteType, landType, startTime, endTime); - } - /** * 根据火点编码修改火点的状态(预警) * diff --git a/src/main/java/com/xkrs/utils/DateTimeUtils.java b/src/main/java/com/xkrs/utils/DateTimeUtils.java index df4f628..e3978ce 100644 --- a/src/main/java/com/xkrs/utils/DateTimeUtils.java +++ b/src/main/java/com/xkrs/utils/DateTimeUtils.java @@ -1,17 +1,25 @@ package com.xkrs.utils; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateTimeUtils { public final static DateTimeFormatter DATE_TIME_FORMATTER_1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + public final static DateTimeFormatter DATE_TIME_FORMATTER_2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + public static String localDateTimeToString(LocalDateTime localDateTime) { return DATE_TIME_FORMATTER_1.format(localDateTime); } public static LocalDateTime stringToLocalDateTime(String localDateTimeString) { - return LocalDateTime.parse(localDateTimeString, DATE_TIME_FORMATTER_1); + try { + return LocalDateTime.parse(localDateTimeString, DATE_TIME_FORMATTER_1); + } catch (Exception e) { + LocalDate localDate = LocalDate.parse(localDateTimeString, DATE_TIME_FORMATTER_2); + return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0); + } } } diff --git a/src/main/java/com/xkrs/utils/FirePointQueryHelper.java b/src/main/java/com/xkrs/utils/FirePointQueryHelper.java index 74a2d64..c3cc997 100644 --- a/src/main/java/com/xkrs/utils/FirePointQueryHelper.java +++ b/src/main/java/com/xkrs/utils/FirePointQueryHelper.java @@ -34,15 +34,23 @@ public class FirePointQueryHelper { } } //限制最早开始时间不得早于一个月 - String earliestStartTime = DateTimeUtils.localDateTimeToString(LocalDateTime.now().minusDays(30)); + LocalDateTime lastMonth1 = LocalDateTime.now().minusMonths(1); + LocalDateTime lastMonth2 = LocalDateTime.of(lastMonth1.getYear(), lastMonth1.getMonth(), lastMonth1.getDayOfMonth(), 0, 0, 0, 0); + String lastMonthString = DateTimeUtils.localDateTimeToString(lastMonth2); //开始时间查询条件 - predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), earliestStartTime)); + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), lastMonthString)); if (!TextUtils.isEmpty(startTime)) { - predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); + LocalDateTime oldStartDateTime = DateTimeUtils.stringToLocalDateTime(startTime); + LocalDateTime newStartDateTime = LocalDateTime.of(oldStartDateTime.getYear(), oldStartDateTime.getMonth(), oldStartDateTime.getDayOfMonth(), 0, 0, 0, 0); + String finalStartTime = DateTimeUtils.localDateTimeToString(newStartDateTime); + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), finalStartTime)); } //结束时间查询条件 if (!TextUtils.isEmpty(endTime)) { - predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); + LocalDateTime oldEndDateTime = DateTimeUtils.stringToLocalDateTime(endTime); + LocalDateTime newEndDateTime = LocalDateTime.of(oldEndDateTime.getYear(), oldEndDateTime.getMonth(), oldEndDateTime.getDayOfMonth() + 1, 0, 0, 0, 0); + String finalEndTime = DateTimeUtils.localDateTimeToString(newEndDateTime); + predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), finalEndTime)); } //卫星类型查询条件 if (!TextUtils.isEmpty(satelliteType)) { diff --git a/src/main/java/com/xkrs/utils/Query.java b/src/main/java/com/xkrs/utils/Query.java index a80fb90..7ad4c21 100644 --- a/src/main/java/com/xkrs/utils/Query.java +++ b/src/main/java/com/xkrs/utils/Query.java @@ -7,7 +7,6 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.persistence.criteria.Predicate; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -21,166 +20,6 @@ public class Query { @Resource private FirePointDao firePointDao; - /** - * 动态多条件查询项目信息(省) - */ - public List selectFirePointByPro(String proName, String cityCode, String satelliteType, String landType, String startTime, String endTime) { - try { - if (cityCode.substring(4).equals("00") && !cityCode.substring(2).equals("0000")) {//省的账号要查市的火点 - if (cityCode.length() > 4) { - cityCode = cityCode.substring(0, 4); - } - } - }catch (Exception e) { - e.printStackTrace(); - cityCode = null; - } - String finalCityCode = cityCode; - Specification specification = (root, criteriaQuery, criteriaBuilder) -> { - List list = new ArrayList<>(); - if (proName != null && !"".equals(proName)) { - list.add(criteriaBuilder.like(root.get("firePointAddress").as(String.class), "%" + proName + "%")); - } - if (finalCityCode != null && !"".equals(finalCityCode)) { - list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + finalCityCode + "%")); - } - if (satelliteType != null && !"".equals(satelliteType)) { - list.add(criteriaBuilder.equal(root.get("satelliteType").as(String.class), satelliteType)); - } - if (landType != null && !"".equals(landType)) { - list.add(criteriaBuilder.equal(root.get("landType").as(String.class), landType)); - } - if (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - LocalDateTime localDateTime = LocalDateTime.now(); - LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTimes = DateTimeUtil.dateTimeToString(localDateTime2); - // 当前时间减30 - LocalDateTime localDateTime1 = localDateTime.minusDays(30); - String startTimes = DateTimeUtil.dateTimeToString(localDateTime1); - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTimes)); - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTimes)); - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 动态多条件查询项目信息(市) - */ - public List selectFirePointByCity(String cityName, String countyCode, String satelliteType, String landType, String startTime, String endTime) { - Specification specification = (root, criteriaQuery, criteriaBuilder) -> { - List list = new ArrayList<>(); - if (cityName != null && !"".equals(cityName)) { - list.add(criteriaBuilder.like(root.get("firePointAddress").as(String.class), "%" + cityName + "%")); - } - if (countyCode != null && !"".equals(countyCode)) { - list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + countyCode + "%")); - } - if (satelliteType != null && !"".equals(satelliteType)) { - list.add(criteriaBuilder.equal(root.get("satelliteType").as(String.class), satelliteType)); - } - if (landType != null && !"".equals(landType)) { - list.add(criteriaBuilder.equal(root.get("landType").as(String.class), landType)); - } - if (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - LocalDateTime localDateTime = LocalDateTime.now(); - LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTimes = DateTimeUtil.dateTimeToString(localDateTime2); - // 当前时间减30 - LocalDateTime localDateTime1 = localDateTime.minusDays(30); - String startTimes = DateTimeUtil.dateTimeToString(localDateTime1); - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTimes)); - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTimes)); - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 动态多条件查询项目信息(县) - */ - public List selectFirePointByCounty(String countyCode, String streetCode, String satelliteType, String landType, String startTime, String endTime) { - Specification specification = (root, criteriaQuery, criteriaBuilder) -> { - List list = new ArrayList<>(); - if (countyCode != null && !"".equals(countyCode)) { - list.add(criteriaBuilder.like(root.get("countyCode").as(String.class), "%" + countyCode + "%")); - } - if (streetCode != null && !"".equals(streetCode)) { - list.add(criteriaBuilder.like(root.get("streetCode").as(String.class), "%" + streetCode + "%")); - } - if (satelliteType != null && !"".equals(satelliteType)) { - list.add(criteriaBuilder.equal(root.get("satelliteType").as(String.class), satelliteType)); - } - if (landType != null && !"".equals(landType)) { - list.add(criteriaBuilder.equal(root.get("landType").as(String.class), landType)); - } - if (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - LocalDateTime localDateTime = LocalDateTime.now(); - LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTimes = DateTimeUtil.dateTimeToString(localDateTime2); - // 当前时间减30 - LocalDateTime localDateTime1 = localDateTime.minusDays(30); - String startTimes = DateTimeUtil.dateTimeToString(localDateTime1); - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTimes)); - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTimes)); - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 动态多条件查询项目信息(街道) - */ - public List selectFirePointByStreet(String streetCode, String satelliteType, String landType, String startTime, String endTime) { - Specification specification = (root, criteriaQuery, criteriaBuilder) -> { - List list = new ArrayList<>(); - if (streetCode != null && !"".equals(streetCode)) { - list.add(criteriaBuilder.like(root.get("streetCode").as(String.class), streetCode)); - } - if (satelliteType != null && !"".equals(satelliteType)) { - list.add(criteriaBuilder.equal(root.get("satelliteType").as(String.class), satelliteType)); - } - if (landType != null && !"".equals(landType)) { - list.add(criteriaBuilder.equal(root.get("landType").as(String.class), landType)); - } - if (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - LocalDateTime localDateTime = LocalDateTime.now(); - LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTimes = DateTimeUtil.dateTimeToString(localDateTime2); - // 当前时间减30 - LocalDateTime localDateTime1 = localDateTime.minusDays(30); - String startTimes = DateTimeUtil.dateTimeToString(localDateTime1); - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTimes)); - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTimes)); - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - /** * 查询近一周的火点 */