2023-09-13 17:15:39 +08:00
|
|
|
package com.whdc.component;
|
|
|
|
|
|
2024-06-13 16:32:32 +08:00
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.whdc.model.dto.ApiDto;
|
|
|
|
|
import com.whdc.model.entity.QXWarning;
|
|
|
|
|
import com.whdc.model.vo.QXWarningVO;
|
|
|
|
|
import com.whdc.model.vo.WarningData;
|
|
|
|
|
import com.whdc.service.IQXWarningService;
|
|
|
|
|
import com.whdc.utils.DateUtils;
|
|
|
|
|
import com.whdc.utils.HttpUtil;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
2023-09-13 17:15:39 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-06-13 16:32:32 +08:00
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
|
|
import org.apache.commons.compress.utils.Lists;
|
2024-06-17 17:51:52 +08:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2024-06-13 16:32:32 +08:00
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2024-06-17 17:51:52 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2024-06-13 16:32:32 +08:00
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
2023-09-13 17:15:39 +08:00
|
|
|
import org.springframework.stereotype.Component;
|
2024-06-13 16:32:32 +08:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2023-09-13 17:15:39 +08:00
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
2024-06-19 15:44:47 +08:00
|
|
|
import java.time.LocalDateTime;
|
2024-06-13 16:32:32 +08:00
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
2023-09-13 17:15:39 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author 李赛
|
|
|
|
|
* @date 2022-07-17 15:33
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class MyPostConstruct {
|
|
|
|
|
|
2024-06-17 17:51:52 +08:00
|
|
|
@Value("${getGroupWarning}")
|
|
|
|
|
public String getGroupWarning;
|
2023-09-13 17:15:39 +08:00
|
|
|
@PostConstruct
|
|
|
|
|
public void initCache() {
|
|
|
|
|
log.debug("加载缓存");
|
2024-06-19 15:44:47 +08:00
|
|
|
|
|
|
|
|
// 预警初始化
|
|
|
|
|
QXWarningInit();
|
2023-09-13 17:15:39 +08:00
|
|
|
}
|
2024-06-13 16:32:32 +08:00
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IQXWarningService service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
|
@ApiOperation(value = "预警数据同步接口", notes = "预警数据同步接口")
|
|
|
|
|
@GetMapping("/syncData")
|
|
|
|
|
@Scheduled(cron ="0 0/1 * * * ?")
|
|
|
|
|
public void syncData() {
|
2024-06-17 17:51:52 +08:00
|
|
|
sync(null,null);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 15:44:47 +08:00
|
|
|
/**
|
|
|
|
|
* 预警初始化
|
|
|
|
|
*/
|
|
|
|
|
private void QXWarningInit(){
|
|
|
|
|
long count = service.count();
|
|
|
|
|
if (count > 0){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
sync(DateUtils.dateToStr(now.minusYears(1)),DateUtils.dateToStr(now));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 17:51:52 +08:00
|
|
|
public void sync(String stm,String etm) {
|
2024-06-13 16:32:32 +08:00
|
|
|
log.info("预警数据同步开始!!!");
|
|
|
|
|
ApiDto apiDto = new ApiDto();
|
2024-06-17 17:51:52 +08:00
|
|
|
if (StringUtils.isNotBlank(stm)){
|
2024-06-19 15:44:47 +08:00
|
|
|
log.info("开始时间 " + stm);
|
2024-06-17 17:51:52 +08:00
|
|
|
apiDto.setStartTime(stm);
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isNotBlank(etm)){
|
2024-06-19 15:44:47 +08:00
|
|
|
log.info("结束时间 " + etm);
|
2024-06-17 17:51:52 +08:00
|
|
|
apiDto.setEndTime(etm);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 16:32:32 +08:00
|
|
|
apiDto.setFilter(Lists.newArrayList());
|
|
|
|
|
|
2024-06-17 17:51:52 +08:00
|
|
|
String str = HttpUtil.sendPost(getGroupWarning, JSON.toJSONString(apiDto));
|
2024-06-13 16:32:32 +08:00
|
|
|
// 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());
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 17:15:39 +08:00
|
|
|
}
|