| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.moral.service.AQIService; |
| | | import com.moral.util.HttpUtils; |
| | | import com.moral.util.RedisUtils; |
| | | import com.moral.util.WxMappingJackson2HttpMessageConverter; |
| | | |
| | | import com.xxl.job.core.biz.model.ReturnT; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import redis.clients.jedis.Jedis; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | @Component |
| | | public class AQIDataInsertTask { |
| | | |
| | | |
| | | |
| | | @Resource |
| | | private AQIService aqiService; |
| | | @Resource |
| | | private RedisUtils redisUtils; |
| | | @XxlJob("insertData") |
| | | public ReturnT insertData(){ |
| | | 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); |
| | | |
| | | Jedis jedis = new Jedis("r-bp1672d21a422a14161.redis.rds.aliyuncs.com", 6379); |
| | | jedis.auth("KtElFcI1sYm9NP3"); |
| | | jedis.select(1); |
| | | |
| | | List<Map<String,Object>> CityAqiConfigs =aqiService.getCityAqiConfig(); |
| | | for (Map<String, Object> cityAqiConfig : CityAqiConfigs) { |
| | | String entity = null ; |
| | | Collection<Object> values = null; |
| | | Map<String, Object> data = null; |
| | | 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<Map<String,Object>> CityAqiConfigs =aqiService.getCityAqiConfig(); |
| | | ReturnT returnT = null; |
| | | for (Map<String, Object> cityAqiConfig : CityAqiConfigs) { |
| | | String entity = null ; |
| | | Map<String, Object> data = null; |
| | | try { |
| | | HttpResponse response = HttpUtils.doGet("https://api.epmap.org", "/api/v1/air/city", "GET", |
| | | new HashMap<String, String>() {{put("Authorization", "APPCODE " + "31b6ea8f804a4472be3b633cfee44849");}}, |
| | | new HashMap<String, String>() {{put("city", cityAqiConfig.get("city_name").toString());}} |
| | | ); |
| | | entity = EntityUtils.toString(response.getEntity()); |
| | | JSONObject json = JSON.parseObject(entity); |
| | | data = (Map<String, Object>) json.get("data"); |
| | | if (!ObjectUtils.isEmpty(data)) { |
| | | pubtime = format.parse(data.get("pubtime").toString()); |
| | | }else { |
| | | continue; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Map<String, Object> parameters = new HashMap<>(); |
| | | parameters.put("time",pubtime); |
| | | parameters.put("data",data.toString()); |
| | | parameters.put("code",cityAqiConfig.get("city_code")); |
| | | if (!ObjectUtils.isEmpty(data)) { |
| | | try { |
| | | HttpResponse response = HttpUtils.doGet("https://api.epmap.org", "/api/v1/air/city", "GET", |
| | | new HashMap<String, String>() {{put("Authorization", "APPCODE " + "31b6ea8f804a4472be3b633cfee44849");}}, |
| | | new HashMap<String, String>() {{put("city", cityAqiConfig.get("city_name").toString());}} |
| | | ); |
| | | entity = EntityUtils.toString(response.getEntity()); |
| | | JSONObject json = JSON.parseObject(entity); |
| | | data = (Map<String, Object>) json.get("data"); |
| | | if (!ObjectUtils.isEmpty(data)) { |
| | | pubtime = format.parse(data.get("pubtime").toString()); |
| | | int i=aqiService.insertAQIData(parameters); |
| | | if (i>0){ |
| | | if ("1".equals(cityAqiConfig.get("is_compensate"))) { |
| | | returnT = new ReturnT(200, " 插入数据成功!"); |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | 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()); |
| | | redisUtils.set("aqi_" + cityAqiConfig.get("city_code"), map); |
| | | } |
| | | }else { |
| | | returnT = new ReturnT(500, " 插入数据失败!"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | Map<String, Object> parameters = new HashMap<>(); |
| | | parameters.put("time",pubtime); |
| | | parameters.put("data",data.toString()); |
| | | parameters.put("code",cityAqiConfig.get("city_code")); |
| | | if (!ObjectUtils.isEmpty(data)) { |
| | | try { |
| | | aqiService.insertAQIData(parameters); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if ("1".equals(cityAqiConfig.get("is_compensate"))) { |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | 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()); |
| | | jedis.hmset("aqi_" + cityAqiConfig.get("city_code"), map); |
| | | } |
| | | } |
| | | } |
| | | ReturnT returnT = new ReturnT(500, " 插入数据失败!"); |
| | | return returnT; |
| | | } |
| | | return returnT; |
| | | } |
| | | |
| | | } |