update city list

master
修改密码漏洞修复完成 6 months ago
parent 8679be33df
commit a5b1ac31dc

@ -0,0 +1,222 @@
package com.rehome.jpahefengweather.service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liuhuiyu.spring_util.SpringUtil;
import com.rehome.jpahefengweather.dto.NmcBaseDto;
import com.rehome.jpahefengweather.entity.*;
import com.rehome.jpahefengweather.service.excel.*;
import com.rehome.jpahefengweather.utils.JwtUtils;
import com.rehome.jpahefengweather.utils.OkHttpUtil;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author huangwenfei
* @version v1.0.0.0
* Created DateTime 2021-04-26 14:35
* @description:
*/
//@Component
public class NmcWeatherScheduledService {
@Value("${rehome.resources-path}")
private String resourcesPath;
@Resource
private CityService cityService;
@Resource
private WeatherService weatherService;
@Resource
private NmcCityService nmcCityService;
@Resource
private NmcWeatherService nmcWeatherService;
@Resource
private NmcStormService nmcStormService;
@Resource
private ZjsltStormService zjsltStormService;
@Resource
private WztfStormService wztfStormService;
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 29 * * * *")
public void getNmcWeatherProvince() {
System.out.println("scheduledGetWeather");
System.out.println("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println(String.valueOf(System.currentTimeMillis()));
String url = "http://www.nmc.cn/rest/province/all?_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
try {
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
Gson gson = new Gson();
List<NmcProvince> provinces = gson.fromJson(body, new TypeToken<List<NmcProvince>>() {
}.getType());
;
for (NmcProvince province : provinces) {
System.out.println(gson.toJson(province));
this.nmcCityService.saveProvince(province);
getNmcWeatherCityByCode(province.getCode());
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(provinces.size());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description: code
* @Param: null
*/
public void getNmcWeatherCityByCode(String code) {
String url = "http://www.nmc.cn/rest/province/" + code + "?_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
try {
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
Gson gson = new Gson();
List<NmcCity> citys = gson.fromJson(body, new TypeToken<List<NmcCity>>() {
}.getType());
;
for (NmcCity city : citys) {
System.out.println(gson.toJson(city));
this.nmcCityService.saveCity(city);
}
System.out.println(citys.size());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 15 * * * *")
public void getNmcNowWeather() {
System.out.println("scheduledGetWeather");
System.out.println("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println(String.valueOf(System.currentTimeMillis()));
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
List<NmcCity> citys = this.nmcCityService.findAllCityList();
for (NmcCity city : citys) {
String url = "http://www.nmc.cn/rest/weather?stationid=" + city.getCode() + "&_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
try {
Gson gson = new Gson();
NmcBaseDto nmcBaseDto = gson.fromJson(body, NmcBaseDto.class);
if (nmcBaseDto != null && nmcBaseDto.getCode() == 0) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nowDate = sdf.format(new Date());
NmcNowWeather nowWeather = new NmcNowWeather();
nowWeather.setCode(city.getCode());
nowWeather.setWeather(body);
nowWeather.setWeatherDate(nowDate);
nowWeather.setLastUpdateDate(now);
nowWeather.setCreateDate(now);
NmcWeatherService nmcWeatherServiceTemp = SpringUtil.getBean(NmcWeatherService.class);
nmcWeatherServiceTemp.saveNowWeather(nowWeather);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
}

@ -51,12 +51,11 @@ public class ScheduledService {
private WztfStormService wztfStormService;
// @Resource
// private UserService userService;
/**
* @date 2022-03-14 15:57
* @description:
* @description: , -
* https://github.com/qwd/LocationList
* https://dev.qweather.com/docs/start/
* @Param: null
*/
//@Scheduled(cron = "0 32 * * * *")//每个小时执行一次
@ -128,9 +127,10 @@ public class ScheduledService {
String jwt = JwtUtils.getJwt();
System.out.println(jwt);
}
/**
* @date 2022-03-16 09:41
* @description: , OkHttpClient
* @description: , api - OkHttpClient
* @Param: null
*/
//@Scheduled(cron = "0 0 10,14,18,20,22 * * *")//每天1014182022点各执行一次一天总共执行五次
@ -185,7 +185,7 @@ public class ScheduledService {
/**
* @date 2022-03-16 09:41
* @description: , 7
* @description: , api - 7
* @Param: null
*/
//@Scheduled(cron = "0 0 9 * * *")//每天9点执行一次
@ -451,516 +451,10 @@ public class ScheduledService {
});
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 29 * * * *")
public void getNmcWeatherProvince() {
System.out.println("scheduledGetWeather");
System.out.println("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println(String.valueOf(System.currentTimeMillis()));
String url = "http://www.nmc.cn/rest/province/all?_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
try {
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
Gson gson = new Gson();
List<NmcProvince> provinces = gson.fromJson(body, new TypeToken<List<NmcProvince>>() {
}.getType());
;
for (NmcProvince province : provinces) {
System.out.println(gson.toJson(province));
this.nmcCityService.saveProvince(province);
getNmcWeatherCityByCode(province.getCode());
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(provinces.size());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description: code
* @Param: null
*/
public void getNmcWeatherCityByCode(String code) {
String url = "http://www.nmc.cn/rest/province/" + code + "?_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
try {
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
Gson gson = new Gson();
List<NmcCity> citys = gson.fromJson(body, new TypeToken<List<NmcCity>>() {
}.getType());
;
for (NmcCity city : citys) {
System.out.println(gson.toJson(city));
this.nmcCityService.saveCity(city);
}
System.out.println(citys.size());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 15 * * * *")
public void getNmcNowWeather() {
System.out.println("scheduledGetWeather");
System.out.println("=====>>>>>使用cron:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println(String.valueOf(System.currentTimeMillis()));
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
List<NmcCity> citys = this.nmcCityService.findAllCityList();
for (NmcCity city : citys) {
String url = "http://www.nmc.cn/rest/weather?stationid=" + city.getCode() + "&_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
try {
Gson gson = new Gson();
NmcBaseDto nmcBaseDto = gson.fromJson(body, NmcBaseDto.class);
if (nmcBaseDto != null && nmcBaseDto.getCode() == 0) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String nowDate = sdf.format(new Date());
NmcNowWeather nowWeather = new NmcNowWeather();
nowWeather.setCode(city.getCode());
nowWeather.setWeather(body);
nowWeather.setWeatherDate(nowDate);
nowWeather.setLastUpdateDate(now);
nowWeather.setCreateDate(now);
NmcWeatherService nmcWeatherServiceTemp = SpringUtil.getBean(NmcWeatherService.class);
nmcWeatherServiceTemp.saveNowWeather(nowWeather);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
// @Scheduled(cron = "0 48 * * * *")
// public void getZjsltHistoryStormList() {
// for(int i=1940;i<2025;i++){
// getZjsltStormList(String.valueOf(i));
// }
// }
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 30 * * * *")
public void getZjsltStormList() {
System.out.println("getZjsltStormList");
String strNow = String.valueOf(System.currentTimeMillis());
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
Thread.sleep(5000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
String nowYear = sdfYear.format(new Date());
Random random = new Random();//以系统自身时间为种子数
int iRandom = random.nextInt();
Long nowLong = 13728353350362976L;
Long nowLongTemp = nowLong + iRandom;
String url = "https://typhoon.slt.zj.gov.cn/Api/TyphoonList/" + nowYear + "?callback=jQuery" + "1830" + String.valueOf(nowLongTemp) + "_" + strNow + "&_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
//System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 0) {
Gson gson = new Gson();
int startIndex = body.indexOf("[");
int endIndex = body.lastIndexOf("]");
// System.out.println(body.indexOf("["));
// System.out.println(body.lastIndexOf("]"));
String stormStr = body.substring(startIndex, endIndex + 1);
//System.out.println(stormStr);
if (stormStr != null && stormStr.length() > 2) {
Type type = new TypeToken<ArrayList<ZjsltStorm>>() {
}.getType();
List<ZjsltStorm> stormList = gson.fromJson(stormStr, type);
//打印测试
for (ZjsltStorm storm : stormList) {
storm.setYear(nowYear);
storm.setCreateDate(new Date());
System.out.println(gson.toJson(storm));
ZjsltStorm stormDB = this.zjsltStormService.findByTfid(storm.getTfid());
if (stormDB == null) {
//数据库无数据,第一次插入,同时从第三方平台拉取台风路径数据入库
this.zjsltStormService.saveZjsltStorm(storm);
this.saveOrUpdateTyphoonInfo(storm);
} else {
//数据库有数据
if (stormDB.getIsactive().equals("1")) {
if (storm.getIsactive().equals("0")) {
//数据库里面台风数据是活跃状态,第三方平台台风数据已变成不活跃状态,更新数据库台风状态,同时更新台风路径
stormDB.setIsactive(storm.getIsactive());
this.zjsltStormService.saveZjsltStorm(stormDB);
this.saveOrUpdateTyphoonInfo(stormDB);
} else {
//数据库里面台风数据是活跃状态,第三方平台台风数据也是活跃状态
if ((!storm.getEndtime().equals(stormDB.getEndtime())) || (!storm.getEnname().equals(stormDB.getEnname())) || (!storm.getName().equals(stormDB.getName()))) {
//台风列表数据有变化,更新台风列表数据
stormDB.setEndtime(storm.getEndtime());
stormDB.setEnname(storm.getEnname());
stormDB.setName(storm.getName());
this.zjsltStormService.saveZjsltStorm(stormDB);
}
//更新台风路径
this.saveOrUpdateTyphoonInfo(stormDB);
}
} else {
//数据库里面台风数据是不活跃状态,无需做任何事情
//this.saveOrUpdateTyphoonInfo(storm);
}
}
}
}
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
public void saveOrUpdateTyphoonInfo(ZjsltStorm storm) {
System.out.println("saveOrUpdateTyphoonInfo");
String strNow = String.valueOf(System.currentTimeMillis());
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
String nowYear = sdfYear.format(new Date());
Random random = new Random();//以系统自身时间为种子数
int iRandom = random.nextInt();
Long nowLong = 25528353350362976L;
Long nowLongTemp = nowLong + iRandom;
String url = "https://typhoon.slt.zj.gov.cn/Api/TyphoonInfo/" + storm.getTfid() + "?callback=jQuery" + "1830" + String.valueOf(nowLongTemp) + "_" + strNow + "&_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
//System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 0) {
Gson gson = new Gson();
int startIndex = body.indexOf("[");
int endIndex = body.lastIndexOf("]");
// System.out.println(body.indexOf("["));
// System.out.println(body.lastIndexOf("]"));
String typhoonInfoArryStr = body.substring(startIndex, endIndex + 1);
//System.out.println(typhoonInfoArryStr);
if (typhoonInfoArryStr != null && typhoonInfoArryStr.length() > 2) {
int startIndex0 = typhoonInfoArryStr.indexOf("{");
int endIndex0 = typhoonInfoArryStr.lastIndexOf("}");
// System.out.println(startIndex0);
// System.out.println(endIndex0);
String typhoonInfoStr = typhoonInfoArryStr.substring(startIndex0, endIndex0 + 1);
//System.out.println(typhoonInfoStr);
TyphoonInfo typhoonInfoDB = this.zjsltStormService.findTyphoonInfoByTfid(storm.getTfid());
TyphoonInfo typhoonInfo = new TyphoonInfo();
if (typhoonInfoDB == null) {
//数据库无数据,保存风路径数据入库
typhoonInfo.setTfid(storm.getTfid());
typhoonInfo.setStormData(typhoonInfoStr);
typhoonInfo.setCreateDate(new Date());
typhoonInfo.setLastUpdateDate(new Date());
this.zjsltStormService.saveTyphoonInfo(typhoonInfo);
} else {
//数据库有数据,更新台风路径数据
typhoonInfoDB.setStormData(typhoonInfoStr);
typhoonInfoDB.setLastUpdateDate(new Date());
this.zjsltStormService.saveTyphoonInfo(typhoonInfoDB);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 30 * * * *")
public void getWztfHistoryStormList() {
for(int i=1945;i<2025;i++){
getWztfStormList(String.valueOf(i));
}
//getWztfStormList(String.valueOf(1944));
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 15 * * * *")
public void getWztfStormList(String nowYear) {
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
Thread.sleep(3000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
//String nowYear = sdfYear.format(new Date());
//nowYear = String.valueOf(i);
String url = "http://data.istrongcloud.com/v2/data/complex/" + nowYear + ".json?v=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 2) {
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<WztfStorm>>() {
}.getType();
List<WztfStorm> stormList = gson.fromJson(body, type);
//打印测试
for (WztfStorm storm : stormList) {
storm.setYear(nowYear);
storm.setCreateDate(new Date());
//System.out.println(gson.toJson(storm));
WztfStorm stormDB = this.wztfStormService.findByTfbh(storm.getTfbh());
if (stormDB == null) {
//数据库无数据,第一次插入,同时从第三方平台拉取台风路径数据入库
this.wztfStormService.saveWztfStorm(storm);
this.saveOrUpdateWztfStormInfo(storm);
} else {
//数据库有数据
if (stormDB.getIs_current() == 1) {
if (storm.getIs_current() == 0) {
//数据库里面台风数据是活跃状态,第三方平台台风数据已变成不活跃状态,更新数据库台风状态,同时更新台风路径
stormDB.setIs_current(storm.getIs_current());
this.wztfStormService.saveWztfStorm(stormDB);
this.saveOrUpdateWztfStormInfo(stormDB);
} else {
//数据库里面台风数据是活跃状态,第三方平台台风数据也是活跃状态
if ((!storm.getEnd_time().equals(stormDB.getEnd_time())) || (!storm.getEname().equals(stormDB.getEname())) || (!storm.getName().equals(stormDB.getName())) || (!storm.getIdent().equals(stormDB.getIdent()))) {
//台风列表数据有变化,更新台风列表数据
stormDB.setEnd_time(storm.getEnd_time());
stormDB.setEname(storm.getEname());
stormDB.setName(storm.getName());
stormDB.setIdent(storm.getIdent());
this.wztfStormService.saveWztfStorm(stormDB);
}
//更新台风路径
this.saveOrUpdateWztfStormInfo(stormDB);
}
} else {
//数据库里面台风数据是不活跃状态,无需做任何事情
//this.saveOrUpdateWztfStormInfo(storm);
}
}
}
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
public void saveOrUpdateWztfStormInfo(WztfStorm storm) {
System.out.println("saveOrUpdateTyphoonInfo");
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
String nowYear = sdfYear.format(new Date());
String url = "http://data.istrongcloud.com/v2/data/complex/" + storm.getTfbh() + ".json?v=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
//System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 2) {
Gson gson = new Gson();
int startIndex0 = body.indexOf("{");
int endIndex0 = body.lastIndexOf("}");
String typhoonInfoStr = body.substring(startIndex0, endIndex0 + 1);
//System.out.println(typhoonInfoStr);
WztfStormInfo wztfStormInfoDB = this.wztfStormService.findWztfStormInfoByTfbh(storm.getTfbh());
WztfStormInfo wztfStormInfo = new WztfStormInfo();
if (wztfStormInfoDB == null) {
//数据库无数据,保存风路径数据入库
wztfStormInfo.setTfbh(storm.getTfbh());
wztfStormInfo.setStormData(typhoonInfoStr);
wztfStormInfo.setCreateDate(new Date());
wztfStormInfo.setLastUpdateDate(new Date());
this.wztfStormService.saveWztfStormInfo(wztfStormInfo);
} else {
//数据库有数据,更新台风路径数据
wztfStormInfoDB.setStormData(typhoonInfoStr);
wztfStormInfoDB.setLastUpdateDate(new Date());
this.wztfStormService.saveWztfStormInfo(wztfStormInfoDB);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,195 @@
package com.rehome.jpahefengweather.service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liuhuiyu.spring_util.SpringUtil;
import com.rehome.jpahefengweather.dto.NmcBaseDto;
import com.rehome.jpahefengweather.entity.*;
import com.rehome.jpahefengweather.service.excel.*;
import com.rehome.jpahefengweather.utils.JwtUtils;
import com.rehome.jpahefengweather.utils.OkHttpUtil;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author huangwenfei
* @version v1.0.0.0
* Created DateTime 2021-04-26 14:35
* @description:
*/
//@Component
public class WztfwScheduledService {
@Resource
private WztfStormService wztfStormService;
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 30 * * * *")
public void getWztfHistoryStormList() {
for(int i=1945;i<2025;i++){
getWztfStormList(String.valueOf(i));
}
//getWztfStormList(String.valueOf(1944));
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 15 * * * *")
public void getWztfStormList(String nowYear) {
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
Thread.sleep(3000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
//String nowYear = sdfYear.format(new Date());
//nowYear = String.valueOf(i);
String url = "http://data.istrongcloud.com/v2/data/complex/" + nowYear + ".json?v=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 2) {
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<WztfStorm>>() {
}.getType();
List<WztfStorm> stormList = gson.fromJson(body, type);
//打印测试
for (WztfStorm storm : stormList) {
storm.setYear(nowYear);
storm.setCreateDate(new Date());
//System.out.println(gson.toJson(storm));
WztfStorm stormDB = this.wztfStormService.findByTfbh(storm.getTfbh());
if (stormDB == null) {
//数据库无数据,第一次插入,同时从第三方平台拉取台风路径数据入库
this.wztfStormService.saveWztfStorm(storm);
this.saveOrUpdateWztfStormInfo(storm);
} else {
//数据库有数据
if (stormDB.getIs_current() == 1) {
if (storm.getIs_current() == 0) {
//数据库里面台风数据是活跃状态,第三方平台台风数据已变成不活跃状态,更新数据库台风状态,同时更新台风路径
stormDB.setIs_current(storm.getIs_current());
this.wztfStormService.saveWztfStorm(stormDB);
this.saveOrUpdateWztfStormInfo(stormDB);
} else {
//数据库里面台风数据是活跃状态,第三方平台台风数据也是活跃状态
if ((!storm.getEnd_time().equals(stormDB.getEnd_time())) || (!storm.getEname().equals(stormDB.getEname())) || (!storm.getName().equals(stormDB.getName())) || (!storm.getIdent().equals(stormDB.getIdent()))) {
//台风列表数据有变化,更新台风列表数据
stormDB.setEnd_time(storm.getEnd_time());
stormDB.setEname(storm.getEname());
stormDB.setName(storm.getName());
stormDB.setIdent(storm.getIdent());
this.wztfStormService.saveWztfStorm(stormDB);
}
//更新台风路径
this.saveOrUpdateWztfStormInfo(stormDB);
}
} else {
//数据库里面台风数据是不活跃状态,无需做任何事情
//this.saveOrUpdateWztfStormInfo(storm);
}
}
}
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
public void saveOrUpdateWztfStormInfo(WztfStorm storm) {
System.out.println("saveOrUpdateTyphoonInfo");
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
String nowYear = sdfYear.format(new Date());
String url = "http://data.istrongcloud.com/v2/data/complex/" + storm.getTfbh() + ".json?v=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
//System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 2) {
Gson gson = new Gson();
int startIndex0 = body.indexOf("{");
int endIndex0 = body.lastIndexOf("}");
String typhoonInfoStr = body.substring(startIndex0, endIndex0 + 1);
//System.out.println(typhoonInfoStr);
WztfStormInfo wztfStormInfoDB = this.wztfStormService.findWztfStormInfoByTfbh(storm.getTfbh());
WztfStormInfo wztfStormInfo = new WztfStormInfo();
if (wztfStormInfoDB == null) {
//数据库无数据,保存风路径数据入库
wztfStormInfo.setTfbh(storm.getTfbh());
wztfStormInfo.setStormData(typhoonInfoStr);
wztfStormInfo.setCreateDate(new Date());
wztfStormInfo.setLastUpdateDate(new Date());
this.wztfStormService.saveWztfStormInfo(wztfStormInfo);
} else {
//数据库有数据,更新台风路径数据
wztfStormInfoDB.setStormData(typhoonInfoStr);
wztfStormInfoDB.setLastUpdateDate(new Date());
this.wztfStormService.saveWztfStormInfo(wztfStormInfoDB);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,219 @@
package com.rehome.jpahefengweather.service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liuhuiyu.spring_util.SpringUtil;
import com.rehome.jpahefengweather.dto.NmcBaseDto;
import com.rehome.jpahefengweather.entity.*;
import com.rehome.jpahefengweather.service.excel.*;
import com.rehome.jpahefengweather.utils.JwtUtils;
import com.rehome.jpahefengweather.utils.OkHttpUtil;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author huangwenfei
* @version v1.0.0.0
* Created DateTime 2021-04-26 14:35
* @description:
*/
//@Component
public class ZjsltStormScheduledService {
@Resource
private ZjsltStormService zjsltStormService;
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
// @Scheduled(cron = "0 48 * * * *")
// public void getZjsltHistoryStormList() {
// for(int i=1940;i<2025;i++){
// getZjsltStormList(String.valueOf(i));
// }
// }
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
//@Scheduled(cron = "0 30 * * * *")
public void getZjsltStormList() {
System.out.println("getZjsltStormList");
String strNow = String.valueOf(System.currentTimeMillis());
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
Thread.sleep(5000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
String nowYear = sdfYear.format(new Date());
Random random = new Random();//以系统自身时间为种子数
int iRandom = random.nextInt();
Long nowLong = 13728353350362976L;
Long nowLongTemp = nowLong + iRandom;
String url = "https://typhoon.slt.zj.gov.cn/Api/TyphoonList/" + nowYear + "?callback=jQuery" + "1830" + String.valueOf(nowLongTemp) + "_" + strNow + "&_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
//System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 0) {
Gson gson = new Gson();
int startIndex = body.indexOf("[");
int endIndex = body.lastIndexOf("]");
// System.out.println(body.indexOf("["));
// System.out.println(body.lastIndexOf("]"));
String stormStr = body.substring(startIndex, endIndex + 1);
//System.out.println(stormStr);
if (stormStr != null && stormStr.length() > 2) {
Type type = new TypeToken<ArrayList<ZjsltStorm>>() {
}.getType();
List<ZjsltStorm> stormList = gson.fromJson(stormStr, type);
//打印测试
for (ZjsltStorm storm : stormList) {
storm.setYear(nowYear);
storm.setCreateDate(new Date());
System.out.println(gson.toJson(storm));
ZjsltStorm stormDB = this.zjsltStormService.findByTfid(storm.getTfid());
if (stormDB == null) {
//数据库无数据,第一次插入,同时从第三方平台拉取台风路径数据入库
this.zjsltStormService.saveZjsltStorm(storm);
this.saveOrUpdateTyphoonInfo(storm);
} else {
//数据库有数据
if (stormDB.getIsactive().equals("1")) {
if (storm.getIsactive().equals("0")) {
//数据库里面台风数据是活跃状态,第三方平台台风数据已变成不活跃状态,更新数据库台风状态,同时更新台风路径
stormDB.setIsactive(storm.getIsactive());
this.zjsltStormService.saveZjsltStorm(stormDB);
this.saveOrUpdateTyphoonInfo(stormDB);
} else {
//数据库里面台风数据是活跃状态,第三方平台台风数据也是活跃状态
if ((!storm.getEndtime().equals(stormDB.getEndtime())) || (!storm.getEnname().equals(stormDB.getEnname())) || (!storm.getName().equals(stormDB.getName()))) {
//台风列表数据有变化,更新台风列表数据
stormDB.setEndtime(storm.getEndtime());
stormDB.setEnname(storm.getEnname());
stormDB.setName(storm.getName());
this.zjsltStormService.saveZjsltStorm(stormDB);
}
//更新台风路径
this.saveOrUpdateTyphoonInfo(stormDB);
}
} else {
//数据库里面台风数据是不活跃状态,无需做任何事情
//this.saveOrUpdateTyphoonInfo(storm);
}
}
}
}
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* @date 2022-03-16 09:41
* @description:
* @Param: null
*/
public void saveOrUpdateTyphoonInfo(ZjsltStorm storm) {
System.out.println("saveOrUpdateTyphoonInfo");
String strNow = String.valueOf(System.currentTimeMillis());
// 初始化 OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
String nowDate = sdf.format(new Date());
String nowYear = sdfYear.format(new Date());
Random random = new Random();//以系统自身时间为种子数
int iRandom = random.nextInt();
Long nowLong = 25528353350362976L;
Long nowLongTemp = nowLong + iRandom;
String url = "https://typhoon.slt.zj.gov.cn/Api/TyphoonInfo/" + storm.getTfid() + "?callback=jQuery" + "1830" + String.valueOf(nowLongTemp) + "_" + strNow + "&_=" + String.valueOf(System.currentTimeMillis());
System.out.println(url);
// 初始化请求体
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 得到返回Response
Response response = client.newCall(request).execute();
String body = response.body().string();
//System.out.println(body);
if (StringUtils.hasText(body)) {
if (body.length() > 0) {
Gson gson = new Gson();
int startIndex = body.indexOf("[");
int endIndex = body.lastIndexOf("]");
// System.out.println(body.indexOf("["));
// System.out.println(body.lastIndexOf("]"));
String typhoonInfoArryStr = body.substring(startIndex, endIndex + 1);
//System.out.println(typhoonInfoArryStr);
if (typhoonInfoArryStr != null && typhoonInfoArryStr.length() > 2) {
int startIndex0 = typhoonInfoArryStr.indexOf("{");
int endIndex0 = typhoonInfoArryStr.lastIndexOf("}");
// System.out.println(startIndex0);
// System.out.println(endIndex0);
String typhoonInfoStr = typhoonInfoArryStr.substring(startIndex0, endIndex0 + 1);
//System.out.println(typhoonInfoStr);
TyphoonInfo typhoonInfoDB = this.zjsltStormService.findTyphoonInfoByTfid(storm.getTfid());
TyphoonInfo typhoonInfo = new TyphoonInfo();
if (typhoonInfoDB == null) {
//数据库无数据,保存风路径数据入库
typhoonInfo.setTfid(storm.getTfid());
typhoonInfo.setStormData(typhoonInfoStr);
typhoonInfo.setCreateDate(new Date());
typhoonInfo.setLastUpdateDate(new Date());
this.zjsltStormService.saveTyphoonInfo(typhoonInfo);
} else {
//数据库有数据,更新台风路径数据
typhoonInfoDB.setStormData(typhoonInfoStr);
typhoonInfoDB.setLastUpdateDate(new Date());
this.zjsltStormService.saveTyphoonInfo(typhoonInfoDB);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save