|  |  |  | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  | import com.xxl.job.core.context.XxlJobHelper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | //        String timeStr = DateUtils.dateToDateString(DateUtils.addHours(time, -1), DateUtils.yyyy_MM_dd_HH_mm_ss_EN); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<HistoryAqi> historyAqis = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (GovMonitorPoint govMonitorPoint : govMonitorPoints) { | 
|---|
|  |  |  | HistoryAqi historyAqi = new HistoryAqi(); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | if (guid.equals("f42092ed-cdf3-4ddc-a56f-0ac80a560847") || guid.equals("cedf9934-f3b0-499d-81cd-a17a109aa100")) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | historyAqis.add(historyAqi); | 
|---|
|  |  |  | 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, "525d6346-f6c9-40e2-b3b6-a86fb7c85855", 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"); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(map)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | historyAqi.setGuid(guid); | 
|---|
|  |  |  | historyAqi.setTime(DateUtils.addHours(time, -1)); | 
|---|
|  |  |  | //存入数据库 | 
|---|
|  |  |  | historyAqi.setValue(JSONObject.toJSONString(map)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<String, Object> value = new HashMap<>(); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | historyAqis.add(historyAqi); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<String, Object> value = new HashMap<>(); | 
|---|
|  |  |  | 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.insert(historyAqi); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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<>(); | 
|---|
|  |  |  | 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.insert(historyAqi); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(historyAqis)){ | 
|---|
|  |  |  | XxlJobHelper.log("数据不存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //存入数据库 | 
|---|
|  |  |  | historyAqiMapper.insertHistoryAqi(historyAqis); | 
|---|