lizijie
2022-08-31 1f88041780dad4d85bc68bbf4876c80c3fc3b614
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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<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",
                        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)) {
                    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<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);
            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<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());
                    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, "无国控数据,插入失败,请查看接口");
        }
    }
}