cjl
2023-12-14 6992aaf0587c09f7c511c1afd12e1519d91363d3
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.GovMonitorPoint;
import com.moral.api.entity.HistoryAqi;
import com.moral.api.mapper.HistoryAqiMapper;
import com.moral.api.service.GovMonitorPointService;
import com.moral.api.service.HistoryAqiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.HttpUtils;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
import com.xxl.job.core.context.XxlJobHelper;
 
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DoubleValue;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
 
/**
 * <p>
 * 国控aqi数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-06-15
 */
@Service
@Slf4j
public class HistoryAqiServiceImpl extends ServiceImpl<HistoryAqiMapper, HistoryAqi> implements HistoryAqiService {
 
    @Autowired
    private HistoryAqiMapper historyAqiMapper;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private GovMonitorPointService govMonitorPointService;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
 
    //国控站aqi数据来自阿里云市场,大中华地区空气质量API--单站点接口
    @Override
    @Transactional
    public void insertHistoryAqi(String dateTime) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "APPCODE 31b6ea8f804a4472be3b633cfee44849");
 
        HttpEntity requestEntity = new HttpEntity<>(headers);
 
        QueryWrapper<GovMonitorPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("guid").eq("is_delete", Constants.NOT_DELETE);
        /*//
        queryWrapper.eq("guid","d0955320-4632-4276-b9e2-959e5ace49b3");*/
        //获取所有国控,省控,县控站点
        String time1 = StringUtils.isNotEmpty(dateTime)?dateTime:DateUtils.dateToDateString(new Date(),DateUtils.yyyy_MM_dd_HH_EN);
 
        List<GovMonitorPoint> govMonitorPoints = govMonitorPointService.list(queryWrapper);
        Date time = DateUtils.dataToTimeStampTime(DateUtils.getDate(time1, DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN);
 
        //String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
        String timeStr = DateUtils.dateToDateString(DateUtils.addHours(time, -1), DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
        List<HistoryAqi> historyAqis = new ArrayList<>();
 
        for (GovMonitorPoint govMonitorPoint : govMonitorPoints) {
            HistoryAqi historyAqi = new HistoryAqi();
            String guid = govMonitorPoint.getGuid();
            if (guid.equals("f42092ed-cdf3-4ddc-a56f-0ac80a560847") || guid.equals("cedf9934-f3b0-499d-81cd-a17a109aa100")) {
 
                ResponseEntity<String> response;
                try {
                    //从第三方接口获取数据
                    response = restTemplate.exchange("http://chinair.market.alicloudapi.com/api/v1/air_all/station_realtime?guid={1}&pubtime={2}", HttpMethod.GET, requestEntity, String.class, "525d6346-f6c9-40e2-b3b6-a86fb7c85855", timeStr);
                } catch (Exception e) {
                    continue;
                }
                String body = response.getBody();
                Map<String, Object> data = JSONObject.parseObject(body, Map.class);
                Map<String, Object> map = (Map<String, Object>) data.get("data");
                if (ObjectUtils.isEmpty(map)) {
                    continue;
                }
                historyAqi.setGuid(guid);
                historyAqi.setTime(DateUtils.addHours(time, -1));
                //存入数据库
                historyAqi.setValue(JSONObject.toJSONString(map));
 
                historyAqis.add(historyAqi);
 
                Map<String, Object> value = new HashMap<>();
                Object pm2_5 = map.get("pm2_5");
                Object pm10 = map.get("pm10");
                Object so2 = map.get("so2");
                Object no2 = map.get("no2");
                Object co = map.get("co");
                Object o3 = map.get("o3");
                if (!ObjectUtils.isEmpty(pm2_5)) {
                    value.put(Constants.SENSOR_CODE_PM25, pm2_5);
                }
 
                if (!ObjectUtils.isEmpty(pm10)) {
                    value.put(Constants.SENSOR_CODE_PM10, pm10);
                }
 
                if (!ObjectUtils.isEmpty(so2)) {
                    value.put(Constants.SENSOR_CODE_SO2, so2);
                }
 
                if (!ObjectUtils.isEmpty(no2)) {
                    value.put(Constants.SENSOR_CODE_NO2, no2);
                }
 
                if (!ObjectUtils.isEmpty(co)) {
                    value.put(Constants.SENSOR_CODE_CO, Double.parseDouble(co.toString()));
                }
 
                if (!ObjectUtils.isEmpty(o3)) {
                    value.put(Constants.SENSOR_CODE_O3, o3);
                }
                //aqi数据存入redis
                redisTemplate.opsForHash().put(RedisConstants.AQI_DATA, guid, value);
            }else {
                ResponseEntity<String> response;
                try {
                    //从第三方接口获取数据
                    response = restTemplate.exchange("http://chinair.market.alicloudapi.com/api/v1/air_all/station_realtime?guid={1}&pubtime={2}", HttpMethod.GET, requestEntity, String.class, guid, timeStr);
                } catch (Exception e) {
                    continue;
                }
                String body = response.getBody();
                Map<String, Object> data = JSONObject.parseObject(body, Map.class);
                Map<String, Object> map = (Map<String, Object>) data.get("data");
                if (ObjectUtils.isEmpty(map)) {
                    continue;
                }
                historyAqi.setGuid(guid);
                historyAqi.setTime(DateUtils.addHours(time, -1));
                //存入数据库
                historyAqi.setValue(JSONObject.toJSONString(map));
                historyAqis.add(historyAqi);
 
                Map<String, Object> value = new HashMap<>();
                Object pm2_5 = map.get("pm2_5");
                Object pm10 = map.get("pm10");
                Object so2 = map.get("so2");
                Object no2 = map.get("no2");
                Object co = map.get("co");
                Object o3 = map.get("o3");
                if (!ObjectUtils.isEmpty(pm2_5)) {
                    value.put(Constants.SENSOR_CODE_PM25, pm2_5);
                }
 
                if (!ObjectUtils.isEmpty(pm10)) {
                    value.put(Constants.SENSOR_CODE_PM10, pm10);
                }
 
                if (!ObjectUtils.isEmpty(so2)) {
                    value.put(Constants.SENSOR_CODE_SO2, so2);
                }
 
                if (!ObjectUtils.isEmpty(no2)) {
                    value.put(Constants.SENSOR_CODE_NO2, no2);
                }
 
                if (!ObjectUtils.isEmpty(co)) {
                    value.put(Constants.SENSOR_CODE_CO, Double.parseDouble(co.toString()));
                }
 
                if (!ObjectUtils.isEmpty(o3)) {
                    value.put(Constants.SENSOR_CODE_O3, o3);
                }
                //aqi数据存入redis
                redisTemplate.opsForHash().put(RedisConstants.AQI_DATA, guid, value);
            }
 
        }
        if (ObjectUtils.isEmpty(historyAqis)){
            XxlJobHelper.log("数据不存在");
        }
        //存入数据库
        historyAqiMapper.insertHistoryAqi(historyAqis);
    }
 
    @Override
    public HistoryAqi getHistoryApiByTimeAndGuid(String guid, String time) {
        QueryWrapper<HistoryAqi> wrapper_historyAqi = new QueryWrapper<>();
        wrapper_historyAqi.eq("guid", guid).eq("time", time);
        HistoryAqi historyAqi = new HistoryAqi();
        if (historyAqiMapper.selectCount(wrapper_historyAqi) == 1) {
            historyAqi = historyAqiMapper.selectOne(wrapper_historyAqi);
        }
        return historyAqi;
    }
 
    @Override
    public List<HistoryAqi> getHistoryAqi() {
        String time = DateUtils.getDateStringOfHour(-1, DateUtils.yyyy_MM_dd_HH_EN) + ":00:00";
        QueryWrapper<HistoryAqi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("time", time);
        return historyAqiMapper.selectList(queryWrapper);
    }
 
    @Override
    public void insertHCHistoryAqi() {
        Date endTime = DateUtils.dataToTimeStampTime(new Date(), "yyyy/MM/dd HH:mm");
        Date startTime = DateUtils.addHours(endTime,-8);
        String endTimeStr = DateUtils.dateToDateString(endTime, "yyyy/MM/dd HH")+":00";
        String startStr = DateUtils.dateToDateString(startTime, "yyyy/MM/dd HH")+":00";
        cityHc(startStr,endTimeStr);
    }
    public void cityHc(String startTime , String endTime){
        String guid = "494942fc-707d-4997-8ada-02cf03a48ca8";
        //String cook ="ASP.NET_SessionId=u1wknnkdq4qjq2rgtdx23jxg; __RequestVerificationToken=Goi78jPi7W5n6rXeetyuDY6meazKED4bLSHbuMiDdJgDH8j5iCW_Dq1hqmTYGKiseHIslQOa9lhOEgGzcFhHxpnYVoC-1fkYp9olbgRyVSfSwGx53EQWZwTqL5X8XszfDTJhuWMcK7qONpSHIBfzzA2; Captcha=A7B9FB; .ASPXAUTH=06E9FA83AFFAD6D023922A9814118F0D3E97B16AF1D814CAAFED4E2E64416E5F4A17649F8C71180BCFE30DD72EB8EDFE6A997B204BF3C6F361C47F650301820CC35C0E9DEC3F22F52F9919EA38A8AFDA294CE107C2F477A4AD5B397268943AE6E7FEB1998DA4A6F6F067B8ABF0C4C8F2C8A6DD337B491D8458B4EA5A9C215549";
        String cook = redisTemplate.opsForHash().get("hc","cook").toString();
        String host = "http://218.60.150.228:8008";
        String path = "//Layui/GetTableData";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Cookie", cook);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("StationId","9efce5f9-f5d0-43e8-83ef-9aa7421efe63");
        querys.put("StationId_Text","海城北顺城路");
        querys.put("MItemId","5780ec98-a292-4727-9db2-d48c23fe5df5%2C262c4695-a376-4d3c-b122-2551897c1f8f%2Cca3ca54d-6525-4087-8fe5-bbdd77790317%2Cdb38b109-aeed-4888-bb2d-7cff951fd8c6%2Cbc3d741f-89b5-486d-aa25-b0e6a8ea8d2f%2C05e1dc49-5f09-45bc-8214-ceeb79b723d4%2Cba36afa5-2af8-4eb8-a52e-e8e653ce480a%2C9422b004-9761-4ca2-a725-4e998e5bb194%2Cdc3e1cf6-0771-4202-976b-199a3c0d66f5%2Cf02e3db5-5a42-4e1e-9bf2-94f8ee506e08%2Cbc39097e-4623-4076-8b5c-41c5b11d4b9b%2C2bcf2b6d-1588-43be-a2a6-ec4d5d52b707%2Cba20e1eb-23f7-44ea-8159-3e165a50c47c%2C06293d01-fb29-4e36-8dfa-763ef917a071%2C34f2e0e3-7230-45f5-aeac-f7da85f0b8a6%2C33f81201-5c25-459f-96ef-89e66e39d518%2C24173325-a890-4326-a909-1fc92da8d905");
        querys.put("sdtDate",startTime);
        querys.put("edtDate",endTime);
        querys.put("WorkCondition","2");
        querys.put("_CTRL_ID","MonValueTable");
        querys.put("_PAGE_ID","MonValue");
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, new HashMap<>(),querys);
            System.out.println(response.toString());
            //获取response的body
            String msg = EntityUtils.toString(response.getEntity());
            Map<String, Object> data = JSON.parseObject(msg, Map.class);
            String mac = data.get("Data").toString();
            JSONArray jsonArray = JSONArray.parseArray(mac);
            if(jsonArray.size()>0){
                JSONObject object = jsonArray.getJSONObject(jsonArray.size()-1);
                String pm2_5 = object.get("262c4695-a376-4d3c-b122-2551897c1f8f_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String pm10 = object.get("ca3ca54d-6525-4087-8fe5-bbdd77790317_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String so2 = object.get("db38b109-aeed-4888-bb2d-7cff951fd8c6_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String no2 = object.get("bc3d741f-89b5-486d-aa25-b0e6a8ea8d2f_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String no = object.get("05e1dc49-5f09-45bc-8214-ceeb79b723d4_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String nox = object.get("ba36afa5-2af8-4eb8-a52e-e8e653ce480a_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String co = object.get("9422b004-9761-4ca2-a725-4e998e5bb194_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String o3 = object.get("dc3e1cf6-0771-4202-976b-199a3c0d66f5_15e1b93e-3827-407f-9121-081555a512c5").toString();
                String pubtime = object.get("DistrictId").toString();
                String station = object.get("TimePoint").toString();
                String city = object.get("CityId").toString();
                Map<String,Object> map = new HashMap<>();
                map.put("pm2_5", StringUtils.isNotEmpty(pm2_5)? Double.parseDouble(pm2_5):0d);
                map.put("pm10", StringUtils.isNotEmpty(pm10)? Double.parseDouble(pm10):0d);
                map.put("so2", StringUtils.isNotEmpty(so2)? Double.parseDouble(so2):0d);
                map.put("no2", StringUtils.isNotEmpty(no2)? Double.parseDouble(no2):0d);
                map.put("no", StringUtils.isNotEmpty(no)? Double.parseDouble(no):0d);
                map.put("co", StringUtils.isNotEmpty(co)? Double.parseDouble(co):0d);
                map.put("o3", StringUtils.isNotEmpty(o3)? Double.parseDouble(o3):0d);
                map.put("pubtime", pubtime);
                map.put("station", station);
                map.put("city", city);
                HistoryAqi historyAqi = new HistoryAqi();
                historyAqi.setValue(JSONObject.toJSONString(map));
                historyAqi.setGuid(guid);
                Date time = DateUtils.getDate(object.get("TimePoint").toString(), DateUtils.yyyy_MM_dd_HH_EN);
                historyAqi.setTime(DateUtils.addHours(time,-1));
                Map<String, Object> value = new HashMap<>();
                if (StringUtils.isNotEmpty(pm2_5)) {
                    value.put(Constants.SENSOR_CODE_PM25, Double.parseDouble(pm2_5));
                }
 
                if (StringUtils.isNotEmpty(pm10)) {
                    value.put(Constants.SENSOR_CODE_PM10, Double.parseDouble(pm10));
                }
 
                if (StringUtils.isNotEmpty(so2)) {
                    value.put(Constants.SENSOR_CODE_SO2, Double.parseDouble(so2));
                }
 
                if (StringUtils.isNotEmpty(no2)) {
                    value.put(Constants.SENSOR_CODE_NO2, Double.parseDouble(no2));
                }
 
                if (StringUtils.isNotEmpty(co)) {
                    value.put(Constants.SENSOR_CODE_CO, Double.parseDouble(co.toString()));
                }
 
                if (StringUtils.isNotEmpty(o3)) {
                    value.put(Constants.SENSOR_CODE_O3, Double.parseDouble(o3));
                }
                //aqi数据存入redis
                redisTemplate.opsForHash().put(RedisConstants.AQI_DATA, guid, value);
                historyAqiMapper.insertHistoryAqi(Arrays.asList(historyAqi));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}