| | |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | queryWrapper.select("guid").eq("is_delete", Constants.NOT_DELETE); |
| | | //获取所有国控,省控,县控站点 |
| | | List<GovMonitorPoint> govMonitorPoints = govMonitorPointService.list(queryWrapper); |
| | | Date time = DateUtils.dataToTimeStampTime(new Date(), DateUtils.yyyy_MM_dd_HH_EN); |
| | | String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); |
| | | |
| | | List<HistoryAqi> historyAqis = new ArrayList<>(); |
| | | HistoryAqi historyAqi = new HistoryAqi(); |
| | | for (GovMonitorPoint govMonitorPoint : govMonitorPoints) { |
| | | String guid = govMonitorPoint.getGuid(); |
| | | ResponseEntity<String> response; |
| | | try { |
| | | //从第三方接口获取数据 |
| | | response = restTemplate.exchange("http://chinair.market.alicloudapi.com/api/v1/air_all/station_realtime?guid={1}", HttpMethod.GET, requestEntity, String.class, guid); |
| | | response = restTemplate.exchange("http://chinair.market.alicloudapi.com/api/v1/air_all/station_realtime?guid={1}&pubtime={2}", HttpMethod.GET, requestEntity, String.class, guid, timeStr); |
| | | } catch (Exception e) { |
| | | continue; |
| | | } |
| | | String body = response.getBody(); |
| | | Map<String, Object> data = JSONObject.parseObject(body, Map.class); |
| | | Map<String, Object> map = (Map<String, Object>) data.get("data"); |
| | | HistoryAqi historyAqi = new HistoryAqi(); |
| | | if (ObjectUtils.isEmpty(map)) { |
| | | continue; |
| | | } |
| | | historyAqi.setGuid(guid); |
| | | historyAqi.setTime(DateUtils.getDate(map.get("pubtime").toString(), DateUtils.yyyy_MM_dd_HH_mm_ss_EN)); |
| | | historyAqi.setTime(DateUtils.addHours(time, -1)); |
| | | //存入数据库 |
| | | historyAqi.setValue(JSONObject.toJSONString(map)); |
| | | historyAqiMapper.insert(historyAqi); |
| | | |
| | | historyAqis.add(historyAqi); |
| | | |
| | | Map<String, Object> value = new HashMap<>(); |
| | | Object pm2_5 = map.get("pm2_5"); |
| | |
| | | //aqi数据存入redis |
| | | redisTemplate.opsForHash().put(RedisConstants.AQI_DATA, guid, value); |
| | | } |
| | | //存入数据库 |
| | | historyAqiMapper.insertHistoryAqi(historyAqis); |
| | | } |
| | | |
| | | @Override |
| | | public HistoryAqi getHistoryApiByTimeAndGuid(String guid, String time) { |
| | | QueryWrapper<HistoryAqi> wrapper_historyAqi = new QueryWrapper<>(); |
| | | wrapper_historyAqi.eq("guid", guid).eq("time", time); |
| | | HistoryAqi historyAqi = new HistoryAqi(); |
| | | if (historyAqiMapper.selectCount(wrapper_historyAqi) == 1) { |
| | | historyAqi = historyAqiMapper.selectOne(wrapper_historyAqi); |
| | | } |
| | | return historyAqi; |
| | | } |
| | | |
| | | @Override |
| | | public List<HistoryAqi> getHistoryAqi() { |
| | | String time = DateUtils.getDateStringOfHour(-1, DateUtils.yyyy_MM_dd_HH_EN) + ":00:00"; |
| | | QueryWrapper<HistoryAqi> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("time", time); |
| | | return historyAqiMapper.selectList(queryWrapper); |
| | | } |
| | | } |