From d9e1536c2d565f5615f4b168c531ff5bbe79626f Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 8 Jun 2022 09:23:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8C=89=E7=85=A7=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xkrs/dao/FirePointDao.java | 30 ---- .../service/impl/FirePointServiceImpl.java | 141 +++++++----------- .../com/xkrs/utils/FirePointCodeUtils.java | 16 ++ .../com/xkrs/utils/FirePointQueryHelper.java | 36 ++--- src/main/java/com/xkrs/utils/Query.java | 126 ---------------- 5 files changed, 90 insertions(+), 259 deletions(-) create mode 100644 src/main/java/com/xkrs/utils/FirePointCodeUtils.java delete mode 100644 src/main/java/com/xkrs/utils/Query.java diff --git a/src/main/java/com/xkrs/dao/FirePointDao.java b/src/main/java/com/xkrs/dao/FirePointDao.java index 0e09c4a..0404bbc 100644 --- a/src/main/java/com/xkrs/dao/FirePointDao.java +++ b/src/main/java/com/xkrs/dao/FirePointDao.java @@ -17,36 +17,6 @@ import java.util.Map; @Component public interface FirePointDao extends JpaRepository, JpaSpecificationExecutor { - /** - * 查询今天的火点信息 - * - * @param addTime - * @param address - * @return - */ - @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); - - /** - * 区县条件查询今天火点信息 - * - * @param addTime - * @param countyCode - * @return - */ - @Query(value = "select * from fire_point where add_time like CONCAT('%',:addTime,'%') and county_code = :countyCode", nativeQuery = true) - List selectTodayFirePointOne(String addTime, String countyCode); - - /** - * 街道条件查询今天火点信息 - * - * @param addTime - * @param streetCode - * @return - */ - @Query(value = "select * from fire_point where add_time like CONCAT('%',:addTime,'%') and street_code = :streetCode", nativeQuery = true) - List selectTodayFirePointOneStreet(String addTime, String streetCode); - /** * 根据火点编码查询火点信息 * diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index bcd2155..21b6e86 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -56,9 +56,6 @@ public class FirePointServiceImpl implements FirePointService { @Resource private ShanDongFirePointDao shanDongFirePointDao; - @Resource - private Query query; - @Resource private SysUserDao sysUserDao; @@ -367,10 +364,13 @@ public class FirePointServiceImpl implements FirePointService { } List weixinIdList = new ArrayList<>(); for (CountyCodeWeiXinEntity countyCodeWeiXin : countyCodeWeiXinList) { - String countyCode = countyCodeWeiXin.getCountyCode(); - String startCountyCode = getStartCountyCode(countyCode); - if (firePointEntity.getStreetCode().startsWith(startCountyCode)) { - weixinIdList.add(countyCodeWeiXin.getWeixinId()); + try { + String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(countyCodeWeiXin.getCountyCode()); + if (firePointEntity.getStreetCode().startsWith(codeNotZeroEnd)) { + weixinIdList.add(countyCodeWeiXin.getWeixinId()); + } + } catch (Exception e) { + e.printStackTrace(); } } if (weixinIdList.size() > 0) { @@ -421,9 +421,8 @@ public class FirePointServiceImpl implements FirePointService { List telephoneList = new ArrayList<>(); for (SysUserEntity sysUser : sysUserList) { try { - String countyCode = sysUser.getCountyCode(); - String startCountyCode = getStartCountyCode(countyCode); - if (firePointEntity.getStreetCode().startsWith(startCountyCode)) { + String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(sysUser.getCountyCode()); + if (firePointEntity.getStreetCode().startsWith(codeNotZeroEnd)) { if (sysUser.getDeleteFlag() != 0) {//被删除的用户不发短信 continue; } @@ -444,7 +443,6 @@ public class FirePointServiceImpl implements FirePointService { } } catch (Exception e) { e.printStackTrace(); - continue; } } if (telephoneList.size() > 0) { @@ -468,19 +466,9 @@ public class FirePointServiceImpl implements FirePointService { return false; } - private String getStartCountyCode(String countyCode) { - try { - String temp = countyCode; - while (temp.endsWith("0")) { - temp = temp.substring(0, temp.length() - 1); - } - return temp; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - + /** + * 下载VIP用户火点数据 + */ @Override public String downloadVipUserFilePoint(HttpServletRequest request, HttpServletResponse response) { LocalDate now = LocalDate.now(); @@ -509,20 +497,52 @@ public class FirePointServiceImpl implements FirePointService { @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") @Override public List selectTodayFirePoint(String countyCode) { - String addTime = DateTimeUtil.dateToString(LocalDate.now()); - if (9 == countyCode.length()) { - return firePointDao.selectTodayFirePointOneStreet(addTime, countyCode); - } else if ("0000".equals(countyCode.substring(2))) { - List byProCode = cityDao.findByProCode(countyCode); - String proName = byProCode.get(0).getProName(); - return firePointDao.selectTodayFirePoint(addTime, proName); - } else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) { - List byCityCode = countyDao.findByCityCode(countyCode); - String cityName = byCityCode.get(0).getCityName(); - return firePointDao.selectTodayFirePoint(addTime, cityName); - } else { - return firePointDao.selectTodayFirePointOne(addTime, countyCode); - } + + LocalDateTime startTime1 = LocalDateTime.now(); + LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0); + String startTimeString = DateTimeUtils.localDateTimeToString(startTime2); + + LocalDateTime endTime1 = LocalDateTime.now(); + LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth() + 1, 0, 0, 0, 0); + String endTimeString = DateTimeUtils.localDateTimeToString(endTime2); + + return firePointQueryHelper.queryFirePoint(countyCode, startTimeString, endTimeString, null, null); + } + + /** + * 查询近一周的火点信息 + */ + @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") + @Override + public List selectFirePointBetweenSeven() { + + LocalDateTime startTime1 = LocalDateTime.now().minusDays(7); + LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0); + String startTimeString = DateTimeUtils.localDateTimeToString(startTime2); + + LocalDateTime endTime1 = LocalDateTime.now(); + LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth() + 1, 0, 0, 0, 0); + String endTimeString = DateTimeUtils.localDateTimeToString(endTime2); + + return firePointQueryHelper.queryFirePoint(null, startTimeString, endTimeString, null, null); + } + + /** + * 查询近一个月的火点信息 + */ + @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") + @Override + public List selectFirePointByMonth(String countyCode) { + + LocalDateTime startTime1 = LocalDateTime.now().minusMonths(1); + LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0); + String startTimeString = DateTimeUtils.localDateTimeToString(startTime2); + + LocalDateTime endTime1 = LocalDateTime.now(); + LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth() + 1, 0, 0, 0, 0); + String endTimeString = DateTimeUtils.localDateTimeToString(endTime2); + + return firePointQueryHelper.queryFirePoint(countyCode, startTimeString, endTimeString, null, null); } /** @@ -644,51 +664,6 @@ public class FirePointServiceImpl implements FirePointService { return outputEncapsulationObject(PromptMessageEnum.SUCCESS, appTaskBodyVo, locale); } - /** - * 查询近一周的火点信息 - * - * @return - */ - @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") - @Override - public List selectFirePointBetweenSeven() { - LocalDateTime localDateTime = LocalDateTime.now(); - String endTime = dateTimeToString(localDateTime); - // 当前时间减7 - LocalDateTime localDateTime1 = localDateTime.minusDays(7); - String startTime = dateTimeToString(localDateTime1); - return query.selectFirePointBetweenThree(startTime, endTime); - } - - /** - * 查询近一个月的火点信息 - * - * @return - */ - @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") - @Override - public List selectFirePointByMonth(String countyCode) { - LocalDateTime localDateTime = LocalDateTime.now(); - String endTime = dateTimeToString(localDateTime); - // 当前时间减31 - LocalDateTime localDateTime1 = localDateTime.minusDays(31); - String startTime = dateTimeToString(localDateTime1); - if (9 == countyCode.length()) { - return query.selectFirePointByMonthStreet(countyCode, startTime, endTime); - } else if ("0000".equals(countyCode.substring(2))) { - List byProCode = cityDao.findByProCode(countyCode); - String proName = byProCode.get(0).getProName(); - return query.selectFirePointByMonthPro(proName, startTime, endTime); - } else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) { - List byCityCode = countyDao.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/FirePointCodeUtils.java b/src/main/java/com/xkrs/utils/FirePointCodeUtils.java new file mode 100644 index 0000000..bdefe87 --- /dev/null +++ b/src/main/java/com/xkrs/utils/FirePointCodeUtils.java @@ -0,0 +1,16 @@ +package com.xkrs.utils; + +public class FirePointCodeUtils { + + private FirePointCodeUtils() { + } + + public static String getCodeNotZeroEnd(String code) throws Exception { + String codeNotZeroEnd = code; + while (codeNotZeroEnd.endsWith("0")) { + codeNotZeroEnd = codeNotZeroEnd.substring(0, codeNotZeroEnd.length() - 1); + } + return codeNotZeroEnd; + } + +} diff --git a/src/main/java/com/xkrs/utils/FirePointQueryHelper.java b/src/main/java/com/xkrs/utils/FirePointQueryHelper.java index c3cc997..d016690 100644 --- a/src/main/java/com/xkrs/utils/FirePointQueryHelper.java +++ b/src/main/java/com/xkrs/utils/FirePointQueryHelper.java @@ -28,29 +28,33 @@ public class FirePointQueryHelper { List predicateList = new ArrayList<>(); //添加区划编码查询条件 if (!TextUtils.isEmpty(code)) { - String codeNotZeroEnd = getCodeNotZeroEnd(code); - if (!TextUtils.isEmpty(codeNotZeroEnd)) { - predicateList.add(criteriaBuilder.like(root.get("streetCode").as(String.class), codeNotZeroEnd + "%")); + try { + String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(code); + if (!TextUtils.isEmpty(codeNotZeroEnd)) { + predicateList.add(criteriaBuilder.like(root.get("streetCode").as(String.class), codeNotZeroEnd + "%")); + } + } catch (Exception e) { + e.printStackTrace(); } } //限制最早开始时间不得早于一个月 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), lastMonthString)); + //开始时间查询条件 if (!TextUtils.isEmpty(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)); + LocalDateTime startTime1 = DateTimeUtils.stringToLocalDateTime(startTime); + LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0); + String startTimeString = DateTimeUtils.localDateTimeToString(startTime2); + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTimeString)); } //结束时间查询条件 if (!TextUtils.isEmpty(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)); + LocalDateTime endTime1 = DateTimeUtils.stringToLocalDateTime(endTime); + LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth() + 1, 0, 0, 0, 0); + String endTimeString = DateTimeUtils.localDateTimeToString(endTime2); + predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTimeString)); } //卫星类型查询条件 if (!TextUtils.isEmpty(satelliteType)) { @@ -66,12 +70,4 @@ public class FirePointQueryHelper { return firePointDao.findAll(specification, Sort.by(Sort.Direction.DESC, "satelliteTime")); } - private String getCodeNotZeroEnd(String code) { - String codeNotZeroEnd = code; - while (codeNotZeroEnd.endsWith("0")) { - codeNotZeroEnd = codeNotZeroEnd.substring(0, codeNotZeroEnd.length() - 1); - } - return codeNotZeroEnd; - } - } \ No newline at end of file diff --git a/src/main/java/com/xkrs/utils/Query.java b/src/main/java/com/xkrs/utils/Query.java deleted file mode 100644 index 7ad4c21..0000000 --- a/src/main/java/com/xkrs/utils/Query.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.xkrs.utils; - -import com.xkrs.dao.FirePointDao; -import com.xkrs.model.entity.FirePointEntity; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import javax.persistence.criteria.Predicate; -import java.util.ArrayList; -import java.util.List; - -/** - * @author XinYi Song - */ - -@Component -public class Query { - - @Resource - private FirePointDao firePointDao; - - /** - * 查询近一周的火点 - */ - public List selectFirePointBetweenThree(String startTime, String endTime) { - Specification specification = (root, criteriaQuery, criteriaBuilder) -> { - List list = new ArrayList<>(); - if (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 查询近一个月的火点信息(省) - */ - public List selectFirePointByMonthPro(String address, String startTime, String endTime) { - Specification specification = (root, criteriaQuery, 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("satelliteTime").as(String.class), startTime)); - } - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 查询近一个月的火点信息(市) - */ - public List selectFirePointByMonthCity(String address, String startTime, String endTime) { - Specification specification = (root, criteriaQuery, 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("satelliteTime").as(String.class), startTime)); - } - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 查询近一个月的火点信息(县) - */ - public List selectFirePointByMonthCounty(String countyCode, 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 (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - /** - * 查询近一个月的火点信息(街道) - */ - public List selectFirePointByMonthStreet(String streetCode, 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 (startTime != null && !"".equals(startTime)) { - list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); - } - if (endTime != null && !"".equals(endTime)) { - list.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); - } - Predicate[] predicates = new Predicate[list.size()]; - return criteriaBuilder.and(list.toArray(predicates)); - }; - return firePointDao.findAll(specification); - } - - -} \ No newline at end of file