gunshi-project-ss/src/main/java/com/gunshi/project/xyt/timetask/DataTask.java

494 lines
22 KiB
Java
Raw Normal View History

package com.gunshi.project.xyt.timetask;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
2024-07-23 10:20:32 +08:00
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gunshi.project.xyt.entity.SkSyncData;
import com.gunshi.project.xyt.entity.SkSyncResp;
import com.gunshi.project.xyt.entity.SyncDataReq;
import com.gunshi.project.xyt.model.StImgR;
import com.gunshi.project.xyt.model.StImgRReal;
import com.gunshi.project.xyt.model.StPptnR;
import com.gunshi.project.xyt.model.StPptnRD;
import com.gunshi.project.xyt.model.StPptnRReal;
import com.gunshi.project.xyt.model.StRsvrR;
import com.gunshi.project.xyt.model.StRsvrRReal;
import com.gunshi.project.xyt.model.StStbprpB;
import com.gunshi.project.xyt.service.StImgRRealService;
import com.gunshi.project.xyt.service.StImgRService;
import com.gunshi.project.xyt.service.StPptnRDService;
import com.gunshi.project.xyt.service.StPptnRRealService;
import com.gunshi.project.xyt.service.StPptnRService;
import com.gunshi.project.xyt.service.StRsvrRRealService;
import com.gunshi.project.xyt.service.StRsvrRService;
import com.gunshi.project.xyt.service.StStbprpBService;
import com.gunshi.project.xyt.util.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import ru.olegcherednik.jackson_utils.JacksonUtils;
/**
* @author cxw
* @description:
* @classname DataTask.java
* @create 2024-07-11, , 14:19:22
*/
@EnableScheduling//开启定时任务
@Component
@Slf4j
2024-07-23 10:20:32 +08:00
@Profile("prod")
public class DataTask {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat sdfD = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat sdfH = new SimpleDateFormat("yyyy-MM-dd HH");
@Value("${owrsvrPath}")
private String owrsvrPath;// 获取水雨情、图像接口 http://owrsvr.cloudowr.cn/
@Value("${apiPath}")
private String apiPath;// 获取雨情按天接口
public static String hbskpprealstsRoute = "pubapi/hbsk/pprealsts";// 站点实时雨情
public static String hbskpphisRoute = "pubapi/hbsk/pphis";// 站点历史雨情
public static String hbskzzrealstsRoute = "pubapi/hbsk/zzrealsts";// 站点实时水情
public static String hbskzzhisRoute = "pubapi/hbsk/zzhis";// 站点历史水情
public static String hbskpicrealstsRoute = "pubapi/hbsk/picrealsts";// 站点实时图像
public static String hbskpichisRoute = "pubapi/hbsk/pichis";// 站点历史图像
// 水库站点
@Autowired
private StStbprpBService stStbprpBService;
// 实时雨情
@Autowired
private StPptnRRealService stPptnRRealService;
// 历史雨情
@Autowired
private StPptnRService stPptnRService;
// 按天雨情
@Autowired
private StPptnRDService stPptnRDService;
// 实时水情
@Autowired
private StRsvrRRealService stRsvrRRealService;
// 历史水情
@Autowired
private StRsvrRService stRsvrRService;
// 实时图像
@Autowired
private StImgRRealService stImgRRealService;
// 历史图像
@Autowired
private StImgRService stImgRService;
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-11, , 14:21:35
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkYqRealData() {
Date now = new Date();
System.out.println("雨情实时定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码
String sts = "[" + stStbprpBService.list(new QueryWrapper<StStbprpB>().eq("source", "SK")).stream().map(StStbprpB::getStcd).collect(Collectors.joining(",")) + "]";
OkHttpClient client = OkHttpUtil.build();
try {
Response resp = client.newCall(new Request.Builder().url(owrsvrPath + hbskpprealstsRoute)
.post(new FormBody.Builder().add("sts", sts).build())
.build()).execute();
String respStr = resp.body().string();
ObjectMapper om = new ObjectMapper();
Map map = om.readValue(respStr, Map.class);
List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("data");
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(list)){
for (Map map1 : list) {
StPptnRReal stPptnRReal = new ObjectMapper().convertValue(map1, StPptnRReal.class);
if(ObjectUtils.isEmpty(stPptnRReal.getChtm())){
stPptnRReal.setChtm(new Date());
}
UpdateWrapper<StPptnRReal> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("stcd", stPptnRReal.getStcd());
stPptnRRealService.saveOrUpdate(stPptnRReal, updateWrapper);
}
}
}
catch (IOException e)
{
log.error("雨情实时定时任务错误:", e.getMessage());
}
}
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-11, , 14:21:35
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkYqHisData() {
Date now = new Date();
System.out.println("雨情历史定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码历史雨情表中站点最新数据时间
List<StPptnR> stcdLast = stPptnRService.getStcdLastPptnData();
OkHttpClient client = OkHttpUtil.build();
try {
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(stcdLast)){
for(StPptnR stPptnR : stcdLast){
Date stm = stPptnR.getStm();
Calendar calendar = Calendar.getInstance();
2024-07-31 14:20:26 +08:00
// 默认40天前接口最多40天存在则加1小时接口是按小时算且大于等于开始时间
2024-07-23 10:10:17 +08:00
if(ObjectUtils.isEmpty(stm)){
calendar.add(Calendar.DATE, -40);
} else {
2024-07-31 14:20:26 +08:00
calendar.add(Calendar.HOUR, 1);
2024-07-23 10:10:17 +08:00
}
stm = calendar.getTime();
Response resp = client.newCall(new Request.Builder().url(owrsvrPath + hbskpphisRoute)
.post(new FormBody.Builder().add("stcd", stPptnR.getStcd())
.add("stm", sdfH.format(stm))
.add("etm", sdfH.format(now))
.build())
.build()).execute();
String respStr = resp.body().string();
ObjectMapper om = new ObjectMapper();
Map map = om.readValue(respStr, Map.class);
if("400".equals(map.get("code").toString())){
continue;
}
List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("data");
if(CollectionUtils.isNotEmpty(list)){
List<StPptnR> rlist = JSONObject.parseArray(JSONObject.toJSONString(list)).toJavaList(StPptnR.class);
stPptnRService.saveBatch(rlist);
}
}
}
}
catch (IOException e)
{
log.error("雨情历史定时任务错误:", e.getMessage());
}
}
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-11, , 14:21:35
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkYqDayData() {
Date now = new Date();
System.out.println("雨情按天定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码按天雨情表中站点最新数据时间
List<StPptnRD> stcdLast = stPptnRDService.getStcdLastPptnDayData();
OkHttpClient client = OkHttpUtil.build();
try {
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(stcdLast)){
for(StPptnRD stPptnRD : stcdLast){
String stcd = stPptnRD.getStcd();
Date stm = stPptnRD.getStm();
Calendar calendar = Calendar.getInstance();
2024-07-31 14:20:26 +08:00
// 默认两周前的时间接口只提供addvcd的条件数据量较大如果存在需要加一天接口是按天算且大于等于开始时间
2024-07-23 10:10:17 +08:00
if(ObjectUtils.isEmpty(stm)){
calendar.add(Calendar.DATE, -14);
} else {
calendar.add(Calendar.DATE, 1);
}
stm = calendar.getTime();
MediaType mediaType = MediaType.parse("application/json");
SyncDataReq syncDataReq = new SyncDataReq();
syncDataReq.setAdcd(stPptnRD.getAddvcd());
syncDataReq.setType("SK");
syncDataReq.setStartDate(sdfD.format(stm));
syncDataReq.setEndDate(sdfD.format(now));
RequestBody body = RequestBody.create(JacksonUtils.writeValue(syncDataReq), mediaType);
Response resp = client.newCall(new Request.Builder().url(apiPath)
.post(body)
.build()).execute();
String respStr = resp.body().string();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SkSyncResp resq = objectMapper.readValue(respStr, SkSyncResp.class);
SkSyncData data = resq.getData();
List<StPptnRD> stPptnRDList = data.getStPptnRD();
if(CollectionUtils.isNotEmpty(stPptnRDList)) {
Iterator<StPptnRD> iterator = stPptnRDList.iterator();
while (iterator.hasNext()) {
StPptnRD stPptnRD1 = iterator.next();
if (!stcd.equals(stPptnRD1.getStcd())) {
iterator.remove();
continue;
}
stPptnRD1.setYear(Integer.valueOf(sdfD.format(stPptnRD1.getTm()).substring(0, 4)));
}
2024-07-23 10:10:17 +08:00
stPptnRDService.saveBatch(stPptnRDList);
}
}
}
}
catch (IOException e)
{
log.error("雨情按天定时任务错误:", e.getMessage());
}
}
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-11, , 14:21:35
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkSqRealData() {
Date now = new Date();
System.out.println("水情实时定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码
String sts = "[" + stStbprpBService.list(new QueryWrapper<StStbprpB>().eq("source", "SK")).stream().map(StStbprpB::getStcd).collect(Collectors.joining(",")) + "]";
OkHttpClient client = OkHttpUtil.build();
try {
Response resp = client.newCall(new Request.Builder().url(owrsvrPath + hbskzzrealstsRoute)
.post(new FormBody.Builder().add("sts", sts).build())
.build()).execute();
String respStr = resp.body().string();
ObjectMapper om = new ObjectMapper();
Map map = om.readValue(respStr, Map.class);
List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("data");
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(list)){
for (Map map1 : list) {
StRsvrRReal stRsvrRReal = new ObjectMapper().convertValue(map1, StRsvrRReal.class);
if(ObjectUtils.isEmpty(stRsvrRReal.getChtm())){
stRsvrRReal.setChtm(new Date());
}
UpdateWrapper<StRsvrRReal> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("stcd", stRsvrRReal.getStcd());
stRsvrRRealService.saveOrUpdate(stRsvrRReal, updateWrapper);
}
}
}
catch (IOException e)
{
log.error("水情实时定时任务错误:", e.getMessage());
}
}
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-11, , 14:21:35
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkSqHisData() {
Date now = new Date();
System.out.println("水情历史定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码历史水情表中站点最新数据时间
List<StRsvrR> stcdLast = stRsvrRService.getStcdLastRsvrData();
OkHttpClient client = OkHttpUtil.build();
try {
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(stcdLast)){
for(StRsvrR stRsvrR : stcdLast){
Date stm = stRsvrR.getStm();
Calendar calendar = Calendar.getInstance();
2024-07-31 14:20:26 +08:00
// 默认40天前接口最多40天存在则加1小时接口是按小时算且大于等于开始时间
2024-07-23 10:10:17 +08:00
if(ObjectUtils.isEmpty(stm)){
calendar.add(Calendar.DATE, -40);
} else {
2024-07-31 14:20:26 +08:00
calendar.add(Calendar.HOUR, 1);
2024-07-23 10:10:17 +08:00
}
stm = calendar.getTime();
Response resp = client.newCall(new Request.Builder().url(owrsvrPath + hbskzzhisRoute)
.post(new FormBody.Builder().add("stcd", stRsvrR.getStcd())
.add("stm", sdfH.format(stm))
.add("etm", sdfH.format(now))
.build())
.build()).execute();
String respStr = resp.body().string();
ObjectMapper om = new ObjectMapper();
Map map = om.readValue(respStr, Map.class);
if("400".equals(map.get("code").toString())){
continue;
}
List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("data");
if(CollectionUtils.isNotEmpty(list)){
List<StRsvrR> rlist = JSONObject.parseArray(JSONObject.toJSONString(list)).toJavaList(StRsvrR.class);
stRsvrRService.saveBatch(rlist);
}
}
}
}
catch (IOException e)
{
log.error("水情历史定时任务错误:", e.getMessage());
}
}
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-22, , 13:54:08
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkImgRealData() {
Date now = new Date();
System.out.println("图像实时图像定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码
String sts = "[" + stStbprpBService.list(new QueryWrapper<StStbprpB>().eq("source", "SK")).stream().map(StStbprpB::getStcd).collect(Collectors.joining(",")) + "]";
OkHttpClient client = OkHttpUtil.build();
try {
Response resp = client.newCall(new Request.Builder().url(owrsvrPath + hbskpicrealstsRoute)
.post(new FormBody.Builder().add("sts", sts).build())
.build()).execute();
String respStr = resp.body().string();
ObjectMapper om = new ObjectMapper();
Map map = om.readValue(respStr, Map.class);
List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("data");
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(list)){
for (Map map1 : list) {
StImgRReal stImgRReal = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).setDateFormat(sdf).convertValue(map1, StImgRReal.class);
if(ObjectUtils.isEmpty(stImgRReal.getChtm())){
stImgRReal.setChtm(new Date());
}
if(ObjectUtils.isEmpty(stImgRReal.getChid())){
stImgRReal.setChid(map1.get("machineid").toString());
}
if(ObjectUtils.isEmpty(stImgRReal.getImgPath())){
stImgRReal.setImgPath(map1.get("url").toString());
}
UpdateWrapper<StImgRReal> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("stcd", stImgRReal.getStcd()).eq("chid", stImgRReal.getChid());
stImgRRealService.saveOrUpdate(stImgRReal, updateWrapper);
}
}
}
catch (IOException e)
{
log.error("图像实时定时任务错误:", e.getMessage());
}
}
/**
* @description:
* @param
* @return: void
* @auther: cxw
* @date: 2024-07-22, , 13:55:45
*/
@Async
@Scheduled(fixedRate = 5, timeUnit = TimeUnit.MINUTES)
public void getSkImgHisData() {
Date now = new Date();
System.out.println("图像历史定时任务,执行时间:" + sdf.format(now));
// 获取水库站点编码历史图像表中站点最新数据时间
List<StImgR> stcdLast = stImgRService.getStcdLastImgData();
OkHttpClient client = OkHttpUtil.build();
try {
2024-07-23 10:10:17 +08:00
if(CollectionUtils.isNotEmpty(stcdLast)){
for (StImgR stImgR : stcdLast) {
Date stm = stImgR.getStm();
Calendar calendar = Calendar.getInstance();
2024-07-31 14:20:26 +08:00
// 默认40天前接口最多40天存在则加1小时接口是按小时算且大于等于开始时间
2024-07-23 10:10:17 +08:00
if(ObjectUtils.isEmpty(stm)){
calendar.add(Calendar.DATE, -40);
} else {
2024-07-31 14:20:26 +08:00
calendar.add(Calendar.HOUR, 1);
2024-07-23 10:10:17 +08:00
}
stm = calendar.getTime();
Response resp = client.newCall(new Request.Builder().url(owrsvrPath + hbskpichisRoute)
.post(new FormBody.Builder().add("stcd", stImgR.getStcd())
.add("stm", sdfH.format(stm))
.add("etm", sdfH.format(now))
.build())
.build()).execute();
String respStr = resp.body().string();
ObjectMapper om = new ObjectMapper();
Map map = om.readValue(respStr, Map.class);
if ("400".equals(map.get("code").toString())) {
continue;
}
List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("data");
if (CollectionUtils.isNotEmpty(list)) {
List<StImgR> rlist = list.stream().map(map1 -> {
StImgR stImgRNew = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).setDateFormat(sdf).convertValue(map1, StImgR.class);
// 手动设置对应属性
if(ObjectUtils.isEmpty(stImgRNew.getChtm())){
stImgRNew.setChtm(new Date());
}
if(ObjectUtils.isEmpty(stImgRNew.getChid())){
stImgRNew.setChid(map1.get("machineid").toString());
}
if(ObjectUtils.isEmpty(stImgRNew.getImgPath())){
stImgRNew.setImgPath(map1.get("url").toString());
}
return stImgRNew;
}).collect(Collectors.toList());
stImgRService.saveBatch(rlist);
}
}
}
}
catch (IOException e)
{
log.error("图像历史定时任务错误:", e.getMessage());
}
}
}