parent
650a3c8ba6
commit
2415bc7da4
@ -0,0 +1,308 @@ |
||||
package com.ruoyi.quartz.timer; |
||||
|
||||
import com.alibaba.fastjson2.JSON; |
||||
import com.alibaba.fastjson2.JSONArray; |
||||
import com.alibaba.fastjson2.JSONObject; |
||||
import com.cjy.enterprise.domain.EnterpriseInfo; |
||||
import com.cjy.enterprise.domain.vo.EnterpriseInfoExtend; |
||||
import com.cjy.enterprise.mapper.EnterpriseInfoMapper; |
||||
import com.cjy.weather.domain.*; |
||||
import com.cjy.weather.mapper.*; |
||||
import com.ruoyi.common.utils.http.HttpUtils; |
||||
import com.ruoyi.framework.web.domain.server.Sys; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @ClassName: WeatherTimer |
||||
* @Author: zc |
||||
* @Date: 2023/3/21 13:37 |
||||
* @Description: TODO |
||||
*/ |
||||
@Component("weather") |
||||
public class WeatherTimer { |
||||
|
||||
private static final String HF_WEATHER_KEY = "5773d9af03544ef59a6bad2a72325aeb"; |
||||
|
||||
@Autowired |
||||
private EnterpriseInfoMapper enterpriseInfoMapper; |
||||
@Autowired |
||||
private WeatherRealtimeMapper weatherRealtimeMapper; |
||||
@Autowired |
||||
private WeatherForecastMapper weatherForecastMapper; |
||||
@Autowired |
||||
private WeatherMeteorologicalMapper weatherMeteorologicalMapper; |
||||
@Autowired |
||||
private WeatherQualityMapper weatherQualityMapper; |
||||
@Autowired |
||||
private WeatherIndexMapper weatherIndexMapper; |
||||
@Autowired |
||||
private WeatherWarningMapper weatherWarningMapper; |
||||
|
||||
/** |
||||
* 功能描述:获取实时天气 |
||||
* @param: |
||||
* @return: |
||||
* @auther: zc |
||||
* @date: 2023/3/21 16:06 |
||||
*/ |
||||
public void getWeatherRealtime(){ |
||||
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList(); |
||||
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) { |
||||
Integer location = enterpriseInfoExtend.getLocation(); |
||||
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location; |
||||
String hfLoncationData = HttpUtils.doGet(locationUrl); |
||||
String code = JSONObject.parseObject(hfLoncationData).getString("code"); |
||||
if("200".equals(code)){ |
||||
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location")); |
||||
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString()); |
||||
String locationId = jsonObject.getString("id"); |
||||
String url = "https://devapi.qweather.com/v7/weather/now?key="+HF_WEATHER_KEY+"&location="+locationId; |
||||
String weatherStr = HttpUtils.doGet(url); |
||||
JSONObject weatherJson = JSONObject.parseObject(weatherStr); |
||||
if("200".equals(weatherJson.get("code").toString())){ |
||||
JSONObject now = JSONObject.parseObject(weatherJson.getString("now")); |
||||
WeatherRealtime weatherRealtime = new WeatherRealtime(); |
||||
weatherRealtime.setObsTime(now.getDate("obsTime")); |
||||
weatherRealtime.setTemp(now.getString("temp")); |
||||
weatherRealtime.setFeelsLike(now.getString("feelsLike")); |
||||
weatherRealtime.setText(now.getString("text")); |
||||
weatherRealtime.setWindDir(now.getString("windDir")); |
||||
weatherRealtime.setWindScale(now.getString("windScale")); |
||||
weatherRealtime.setWindSpeed(now.getString("windSpeed")); |
||||
weatherRealtime.setHumidity(now.getString("humidity")); |
||||
weatherRealtime.setPrecip(now.getString("precip")); |
||||
weatherRealtime.setPressure(now.getString("pressure")); |
||||
weatherRealtime.setVis(now.getString("vis")); |
||||
weatherRealtime.setCloud(now.getString("cloud")); |
||||
weatherRealtime.setDew(now.getString("dew")); |
||||
weatherRealtime.setCreateTime(new Date()); |
||||
weatherRealtime.setOrganCode(enterpriseInfoExtend.getOrganCode()); |
||||
weatherRealtimeMapper.insertWeatherRealtime(weatherRealtime); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 功能描述:获取天气预报 |
||||
* @param: |
||||
* @return: |
||||
* @auther: zc |
||||
* @date: 2023/3/21 16:07 |
||||
*/ |
||||
public void getWeatherForecast(){ |
||||
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList(); |
||||
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) { |
||||
Integer location = enterpriseInfoExtend.getLocation(); |
||||
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location; |
||||
String hfLoncationData = HttpUtils.doGet(locationUrl); |
||||
String code = JSONObject.parseObject(hfLoncationData).getString("code"); |
||||
if("200".equals(code)){ |
||||
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location")); |
||||
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString()); |
||||
String locationId = jsonObject.getString("id"); |
||||
String url = "https://devapi.qweather.com/v7/weather/7d?key="+HF_WEATHER_KEY+"&location="+locationId; |
||||
String weatherStr = HttpUtils.doGet(url); |
||||
JSONObject weatherJson = JSONObject.parseObject(weatherStr); |
||||
if("200".equals(weatherJson.get("code").toString())){ |
||||
JSONArray daily = JSONArray.parseArray(weatherJson.getString("daily")); |
||||
for (Object obj : daily) { |
||||
//天气预报表
|
||||
JSONObject dailyObj = JSONObject.parseObject(obj.toString()); |
||||
WeatherForecast weatherForecast = new WeatherForecast(); |
||||
weatherForecast.setOrganCode(enterpriseInfoExtend.getOrganCode()); |
||||
weatherForecast.setForecastDate(dailyObj.getDate("fxDate")); |
||||
weatherForecast.setSunrise(dailyObj.getString("sunrise")); |
||||
weatherForecast.setSunset(dailyObj.getString("sunset")); |
||||
weatherForecast.setMoonrise(dailyObj.getString("moonrise")); |
||||
weatherForecast.setMoonset(dailyObj.getString("moonset")); |
||||
weatherForecast.setMoonPhase(dailyObj.getString("moonPhase")); |
||||
weatherForecast.setTempMax(dailyObj.getString("tempMax")); |
||||
weatherForecast.setTempMin(dailyObj.getString("tempMin")); |
||||
weatherForecast.setTextDay(dailyObj.getString("textDay")); |
||||
weatherForecast.setTextNight(dailyObj.getString("textNight")); |
||||
weatherForecast.setWindDirDay(dailyObj.getString("windDirDay")); |
||||
weatherForecast.setWindScaleDay(dailyObj.getString("windScaleDay")); |
||||
weatherForecast.setWindSpeedDay(dailyObj.getString("windSpeedDay")); |
||||
weatherForecast.setWindDirNight(dailyObj.getString("windDirNight")); |
||||
weatherForecast.setWindScaleNight(dailyObj.getString("windScaleNight")); |
||||
weatherForecast.setWindSpeedNight(dailyObj.getString("windSpeedNight")); |
||||
weatherForecast.setHumidity(dailyObj.getString("humidity")); |
||||
weatherForecast.setPrecip(dailyObj.getString("precip")); |
||||
weatherForecast.setPressure(dailyObj.getString("pressure")); |
||||
weatherForecast.setVis(dailyObj.getString("vis")); |
||||
weatherForecast.setCloud(dailyObj.getString("cloud")); |
||||
weatherForecast.setUvIndex(dailyObj.getString("uvIndex")); |
||||
weatherForecast.setCreateTime(new Date()); |
||||
weatherForecastMapper.insertWeatherForecast(weatherForecast); |
||||
|
||||
//用于统计的表
|
||||
WeatherMeteorological weatherMeteorological = new WeatherMeteorological(); |
||||
weatherMeteorological.setForecastDate(dailyObj.getDate("fxDate")); |
||||
weatherMeteorological.setTempMax(dailyObj.getString("tempMax")); |
||||
weatherMeteorological.setTempMin(dailyObj.getString("tempMin")); |
||||
weatherMeteorological.setTextDay(dailyObj.getString("textDay")); |
||||
weatherMeteorological.setOrganCode(enterpriseInfoExtend.getOrganCode()); |
||||
weatherMeteorological.setCreateTime(new Date()); |
||||
weatherMeteorologicalMapper.insertWeatherMeteorological(weatherMeteorological); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 功能描述:获取空气质量 |
||||
* @param: |
||||
* @return: |
||||
* @auther: zc |
||||
* @date: 2023/3/21 17:05 |
||||
*/ |
||||
public void getWeatherQuality(){ |
||||
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList(); |
||||
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) { |
||||
Integer location = enterpriseInfoExtend.getLocation(); |
||||
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location; |
||||
String hfLoncationData = HttpUtils.doGet(locationUrl); |
||||
String code = JSONObject.parseObject(hfLoncationData).getString("code"); |
||||
if("200".equals(code)){ |
||||
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location")); |
||||
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString()); |
||||
String locationId = jsonObject.getString("id"); |
||||
String url = "https://devapi.qweather.com/v7/air/now?key="+HF_WEATHER_KEY+"&location="+locationId; |
||||
String weatherStr = HttpUtils.doGet(url); |
||||
JSONObject weatherJson = JSONObject.parseObject(weatherStr); |
||||
if("200".equals(weatherJson.get("code").toString())){ |
||||
JSONObject now = JSONObject.parseObject(weatherJson.getString("now")); |
||||
WeatherQuality weatherQuality = new WeatherQuality(); |
||||
weatherQuality.setPubTime(now.getDate("pubTime")); |
||||
weatherQuality.setAqi(now.getString("aqi")); |
||||
weatherQuality.setQualityLevel(now.getString("level")); |
||||
weatherQuality.setCategory(now.getString("category")); |
||||
weatherQuality.setPollutant(now.getString("primary")); |
||||
weatherQuality.setPm2p5(now.getString("pm2p5")); |
||||
weatherQuality.setPm10(now.getString("pm10")); |
||||
weatherQuality.setNitrogenDioxide(now.getString("no2")); |
||||
weatherQuality.setSulfurDioxide(now.getString("so2")); |
||||
weatherQuality.setCarbonMonoxide(now.getString("co")); |
||||
weatherQuality.setOzone(now.getString("o3")); |
||||
weatherQuality.setOrganCode(enterpriseInfoExtend.getOrganCode()); |
||||
weatherQuality.setCreateTime(new Date()); |
||||
weatherQualityMapper.insertWeatherQuality(weatherQuality); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 功能描述:获取天气指数 |
||||
* @param: |
||||
* @return: |
||||
* @auther: zc |
||||
* @date: 2023/3/21 17:30 |
||||
*/ |
||||
public void getWeatherIndex(){ |
||||
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList(); |
||||
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) { |
||||
Integer location = enterpriseInfoExtend.getLocation(); |
||||
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location; |
||||
String hfLoncationData = HttpUtils.doGet(locationUrl); |
||||
String code = JSONObject.parseObject(hfLoncationData).getString("code"); |
||||
if("200".equals(code)){ |
||||
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location")); |
||||
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString()); |
||||
String locationId = jsonObject.getString("id"); |
||||
String url = "https://devapi.qweather.com/v7/indices/1d?key="+HF_WEATHER_KEY+"&location="+locationId+"&type=0"; |
||||
String weatherStr = HttpUtils.doGet(url); |
||||
JSONObject weatherJson = JSONObject.parseObject(weatherStr); |
||||
if("200".equals(weatherJson.get("code").toString())){ |
||||
JSONArray daily = JSONArray.parseArray(weatherJson.getString("daily")); |
||||
for (Object obj : daily) { |
||||
JSONObject dailyObj = JSONObject.parseObject(obj.toString()); |
||||
WeatherIndex weatherIndex = new WeatherIndex(); |
||||
weatherIndex.setForecastDate(dailyObj.getDate("date")); |
||||
weatherIndex.setType(dailyObj.getString("type")); |
||||
weatherIndex.setIndexName(dailyObj.getString("name")); |
||||
weatherIndex.setIndexLevel(dailyObj.getString("level")); |
||||
weatherIndex.setCategory(dailyObj.getString("category")); |
||||
weatherIndex.setText(dailyObj.getString("text")); |
||||
weatherIndex.setOrganCode(enterpriseInfoExtend.getOrganCode()); |
||||
weatherIndex.setCreateTime(new Date()); |
||||
weatherIndexMapper.insertWeatherIndex(weatherIndex); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 功能描述:获取预警信息 |
||||
* @param: |
||||
* @return: |
||||
* @auther: zc |
||||
* @date: 2023/3/21 17:44 |
||||
*/ |
||||
public void getWeatherWarning(){ |
||||
List<EnterpriseInfoExtend> entitiesInfoList = getEntitiesInfoList(); |
||||
for (EnterpriseInfoExtend enterpriseInfoExtend : entitiesInfoList) { |
||||
Integer location = enterpriseInfoExtend.getLocation(); |
||||
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location="+location; |
||||
String hfLoncationData = HttpUtils.doGet(locationUrl); |
||||
String code = JSONObject.parseObject(hfLoncationData).getString("code"); |
||||
if("200".equals(code)){ |
||||
JSONArray objects = JSONArray.parseArray(JSONObject.parseObject(hfLoncationData).getString("location")); |
||||
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString()); |
||||
String locationId = jsonObject.getString("id"); |
||||
String url = "https://devapi.qweather.com/v7/warning/now?key="+HF_WEATHER_KEY+"&location="+locationId; |
||||
String weatherStr = HttpUtils.doGet(url); |
||||
JSONObject weatherJson = JSONObject.parseObject(weatherStr); |
||||
if("200".equals(weatherJson.get("code").toString())){ |
||||
JSONArray daily = JSONArray.parseArray(weatherJson.getString("daily")); |
||||
for (Object obj : daily) { |
||||
JSONObject dailyObj = JSONObject.parseObject(obj.toString()); |
||||
WeatherWarning weatherWarningData = new WeatherWarning(); |
||||
weatherWarningData.setWarningId(dailyObj.getString("id")); |
||||
List<WeatherWarning> weatherWarnings = weatherWarningMapper.selectWeatherWarningList(weatherWarningData); |
||||
if(!weatherWarnings.isEmpty()){ |
||||
WeatherWarning weatherWarning = new WeatherWarning(); |
||||
weatherWarning.setWarningId(dailyObj.getString("id")); |
||||
weatherWarning.setSender(dailyObj.getString("sender")); |
||||
weatherWarning.setPubTime(dailyObj.getDate("pubTime")); |
||||
weatherWarning.setTitle(dailyObj.getString("title")); |
||||
weatherWarning.setStartTime(dailyObj.getDate("startTime")); |
||||
weatherWarning.setEndTime(dailyObj.getDate("endTime")); |
||||
weatherWarning.setWarningStatus(dailyObj.getString("status")); |
||||
weatherWarning.setSeverity(dailyObj.getString("severity")); |
||||
weatherWarning.setSeverityColor(dailyObj.getString("severityColor")); |
||||
weatherWarning.setType(dailyObj.getString("type")); |
||||
weatherWarning.setTypeName(dailyObj.getString("typeName")); |
||||
weatherWarning.setText(dailyObj.getString("text")); |
||||
weatherWarning.setRelated(dailyObj.getString("related")); |
||||
weatherWarning.setOrganCode(enterpriseInfoExtend.getOrganCode()); |
||||
weatherWarning.setCreateTime(new Date()); |
||||
weatherWarningMapper.insertWeatherWarning(weatherWarning); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private List<EnterpriseInfoExtend> getEntitiesInfoList(){ |
||||
List<EnterpriseInfoExtend> enterpriseInfoExtends = enterpriseInfoMapper.selectEnterpriseInfoList(new EnterpriseInfo()); |
||||
return enterpriseInfoExtends; |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
String locationUrl = "https://geoapi.qweather.com/v2/city/lookup?key="+HF_WEATHER_KEY+"&location=130110"; |
||||
String hfLoncationData = HttpUtils.doGet(locationUrl); |
||||
String location = JSONObject.parseObject(hfLoncationData).getString("location"); |
||||
JSONArray objects = JSONArray.parseArray(location); |
||||
JSONObject jsonObject = JSONObject.parseObject(objects.get(0).toString()); |
||||
System.out.println(jsonObject.getString("id")); |
||||
} |
||||
} |
Loading…
Reference in new issue