添加了根据设备编号和时间段查询火情信息的功能模块

This commit is contained in:
XinYi Song 2022-02-14 11:59:14 +08:00
parent 20eec3a117
commit 8eb571336e
6 changed files with 109 additions and 2 deletions

View File

@ -1,11 +1,20 @@
package com.xkrs.controller;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.model.entity.Fire;
import com.xkrs.service.FireService;
import com.xkrs.util.Query;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
/**
* @Author: XinYi Song
@ -26,4 +35,16 @@ public class FireController {
public String getFireInformation(@RequestParam("code") String code){
return fireService.getFireInformation(code);
}
/**
* 根据设备编码和时间段查询火情信息
* @param code
* @param startTime
* @param endTime
* @return
*/
@GetMapping("/selectFireBetweenTime")
public String selectFireBetweenTime(@RequestParam("code") String code, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime){
return fireService.selectFireBetweenTime(code,startTime,endTime);
}
}

View File

@ -34,7 +34,7 @@ public class WeatherController {
public String getWeatherByTime(@RequestParam("weatherTime") String weatherTime){
Weather byWeatherTime = weatherDao.findByWeatherTime(weatherTime);
if(byWeatherTime == null){
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"暂时还没有该天的天气状况!",locale);
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时还没有该天的天气状况!",locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,byWeatherTime,locale);
}

View File

@ -2,6 +2,7 @@ package com.xkrs.dao;
import com.xkrs.model.entity.Fire;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
@ -13,7 +14,7 @@ import java.util.List;
* @Date: 2022/2/8 17:12
*/
@Component
public interface FireDao extends JpaRepository<Fire,Long> {
public interface FireDao extends JpaRepository<Fire,Long>, JpaSpecificationExecutor<Fire> {
/**
* 根据报警编码查询火情信息

View File

@ -1,5 +1,7 @@
package com.xkrs.service;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @Author: XinYi Song
* @Date: 2022/2/11 8:51
@ -12,4 +14,13 @@ public interface FireService {
* @return
*/
String getFireInformation(String code);
/**
* 根据设备编码和时间段查询火情信息
* @param code
* @param startTime
* @param endTime
* @return
*/
String selectFireBetweenTime(String code, String startTime, String endTime);
}

View File

@ -4,6 +4,7 @@ import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.FireDao;
import com.xkrs.model.entity.Fire;
import com.xkrs.service.FireService;
import com.xkrs.util.Query;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.i18n.LocaleContextHolder;
@ -26,6 +27,9 @@ public class FireServerImpl implements FireService {
@Resource
private FireDao fireDao;
@Resource
private Query query;
Locale locale = LocaleContextHolder.getLocale();
@ -43,4 +47,21 @@ public class FireServerImpl implements FireService {
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,byDeviceCode,locale);
}
/**
* 根据设备编码和时间段查询火情信息
* @param code
* @param startTime
* @param endTime
* @return
*/
@Cacheable(keyGenerator = "keyGenerator",unless="#result == null")
@Override
public String selectFireBetweenTime(String code, String startTime, String endTime) {
List<Fire> fires = query.selectFireBetweenTime(code, startTime, endTime);
if(fires == null || fires.size() == 0){
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"暂时还没有该设备编号或时间段内的火情!",locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,fires,locale);
}
}

View File

@ -0,0 +1,53 @@
package com.xkrs.util;
import com.xkrs.dao.FireDao;
import com.xkrs.dao.WeatherDao;
import com.xkrs.model.entity.Fire;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: XinYi Song
* @Date: 2022/2/14 11:29
*/
@Component
public class Query {
@Resource
private FireDao fireDao;
/**
* 根据设备编号和时间段动态查询报警信息
* @param startTime
* @param endTime
* @return
*/
public List<Fire> selectFireBetweenTime(String code, String startTime, String endTime) {
Specification<Fire> specification = new Specification<Fire>() {
@Override
public Predicate toPredicate(Root<Fire> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> list = new ArrayList<>();
if (code != null && !"".equals(code)) {
list.add(criteriaBuilder.equal(root.get("deviceCode").as(String.class), code));
}
if(startTime != null && !"".equals(startTime)){
list.add(criteriaBuilder.greaterThanOrEqualTo(root.get("alarmDate").as(String.class), startTime));
}
if(endTime != null && !"".equals(endTime)){
list.add(criteriaBuilder.lessThanOrEqualTo(root.get("alarmDate").as(String.class), endTime));
}
Predicate[] predicates = new Predicate[list.size()];
return criteriaBuilder.and(list.toArray(predicates));
}
};
return fireDao.findAll(specification);
}
}