69 lines
3.0 KiB
Java
69 lines
3.0 KiB
Java
|
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<FirePointEntity> queryFirePoint(String code, String startTime, String endTime, String satelliteType, String landType) {
|
||
|
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
|
||
|
//查询条件集合
|
||
|
List<Predicate> 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;
|
||
|
}
|
||
|
|
||
|
}
|