| | |
| | | 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.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.data.redis.core.RedisTemplate; |
| | | 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; |
| | | private RedisTemplate redisTemplate; |
| | | @XxlJob("insertData") |
| | | public ReturnT 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<Map<String,Object>> CityAqiConfigs =aqiService.getCityAqiConfig(); |
| | | ReturnT returnT = null; |
| | | |
| | | for (Map<String, Object> cityAqiConfig : CityAqiConfigs) { |
| | | String entity = null ; |
| | | Collection<Object> values = null; |
| | | Map<String, Object> data = null; |
| | | try { |
| | | HttpResponse response = HttpUtils.doGet("https://api.epmap.org", "/api/v1/air/city", "GET", |
| | |
| | | ); |
| | | entity = EntityUtils.toString(response.getEntity()); |
| | | JSONObject json = JSON.parseObject(entity); |
| | | |
| | | data = (Map<String, Object>) json.get("data"); |
| | | if (!ObjectUtils.isEmpty(data)) { |
| | | values = data.values(); |
| | | pubtime = format.parse(data.get("pubtime").toString()); |
| | | }else { |
| | | continue; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (ObjectUtils.isEmpty(entity) || ObjectUtils.isEmpty(values) || values.contains("None")) { |
| | | Map<String, Object> 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<String, Object>) ((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<String, Object>) JSON.parse(string); |
| | | } |
| | | } |
| | | Map<String, Object> parameters = new HashMap<>(); |
| | | parameters.put("time",pubtime); |
| | |
| | | try { |
| | | 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); |
| | | } |
| | | returnT = new ReturnT(200, " 插入数据成功!"); |
| | | }else { |
| | | returnT = new ReturnT(500, " 插入数据失败!"); |
| | | } |
| | | } 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()); |
| | | redisTemplate.opsForHash().putAll("aqi_" + cityAqiConfig.get("city_code"),map); |
| | | } |
| | | } |
| | | } |
| | | return returnT; |