From 0f41a81a6b28e591bfe734ccd8327a936f9c29e0 Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Mon, 01 Nov 2021 09:16:41 +0800
Subject: [PATCH] screen-api            增加获取一个城市24小时AQI接口

---
 screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java |  139 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 135 insertions(+), 4 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java
index ac689eb..67408e5 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java
@@ -3,7 +3,9 @@
 import com.alibaba.fastjson.JSON;
 import com.moral.api.entity.*;
 import com.moral.api.pojo.dto.dataDisplay.MonitorPointDataDisplayDTO;
+import com.moral.api.pojo.dto.dataDisplay.SensorComparisonDisplayDTO;
 import com.moral.api.pojo.form.dataDisplay.MonitorPointDataDisplayForm;
+import com.moral.api.pojo.form.dataDisplay.SensorComparisonDisplayForm;
 import com.moral.api.service.*;
 import com.moral.constant.Constants;
 import com.moral.util.*;
@@ -65,7 +67,7 @@
                 dtos = calculateCustomData(macDataMap, deviceMap, startTime, endTime);
         }
         //���������������������������������������������������������
-        else if (reportType.equals(Constants.HOURYLYREPORT)) {
+        else if (reportType.equals(Constants.HOURLY_REPORT)) {
             Map<String, HistoryHourly> macDataMap = new HashMap<>();
             //������������
             macs.forEach(value -> {
@@ -77,21 +79,21 @@
                 dtos = calculateReportData(macDataMap, deviceMap, startTime);
         }
         //���������������������������������������������
-        else if (reportType.equals(Constants.DAILYREPORT)) {
+        else if (reportType.equals(Constants.DAILY_REPORT)) {
             //������������
             Map<String, HistoryDaily> macDataMap = historyDailyService.getHistoryDailyByMacsAndDate(macs, startTime);
             if (macDataMap.size() != 0)
                 dtos = calculateReportData(macDataMap, deviceMap, startTime);
         }
         //���������������������������������������������
-        else if (reportType.equals(Constants.WEEKLYREPORT)) {
+        else if (reportType.equals(Constants.WEEKLY_REPORT)) {
             //������������
             Map<String, HistoryWeekly> macDataMap = historyWeeklyService.getHistoryWeeklyByMacsAndDate(macs, startTime);
             if (macDataMap.size() != 0)
                 dtos = calculateReportData(macDataMap, deviceMap, startTime, endTime);
         }
         //������������������
-        else if (reportType.equals(Constants.MONTHLYREPORT)) {
+        else if (reportType.equals(Constants.MONTHLY_REPORT)) {
             //������������������������������������������������������������������������������������������������
             Map<String, List<HistoryDaily>> macDataDailyMap = new HashMap<>();
             Map<String, HistoryMonthly> macDataMonthlyMap = historyMonthlyService.getHistoryMonthlyByMacsAndDate(macs, startTime);
@@ -108,6 +110,135 @@
         return dtos;
     }
 
+    @Override
+    public List<SensorComparisonDisplayDTO> getSensorComparisonDisplayData(SensorComparisonDisplayForm form) {
+        //������
+        List<String> sensors = form.getSensors();
+        Date startDate = form.getStartDate();
+        Date endDate = form.getEndDate();
+        String mac = form.getMac();
+        String reportType = form.getReportType();
+        Map<String, String> timeValueMap = new LinkedHashMap<>();//key���time���value������������json
+        //������������������
+        if (reportType.equals(Constants.DAILY_REPORT)) {
+            List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate);
+            for (HistoryHourly historyHourly : hourlies) {
+                Date time = historyHourly.getTime();
+                String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH");
+                String value = historyHourly.getValue();
+                timeValueMap.put(dateStr, value);
+            }
+            //���������������������������������������24������������������
+            for (int i = 0; i < 24; i++) {
+                //������������
+                String time = DateUtils.dateToDateString(startDate, "yyyy-MM-dd");
+                if (i < 10)
+                    time = time + " 0" + i;
+                else
+                    time = time + " " + i;
+                if (timeValueMap.get(time) == null)
+                    timeValueMap.put(time, null);
+            }
+        }
+        //������������������
+        else if (reportType.equals(Constants.MONTHLY_REPORT)) {
+            List<HistoryDaily> dailies = historyDailyService.getHistoryDailyByMacAndTimeSlot(mac, startDate, endDate);
+            for (HistoryDaily historyDaily : dailies) {
+                Date time = historyDaily.getTime();
+                String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd");
+                String value = historyDaily.getValue();
+                timeValueMap.put(String.valueOf(dateStr), value);
+            }
+            //������������������������������������������������������
+            int days = DateUtils.getMonthDay(startDate);
+            for (int i = 1; i <= days; i++) {
+                //������������
+                String time = DateUtils.dateToDateString(startDate, "yyyy-MM");
+                if (i < 10)
+                    time = time + "-0" + i;
+                else
+                    time = time + "-" + i;
+                if (timeValueMap.get(time) == null)
+                    timeValueMap.put(time, null);
+            }
+        } else
+            return null;
+        //������������
+        timeValueMap = sortMapByTime(timeValueMap, reportType);
+        //������������������
+        List<SensorComparisonDisplayDTO> dtos = new ArrayList<>();
+        for (String sensor : sensors) {
+            SensorComparisonDisplayDTO dto = new SensorComparisonDisplayDTO();
+            List<Map<String, Object>> dtoTimeValueList = new ArrayList<>();
+            dto.setSensorCode(sensor);
+            timeValueMap.forEach((time, valueJson) -> {
+                Map<String, Object> listMap = new HashMap<>();
+                //���������������������������������������������
+                if (valueJson == null) {
+                    listMap.put("time", time);
+                    listMap.put("value", "");
+                    dtoTimeValueList.add(listMap);
+                    return;
+                }
+                Map<String, Object> valueMap = JSON.parseObject(valueJson, Map.class);
+                Object sensorValueObject = valueMap.get(sensor);
+                //���������������������������������������������
+                if (sensorValueObject == null) {
+                    listMap.put("time", time);
+                    listMap.put("value", "");
+                    dtoTimeValueList.add(listMap);
+                    return;
+                }
+                //������������������������������������������
+                if (reportType.equals(Constants.DAILY_REPORT)) {
+                    //���������������������N���������������
+                    if (!Constants.NORMAL_FLAG.equals(valueMap.get(sensor + "-Flag"))) {
+                        listMap.put("time", time);
+                        listMap.put("value", "");
+                        dtoTimeValueList.add(listMap);
+                        return;
+                    }
+                }
+                //������������
+                Double sensorValue = Double.parseDouble(sensorValueObject.toString());
+                //������������
+                listMap.put("time", time);
+                listMap.put("value", sensorValue);
+                dtoTimeValueList.add(listMap);
+            });
+            dto.setTimeValueList(dtoTimeValueList);
+            dtos.add(dto);
+        }
+        return dtos;
+    }
+
+    private Map<String, String> sortMapByTime(Map<String, String> timeValueMap, String reportType) {
+        List<Map.Entry<String, String>> entries = new ArrayList(timeValueMap.entrySet());
+        Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
+            @Override
+            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
+                if (Constants.DAILY_REPORT.equals(reportType)) {
+                    String atime = o1.getKey();
+                    String btime = o2.getKey();
+                    atime = atime.substring(11, 13);
+                    btime = btime.substring(11, 13);
+                    return Integer.parseInt(atime) - Integer.parseInt(btime);
+                } else if (Constants.MONTHLY_REPORT.equals(reportType)) {
+                    String atime = o1.getKey();
+                    String btime = o2.getKey();
+                    atime = atime.substring(8, 10);
+                    btime = btime.substring(8, 10);
+                    return Integer.parseInt(atime) - Integer.parseInt(btime);
+                }
+                return 0;
+            }
+        });
+        Map<String, String> sortedMap = new LinkedHashMap<>();
+        for (Map.Entry<String, String> entry : entries) {
+            sortedMap.put(entry.getKey(), entry.getValue());
+        }
+        return sortedMap;
+    }
 
     /**
      * @Description: ������������������������������

--
Gitblit v1.8.0