|  |  | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.moral.api.entity.CityConfig; | 
 |  |  | import com.moral.api.entity.CityConfigWeather; | 
 |  |  | import com.moral.api.entity.CityWeather; | 
 |  |  | import com.moral.api.mapper.CityWeatherMapper; | 
 |  |  | import com.moral.api.service.CityConfigService; | 
 |  |  | import com.moral.api.service.CityConfigWeatherService; | 
 |  |  | import com.moral.api.service.CityWeatherService; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | 
 |  |  | 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; | 
 |  |  | 
 |  |  |     private CityWeatherMapper cityWeatherMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private CityConfigService cityConfigService; | 
 |  |  |     private CityConfigWeatherService cityConfigWeatherService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RestTemplate restTemplate; | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RedisTemplate redisTemplate; | 
 |  |  |  | 
 |  |  |     //城市气象数据来源于,和风天气,实时天气商业版 | 
 |  |  |     @Override | 
 |  |  |     public void insertCityWeather() { | 
 |  |  |         //获取城市配置 | 
 |  |  |         QueryWrapper<CityConfig> wrapper = new QueryWrapper<>(); | 
 |  |  |         QueryWrapper<CityConfigWeather> wrapper = new QueryWrapper<>(); | 
 |  |  |         wrapper.select("city_code", "location_id").eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         List<CityConfig> list = cityConfigService.list(wrapper); | 
 |  |  |         for (CityConfig cityConfig : list) { | 
 |  |  |             Integer cityCode = cityConfig.getCityCode(); | 
 |  |  |             Integer locationId = cityConfig.getLocationId(); | 
 |  |  |         List<CityConfigWeather> list = cityConfigWeatherService.list(wrapper); | 
 |  |  |  | 
 |  |  |         List<CityWeather> cityWeathers = new ArrayList<>(); | 
 |  |  |  | 
 |  |  |         for (CityConfigWeather cityConfigWeather : list) { | 
 |  |  |             CityWeather cityWeather = new CityWeather(); | 
 |  |  |             Integer cityCode = cityConfigWeather.getCityCode(); | 
 |  |  |             Integer locationId = cityConfigWeather.getLocationId(); | 
 |  |  |             Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId); | 
 |  |  |             Map<String, Object> now = (Map<String, Object>) data.get("now"); | 
 |  |  |             CityWeather cityWeather = new CityWeather(); | 
 |  |  |             cityWeather.setCityCode(cityCode); | 
 |  |  |             //风速km/h->m/s | 
 |  |  |             double windSpeed = Double.parseDouble(now.get("windSpeed").toString()); | 
 |  |  | 
 |  |  |             cityWeather.setValue(JSONObject.toJSONString(now)); | 
 |  |  |             Date time = DateUtils.dataToTimeStampTime(new Date(), DateUtils.yyyy_MM_dd_HH_EN); | 
 |  |  |             cityWeather.setTime(time); | 
 |  |  |             //存入数据库 | 
 |  |  |             cityWeatherMapper.insert(cityWeather); | 
 |  |  |             cityWeathers.add(cityWeather); | 
 |  |  |             //存入redis | 
 |  |  |             redisTemplate.opsForHash().put(RedisConstants.CITY_WEATHER, cityCode, now); | 
 |  |  |             redisTemplate.opsForHash().put(RedisConstants.CITY_WEATHER, String.valueOf(cityCode), now); | 
 |  |  |         } | 
 |  |  |         cityWeatherMapper.insertCityWeather(cityWeathers); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |         } | 
 |  |  |         return cityWeather; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<CityWeather> getCityWeather() { | 
 |  |  |         String time = DateUtils.getDateStringOfHour(-1, DateUtils.yyyy_MM_dd_HH_EN) + ":00:00"; | 
 |  |  |         QueryWrapper<CityWeather> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.eq("time", time); | 
 |  |  |         return cityWeatherMapper.selectList(queryWrapper); | 
 |  |  |     } | 
 |  |  | } |