From e68a996df00cdd37a19ee130de8833cb8e89b3f1 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 7 Jun 2022 16:24:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E7=81=AB=E7=82=B9=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/FirePointServiceImpl.java | 3 + .../java/com/xkrs/utils/DateTimeUtils.java | 13 ++++ .../com/xkrs/utils/FirePointQueryHelper.java | 69 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/main/java/com/xkrs/utils/FirePointQueryHelper.java diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index 6a9e339..6c06655 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -95,6 +95,9 @@ public class FirePointServiceImpl implements FirePointService { @Resource private RelRoleAuthorityDao relRoleAuthorityDao; + @Resource + private FirePointQueryHelper firePointQueryHelper; + /** * 添加火点信息 * diff --git a/src/main/java/com/xkrs/utils/DateTimeUtils.java b/src/main/java/com/xkrs/utils/DateTimeUtils.java index 0fca6f3..df4f628 100644 --- a/src/main/java/com/xkrs/utils/DateTimeUtils.java +++ b/src/main/java/com/xkrs/utils/DateTimeUtils.java @@ -1,4 +1,17 @@ package com.xkrs.utils; +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 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); + } } diff --git a/src/main/java/com/xkrs/utils/FirePointQueryHelper.java b/src/main/java/com/xkrs/utils/FirePointQueryHelper.java new file mode 100644 index 0000000..74a2d64 --- /dev/null +++ b/src/main/java/com/xkrs/utils/FirePointQueryHelper.java @@ -0,0 +1,69 @@ +package com.xkrs.utils; + +import com.xkrs.dao.FirePointDao; +import com.xkrs.model.entity.FirePointEntity; +import org.apache.hc.core5.util.TextUtils; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; +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; + +/** + * 火点查询工具类 + */ +@Component +public class FirePointQueryHelper { + + @Resource + private FirePointDao firePointDao; + + public List queryFirePoint(String code, String startTime, String endTime, String satelliteType, String landType) { + Specification specification = (root, criteriaQuery, criteriaBuilder) -> { + //查询条件集合 + 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 + "%")); + } + } + //限制最早开始时间不得早于一个月 + String earliestStartTime = DateTimeUtils.localDateTimeToString(LocalDateTime.now().minusDays(30)); + //开始时间查询条件 + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), earliestStartTime)); + if (!TextUtils.isEmpty(startTime)) { + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTime)); + } + //结束时间查询条件 + if (!TextUtils.isEmpty(endTime)) { + predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTime)); + } + //卫星类型查询条件 + if (!TextUtils.isEmpty(satelliteType)) { + predicateList.add(criteriaBuilder.equal(root.get("satelliteType").as(String.class), satelliteType)); + } + //地物类型查询条件 + if (!TextUtils.isEmpty(landType)) { + predicateList.add(criteriaBuilder.equal(root.get("landType").as(String.class), landType)); + } + Predicate[] predicateArray = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicateArray)); + }; + 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