优化按照时间查询

This commit is contained in:
liuchengqian 2022-06-08 09:23:33 +08:00
parent fe880979dd
commit d9e1536c2d
5 changed files with 90 additions and 259 deletions

View File

@ -17,36 +17,6 @@ import java.util.Map;
@Component @Component
public interface FirePointDao extends JpaRepository<FirePointEntity, Long>, JpaSpecificationExecutor<FirePointEntity> { public interface FirePointDao extends JpaRepository<FirePointEntity, Long>, JpaSpecificationExecutor<FirePointEntity> {
/**
* 查询今天的火点信息
*
* @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<FirePointEntity> 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<FirePointEntity> 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<FirePointEntity> selectTodayFirePointOneStreet(String addTime, String streetCode);
/** /**
* 根据火点编码查询火点信息 * 根据火点编码查询火点信息
* *

View File

@ -56,9 +56,6 @@ public class FirePointServiceImpl implements FirePointService {
@Resource @Resource
private ShanDongFirePointDao shanDongFirePointDao; private ShanDongFirePointDao shanDongFirePointDao;
@Resource
private Query query;
@Resource @Resource
private SysUserDao sysUserDao; private SysUserDao sysUserDao;
@ -367,10 +364,13 @@ public class FirePointServiceImpl implements FirePointService {
} }
List<String> weixinIdList = new ArrayList<>(); List<String> weixinIdList = new ArrayList<>();
for (CountyCodeWeiXinEntity countyCodeWeiXin : countyCodeWeiXinList) { for (CountyCodeWeiXinEntity countyCodeWeiXin : countyCodeWeiXinList) {
String countyCode = countyCodeWeiXin.getCountyCode(); try {
String startCountyCode = getStartCountyCode(countyCode); String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(countyCodeWeiXin.getCountyCode());
if (firePointEntity.getStreetCode().startsWith(startCountyCode)) { if (firePointEntity.getStreetCode().startsWith(codeNotZeroEnd)) {
weixinIdList.add(countyCodeWeiXin.getWeixinId()); weixinIdList.add(countyCodeWeiXin.getWeixinId());
}
} catch (Exception e) {
e.printStackTrace();
} }
} }
if (weixinIdList.size() > 0) { if (weixinIdList.size() > 0) {
@ -421,9 +421,8 @@ public class FirePointServiceImpl implements FirePointService {
List<String> telephoneList = new ArrayList<>(); List<String> telephoneList = new ArrayList<>();
for (SysUserEntity sysUser : sysUserList) { for (SysUserEntity sysUser : sysUserList) {
try { try {
String countyCode = sysUser.getCountyCode(); String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(sysUser.getCountyCode());
String startCountyCode = getStartCountyCode(countyCode); if (firePointEntity.getStreetCode().startsWith(codeNotZeroEnd)) {
if (firePointEntity.getStreetCode().startsWith(startCountyCode)) {
if (sysUser.getDeleteFlag() != 0) {//被删除的用户不发短信 if (sysUser.getDeleteFlag() != 0) {//被删除的用户不发短信
continue; continue;
} }
@ -444,7 +443,6 @@ public class FirePointServiceImpl implements FirePointService {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue;
} }
} }
if (telephoneList.size() > 0) { if (telephoneList.size() > 0) {
@ -468,19 +466,9 @@ public class FirePointServiceImpl implements FirePointService {
return false; return false;
} }
private String getStartCountyCode(String countyCode) { /**
try { * 下载VIP用户火点数据
String temp = countyCode; */
while (temp.endsWith("0")) {
temp = temp.substring(0, temp.length() - 1);
}
return temp;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override @Override
public String downloadVipUserFilePoint(HttpServletRequest request, HttpServletResponse response) { public String downloadVipUserFilePoint(HttpServletRequest request, HttpServletResponse response) {
LocalDate now = LocalDate.now(); LocalDate now = LocalDate.now();
@ -509,20 +497,52 @@ public class FirePointServiceImpl implements FirePointService {
@Cacheable(keyGenerator = "keyGenerator", unless = "#result == null") @Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
@Override @Override
public List<FirePointEntity> selectTodayFirePoint(String countyCode) { public List<FirePointEntity> selectTodayFirePoint(String countyCode) {
String addTime = DateTimeUtil.dateToString(LocalDate.now());
if (9 == countyCode.length()) { LocalDateTime startTime1 = LocalDateTime.now();
return firePointDao.selectTodayFirePointOneStreet(addTime, countyCode); LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0);
} else if ("0000".equals(countyCode.substring(2))) { String startTimeString = DateTimeUtils.localDateTimeToString(startTime2);
List<CityEntity> byProCode = cityDao.findByProCode(countyCode);
String proName = byProCode.get(0).getProName(); LocalDateTime endTime1 = LocalDateTime.now();
return firePointDao.selectTodayFirePoint(addTime, proName); LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth() + 1, 0, 0, 0, 0);
} else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) { String endTimeString = DateTimeUtils.localDateTimeToString(endTime2);
List<CountyEntity> byCityCode = countyDao.findByCityCode(countyCode);
String cityName = byCityCode.get(0).getCityName(); return firePointQueryHelper.queryFirePoint(countyCode, startTimeString, endTimeString, null, null);
return firePointDao.selectTodayFirePoint(addTime, cityName); }
} else {
return firePointDao.selectTodayFirePointOne(addTime, countyCode); /**
} * 查询近一周的火点信息
*/
@Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
@Override
public List<FirePointEntity> 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<FirePointEntity> 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 outputEncapsulationObject(PromptMessageEnum.SUCCESS, appTaskBodyVo, locale);
} }
/**
* 查询近一周的火点信息
*
* @return
*/
@Cacheable(keyGenerator = "keyGenerator", unless = "#result == null")
@Override
public List<FirePointEntity> 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<FirePointEntity> 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<CityEntity> 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<CountyEntity> byCityCode = countyDao.findByCityCode(countyCode);
String cityName = byCityCode.get(0).getCityName();
return query.selectFirePointByMonthCity(cityName, startTime, endTime);
} else {
return query.selectFirePointByMonthCounty(countyCode, startTime, endTime);
}
}
/** /**
* 查询近一个月的火点数量 * 查询近一个月的火点数量
* *

View File

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

View File

@ -28,29 +28,33 @@ public class FirePointQueryHelper {
List<Predicate> predicateList = new ArrayList<>(); List<Predicate> predicateList = new ArrayList<>();
//添加区划编码查询条件 //添加区划编码查询条件
if (!TextUtils.isEmpty(code)) { if (!TextUtils.isEmpty(code)) {
String codeNotZeroEnd = getCodeNotZeroEnd(code); try {
if (!TextUtils.isEmpty(codeNotZeroEnd)) { String codeNotZeroEnd = FirePointCodeUtils.getCodeNotZeroEnd(code);
predicateList.add(criteriaBuilder.like(root.get("streetCode").as(String.class), codeNotZeroEnd + "%")); 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 lastMonth1 = LocalDateTime.now().minusMonths(1);
LocalDateTime lastMonth2 = LocalDateTime.of(lastMonth1.getYear(), lastMonth1.getMonth(), lastMonth1.getDayOfMonth(), 0, 0, 0, 0); LocalDateTime lastMonth2 = LocalDateTime.of(lastMonth1.getYear(), lastMonth1.getMonth(), lastMonth1.getDayOfMonth(), 0, 0, 0, 0);
String lastMonthString = DateTimeUtils.localDateTimeToString(lastMonth2); String lastMonthString = DateTimeUtils.localDateTimeToString(lastMonth2);
//开始时间查询条件
predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), lastMonthString)); predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), lastMonthString));
//开始时间查询条件
if (!TextUtils.isEmpty(startTime)) { if (!TextUtils.isEmpty(startTime)) {
LocalDateTime oldStartDateTime = DateTimeUtils.stringToLocalDateTime(startTime); LocalDateTime startTime1 = DateTimeUtils.stringToLocalDateTime(startTime);
LocalDateTime newStartDateTime = LocalDateTime.of(oldStartDateTime.getYear(), oldStartDateTime.getMonth(), oldStartDateTime.getDayOfMonth(), 0, 0, 0, 0); LocalDateTime startTime2 = LocalDateTime.of(startTime1.getYear(), startTime1.getMonth(), startTime1.getDayOfMonth(), 0, 0, 0, 0);
String finalStartTime = DateTimeUtils.localDateTimeToString(newStartDateTime); String startTimeString = DateTimeUtils.localDateTimeToString(startTime2);
predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), finalStartTime)); predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("satelliteTime").as(String.class), startTimeString));
} }
//结束时间查询条件 //结束时间查询条件
if (!TextUtils.isEmpty(endTime)) { if (!TextUtils.isEmpty(endTime)) {
LocalDateTime oldEndDateTime = DateTimeUtils.stringToLocalDateTime(endTime); LocalDateTime endTime1 = DateTimeUtils.stringToLocalDateTime(endTime);
LocalDateTime newEndDateTime = LocalDateTime.of(oldEndDateTime.getYear(), oldEndDateTime.getMonth(), oldEndDateTime.getDayOfMonth() + 1, 0, 0, 0, 0); LocalDateTime endTime2 = LocalDateTime.of(endTime1.getYear(), endTime1.getMonth(), endTime1.getDayOfMonth() + 1, 0, 0, 0, 0);
String finalEndTime = DateTimeUtils.localDateTimeToString(newEndDateTime); String endTimeString = DateTimeUtils.localDateTimeToString(endTime2);
predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), finalEndTime)); predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("satelliteTime").as(String.class), endTimeString));
} }
//卫星类型查询条件 //卫星类型查询条件
if (!TextUtils.isEmpty(satelliteType)) { if (!TextUtils.isEmpty(satelliteType)) {
@ -66,12 +70,4 @@ public class FirePointQueryHelper {
return firePointDao.findAll(specification, Sort.by(Sort.Direction.DESC, "satelliteTime")); 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;
}
} }

View File

@ -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<FirePointEntity> selectFirePointBetweenThree(String startTime, String endTime) {
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<FirePointEntity> selectFirePointByMonthPro(String address, String startTime, String endTime) {
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<FirePointEntity> selectFirePointByMonthCity(String address, String startTime, String endTime) {
Specification<FirePointEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<FirePointEntity> selectFirePointByMonthCounty(String countyCode, 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 (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<FirePointEntity> selectFirePointByMonthStreet(String streetCode, 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 (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);
}
}