From 3a4442da13b6f1300956bd63a5d106c5b2fc7184 Mon Sep 17 00:00:00 2001
From: 于紫祥_1901 <email@yuzixiang_1910>
Date: Thu, 22 Oct 2020 11:19:00 +0800
Subject: [PATCH] update(post)

---
 src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java |  234 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 202 insertions(+), 32 deletions(-)

diff --git a/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
index 6ba5929..45ae468 100644
--- a/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
@@ -1,11 +1,9 @@
 package com.moral.service.impl;
 
 import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-import javax.annotation.Resource;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 import org.apache.commons.collections.MapUtils;
 import org.springframework.stereotype.Service;
@@ -17,6 +15,9 @@
 import com.moral.mapper.SensorMapper;
 import com.moral.service.DeviceService;
 import com.moral.service.HistoryHourlyService;
+import com.moral.service.SensorService;
+
+import javax.annotation.Resource;
 
 @Service
 public class HistoryHourlyServiceImpl implements HistoryHourlyService {
@@ -25,6 +26,9 @@
 
     @Resource
     private DeviceService deviceService;
+
+    @Resource
+    private SensorService sensorService;
 
     @Resource
     private SensorMapper sensorMapper;
@@ -37,31 +41,197 @@
             sensorKeys.add(sensor.getSensorKey());
         }
         parameters.put("sensorKeys", sensorKeys);
-       /* if(pollutionSourceData.get("e6")!=null&&pollutionSourceData.get("e18")!=null){
-            pollutionSourceData=historyHourlyMapper.getPollutionSourceData(parameters);
+        Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceData(parameters);
+        if (MapUtils.isNotEmpty(pollutionSourceData)) {
+            Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
+            String selectSensorKey = parameters.get("sensorKey").toString();
+            for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
+                if (selectSensorKey.equals(s.getKey())) {
+                    selectSensorKey = s.getValue();
+                }
+            }
+            pollutionSourceData.put("selectSensorKey", selectSensorKey);
         }
-        return pollutionSourceData;*/
-        System.out.println("----"+historyHourlyMapper.getPollutionSourceData(parameters));
-       return historyHourlyMapper.getPollutionSourceData(parameters);
+        return pollutionSourceData;
     }
 
     @Override
-    public Point getDirPoint(Map<String, Object> parameters) throws Exception{
-        Map<String,Object> pollutionSourceData=getPollutionSourceData(parameters);
+    public Map<String, Object> getPollutionSourceDataByHour(Map<String, Object> parameters) throws Exception {
+        List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
+        List<String> sensorKeys = new ArrayList<>();
+        for (Sensor sensor : sensors) {
+            sensorKeys.add(sensor.getSensorKey());
+        }
+        parameters.put("sensorKeys", sensorKeys);
+        Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceDataByHour(parameters);
+        if (MapUtils.isNotEmpty(pollutionSourceData)) {
+            Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
+            String selectSensorKey = parameters.get("sensorKey").toString();
+            for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
+                if (selectSensorKey.equals(s.getKey())) {
+                    selectSensorKey = s.getValue();
+                }
+            }
+            pollutionSourceData.put("selectSensorKey", selectSensorKey);
+        }
+        return pollutionSourceData;
+    }
+
+    @Override
+    public Map<String, Object> getPollutionSourceDataAll(Map<String, Object> parameters) throws Exception {
+        List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
+        List<String> sensorKeys = new ArrayList<>();
+        for (Sensor sensor : sensors) {
+            sensorKeys.add(sensor.getSensorKey());
+        }
+        parameters.put("sensorKeys", sensorKeys);
+        Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceDataAll(parameters);
+        if (MapUtils.isNotEmpty(pollutionSourceData)) {
+            Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
+            String selectSensorKey = parameters.get("sensorKey").toString();
+            for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
+                if (selectSensorKey.equals(s.getKey())) {
+                    selectSensorKey = s.getValue();
+                }
+            }
+            pollutionSourceData.put("selectSensorKey", selectSensorKey);
+        }
+        return pollutionSourceData;
+    }
+
+    @Override
+    public String getTVOCByMac(String mac,String time,String sensor) {
+        return historyHourlyMapper.getTVOCByMac(mac,time,sensor);
+    }
+
+    @Override
+    public String getPressureByMac(String mac, String time) {
+        return historyHourlyMapper.getPressureByMac(mac,time);
+    }
+
+    @Override
+    public Map<String,Object> getDataByMac(String mac, String time) {
+        return historyHourlyMapper.getDataByMac(mac,time);
+    }
+
+    @Override
+    public List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception {
+        List<Map<String, Object>> resultMap = historyHourlyMapper.getDataByTimeSlot(mac,startTime,endTime);
+        return resultMap;
+    }
+
+    @Override
+    public List<Map> getDataByMacAndTime(Map param) {
+        String mac = param.get("mac").toString();
+        String sensor = param.get("sensor").toString();
+        String time = param.get("time").toString();
+        String[] times = time.split("���");
+
+        String time1=times[0].substring(0,8);
+        for (int i = 0; i <times.length ; i++) {
+            if (i!=0){
+                times[i]=time1+""+times[i];
+            }
+        }
+        List<List> lists = new ArrayList<List>();
+        List<Map> perList = new ArrayList<Map>();
+        try {
+            for (String perTime : times) {
+                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//���������������MM
+                Date date = simpleDateFormat.parse(perTime);
+                Calendar calendar = Calendar.getInstance();
+                calendar.setTime(date);
+                calendar.add(Calendar.DATE, 1);
+                Date date1 = calendar.getTime();
+                String date2 = simpleDateFormat.format(date1);
+                List<Map> list = historyHourlyMapper.getDataByMacAndTime(mac, sensor, perTime, date2);
+                List<Map> newList = new ArrayList<Map>();
+                //System.out.println(list);
+                for (int i = 0; i < 24; i++) {
+                    Map map=new HashMap();
+                    if (i < 10) {
+                        map.put("time","0"+i);
+                        map.put(sensor,"");
+                        newList.add(map);
+                    }else {
+                        map.put("time",""+i);
+                        map.put(sensor,"");
+                        newList.add(map);
+                    }
+                }
+                //System.out.println(newList);
+                if (list.size() <= 24 && list.size() >= 0) {
+                    for (int i = 0; i <list.size() ; i++) {
+                        String timeS = list.get(i).get("time").toString().substring(11, 13);
+                        for (Map map : newList) {
+                            if (timeS.equals(map.get("time"))){
+                                map.replace(sensor,"",list.get(i).get(sensor));
+                            }
+                        }
+                    }
+                }
+                lists.add(newList);
+            }
+
+            for (int i = 0; i < 24; i++) {
+                Map<String, Object> map = new HashMap<>();
+                List iList = new ArrayList();
+                for (List<Map> list : lists) {
+                    String timeS = list.get(i).get("time").toString();
+                    if (i < 10) {
+                        if (timeS.equals("0" + i)) {
+                            iList.add(list.get(i).get(sensor));
+                        } else {
+                            iList.add("");
+                        }
+                    } else {
+                        if (timeS.equals(i + "")) {
+                            iList.add(list.get(i).get(sensor));
+                        } else {
+                            iList.add("");
+                        }
+                    }
+                }
+                map.put("values", iList);
+                map.put("time", i);
+                perList.add(map);
+            }
+
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+        for (int i = 0; i <perList.size() ; i++) {
+            perList.get(i).replace("time",perList.get(i).get("time"),(Integer) perList.get(i).get("time")+1);
+        }
+        int j=0;
+        for (int i = 0; i <perList.size() ; i++) {
+            if (!((List)perList.get(i).get("values")).get(0).equals("")){
+                j=1;
+            }
+        }
+        if (j==0){
+            perList.clear();
+        }
+        return perList;
+    }
+
+    @Override
+    public Point getDirPoint(Map<String, Object> parameters) throws Exception {
+        Map<String, Object> pollutionSourceData = getPollutionSourceDataByHour(parameters);
+        if (MapUtils.isEmpty(pollutionSourceData)) {
+            pollutionSourceData = getPollutionSourceDataAll(parameters);
+        }
         String mac = parameters.get("mac").toString();
         Device device = deviceService.getDeviceByMac(mac, false);
-        Point pointEnd=new Point();
-        if(MapUtils.isNotEmpty(pollutionSourceData)){
-            System.out.println("pollutionSourceData"+pollutionSourceData);
-            if(pollutionSourceData.get("e18")!=null&&pollutionSourceData.get("e23")!=null&&pollutionSourceData.get("e6")!=null){
-                double windSpeed=Double.valueOf(pollutionSourceData.get("e18").toString());
-                double winDir=Double.valueOf(pollutionSourceData.get("e23").toString());
-                double distance=windSpeed*3600;
-                double long1=device.getLongitude();
-                double lat1=device.getLatitude();
-                System.out.println("windSpeed:"+windSpeed+"---winDir:"+winDir+"---distance:"+distance+"--long1:"+long1+"--lat1:"+lat1);
-                String[] result=calLocationByDistanceAndLocationAndDirection(winDir,long1,lat1,distance);
-                System.out.println("result1���"+Double.valueOf(result[0])+"result2���"+Double.valueOf(result[1]));
+        Point pointEnd = new Point();
+        if (MapUtils.isNotEmpty(pollutionSourceData)) {
+            if (pollutionSourceData.get("e18") != null && pollutionSourceData.get("e23") != null) {
+                double windSpeed = Double.valueOf(pollutionSourceData.get("e18").toString());
+                double winDir = Double.valueOf(pollutionSourceData.get("e23").toString());
+                double distance = windSpeed * 3600;
+                double long1 = device.getLongitude();
+                double lat1 = device.getLatitude();
+                String[] result = calLocationByDistanceAndLocationAndDirection(winDir, long1, lat1, distance);
                 pointEnd.setLng(Double.valueOf(result[0]));
                 pointEnd.setLat(Double.valueOf(result[1]));
             }
@@ -70,29 +240,29 @@
     }
 
 
-
     /**
      * ���������������������������������������������������������������������������
-     * @param angle ���������������������������������������������
+     *
+     * @param angle     ���������������������������������������������
      * @param startLong ���������������
-     * @param startLat ���������������
-     * @param distance ���������������m
+     * @param startLat  ���������������
+     * @param distance  ���������������m
      * @return
      */
-    private String[] calLocationByDistanceAndLocationAndDirection(double angle, double startLong,double startLat, double distance){
+    private String[] calLocationByDistanceAndLocationAndDirection(double angle, double startLong, double startLat, double distance) {
         /** ������������ **/
         final double R = 6371e3;
         /** 180�� **/
         final DecimalFormat df = new DecimalFormat("0.000000");
         String[] result = new String[2];
         //���������������������������������������
-        double �� = distance/R;
+        double �� = distance / R;
         // ���������radian���������������������������
         angle = Math.toRadians(angle);
         startLong = Math.toRadians(startLong);
         startLat = Math.toRadians(startLat);
-        double lat = Math.asin(Math.sin(startLat)*Math.cos(��)+Math.cos(startLat)*Math.sin(��)*Math.cos(angle));
-        double lng = startLong + Math.atan2(Math.sin(angle)*Math.sin(��)*Math.cos(startLat),Math.cos(��)-Math.sin(startLat)*Math.sin(lat));
+        double lat = Math.asin(Math.sin(startLat) * Math.cos(��) + Math.cos(startLat) * Math.sin(��) * Math.cos(angle));
+        double lng = startLong + Math.atan2(Math.sin(angle) * Math.sin(��) * Math.cos(startLat), Math.cos(��) - Math.sin(startLat) * Math.sin(lat));
         // ���������������10���������������
         lng = Math.toDegrees(lng);
         lat = Math.toDegrees(lat);

--
Gitblit v1.8.0