优化多条件动态查询

This commit is contained in:
liuchengqian 2022-06-07 21:14:14 +08:00
parent e68a996df0
commit fe880979dd
6 changed files with 36 additions and 336 deletions

View File

@ -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<FirePointEntity> firePointEntities = firePointService.selectFirePointByStreet(sysUserEntity.getCountyCode(), satelliteType, landType, startTime, endTime);
if (firePointEntities == null || firePointEntities.size() == 0) {
// 用于查询的区划编码可能是省市区县街道任意级别
String queryCode = TextUtils.isEmpty(cityCode) ? sysUserEntity.getCountyCode() : cityCode;
List<FirePointEntity> firePointList = firePointQueryHelper.queryFirePoint(queryCode, startTime, endTime, satelliteType, landType);
if (firePointList == null || firePointList.size() == 0) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有火点数据", locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointEntities, locale);
} else if (sysUserEntity.getCountyCode().substring(2).equals("0000")) {
// 查询省
List<FirePointEntity> 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<FirePointEntity> 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<FirePointEntity> 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);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, firePointList, locale);
}
/**

View File

@ -41,57 +41,6 @@ public interface FirePointService {
*/
List<FirePointEntity> selectTodayFirePoint(String countyCode);
/**
* 动态多条件查询火点信息()
*
* @param proCode
* @param cityCode
* @param satelliteType
* @param landType
* @param startTime
* @param endTime
* @return
*/
List<FirePointEntity> 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<FirePointEntity> 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<FirePointEntity> 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<FirePointEntity> selectFirePointByStreet(String streetCode, String satelliteType, String landType, String startTime, String endTime);
/**
* 根据火点编码修改火点的状态预警
*

View File

@ -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<FirePointEntity> selectFirePoint(String proCode, String cityCode, String satelliteType, String landType, String startTime, String endTime) {
List<CityEntity> 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<FirePointEntity> selectFirePointByCity(String cityCode, String countyCode, String satelliteType, String landType, String startTime, String endTime) {
List<CountyEntity> 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<FirePointEntity> 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<FirePointEntity> 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);
}
/**
* 根据火点编码修改火点的状态预警
*

View File

@ -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) {
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);
}
}
}

View File

@ -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)) {

View File

@ -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<FirePointEntity> 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<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<FirePointEntity> selectFirePointByCity(String cityName, String countyCode, String satelliteType, String landType, String startTime, String endTime) {
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<FirePointEntity> selectFirePointByCounty(String countyCode, String streetCode, String satelliteType, String landType, String startTime, String endTime) {
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<FirePointEntity> selectFirePointByStreet(String streetCode, String satelliteType, String landType, String startTime, String endTime) {
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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);
}
/**
* 查询近一周的火点
*/