package com.moral.task; import com.alibaba.fastjson.JSON; 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 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.stereotype.Component; import org.springframework.util.ObjectUtils; import org.springframework.web.client.RestTemplate; 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); List> CityAqiConfigs =aqiService.getCityAqiConfig(); ReturnT returnT = null; for (Map cityAqiConfig : CityAqiConfigs) { String entity = 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)) { pubtime = format.parse(data.get("pubtime").toString()); }else { continue; } } catch (Exception e) { e.printStackTrace(); } 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){ if ("1".equals(cityAqiConfig.get("is_compensate"))) { returnT = new ReturnT(200, " 插入数据成功!"); 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()); redisUtils.set("aqi_" + cityAqiConfig.get("city_code"), map); } }else { returnT = new ReturnT(500, " 插入数据失败!"); } } catch (Exception e) { e.printStackTrace(); } } } return returnT; } }