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 + "%")); } } //限制最早开始时间不得早于一个月 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)); } //结束时间查询条件 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)); } //卫星类型查询条件 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; } }