cjl
2024-01-24 c0785bf79cdd8bda01e914653c28ada7b14ddee9
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
package com.moral.api.task;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.entity.*;
import com.moral.api.service.*;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @program: screen
 * @description: 提示信息自动生成接口
 * @author: lizijie
 * @create: 2021-10-12 08:53
 **/
@Component
public class InformationTask {
 
    @Autowired
    private OrganizationService organizationService;
 
    @Autowired
    private GovMonitorPointService govMonitorPointService;
 
    @Autowired
    private HistoryAqiService historyAqiService;
 
    @Autowired
    private DeviceService deviceService;
 
    @Autowired
    private HistoryHourlyService historyHourlyService;
 
    @Autowired
    private CityWeatherService cityWeatherService;
 
    @Autowired
    private InformationService informationService;
 
    @XxlJob("informationInsert")
    public ReturnT informationInsert(){
        String params = XxlJobHelper.getJobParam();
        Map organizationIdMap = JSON.parseObject(params);
        List<Integer> orgIdList = (List<Integer>) organizationIdMap.get("orgId");
        Calendar nowCalendar = Calendar.getInstance();
        Calendar beforeCalendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        nowCalendar.set(Calendar.HOUR_OF_DAY, nowCalendar.get(Calendar.HOUR_OF_DAY) - 1);
        beforeCalendar.set(Calendar.HOUR_OF_DAY, beforeCalendar.get(Calendar.HOUR_OF_DAY) - 2);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        String nowTime = df.format(nowCalendar.getTime())+":00:00";
        String beforeTime = df.format(beforeCalendar.getTime())+":00:00";
        String realTime = df.format(new Date())+":00:00";
        for (Integer orgId:orgIdList) {
            if (!ObjectUtils.isEmpty(organizationService.getOrganizationById(orgId))){
                Organization organization = organizationService.getOrganizationById(orgId);
                Integer locationLevelCode = organization.getLocationLevelCode();
                List<GovMonitorPoint> govMonitorPoints = govMonitorPointService.getGovMonitorPointByRegionCode(locationLevelCode);
                Double PM2_5AvgBefore = null;
                Double PM10AvgBefore = null;
                Double O3AvgBefore = null;
                Double PM2_5AvgNow = null;
                Double PM10AvgNow = null;
                Double O3AvgNow = null;
                Double AQIAvgNow = null;
                Double TVOCAvgNow = null;
                Double TVOCAvgBefore = null;
                if (govMonitorPoints.size()>0){
                    List<Double> PM2_5ListBefore = new ArrayList<>();
                    List<Double> PM10ListBefore = new ArrayList<>();
                    List<Double> O3ListBefore = new ArrayList<>();
                    List<Double> PM2_5ListNow = new ArrayList<>();
                    List<Double> PM10ListNow = new ArrayList<>();
                    List<Double> O3ListNow = new ArrayList<>();
                    List<Double> AQIListNow = new ArrayList<>();
                    for (GovMonitorPoint govMonitorPoint:govMonitorPoints) {
                        String guid = govMonitorPoint.getGuid();
                        HistoryAqi beforeHistoryAqi = new HistoryAqi();
                        beforeHistoryAqi = historyAqiService.getHistoryApiByTimeAndGuid(guid,beforeTime);
                        if (!ObjectUtils.isEmpty(beforeHistoryAqi)){
                            String value = beforeHistoryAqi.getValue();
                            if (!ObjectUtils.isEmpty(value)){
                                JSONObject jsonObject = new JSONObject();
                                jsonObject = JSONObject.parseObject(value);
                                if (!ObjectUtils.isEmpty(jsonObject.get("pm2_5"))){
                                    PM2_5ListBefore.add(Double.parseDouble(jsonObject.get("pm2_5").toString()));
                                }
                                if (!ObjectUtils.isEmpty(jsonObject.get("pm10"))){
                                    PM10ListBefore.add(Double.parseDouble(jsonObject.get("pm10").toString()));
                                }
                                if (!ObjectUtils.isEmpty(jsonObject.get("o3"))){
                                    O3ListBefore.add(Double.parseDouble(jsonObject.get("o3").toString()));
                                }
                            }
                        }
                        HistoryAqi nowHistoryAqi = new HistoryAqi();
                        nowHistoryAqi = historyAqiService.getHistoryApiByTimeAndGuid(guid,nowTime);
                        if (!ObjectUtils.isEmpty(nowHistoryAqi)){
                            String value = nowHistoryAqi.getValue();
                            if (!ObjectUtils.isEmpty(value)){
                                JSONObject jsonObject = JSONObject.parseObject(value);
                                if (!ObjectUtils.isEmpty(jsonObject.get("pm2_5"))){
                                    PM2_5ListNow.add(Double.parseDouble(jsonObject.get("pm2_5").toString()));
                                }
                                if (!ObjectUtils.isEmpty(jsonObject.get("pm10"))){
                                    PM10ListNow.add(Double.parseDouble(jsonObject.get("pm10").toString()));
                                }
                                if (!ObjectUtils.isEmpty(jsonObject.get("o3"))){
                                    O3ListNow.add(Double.parseDouble(jsonObject.get("o3").toString()));
                                }
                                if (!ObjectUtils.isEmpty(jsonObject.get("aqi"))){
                                    AQIListNow.add(Double.parseDouble(jsonObject.get("aqi").toString()));
                                }
                            }
                        }
                    }
                    if (PM2_5ListBefore.size()>0){
                        PM2_5AvgBefore = (double)Math.round(PM2_5ListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (PM10ListBefore.size()>0){
                        PM10AvgBefore = (double)Math.round(PM10ListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (O3ListBefore.size()>0){
                        O3AvgBefore = (double)Math.round(O3ListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (PM2_5ListNow.size()>0){
                        PM2_5AvgNow = (double)Math.round(PM2_5ListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (PM10ListNow.size()>0){
                        PM10AvgNow = (double)Math.round(PM10ListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (O3ListNow.size()>0){
                        O3AvgNow = (double)Math.round(O3ListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (AQIListNow.size()>0){
                        AQIAvgNow = (double)Math.round(AQIListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    List<Device> devices = new ArrayList<>();
                    devices = deviceService.getDateByOrgId(orgId);
                    List<Double> TVOCListNow = new ArrayList<>();
                    List<Double> TVOCListBefore = new ArrayList<>();
                    SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        for (Device device:devices) {
                            String mac = device.getMac();
                            List<HistoryHourly> historyHourliesNow = new ArrayList<>();
                            historyHourliesNow = historyHourlyService.getValueByMacAndTime(mac, df1.parse(nowTime), df1.parse(df.format(nowCalendar.getTime()) + ":00:01"));
                            if (!ObjectUtils.isEmpty(historyHourliesNow) && historyHourliesNow.size()>0){
                                String value = historyHourliesNow.get(0).getValue();
                                JSONObject jsonObject = JSONObject.parseObject(value);
                                TVOCListNow.add(Double.parseDouble(jsonObject.get("a99054").toString()));
                            }
                            List<HistoryHourly> historyHourliesBefore = new ArrayList<>();
                            historyHourliesBefore = historyHourlyService.getValueByMacAndTime(mac, df1.parse(beforeTime), df1.parse(df.format(beforeCalendar.getTime()) + ":00:01"));
                            if (!ObjectUtils.isEmpty(historyHourliesBefore) && historyHourliesBefore.size()>0){
                                String value = historyHourliesBefore.get(0).getValue();
                                JSONObject jsonObject = JSONObject.parseObject(value);
                                TVOCListBefore.add(Double.parseDouble(jsonObject.get("a99054").toString()));
                            }
                        }
                    }catch (ParseException e){
                        e.printStackTrace();
                    }
                    if (TVOCListNow.size()>0){
                        TVOCAvgNow = (double)Math.round(TVOCListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                    if (TVOCListBefore.size()>0){
                        TVOCAvgBefore = (double)Math.round(TVOCListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage());
                    }
                }
                String temp = "";
                String humidity = "";
                String windDir = "";
                String windScale = "";
                CityWeather cityWeather = new CityWeather();
                cityWeather = cityWeatherService.getDataByCityCodeAndTime(locationLevelCode.toString(),nowTime);
                if (!ObjectUtils.isEmpty(cityWeather)){
                    String value = cityWeather.getValue();
                    if (!ObjectUtils.isEmpty(value)){
                        JSONObject jsonObject = JSONObject.parseObject(value);
                        if (!ObjectUtils.isEmpty(jsonObject.get("temp"))){
                            temp = jsonObject.getString("temp");
                        }
                        if (!ObjectUtils.isEmpty(jsonObject.get("humidity"))){
                            humidity = jsonObject.getString("humidity");
                        }
                        if (!ObjectUtils.isEmpty(jsonObject.get("windDir"))){
                            windDir = jsonObject.getString("windDir");
                        }
                        if (!ObjectUtils.isEmpty(jsonObject.get("windScale "))){
                            windScale = jsonObject.getString("windScale");
                        }
                    }
                }
                Information information = new Information();
                JSONObject jsonObject = new JSONObject();
                String realHour = realTime.substring(11,13);
                String nowHour = nowTime.substring(11,13);
                String beforeHour = beforeTime.substring(11,13);
                String title = "【"+realHour+"时数据提醒】";
                String airInfo = nowHour+"时,";
                String AQILevel = "";
                if (!ObjectUtils.isEmpty(AQIAvgNow)){
                    airInfo = airInfo + "我市AQI:"+AQIAvgNow+",等级:";
                    if (AQIAvgNow<50 || AQIAvgNow==50){
                        AQILevel = "优";
                    }else if (AQIAvgNow<100 || AQIAvgNow==100){
                        AQILevel = "良好";
                    }else if (AQIAvgNow<200 || AQIAvgNow==200){
                        AQILevel = "轻度污染";
                    }else if (AQIAvgNow<300 || AQIAvgNow==300){
                        AQILevel = "中度污染";
                    }else{
                        AQILevel = "重度污染";
                    }
                }else {
                    airInfo = airInfo + "我市AQI:"+",等级:";
                }
                airInfo = airInfo+AQILevel+"。";
                String PM10Info = "";
                PM10Info = PM10Info+"PM10:"+PM10AvgNow+"微克/立方米,";
                if (!ObjectUtils.isEmpty(PM10AvgBefore) && !ObjectUtils.isEmpty(PM10AvgNow)){
                    String upOrDown = (PM10AvgBefore<PM10AvgNow)?"上升":"下降";
                    PM10Info = PM10Info + upOrDown+Math.abs(PM10AvgNow-PM10AvgBefore)+";";
                }
                String PM2_5Info = "";
                PM2_5Info = PM2_5Info+"PM10:"+PM2_5AvgNow+"微克/立方米,";
                if (!ObjectUtils.isEmpty(PM2_5AvgBefore) && !ObjectUtils.isEmpty(PM2_5AvgNow)){
                    String upOrDown = (PM2_5AvgBefore<PM2_5AvgNow)?"上升":"下降";
                    PM2_5Info = PM2_5Info + upOrDown+Math.abs(PM2_5AvgNow-PM2_5AvgBefore)+";";
                }
                String O3Info = "";
                O3Info = O3Info+"O3:"+O3AvgNow+"微克/立方米,";
                if (!ObjectUtils.isEmpty(O3AvgBefore) && !ObjectUtils.isEmpty(O3AvgNow)){
                    String upOrDown = (O3AvgBefore<PM10AvgNow)?"上升":"下降";
                    O3Info = O3Info + upOrDown+Math.abs(O3AvgNow-O3AvgBefore)+";";
                }
                String TVOCInfo = "";
                TVOCInfo = TVOCInfo+"TVOC总量:"+TVOCAvgNow+"微克/立方米。";
                String cityWeatherInfo = "";
                cityWeatherInfo = cityWeatherInfo + "当天天气状况:温度"+temp+"℃,湿度"+humidity+"%,"+windDir+windScale+"级。";
                airInfo = airInfo+"与"+beforeHour+"相比,"+PM10Info+PM2_5Info+O3Info+TVOCInfo;
                airInfo = airInfo.replace("null","");
                cityWeatherInfo = cityWeatherInfo.replace("null","");
                List info = new ArrayList();
                info.add(airInfo);
                info.add(cityWeatherInfo);
                String author = "(七星瓢虫专家组)";
                jsonObject.put("title",title);
                jsonObject.put("info",info);
                jsonObject.put("author",author);
 
                information.setInfo(jsonObject.toString());
                information.setOrganizationId(orgId);
                information.setType("0");
                informationService.insert(information);
            }
        }
        return ReturnT.SUCCESS;
    }
}