| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.moral.api.entity.CityAqiConfig; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.GovMonitorPoint; |
| | | import com.moral.api.entity.HistoryAqi; |
| | | import com.moral.api.mapper.HistoryAqiMapper; |
| | | import com.moral.api.service.CityAqiConfigService; |
| | | import com.moral.api.service.GovMonitorPointService; |
| | | import com.moral.api.service.HistoryAqiService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.DateUtils; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | 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.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | 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; |
| | |
| | | private HistoryAqiMapper historyAqiMapper; |
| | | |
| | | @Autowired |
| | | private CityAqiConfigService cityAqiConfigService; |
| | | private RestTemplate restTemplate; |
| | | |
| | | @Autowired |
| | | private GovMonitorPointService govMonitorPointService; |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void insertHistoryAqi() { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | //获取aqi城市配置 |
| | | List<CityAqiConfig> cityAqiConfigs = cityAqiConfigService.getCityAqiConfigs(); |
| | | for (CityAqiConfig cityAqiConfig : cityAqiConfigs) { |
| | | Map<String, Object> mjMap = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=aqi", Map.class, cityAqiConfig.getCityId()); |
| | | if (ObjectUtils.isEmpty(mjMap)) { |
| | | //设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.set("Authorization", "APPCODE 31b6ea8f804a4472be3b633cfee44849"); |
| | | |
| | | HttpEntity requestEntity = new HttpEntity<>(headers); |
| | | |
| | | QueryWrapper<GovMonitorPoint> queryWrapper = new QueryWrapper<>(); |
| | | 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}&pubtime={2}", HttpMethod.GET, requestEntity, String.class, guid, timeStr); |
| | | } catch (Exception e) { |
| | | continue; |
| | | } |
| | | HistoryAqi historyAqi = new HistoryAqi(); |
| | | //city_code |
| | | String cityCode = cityAqiConfig.getCityCode(); |
| | | historyAqi.setCityCode(cityCode); |
| | | String body = response.getBody(); |
| | | Map<String, Object> data = JSONObject.parseObject(body, Map.class); |
| | | Map<String, Object> map = (Map<String, Object>) data.get("data"); |
| | | if (ObjectUtils.isEmpty(map)) { |
| | | continue; |
| | | } |
| | | historyAqi.setGuid(guid); |
| | | historyAqi.setTime(DateUtils.addHours(time, -1)); |
| | | //存入数据库 |
| | | historyAqi.setValue(JSONObject.toJSONString(map)); |
| | | |
| | | historyAqis.add(historyAqi); |
| | | |
| | | Map<String, Object> value = new HashMap<>(); |
| | | Map<String, Object> data = (Map<String, Object>) ((Map) mjMap.get("data")).get("aqi"); |
| | | //数据时间 |
| | | historyAqi.setTime(new Date(Long.parseLong(data.get("pubtime").toString()))); |
| | | value.put("PM25", data.get("pm25C")); |
| | | value.put("PM10", data.get("pm10C")); |
| | | value.put("SO2", data.get("so2C")); |
| | | value.put("NO2", data.get("no2C")); |
| | | value.put("CO", data.get("coC")); |
| | | value.put("O3", data.get("o3C")); |
| | | value.put("AQI", data.get("value")); |
| | | //数据 |
| | | historyAqi.setValue(JSONObject.toJSONString(value)); |
| | | //数据存入数据库 |
| | | historyAqiMapper.insert(historyAqi); |
| | | //存入redis |
| | | redisTemplate.opsForHash().putAll(RedisConstants.AQI_DATA + cityCode, value); |
| | | Object pm2_5 = map.get("pm2_5"); |
| | | Object pm10 = map.get("pm10"); |
| | | Object so2 = map.get("so2"); |
| | | Object no2 = map.get("no2"); |
| | | Object co = map.get("co"); |
| | | Object o3 = map.get("o3"); |
| | | if (!ObjectUtils.isEmpty(pm2_5)) { |
| | | value.put(Constants.SENSOR_CODE_PM25, pm2_5); |
| | | } |
| | | |
| | | if (!ObjectUtils.isEmpty(pm10)) { |
| | | value.put(Constants.SENSOR_CODE_PM10, pm10); |
| | | } |
| | | |
| | | if (!ObjectUtils.isEmpty(so2)) { |
| | | value.put(Constants.SENSOR_CODE_SO2, so2); |
| | | } |
| | | |
| | | if (!ObjectUtils.isEmpty(no2)) { |
| | | value.put(Constants.SENSOR_CODE_NO2, no2); |
| | | } |
| | | |
| | | if (!ObjectUtils.isEmpty(co)) { |
| | | value.put(Constants.SENSOR_CODE_CO, Double.parseDouble(co.toString())); |
| | | } |
| | | |
| | | if (!ObjectUtils.isEmpty(o3)) { |
| | | value.put(Constants.SENSOR_CODE_O3, o3); |
| | | } |
| | | |
| | | //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); |
| | | } |
| | | } |