From ab33cd64a1c4f4c59b3af97dd9b6391126646d19 Mon Sep 17 00:00:00 2001
From: ZhuDongming <773644075@qq.com>
Date: Mon, 22 Jun 2020 17:33:13 +0800
Subject: [PATCH] update
---
src/main/java/com/moral/controller/ReportController.java | 245 +++++++++++++++++++++++++++++++++++++-----------
1 files changed, 186 insertions(+), 59 deletions(-)
diff --git a/src/main/java/com/moral/controller/ReportController.java b/src/main/java/com/moral/controller/ReportController.java
index b4ef1d7..29ccd68 100644
--- a/src/main/java/com/moral/controller/ReportController.java
+++ b/src/main/java/com/moral/controller/ReportController.java
@@ -1,17 +1,22 @@
package com.moral.controller;
import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+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;
@@ -20,7 +25,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
-import com.alibaba.fastjson.JSONObject;
import com.moral.common.bean.ResultBean;
import com.moral.common.util.ParameterUtils;
import com.moral.common.util.WebUtils;
@@ -29,10 +33,6 @@
import com.moral.entity.charts.LineChartCriteria;
import com.moral.security.auth.JwtAuthenticationToken;
import com.moral.security.model.UserContext;
-import com.moral.service.AlarmDailyService;
-import com.moral.service.HistoryDailyService;
-import com.moral.service.HistoryMinutelyService;
-import com.moral.service.MonitorPointService;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
@@ -55,6 +55,12 @@
@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);
@@ -73,6 +79,7 @@
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("-");
@@ -85,6 +92,168 @@
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);
@@ -142,27 +311,13 @@
@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[] sensorsStr = sensorsInfo.split(",");
- List<String> sensorKeys = new ArrayList<>();
- List<String> sensorKeysNames = new ArrayList<>();
- List<String> sensorKeysUnits = new ArrayList<>();
- for (int index = 0; index < sensorsStr.length; index++) {
- String[] split = sensorsStr[index].split("-");
- String key;
- if (index == 0) {
- key = split[0].replace("\"", "").substring(1);
- } else {
- key = split[0].replace("\"", "");
- }
- String name = split[1].replace("\"", "");
- String Unit = split[2].replace("\"", "");
- sensorKeys.add(key);
- sensorKeysNames.add(name);
- sensorKeysUnits.add(Unit);
- }
- parameters.put("sensorKeys", sensorKeys);
+ 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) {
@@ -175,44 +330,16 @@
if (!CollectionUtils.isEmpty(macList)) {
list = historyMinutelyService.getDevicesSensorsAvgDataToExcel(parameters);
}
- List<List<Map<String, Object>>> sheets = new ArrayList<>();
- for (int i = 0; i < sensorKeys.size(); i++) {
- List<Map<String, Object>> sheet = new ArrayList<>();
- for (int j = 0; j < list.size(); j++) {
- Map<String, Object> data = new LinkedHashMap<>();
- for (Map.Entry<String, Object> kv : list.get(j).entrySet()) {
- if ("monitorPointName".equals(kv.getKey())) {
- data.put("������������(������:" + sensorKeysUnits.get(i) + ")", kv.getValue());
- } else if ("name".equals(kv.getKey())) {
- data.put("������������", kv.getValue());
- } else {
- String sensorsValue = kv.getValue().toString();
- JSONObject jsonObject = JSONObject.parseObject(sensorsValue);
- if (jsonObject != null) {
- List<Object> sensorsValueList = (List<Object>) jsonObject.get(sensorKeys.get(i));
- if (sensorsValueList != null) {
- data.put(kv.getKey(), sensorsValueList.get(0));
- } else {
- data.put(kv.getKey(), "");
- }
- } else {
- data.put(kv.getKey(), "");
- }
- }
- }
- sheet.add(data);
- }
- sheets.add(sheet);
- }
+ List<List<Map<String, Object>>> sheets = sensorService.listExcelDatas(sensorInfos, list);
if (!CollectionUtils.isEmpty(sheets)) {
- ExcelWriter writer = new ExcelWriter(false, sensorKeysNames.get(0));
+ 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(sensorKeysNames.get(i));
+ writer.setSheet(sensorInfos.get(1).get(i));
writer.write(sheets.get(i), true);
writer.autoSizeColumnAll();
writer.setColumnWidth(0, 25);
--
Gitblit v1.8.0