213 lines
8.5 KiB
Java
213 lines
8.5 KiB
Java
|
|
package com.whdc.legacy.component;
|
||
|
|
|
||
|
|
import com.alibaba.fastjson.JSON;
|
||
|
|
import com.alibaba.fastjson.JSONObject;
|
||
|
|
import com.whdc.legacy.model.dto.ApiDto;
|
||
|
|
import com.whdc.legacy.model.entity.QXWarning;
|
||
|
|
import com.whdc.legacy.model.entity.ShAddressBook;
|
||
|
|
import com.whdc.legacy.model.vo.QXWarningVO;
|
||
|
|
import com.whdc.legacy.model.vo.WarningData;
|
||
|
|
import com.whdc.legacy.service.IQXWarningService;
|
||
|
|
import com.whdc.legacy.service.ShAddressBookService;
|
||
|
|
import com.whdc.common.utils.DateUtils;
|
||
|
|
import com.whdc.common.utils.HttpUtil;
|
||
|
|
import io.swagger.annotations.ApiOperation;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.apache.commons.collections4.CollectionUtils;
|
||
|
|
import org.apache.commons.compress.utils.Lists;
|
||
|
|
import org.apache.commons.lang3.StringUtils;
|
||
|
|
import org.apache.http.HttpStatus;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.beans.factory.annotation.Value;
|
||
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||
|
|
import org.springframework.context.annotation.Profile;
|
||
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
||
|
|
import org.springframework.scheduling.annotation.Async;
|
||
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
||
|
|
|
||
|
|
import javax.annotation.PostConstruct;
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.Comparator;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Set;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
import static com.whdc.legacy.controller.QXWarnController.QX_TEMP_REDIS_KEY;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author 李赛
|
||
|
|
* @date 2022-07-17 15:33
|
||
|
|
*/
|
||
|
|
@Component
|
||
|
|
@Slf4j
|
||
|
|
@ConditionalOnProperty(
|
||
|
|
name = "enableSchedules.qxwarning",
|
||
|
|
havingValue = "true"
|
||
|
|
)
|
||
|
|
public class MyPostConstruct {
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private ShAddressBookService shAddressBookService;
|
||
|
|
|
||
|
|
@Value("${getGroupWarning}")
|
||
|
|
public String getGroupWarning;
|
||
|
|
@PostConstruct
|
||
|
|
public void initCache() {
|
||
|
|
log.debug("加载缓存");
|
||
|
|
|
||
|
|
// 预警初始化
|
||
|
|
QXWarningInit();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private IQXWarningService service;
|
||
|
|
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private RedisTemplate<String, String> stringRedisTemplate;
|
||
|
|
|
||
|
|
|
||
|
|
@Async
|
||
|
|
@ApiOperation(value = "预警数据同步接口", notes = "预警数据同步接口")
|
||
|
|
@GetMapping("/syncData")
|
||
|
|
@Scheduled(cron ="*/20 * * * * ?")
|
||
|
|
public void syncData() {
|
||
|
|
LocalDateTime now = LocalDateTime.now();
|
||
|
|
sync(DateUtils.dateToStr(now.minusHours(1)),DateUtils.dateToStr(now.plusHours(1)));
|
||
|
|
// sync(null,null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 预警初始化
|
||
|
|
*/
|
||
|
|
private void QXWarningInit(){
|
||
|
|
long count = service.count();
|
||
|
|
if (count > 0){
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
LocalDateTime now = LocalDateTime.now();
|
||
|
|
sync(DateUtils.dateToStr(now.minusYears(1)),DateUtils.dateToStr(now));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void sync(String stm,String etm) {
|
||
|
|
log.info("预警数据同步开始!!!");
|
||
|
|
ApiDto apiDto = new ApiDto();
|
||
|
|
if (StringUtils.isNotBlank(stm)){
|
||
|
|
log.info("开始时间 " + stm);
|
||
|
|
apiDto.setStartTime(stm);
|
||
|
|
}
|
||
|
|
if (StringUtils.isNotBlank(etm)){
|
||
|
|
log.info("结束时间 " + etm);
|
||
|
|
apiDto.setEndTime(etm);
|
||
|
|
}
|
||
|
|
|
||
|
|
apiDto.setFilter(Lists.newArrayList());
|
||
|
|
|
||
|
|
String str = HttpUtil.sendPost(getGroupWarning, JSON.toJSONString(apiDto));
|
||
|
|
// String str = HttpUtil.sendPost("http://127.0.0.1:20000/shzh/met/zyqxfw/api/warning/getGroupWarning", JSON.toJSONString(apiDto));
|
||
|
|
JSONObject json = JSON.parseObject(str);
|
||
|
|
if (json != null && json.getInteger("code") == HttpStatus.SC_OK) {
|
||
|
|
List<WarningData> data = json.getJSONArray("data").toJavaList(WarningData.class);
|
||
|
|
List<QXWarningVO> warningList = getList(data);
|
||
|
|
log.info("预警数据同步获取数据条数 " + warningList.size());
|
||
|
|
List<Integer> warnIds = warningList.stream().map(QXWarningVO::getWarnid).collect(Collectors.toList());
|
||
|
|
|
||
|
|
List<QXWarning> list = service.lambdaQuery().in(QXWarning::getWarnid, warnIds).list();
|
||
|
|
log.info("预警数据同步已存预警 " + list.size());
|
||
|
|
|
||
|
|
Set<Integer> dbWarnSet = list.stream().map(QXWarning::getWarnid).collect(Collectors.toSet());
|
||
|
|
|
||
|
|
List<QXWarning> adds = Lists.newArrayList();
|
||
|
|
for (QXWarningVO warningVO : warningList) {
|
||
|
|
if (!dbWarnSet.contains(warningVO.getWarnid())) {
|
||
|
|
QXWarning qxwarning = new QXWarning();
|
||
|
|
qxwarning.setCreateTime(DateUtils.dateToStrYMDHds(warningVO.getCreateTime()));
|
||
|
|
qxwarning.setPublishTime(DateUtils.dateToStrYMDHds(warningVO.getPublishTime()));
|
||
|
|
qxwarning.setStartTime(DateUtils.dateToStrYMDHds(warningVO.getStartTime()));
|
||
|
|
qxwarning.setEndTime(DateUtils.dateToStrYMDHds(warningVO.getEndTime()));
|
||
|
|
qxwarning.setWarnSignalType(warningVO.getWarnSignalType());
|
||
|
|
qxwarning.setWarnSignalLevel(warningVO.getWarnSignalLevel());
|
||
|
|
qxwarning.setPublishUnit(warningVO.getPublishUnit());
|
||
|
|
qxwarning.setContent(warningVO.getContent());
|
||
|
|
qxwarning.setWarnid(warningVO.getWarnid());
|
||
|
|
qxwarning.setCtnm(warningVO.getCtnm());
|
||
|
|
qxwarning.setCnnm(warningVO.getCnnm());
|
||
|
|
|
||
|
|
stringRedisTemplate.opsForValue().set(QX_TEMP_REDIS_KEY + qxwarning.getWarnid(),JSON.toJSONString(warningVO));
|
||
|
|
adds.add(qxwarning);
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if (CollectionUtils.isNotEmpty(adds)) {
|
||
|
|
log.info("预警数据同步待添加预警 " + adds.size());
|
||
|
|
if (this.service.saveBatch(adds)) {
|
||
|
|
log.info("添加成功 " + adds.size());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
log.info("预警数据同步执行完成!!!");
|
||
|
|
|
||
|
|
}
|
||
|
|
private List<QXWarningVO> getList(List<WarningData> data) {
|
||
|
|
|
||
|
|
List<QXWarningVO> respList = Lists.newArrayList();
|
||
|
|
for (WarningData datum : data) {//最外层的列表
|
||
|
|
String ctnm = datum.getEffectArea();//市级范围
|
||
|
|
for (WarningData.TypeList typeList : datum.getTypeList()) {//里面的
|
||
|
|
// 预警类型
|
||
|
|
String type = typeList.getType();
|
||
|
|
List<WarningData.Warning> warnList = typeList.getWarnList();
|
||
|
|
|
||
|
|
for (WarningData.Warning warning : warnList) {
|
||
|
|
String cnnm = warning.getEffectArea();
|
||
|
|
QXWarningVO vo = new QXWarningVO();
|
||
|
|
String publishUnit = warning.getPublishUnit();
|
||
|
|
// vo.setTitle(publishUnit + "发布" + type + "预警");
|
||
|
|
vo.setCtnm(ctnm);//市级名称
|
||
|
|
vo.setCnnm(cnnm);//县级名称
|
||
|
|
vo.setPublishUnit(publishUnit);//发布单位
|
||
|
|
vo.setPublishTime(warning.getPublishTime());//预警发布时间
|
||
|
|
vo.setWarnSignalType(warning.getWarnSignalType());//预警类型
|
||
|
|
vo.setWarnSignalLevel(warning.getWarnSignalLevel());//预警级别
|
||
|
|
vo.setContent(warning.getContent());//预警内容
|
||
|
|
vo.setWarnid(warning.getId());
|
||
|
|
vo.setCreateTime(warning.getCreateTime());
|
||
|
|
vo.setStartTime(warning.getStartTime());
|
||
|
|
vo.setEndTime(warning.getEndTime());
|
||
|
|
respList.add(vo);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
respList = respList.stream().filter(o -> "暴雨".equals(o.getWarnSignalType()) || "雷雨大风".equals(o.getWarnSignalType())).collect(Collectors.toList());
|
||
|
|
respList = respList.stream().filter(o -> "红色".equals(o.getWarnSignalLevel()) || "橙色".equals(o.getWarnSignalLevel())).collect(Collectors.toList());
|
||
|
|
return respList.stream().sorted(Comparator.comparing(QXWarningVO::getPublishTime).reversed())
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
@Async
|
||
|
|
@ApiOperation(value = "恢复山洪负责人抽查状态定时任务", notes = "预警数据同步接口")
|
||
|
|
@GetMapping("/recoveryShAddressBookSpotCheck")
|
||
|
|
@Scheduled(cron = "0 0 0 * * ?")
|
||
|
|
public void recoveryShAddressBookSpotCheck() {
|
||
|
|
boolean update = shAddressBookService.lambdaUpdate()
|
||
|
|
.set(ShAddressBook::getIsSpotCheck, 0)
|
||
|
|
.set(ShAddressBook::getCallStatus, 0)
|
||
|
|
.eq(ShAddressBook::getIsSpotCheck, 1)
|
||
|
|
.eq(ShAddressBook::getDel, 1)
|
||
|
|
.update();
|
||
|
|
log.info("恢复山洪负责人抽查状态定时任务执行结果: " + update);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|