From 20eec3a1174fb311cc934716f5cf1fe9a146dc30 Mon Sep 17 00:00:00 2001 From: XinYi Song <2037158277@qq.com> Date: Mon, 14 Feb 2022 11:27:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=A4=A9=E6=B0=94?= =?UTF-8?q?=E7=9A=84=E7=9B=B8=E5=BA=94=E7=9A=84=E5=8A=9F=E8=83=BD=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/schedule/SaticScheduleTask.java | 33 +++- .../xkrs/controller/WeatherController.java | 43 ++++ src/main/java/com/xkrs/dao/WeatherDao.java | 20 ++ .../java/com/xkrs/model/entity/Weather.java | 186 ++++++++++++++++++ 4 files changed, 277 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/xkrs/controller/WeatherController.java create mode 100644 src/main/java/com/xkrs/dao/WeatherDao.java create mode 100644 src/main/java/com/xkrs/model/entity/Weather.java diff --git a/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java b/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java index f3caf3b..2f0cdca 100644 --- a/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java +++ b/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java @@ -1,11 +1,16 @@ package com.xkrs.common.schedule; +import com.fasterxml.jackson.databind.JsonNode; +import com.xkrs.dao.WeatherDao; +import com.xkrs.model.entity.Weather; +import com.xkrs.util.WeatherUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; +import javax.annotation.Resource; import java.time.LocalDateTime; @@ -19,15 +24,33 @@ public class SaticScheduleTask { Logger logger = LoggerFactory.getLogger(SaticScheduleTask.class); + @Resource + private WeatherDao weatherDao; + /** - * 查询新的火点信息 - *//* - @Scheduled(cron = "0 0/5 * * * ?") + * 每天零点入库天气信息 + */ + @Scheduled(cron = "0 0 0 * * ?") private void configureTasks() { logger.info("执行静态定时任务时间: " + LocalDateTime.now()); System.out.println("------" + "开始执行定时任务"); -// tempQueryPoint.findPointToLocal(); - }*/ + JsonNode forecastWeather = WeatherUtils.getForecastWeather(); + JsonNode results = forecastWeather.path("results"); + JsonNode jsonNode = results.get(0); + JsonNode daily = jsonNode.path("daily"); + JsonNode jsonNode1 = daily.get(0); + Weather weather = new Weather(); + weather.setWindSpeed(jsonNode1.get("wind_speed").asText()); + weather.setWindDirection(jsonNode1.get("wind_direction").asText()); + weather.setHighTemperature(jsonNode1.get("high").asText()); + weather.setLowTemperature(jsonNode1.get("low").asText()); + weather.setHumidity(jsonNode1.get("humidity").asText()); + weather.setRainfall(jsonNode1.get("rainfall").asText()); + weather.setWeatherDay(jsonNode1.get("text_day").asText()); + weather.setWeatherNight(jsonNode1.get("text_night").asText()); + weather.setWeatherTime(jsonNode1.get("date").asText()); + weatherDao.save(weather); + } } diff --git a/src/main/java/com/xkrs/controller/WeatherController.java b/src/main/java/com/xkrs/controller/WeatherController.java new file mode 100644 index 0000000..922e98d --- /dev/null +++ b/src/main/java/com/xkrs/controller/WeatherController.java @@ -0,0 +1,43 @@ +package com.xkrs.controller; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.dao.WeatherDao; +import com.xkrs.model.entity.Weather; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.Locale; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +/** + * @Author: XinYi Song + * @Date: 2022/2/14 10:50 + */ +@RestController +public class WeatherController { + + @Resource + private WeatherDao weatherDao; + + Locale locale = LocaleContextHolder.getLocale(); + + /** + * 根据时间查询天气的状况 + * @param weatherTime + * @return + */ + @GetMapping("/getWeatherByTime") + public String getWeatherByTime(@RequestParam("weatherTime") String weatherTime){ + Weather byWeatherTime = weatherDao.findByWeatherTime(weatherTime); + if(byWeatherTime == null){ + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"暂时还没有该天的天气状况!",locale); + } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,byWeatherTime,locale); + } + + +} diff --git a/src/main/java/com/xkrs/dao/WeatherDao.java b/src/main/java/com/xkrs/dao/WeatherDao.java new file mode 100644 index 0000000..59112bb --- /dev/null +++ b/src/main/java/com/xkrs/dao/WeatherDao.java @@ -0,0 +1,20 @@ +package com.xkrs.dao; + +import com.xkrs.model.entity.Weather; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Component; + +/** + * @Author: XinYi Song + * @Date: 2022/2/14 10:50 + */ +@Component +public interface WeatherDao extends JpaRepository { + + /** + * 根据时间查询天气的状况 + * @param time + * @return + */ + Weather findByWeatherTime(String time); +} diff --git a/src/main/java/com/xkrs/model/entity/Weather.java b/src/main/java/com/xkrs/model/entity/Weather.java new file mode 100644 index 0000000..0bfd47f --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/Weather.java @@ -0,0 +1,186 @@ +package com.xkrs.model.entity; + +import javax.persistence.*; + +/** + * 天气实体类 + * @Author: XinYi Song + * @Date: 2022/2/14 10:35 + */ +@Entity +@Table(name = "weather") +public class Weather { + /** + * 主键id + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "weather_seq_gen") + @SequenceGenerator(name = "weather_seq_gen", sequenceName = "weather_id_seq",allocationSize = 1) + private Integer id; + + /** + * 风速 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String windSpeed; + + /** + * 风向 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String windDirection; + + /** + * 最高温度 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String highTemperature; + + /** + * 最低温度 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String lowTemperature; + + /** + * 湿度 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String humidity; + + /** + * 雨量 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String rainfall; + + /** + * 时间 + */ + @Column(length = 65, columnDefinition = "varchar(65)") + private String weatherTime; + + /** + * 白天天气 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String weatherDay; + + /** + * 夜间天气 + */ + @Column(length = 32, columnDefinition = "varchar(32)") + private String weatherNight; + + public Weather() { + } + + public Weather(Integer id, String windSpeed, String windDirection, String highTemperature, String lowTemperature, String humidity, String rainfall, String weatherTime, String weatherDay, String weatherNight) { + this.id = id; + this.windSpeed = windSpeed; + this.windDirection = windDirection; + this.highTemperature = highTemperature; + this.lowTemperature = lowTemperature; + this.humidity = humidity; + this.rainfall = rainfall; + this.weatherTime = weatherTime; + this.weatherDay = weatherDay; + this.weatherNight = weatherNight; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getWindSpeed() { + return windSpeed; + } + + public void setWindSpeed(String windSpeed) { + this.windSpeed = windSpeed; + } + + public String getWindDirection() { + return windDirection; + } + + public void setWindDirection(String windDirection) { + this.windDirection = windDirection; + } + + public String getHighTemperature() { + return highTemperature; + } + + public void setHighTemperature(String highTemperature) { + this.highTemperature = highTemperature; + } + + public String getLowTemperature() { + return lowTemperature; + } + + public void setLowTemperature(String lowTemperature) { + this.lowTemperature = lowTemperature; + } + + public String getHumidity() { + return humidity; + } + + public void setHumidity(String humidity) { + this.humidity = humidity; + } + + public String getRainfall() { + return rainfall; + } + + public void setRainfall(String rainfall) { + this.rainfall = rainfall; + } + + public String getWeatherTime() { + return weatherTime; + } + + public void setWeatherTime(String weatherTime) { + this.weatherTime = weatherTime; + } + + public String getWeatherDay() { + return weatherDay; + } + + public void setWeatherDay(String weatherDay) { + this.weatherDay = weatherDay; + } + + public String getWeatherNight() { + return weatherNight; + } + + public void setWeatherNight(String weatherNight) { + this.weatherNight = weatherNight; + } + + @Override + public String toString() { + return "Weather{" + + "id=" + id + + ", windSpeed='" + windSpeed + '\'' + + ", windDirection='" + windDirection + '\'' + + ", highTemperature='" + highTemperature + '\'' + + ", lowTemperature='" + lowTemperature + '\'' + + ", humidity='" + humidity + '\'' + + ", rainfall='" + rainfall + '\'' + + ", weatherTime='" + weatherTime + '\'' + + ", weatherDay='" + weatherDay + '\'' + + ", weatherNight='" + weatherNight + '\'' + + '}'; + } +}