package com.moral.task; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.moral.service.AQIService; import com.moral.util.HttpUtils; import com.moral.util.WxMappingJackson2HttpMessageConverter; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import org.apache.commons.lang3.time.DateUtils; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import org.springframework.web.client.RestTemplate; @Component public class AQIDataInsertTask { @Resource private AQIService aqiService; @Resource private RedisTemplate redisTemplate; @XxlJob("insertData") public ReturnT insertData(String param) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter()); Date pubtime = DateUtils.truncate(new Date(), Calendar.HOUR); List> CityAqiConfigs = aqiService.getCityAqiConfig(); ReturnT returnT = null; for (Map cityAqiConfig : CityAqiConfigs) { String entity = null; Collection values = null; Map data = null; /* try { HttpResponse response = HttpUtils.doGet("https://api.epmap.org", "/api/v1/air/city", "GET", new HashMap() {{put("Authorization", "APPCODE " + "31b6ea8f804a4472be3b633cfee44849");}}, new HashMap() {{put("city", cityAqiConfig.get("city_name").toString());}} ); entity = EntityUtils.toString(response.getEntity()); JSONObject json = JSON.parseObject(entity); data = (Map) json.get("data"); if (!ObjectUtils.isEmpty(data)) { values = data.values(); pubtime = format.parse(data.get("pubtime").toString()); int hours = pubtime.getHours(); Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); if (hour != hours){ pubtime = null; } } } catch (Exception e) { e.printStackTrace(); }*/ if (ObjectUtils.isEmpty(entity) || ObjectUtils.isEmpty(values) || values.contains("None") || pubtime == null) { Map mjMap = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=aqi", Map.class, cityAqiConfig.get("city_id")); if (ObjectUtils.isEmpty(mjMap)) { continue; } else { data = (Map) ((Map) mjMap.get("data")).get("aqi"); data.put("aqi", data.remove("value")); data.put("PM2_5", data.remove("pm25")); pubtime = new Date(Long.valueOf(data.get("pubtime").toString())); String string = JSONObject.toJSONString(data).toUpperCase(); data = (Map) JSON.parse(string); } } Map parameters = new HashMap<>(); parameters.put("time", pubtime); parameters.put("data", data.toString()); parameters.put("code", cityAqiConfig.get("city_code")); if (!ObjectUtils.isEmpty(data)) { try { int i = aqiService.insertAQIData(parameters); if (i > 0) { returnT = new ReturnT(200, " 插入数据成功!"); } else { returnT = new ReturnT(500, " 插入数据失败!"); } } catch (Exception e) { e.printStackTrace(); } if ("1".equals(cityAqiConfig.get("is_compensate"))) { Map map = new HashMap(); map.put("e1", data.containsKey("PM25C") ? data.get("PM25C").toString() : data.get("PM2_5").toString()); map.put("e2", data.containsKey("PM10C") ? data.get("PM10C").toString() : data.get("PM10").toString()); map.put("e10", data.containsKey("COC") ? data.get("COC").toString() : data.get("CO").toString()); map.put("e11", data.containsKey("SO2C") ? data.get("SO2C").toString() : data.get("SO2").toString()); map.put("e15", data.containsKey("O3C") ? data.get("O3C").toString() : data.get("O3").toString()); map.put("e16", data.containsKey("NO2C") ? data.get("NO2C").toString() : data.get("NO2").toString()); if (data.get("CITYNAME").equals("虎丘区")){ map.put("e1", data.get("PM2_5").toString()); } redisTemplate.opsForHash().putAll("aqi_" + cityAqiConfig.get("city_code"), map); } } } return returnT; } @XxlJob("insertStationData") public ReturnT insertStationData(String param) { int state = aqiService.insertStationData(); if (state == 200) { return new ReturnT(200, "插入成功"); } else { return new ReturnT(500, "无国控数据,插入失败,请查看接口"); } } }