|  |  |  | 
|---|
|  |  |  | import com.moral.api.service.CityWeatherService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.web.client.RestTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RestTemplate restTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void insertCityWeather() { | 
|---|
|  |  |  | //获取城市配置 | 
|---|
|  |  |  | 
|---|
|  |  |  | wrapper.select("city_code", "location_id").eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | List<CityConfig> list = cityConfigService.list(wrapper); | 
|---|
|  |  |  | for (CityConfig cityConfig : list) { | 
|---|
|  |  |  | String cityCode = cityConfig.getCityCode(); | 
|---|
|  |  |  | Integer cityCode = cityConfig.getCityCode(); | 
|---|
|  |  |  | Integer locationId = cityConfig.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.setValue(JSONObject.toJSONString(now)); | 
|---|
|  |  |  | Date time = DateUtils.dataToTimeStampTime(new Date(), DateUtils.yyyy_MM_dd_HH_EN); | 
|---|
|  |  |  | cityWeather.setTime(time); | 
|---|
|  |  |  | //存入数据库 | 
|---|
|  |  |  | cityWeatherMapper.insert(cityWeather); | 
|---|
|  |  |  | //存入redis | 
|---|
|  |  |  | redisTemplate.opsForHash().put(RedisConstants.CITY_WEATHER, cityCode, now); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public CityWeather getDataByCityCodeAndTime(String cityCode, String time) { | 
|---|
|  |  |  | QueryWrapper wrapper_cityWeather = new QueryWrapper(); | 
|---|
|  |  |  | wrapper_cityWeather.eq("city_code", cityCode); | 
|---|
|  |  |  | wrapper_cityWeather.eq("time", time); | 
|---|
|  |  |  | CityWeather cityWeather = new CityWeather(); | 
|---|
|  |  |  | if (cityWeatherMapper.selectCount(wrapper_cityWeather) == 1) { | 
|---|
|  |  |  | cityWeather = cityWeatherMapper.selectOne(wrapper_cityWeather); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return cityWeather; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|