package com.moral.api.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.moral.api.entity.CityConfigWeatherForecast; import com.moral.api.entity.CityWeatherForecast; import com.moral.api.mapper.CityWeatherForecastMapper; import com.moral.api.service.CityConfigWeatherForecastService; import com.moral.api.service.CityWeatherForecastService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.constant.Constants; import com.moral.util.DateUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; /** *

* 城市预测气象数据表 服务实现类 *

* * @author moral * @since 2021-12-30 */ @Service public class CityWeatherForecastServiceImpl extends ServiceImpl implements CityWeatherForecastService { @Autowired private CityConfigWeatherForecastService cityConfigWeatherForecastService; @Autowired private RestTemplate restTemplate; @Autowired private CityWeatherForecastMapper cityWeatherForecastMapper; @Override public void insertCityWeatherForecast() { Date nextDay = DateUtils.addDays(new Date(), 1); String nextTime = DateUtils.dateToDateString(nextDay, DateUtils.yyyy_MM_dd_EN); //获取城市配置 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.select("city_code", "location_id").eq("is_delete", Constants.NOT_DELETE); List list = cityConfigWeatherForecastService.list(wrapper); List cityWeatherForecasts = new ArrayList<>(); CityWeatherForecast cityWeatherForecast = new CityWeatherForecast(); for (CityConfigWeatherForecast cityConfigWeatherForecast : list) { Integer cityCode = cityConfigWeatherForecast.getCityCode(); Integer locationId = cityConfigWeatherForecast.getLocationId(); Map data = restTemplate.getForObject("https://api.qweather.com/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId); List> hourly = (List>) data.get("hourly"); for (Map hourlyMap : hourly) { String fxTime = hourlyMap.get("fxTime").toString(); String dayTime = fxTime.split("T")[0]; if (dayTime.equals(nextTime)) { String hourTime = fxTime.substring(0, 17).replaceAll("T", " "); cityWeatherForecast.setCityCode(cityCode); cityWeatherForecast.setTime(DateUtils.getDate(hourTime, DateUtils.yyyy_MM_dd_HH_EN)); cityWeatherForecast.setValue(JSONObject.toJSONString(hourlyMap)); cityWeatherForecasts.add(cityWeatherForecast); } } } cityWeatherForecastMapper.insertCityWeatherForecast(cityWeatherForecasts); } }