于紫祥_1901
2020-12-23 e745dd38c5a413eaa000c7c5434621fbcd1800d5
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package com.moral.controller;
 
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.moral.service.*;
import com.moral.util.DatesUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.moral.common.bean.ResultBean;
import com.moral.common.util.ParameterUtils;
import com.moral.common.util.WebUtils;
import com.moral.entity.Device;
import com.moral.entity.MonitorPoint;
import com.moral.entity.charts.LineChartCriteria;
import com.moral.security.auth.JwtAuthenticationToken;
import com.moral.security.model.UserContext;
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
 
import static com.moral.common.util.ExportExcelUtils.exportData;
import static com.moral.common.util.WebUtils.getParametersStartingWith;
 
@SuppressWarnings({"unchecked", "rawtypes"})
@RestController
@RequestMapping("report")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ReportController {
 
    @Resource
    private HistoryMinutelyService historyMinutelyService;
 
    @Resource
    private AlarmDailyService alarmDailyService;
 
    @Resource
    private MonitorPointService monitorPointService;
 
    @Resource
    private SensorService sensorService;
 
    @Resource
    private HistoryService historyService;
 
    @GetMapping("compare")
    public ResultBean<Map<String, List>> getCompareReport(HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        Map<String, List> demo = historyMinutelyService.getCompareReport(parameters);
        return new ResultBean<Map<String, List>>(demo);
    }
 
    @PostMapping("line-chart")
    public ResultBean<Map<String, List<List<Double>>>> lineChart(@RequestBody LineChartCriteria lineChartCriteria) {
        return new ResultBean<>(historyMinutelyService.queryLineChartDateByCrieria(lineChartCriteria));
    }
 
    @GetMapping("excel")
    public ResultBean<Boolean> getExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Map<String, Object>> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters);
        List<String> sensors = (List<String>) parameters.get("sensors");
        String[][] exportColumn = new String[sensors.size() + 1][];
        System.out.println(list.toString());
        exportColumn[0] = new String[]{"时间", "20", "time"};
        for (int index = 0; index < sensors.size(); index++) {
            String[] split = sensors.get(index).split("-");
            String name = split[1];
            String key = split[0];
            String unit = split[2];
            if (!ObjectUtils.isEmpty(unit) && !"null".equals(unit)) {
                name += "-" + unit;
            }
            exportColumn[index + 1] = new String[]{name, "10", key};
        }
        OutputStream outputStream = exportData(response, "Excel数据", list, exportColumn);
        outputStream.flush();
        outputStream.close();
        return new ResultBean<Boolean>(true);
    }
 
    //计算臭氧平缓平均值并导出
    @GetMapping("O3Excel")
    public ResultBean<Boolean> getO3ExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Map<String,Object>> maxList=historyService.getO3AVG(parameters);
        String[][] exportColumn = new String[2][];
        exportColumn[0] = new String[]{"时间", "20", "time"};
        String name = "O3浓度";
        String key = "e15";
        String unit = "ug/m³";
        if (!ObjectUtils.isEmpty(unit) && !"null".equals(unit)) {
            name += "-" + unit;
        }
        exportColumn[1] = new String[]{name, "10", key};
        OutputStream outputStream = exportData(response, "Excel数据", maxList, exportColumn);
        outputStream.flush();
        outputStream.close();
        return new ResultBean<Boolean>(true);
    }
 
 
 
    @GetMapping("newExcel")
    public ResultBean<Boolean> getNExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        String type = parameters.get("type").toString();
        String time = parameters.get("time").toString();
        String timeb;
        if (parameters.get("timeb") == null) {
            timeb = time;
        }else {
            timeb=parameters.get("timeb").toString();
        }
 
        String[] endTimes = timeb.split("-");
        String dateFormat = "";
        String beginTime = "";
        String endTime = "";
        List<String> monthList1 = new ArrayList<>();
        Collections.addAll(monthList1,"01","03","05","07","08","10","12");
        List<String> monthList2 = new ArrayList<>();
        Collections.addAll(monthList2,"04","06","09","11");
 
        Integer year = Integer.valueOf(endTimes[0]);
        int i = 0;
        if (type.equals("year")) {
            dateFormat = "yyyy-MM";
            beginTime = time + "-01";
            endTime = timeb + "-12";
            i = Calendar.MONTH;
        } else if (type.equals("month")) {
            dateFormat = "yyyy-MM-dd";
            beginTime = time + "-01";
            i = Calendar.DAY_OF_MONTH;
            if (monthList1.contains(endTimes[1])) {
                endTime = timeb + "-31";
            } else if (monthList2.contains(endTimes[1])) {
                endTime = timeb + "-30";
            } else {
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    endTime = timeb + "-29";
                } else {
                    endTime = timeb + "-28";
                }
            }
        } else if (type.equals("day")) {
            dateFormat = "yyyy-MM-dd HH";
            beginTime = time + " 00";
            endTime = timeb + " 23";
            i = Calendar.HOUR_OF_DAY;
        }else if(type.equals("hour")){
            dateFormat="yyyy-MM-dd HH:mm";
            beginTime=time+":00:00";
            endTime=timeb+":59:00";
            i=Calendar.MINUTE;
        }
 
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginTime));
 
        ArrayList<Map<String, Object>> resultList = new ArrayList<>();
        for (long d = cal.getTimeInMillis(); d <= sdf.parse(endTime).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) {
            String format = sdf.format(d);
            Map<String, Object> map = new HashMap<>();
            map.put("time", format);
            resultList.add(map);
        }
 
        if(type.equals("year")){
            SimpleDateFormat sd = new SimpleDateFormat("yyyy");
            Date dt = sd.parse(timeb);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt);
            calendar.add(Calendar.YEAR, 1);
            Date dt1 = calendar.getTime();
            timeb = sd.format(dt1);
        }else if(type.equals("month")){
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
            Date dt = sd.parse(timeb);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt);
            calendar.add(Calendar.MONTH, 1);
            Date dt1 = calendar.getTime();
            timeb = sd.format(dt1);
        }else if(type.equals("day")){
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            Date dt = sd.parse(timeb);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date dt1 = calendar.getTime();
            timeb = sd.format(dt1);
        }else if(type.equals("hour")){
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH");
            Date dt = sd.parse(timeb);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt);
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            Date dt1 = calendar.getTime();
            timeb = sd.format(dt1);
        }
        parameters.put("timeb",timeb);
        List<Map<String, Object>> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters);
        ArrayList<String> sensorKeys = (ArrayList<String>)parameters.get("sensorKeys");
        for (Map<String, Object> map : list) {
            for (String sensorKey : sensorKeys) {
                if(map.get(sensorKey)==null){
                    map.put(sensorKey,"");
                }
            }
        }
 
        for (Map<String, Object> map : resultList) {
            for (Map<String, Object> resultMap : list) {
                if (resultMap.get("time").equals(map.get("time"))) {
                    map.putAll(resultMap);
                }
            }
        }
 
        List<String> sensors = (List<String>) parameters.get("sensors");//[e1,e2]
        String[][] exportColumn = new String[sensors.size() + 1][];
        exportColumn[0] = new String[]{"时间", "20", "time"};
        for (
                int index = 0; index < sensors.size(); index++) {
            String[] split = sensors.get(index).split("-");
            String name = split[1];
            String key = split[0];
            String unit = split[2];
            if (!ObjectUtils.isEmpty(unit) && !"null".equals(unit)) {
                name += "-" + unit;
            }
            exportColumn[index + 1] = new String[]{name, "10", key};
        }
 
        OutputStream outputStream = exportData(response, "Excel数据", resultList, exportColumn);
        outputStream.flush();
        outputStream.close();
        return new ResultBean<Boolean>(true);
    }
 
    @GetMapping("pie")
    public ResultBean<Map<String, Object>> getPieData(HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        Map pieData = alarmDailyService.getPieData(parameters);
 
        return new ResultBean<Map<String, Object>>(pieData);
    }
 
    @GetMapping("alarm-year")
    public ResultBean<List<Integer>> getAlarmDataByYear(HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Integer> result = alarmDailyService.getAlarmDataByYear(parameters);
 
        return new ResultBean<List<Integer>>(result);
    }
 
    @GetMapping("alarm-month")
    public ResultBean<List<Map<String, Object>>> getAlarmDataByMonth(HttpServletRequest request) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Map<String, Object>> result = alarmDailyService.getAlarmDataByMonth(parameters);
 
        return new ResultBean<List<Map<String, Object>>>(result);
    }
 
    @Resource
    private HistoryDailyService historyDailyService;
 
    @GetMapping("emissions")
    public ResultBean<List<Map<String, Object>>> getemissionsData(HttpServletRequest request, JwtAuthenticationToken token) throws Exception {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        UserContext userContext = token.getPrincipal();
        Integer orgId = userContext.getOrganizationId();
        parameters.put("orgId", orgId);
        List<Map<String, Object>> result = historyDailyService.getEmissionsData(parameters);
        return new ResultBean<List<Map<String, Object>>>(result);
    }
 
 
    @GetMapping("overproof")
    public ResultBean<Map> getOverproofData(HttpServletRequest request, JwtAuthenticationToken token) throws Exception {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        UserContext userContext = token.getPrincipal();
        Integer orgId = userContext.getOrganizationId();
        parameters.put("orgId", orgId);
        //List<Map<String, Object>> result = null;
        Map result = historyDailyService.getOverproofData(parameters);
        return new ResultBean<Map>(result);
    }
 
    @GetMapping("custom-made-excel")
    public ResultBean<Boolean> getCustomMadeExcelReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        System.out.println(parameters.toString());
        ParameterUtils.getTimeType4Time(parameters);
        String sensorsInfo = parameters.get("sensors").toString();
        String sensorsInfoNew = StringUtils.strip(sensorsInfo, "[]");
        String[] sensorsStr = sensorsInfoNew.split(",");
        List<List<String>> sensorInfos = sensorService.listSensorInfos(sensorsStr);
        parameters.put("sensorKeys", sensorInfos.get(0));
        List<String> macList = new ArrayList<>();
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsAndDevicesByRegion(parameters);
        for (MonitorPoint m : monitorPoints) {
            for (Device d : m.getDevices()) {
                macList.add(d.getMac());
            }
        }
        parameters.put("macs", macList);
        List<Map<String, Object>> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(macList)) {
            list = historyMinutelyService.getDevicesSensorsAvgDataToExcel(parameters);
        }
        List<List<Map<String, Object>>> sheets = sensorService.listExcelDatas(sensorInfos, list);
        if (!CollectionUtils.isEmpty(sheets)) {
            ExcelWriter writer = new ExcelWriter(false, sensorInfos.get(1).get(0));
            writer.write(sheets.get(0), true);
            writer.autoSizeColumnAll();
            writer.setColumnWidth(0, 25);
            writer.setColumnWidth(1, 25);
            if (sheets.size() >= 2) {
                for (int i = 1; i < sheets.size(); i++) {
                    writer.setSheet(sensorInfos.get(1).get(i));
                    writer.write(sheets.get(i), true);
                    writer.autoSizeColumnAll();
                    writer.setColumnWidth(0, 25);
                    writer.setColumnWidth(1, 25);
                }
            }
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            String codedFileName = java.net.URLEncoder.encode("Excel数据", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName + ".xls");
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out, true);
            // 关闭writer,释放内存
            writer.close();
            //此处记得关闭输出Servlet流
            IoUtil.close(out);
        }
        return new ResultBean<>(true);
    }
 
}