|  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.http.HttpEntity; | 
|---|
|  |  |  | import org.springframework.http.HttpHeaders; | 
|---|
|  |  |  | import org.springframework.http.ResponseEntity; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.LinkedMultiValueMap; | 
|---|
|  |  |  | import org.springframework.util.MultiValueMap; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.web.client.RestTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //城市aqi数据来源于,阿里云市场:墨迹天气(基础版CityID)全国历史天气预报接口 | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void insertCityAqi() { | 
|---|
|  |  |  | //pubtime=08的数据,是07-08之间的数据,存入数据库时的时间为07点 | 
|---|
|  |  |  | 
|---|
|  |  |  | .collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | queryWrapper.select("city_code", "time", "value") | 
|---|
|  |  |  | .ge("time", DateUtils.dateToDateString(start)) | 
|---|
|  |  |  | .ge("time", start) | 
|---|
|  |  |  | .in("city_code", cityCodes); | 
|---|
|  |  |  | List<Map<String, Object>> selectMaps = cityAqiMapper.selectMaps(queryWrapper); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<CityAqi> cityAqiList = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String url = "http://aliv13.data.moji.com/whapi/json/alicityweather/aqi"; | 
|---|
|  |  |  | String appcode = "31b6ea8f804a4472be3b633cfee44849"; | 
|---|
|  |  |  | HttpHeaders httpHeaders = new HttpHeaders(); | 
|---|
|  |  |  | httpHeaders.add("Authorization", "APPCODE " + appcode); | 
|---|
|  |  |  | //请求参数 | 
|---|
|  |  |  | LinkedMultiValueMap<String, Object> requestParams = new LinkedMultiValueMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (CityConfigAqi cityConfigAqi : list) { | 
|---|
|  |  |  | CityAqi cityAqi = new CityAqi(); | 
|---|
|  |  |  | Integer cityCode = cityConfigAqi.getCityCode(); | 
|---|
|  |  |  | Map<String, Object> data; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //从第三方接口获取数据 | 
|---|
|  |  |  | data = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=aqi", Map.class, cityConfigAqi.getCityId()); | 
|---|
|  |  |  | requestParams.set("cityId", cityConfigAqi.getCityId()); | 
|---|
|  |  |  | HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(requestParams, httpHeaders); | 
|---|
|  |  |  | ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class); | 
|---|
|  |  |  | data = JSONObject.parseObject(response.getBody(), Map.class); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|