From e25c689888a0dd80d07ffc4e4a45bd290b3d010c Mon Sep 17 00:00:00 2001
From: yuzixiang <yzx123456>
Date: Tue, 19 May 2020 16:50:21 +0800
Subject: [PATCH] 计算臭氧平缓平均值

---
 src/main/java/com/moral/service/HistoryService.java          |    6 ++
 src/main/java/com/moral/service/impl/HistoryServiceImpl.java |   10 +++
 src/main/java/com/moral/util/DatesUtil.java                  |   56 ++++++++++++++++++
 src/main/java/com/moral/controller/ReportController.java     |   77 ++++++++++++++++++++++++-
 src/main/java/com/moral/mapper/HistoryMapper.java            |    6 ++
 src/main/resources/mapper/HistoryMapper.xml                  |   10 +++
 6 files changed, 160 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/moral/controller/ReportController.java b/src/main/java/com/moral/controller/ReportController.java
index d3c7e6c..23dec7c 100644
--- a/src/main/java/com/moral/controller/ReportController.java
+++ b/src/main/java/com/moral/controller/ReportController.java
@@ -15,6 +15,8 @@
 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.springframework.util.ObjectUtils;
@@ -33,11 +35,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 com.moral.service.SensorService;
 
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.poi.excel.ExcelWriter;
@@ -63,6 +60,9 @@
     @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);
@@ -81,6 +81,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("-");
@@ -97,6 +98,71 @@
         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);
+        String mac= (String) parameters.get("mac");
+        String stime= (String) parameters.get("time");
+        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM");
+        Date time =format.parse(stime);
+        List<String> stringListDate = DatesUtil.getAllTheDateOftheMonth(time);
+        List<Map<String,Object>> maxList=new ArrayList<Map<String,Object>>();
+        for (String sld : stringListDate) {
+            Map<String,Object>  avgMap=new HashMap<String,Object>();
+            List<String> listavg=new ArrayList<String>();
+            String s1="";
+            for(int i=0;i<24;i++){
+                if (i<10){
+                    sld=sld.substring(0,10);
+                    sld=sld+" 0"+i+":00:00";
+                    s1=DatesUtil.pinDate(sld);
+                }else {
+                    sld=sld.substring(0,10);
+                    sld=sld+" "+i+":00:00";
+                    s1=DatesUtil.pinDate(sld);
+                }
+                System.out.println(sld+"        "+s1);
+                Map<String, Object> parm = new HashMap<String, Object>();
+                parm.put("frontTime", sld);
+                parm.put("afterTime", s1);
+                parm.put("mac", mac);
+                String avg = historyService.getO3AVG(parm);
+                if (avg!=null){
+                    listavg.add(avg);
+                }
+                }
+            String maxO3="";
+            if (listavg.size()!=0){
+                String subs=sld.substring(0,10);
+                String timef=subs+" 00:00:00";
+                String timea=subs+" 23:59:59";
+                int num=historyService.getNum(timef,timea);
+                if (num==0){
+                    maxO3="";
+                }else {
+                    maxO3= Collections.max(listavg);
+                }
+            }
+            avgMap.put("time",sld.substring(0,10));
+            avgMap.put("e15",maxO3);
+            maxList.add(avgMap);
+        }
+        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 {
@@ -290,6 +356,7 @@
     @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, "[]");
diff --git a/src/main/java/com/moral/mapper/HistoryMapper.java b/src/main/java/com/moral/mapper/HistoryMapper.java
index f15fe72..2779451 100644
--- a/src/main/java/com/moral/mapper/HistoryMapper.java
+++ b/src/main/java/com/moral/mapper/HistoryMapper.java
@@ -31,4 +31,10 @@
     List<Map<String, Object>> getCarSensorData(Map<String, Object> parameters);
 
     List<Map<String, Object>> listGetSensorData(Map<String, Object> parameters);
+
+	//���������������������������
+	String getO3AVG(Map<String, Object> parameters);
+
+	int getNum(@Param("timef") String timef,
+			   @Param("timea") String timea);
 }
\ No newline at end of file
diff --git a/src/main/java/com/moral/service/HistoryService.java b/src/main/java/com/moral/service/HistoryService.java
index aad4b39..5956c32 100644
--- a/src/main/java/com/moral/service/HistoryService.java
+++ b/src/main/java/com/moral/service/HistoryService.java
@@ -30,4 +30,10 @@
 	Map<String, Object> gitHourlyAQIByMonitorPointIdAndTimeslot(Map<String, Object> parameters);
 
     List<List<Map<String, Object>>> getCarSensorData(Map<String, Object> parameters) throws Exception;
+
+    //���������������������������
+	String getO3AVG(Map<String, Object> parameters);
+
+	int getNum(String timef,String timea);
+
 }
diff --git a/src/main/java/com/moral/service/impl/HistoryServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
index 0de5639..797a392 100644
--- a/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/HistoryServiceImpl.java
@@ -548,4 +548,14 @@
         }
         return listMaps;
     }
+
+    @Override
+    public String getO3AVG(Map<String, Object> parameters) {
+        return historyMapper.getO3AVG(parameters);
+    }
+
+    @Override
+    public int getNum(String timef, String timea) {
+        return historyMapper.getNum(timef,timea);
+    }
 }
diff --git a/src/main/java/com/moral/util/DatesUtil.java b/src/main/java/com/moral/util/DatesUtil.java
new file mode 100644
index 0000000..0e5ce58
--- /dev/null
+++ b/src/main/java/com/moral/util/DatesUtil.java
@@ -0,0 +1,56 @@
+package com.moral.util;
+
+import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+public class DatesUtil {
+    public static List<String> getAllTheDateOftheMonth(Date date) {
+        List<String> list = new ArrayList<String>();
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(date);
+        cal.set(Calendar.DATE, 1);
+        int month = cal.get(Calendar.MONTH);
+        while(cal.get(Calendar.MONTH) == month){
+            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
+            String time=sf.format(cal.getTime());
+            list.add(time);
+            cal.add(Calendar.DATE, 1);
+        }
+        return list;
+    }
+    public static  String pinDate(String s) {
+        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        LocalDateTime time = LocalDateTime.parse(s, df);
+        LocalDateTime time1 = time.plusHours(8);
+        int year = time1.getYear();
+        int month = time1.getMonthValue();
+        int day = time1.getDayOfMonth();
+        int hour = time1.getHour();
+        String m = "";
+        String d = "";
+        String h = "";
+        if (month < 10) {
+            m = "0" + month;
+        }else {
+            m=""+month;
+        }
+        if (day < 10) {
+            d = "0" + day;
+        }else {
+            d=""+day;
+        }
+        if (hour < 10) {
+            h = "0" + hour;
+        }else {
+            h=""+hour;
+        }
+        s = year + "-" + m + "-" + d + " " + h + ":00:00";
+        return s;
+    }
+
+}
diff --git a/src/main/resources/mapper/HistoryMapper.xml b/src/main/resources/mapper/HistoryMapper.xml
index a9ce061..603a438 100644
--- a/src/main/resources/mapper/HistoryMapper.xml
+++ b/src/main/resources/mapper/HistoryMapper.xml
@@ -254,5 +254,15 @@
         ORDER BY
         h.time
     </select>
+    <select id="getO3AVG" resultType="java.lang.String">
+        select AVG(history_hourly.json->'$.e15[0]')
+        FROM history_hourly
+         where time <![CDATA[>=]]>#{frontTime}
+         and time <![CDATA[<]]>#{afterTime}
+         and mac=#{mac}
+    </select>
+    <select id="getNum" resultType="java.lang.Integer">
+        select count(*) from history_hourly where time <![CDATA[>=]]> #{timef} and time <![CDATA[<=]]> #{timea}
+    </select>
 
 </mapper>
\ No newline at end of file

--
Gitblit v1.8.0