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 + '\'' + + '}'; + } +}