ZhuDongming
2020-06-28 df80448fbd1bccdba131d797c0c7e93274ed6ba1
src/main/java/com/moral/service/impl/MonitorPointServiceImpl.java
@@ -1,5 +1,6 @@
package com.moral.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -13,8 +14,14 @@
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moral.mapper.DictionaryDataMapper;
import com.moral.mapper.OrganizationMapper;
import com.moral.service.HistoryHourlyService;
import com.moral.util.MyLatLng;
import com.moral.util.mapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@@ -56,6 +63,9 @@
    
    @Resource
    private OrganizationService organizationService;
    @Resource
    private HistoryHourlyService historyHourlyService;
    
    private static Class ENTITY_CLASS = MonitorPoint.class;
    @Override
@@ -358,4 +368,367 @@
      }
       
   }
   @Override
   public Map<String, Object> selectAllById(String id) {
      int id2 = Integer.parseInt(id);
      Map<String, Object> map = monitorPointMapper.selectAllById(id2);
      return map;
   }
    @Override
    public List<Device> getDeviceList(int id) {
        return monitorPointMapper.getDeviceList(id);
    }
    @Override
    public JSONObject getMonitorPointById(int id, String Time, int i, String sensor) {
        JSONObject params = new JSONObject();
        MonitorPoint monitorPoint = monitorPointMapper.getMonitorPointById(id);
        if (i == 1) {
            Double longitude = monitorPoint.getLongitude();
            Double latitude = monitorPoint.getLatitude();
            Double workshop = 0.0;
            Double flue = 0.0;
            //烟道设备
            List<Device> device1 = deviceService.getDeviceById1(id);
            //厂界设备
            List<Device> device2 = deviceService.getDeviceById2(id);
            //车间设备
            List<Device> device3 = deviceService.getDeviceById3(id);
            String purificationRate = null;//净化率
            if (device3.size() == 0) {
                purificationRate = "公司无车间设备,无法计算净化率";
                params.put("device2", device2);
                params.put("device1", device1);
            } else {
                params.put("device1", device1);
                params.put("device2", device2);
                params.put("device3", device3);
                for (Device device33 : device3) {
                    String mac3 = device33.getMac();
                    String tvoc = historyHourlyService.getTVOCByMac(mac3, Time,sensor);
                    if (tvoc != null) {
                        workshop = Double.parseDouble(tvoc);
                    } else {
                        workshop = 0.0;
                    }
                }
                if (device1.size() == 0) {
                    purificationRate = "公司无烟道设备,无法计算净化率";
                } else {
                    for (Device device11 : device1) {
                        String mac1 = device11.getMac();
                        flue = Double.parseDouble(historyHourlyService.getTVOCByMac(mac1, Time,sensor));
                        if (workshop == 0.0) {
                            purificationRate = "车间设备无数据浓度,无法计算浓度值";
                        } else {
                            if (flue == 0.0) {
                                purificationRate = "烟道设备无数据浓度,无法计算净化率";
                            } else {
                                BigDecimal b = new BigDecimal(((workshop - flue) / workshop) * 100);
                                double purity2 = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                                purificationRate = (purity2) + "%";
                            }
                        }
                    }
                }
            }
            params.put("latitude", latitude);
            params.put("longitude", longitude);
            params.put("purificationRate", purificationRate);
        }
        if (i == 2) {
            Double longitude = monitorPoint.getLongitude();
            Double latitude = monitorPoint.getLatitude();
            //烟道设备
            List<Device> device1 = deviceService.getDeviceById1(id);
            //厂界设备
            List<Device> device2 = deviceService.getDeviceById2(id);
            //车间设备
            List<Device> device3 = deviceService.getDeviceById3(id);
            String collectionRate = null;//收集率
            Double pressure = 0.0;//平均压强
            List<Double> list1 = new ArrayList();
            if (device3.size() == 0) {
                collectionRate = "该公司车间无设备,无法计算收集率";
                params.put("device1", device1);
                params.put("device2", device2);
            } else {
                params.put("device3", device3);
                if (device2.size() == 0) {
                    collectionRate = "该公司厂界无设备,无法计算收集率";
                    params.put("device1", device1);
                } else {
                    params.put("device2", device2);
                    params.put("device1", device1);
                    Double sum = 0.0;
                    for (Device device22 : device2) {
                        String mac2 = device22.getMac();
                        String pressure2 = historyHourlyService.getPressureByMac(mac2, Time);
                        if (pressure2 != null) {
                            pressure = Double.parseDouble(pressure2);
                            list1.add(pressure);
                        }else {
                            collectionRate = "该公司厂界设备无数据浓度,无法计算收集率";
                        }
                    }
                    for (Double d : list1) {
                        sum = sum + d;
                    }
                    if (list1.size() != 0) {
                        pressure = sum / list1.size();
                    } else {
                        collectionRate = "该公司厂界设备无数据浓度,无法计算收集率";
                    }
                    Double pressure33 = 0.0;
                    for (Device device33 : device3) {
                        String mac3 = device33.getMac();
                        String pressure3 = historyHourlyService.getPressureByMac(mac3, Time);
                        if (pressure3 != null) {
                            pressure33 = Double.parseDouble(pressure3);
                            BigDecimal b = new BigDecimal((pressure - pressure33 + 3.5) * 20);//临时加2.5,有具体设备就不需要加
                            double collection = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                            collectionRate = collection + "%";
                        }
                    }
                }
            }
            params.put("latitude", latitude);
            params.put("longitude", longitude);
            params.put("collectionRate", collectionRate);
        }
        if (i == 3) {
            Double longitudeCompany = monitorPoint.getLongitude();
            Double latitudeCompany = monitorPoint.getLatitude();
            Double longitude = 0.0;//每台设备的经纬度
            Double latitude = 0.0;//每台设备的经纬度
            List<Device> deviceList = deviceService.getDeviceById2(id);
            //List<Device> deviceList = monitorPointService.getDeviceList(id);
            List<Map> list = new ArrayList<Map>();//存放historyHourly的json数据
            String smac = "";//,每台设备的Mac号
            List<Map> windList = new ArrayList<Map>();//存放每台设备的风向和经纬度
            Map<String, Object> jsonMap = new HashMap<String, Object>();//每台设备的数据
            Map<String, Object> deviceMap = null;
            for (Device device : deviceList) {
                deviceMap = new HashMap<String, Object>();
                smac = device.getMac();
                latitude = device.getLatitude();
                longitude = device.getLongitude();
                jsonMap = historyHourlyService.getDataByMac(smac, Time);
                deviceMap.put("longitude", longitude);
                deviceMap.put("latitude", latitude);
                if (jsonMap != null) {
                    deviceMap.put("jsonMap", jsonMap);
                } else {
                    deviceMap.put("jsonMap", "该设备该时间点无数据");
                }
                list.add(deviceMap);
            }
            Map<String, Object> mapData = new HashMap<String, Object>();
            Map<String, Object> mapDevice;
            JSONArray windDir;
            for (Map<String, Object> objectMap : list) {
                mapDevice = new HashMap<String, Object>();
                Map<String, Object> map = null;
                if (!(objectMap.get("jsonMap") instanceof String)) {
                    mapData = (Map<String, Object>) objectMap.get("jsonMap");
                    map = (Map) JSON.parseObject((String) mapData.get("json"));
                    windDir = (JSONArray) JSONArray.toJSON(map.get("e23"));
                    JSONArray concentration = (JSONArray) JSONArray.toJSON(map.get(sensor));
                    BigDecimal bigDecimal1=new BigDecimal(0);
                    BigDecimal bigDecimal=new BigDecimal(0);
                    if (concentration!=null){
                        if (concentration.get(0) instanceof Integer){
                            bigDecimal1 =new BigDecimal(concentration.get(0).toString());
                        }else {
                            bigDecimal1= (BigDecimal) concentration.get(0);
                        }
                        if (windDir!=null){
                            if (windDir.get(0) instanceof Integer){
                                bigDecimal =new BigDecimal(windDir.get(0).toString());
                            }else {
                                bigDecimal=(BigDecimal) windDir.get(0);
                            }
                            mapDevice.put("e23", bigDecimal.doubleValue());
                            mapDevice.put("sensor", bigDecimal1.doubleValue());
                            mapDevice.put("longitude", objectMap.get("longitude"));
                            mapDevice.put("latitude", objectMap.get("latitude"));
                        }else {
                            mapDevice.put("noSensor", "无风向数据");
                            mapDevice.put("longitude", objectMap.get("longitude"));
                            mapDevice.put("latitude", objectMap.get("latitude"));
                        }
                    }
                } else {
                    mapDevice.put("info", objectMap.get("jsonMap"));
                    mapDevice.put("longitude", objectMap.get("longitude"));
                    mapDevice.put("latitude", objectMap.get("latitude"));
                }
                windList.add(mapDevice);
            }
            // if (windDir!=null){}
            Double differenceNum = 0.0;
            Map<String, Object> indexMap;
            List<Map> mapList = new ArrayList<>();
            for (int j = 0; j < windList.size(); j++) {
                for (int k = 0; k < windList.size(); k++) {
                    if (k != j) {
                        indexMap = new HashMap<String, Object>();
                        if (windList.get(k).containsKey("e23") && windList.get(j).containsKey("e23")) {
                            Double e23Numk = (Double) windList.get(k).get("e23");
                            Double e23Numj = (Double) windList.get(j).get("e23");
                            Double diff = e23Numk - e23Numj;
                            BigDecimal b = new BigDecimal(Math.abs(diff));
                            differenceNum = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                            if (differenceNum > 180) {
                                differenceNum = 360 - differenceNum;
                            }
                            indexMap.put("k", k);
                            indexMap.put("j", j);
                            indexMap.put("diff", differenceNum);
                        } else {
                            indexMap.put("k", k);
                            indexMap.put("j", j);
                            indexMap.put("info", "设备无数据");
                        }
//                     indexMap.put("longitude",windList.get(k).get("longitude"));
//                     indexMap.put("latitude",windList.get(k).get("latitude"));
                        mapList.add(indexMap);
                    } else {
                        continue;
                    }
                }
            }
            Double min = 0.0;
            int indexMin = 0;
            if (mapList.size()>0){
                if (mapList.get(0).containsKey("diff")) {
                    min = (Double) mapList.get(0).get("diff");
                    for (int q = 1; q < mapList.size(); q++) {
                        if (mapList.get(q).containsKey("diff")) {
                            if (min > (Double) mapList.get(q).get("diff")) {
                                min = (Double) mapList.get(q).get("diff");
                                indexMin = q;
                            }
                        } else {
                            indexMin = q;
                        }
                    }
                }
            }
            Map twoDeviceMap = mapList.get(indexMin);
            List<Map> list1 = new ArrayList<Map>();           //存放风向夹角最小的两台设备的经纬度
            list1.add(windList.get((Integer) twoDeviceMap.get("k")));
            list1.add(windList.get((Integer) twoDeviceMap.get("j")));
            Double wind = 0.0;
            Double sum = 0.0;
            if (list1.get(0).containsKey("e23")) {
                if (Math.abs((Double) list1.get(0).get("e23") - (Double) list1.get(1).get("e23")) > 180) {
                    sum = (Double) list1.get(0).get("e23") + (Double) list1.get(1).get("e23");
                    wind = sum / 2 + 180;
                    if (wind > 360) {
                        wind = wind - 360;
                    }
                } else {
                    for (Map map : list1) {
                        sum += (Double) map.get("e23");
                    }
                    wind = sum / 2;
                }
            } else {
                wind = 0.0;
            }
            Map<String, Object> longAndLatiMap;
            List<Map> longAndLatiList = new ArrayList<>();//经纬度夹角集合,存放的是windList中两台设备的下标和两台设备夹角与风向角的差值
            List<Map> preAngleDeviceList = new ArrayList<Map>(); //角度减去风向最小的两台设备
            if (wind != 0.0) {
                for (int f = 0; f < windList.size(); f++) {
                    for (int h = 0; h < windList.size(); h++) {
                        if (f != h) {
                            longAndLatiMap = new HashMap<String, Object>();
                            longAndLatiMap.put("h", h);
                            longAndLatiMap.put("f", f);
                            Double angle = mapUtils.getAngle(
                                    new MyLatLng((Double) windList.get(h).get("longitude"),
                                            (Double) windList.get(h).get("latitude")),
                                    new MyLatLng((Double) windList.get(f).get("longitude"),
                                            (Double) windList.get(f).get("latitude")));
                            Double angleDiff = Math.abs(angle - wind);
                            longAndLatiMap.put("angle", angleDiff);
                            longAndLatiList.add(longAndLatiMap);
                        }
                    }
                }
                Double minAngle = (Double) longAndLatiList.get(0).get("angle");
                int indexAngle = 0;
                for (int j = 0; j < longAndLatiList.size(); j++) {
                    if (minAngle > (Double) longAndLatiList.get(j).get("angle")) {
                        minAngle = (Double) longAndLatiList.get(j).get("angle");
                        indexAngle = j;
                    }
                }
                //windList.get((Integer) longAndLatiList.get(indexAngle).get("f"))获得风向和经纬度
                preAngleDeviceList.add(windList.get((Integer) longAndLatiList.get(indexAngle).get("h")));
                preAngleDeviceList.add(windList.get((Integer) longAndLatiList.get(indexAngle).get("f")));
                Double length = mapUtils.getDistance((Double) preAngleDeviceList.get(0).get("longitude"), (Double) preAngleDeviceList.get(0).get("latitude"),
                        (Double) preAngleDeviceList.get(1).get("longitude"), (Double) preAngleDeviceList.get(1).get("latitude"));
                Double subLength = length / 5;
                Double angle = mapUtils.getAngle(
                        new MyLatLng((Double) preAngleDeviceList.get(0).get("longitude"),
                                (Double) preAngleDeviceList.get(0).get("latitude")),
                        new MyLatLng((Double) preAngleDeviceList.get(1).get("longitude"),
                                (Double) preAngleDeviceList.get(1).get("latitude")));
                params.put("preAngleDeviceList", preAngleDeviceList);
                params.put("angle", angle);
                List locationList = new ArrayList();
                String[] firstLocation = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, (Double) preAngleDeviceList.get(0).get("longitude"),
                        (Double) preAngleDeviceList.get(0).get("latitude"), subLength);
                String[] secondLoction = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(firstLocation[0]),
                        Double.parseDouble(firstLocation[1]), subLength);
                String[] thirdLocation = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(secondLoction[0]),
                        Double.parseDouble(secondLoction[1]), subLength);
                String[] fourthLoction = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(thirdLocation[0]),
                        Double.parseDouble(thirdLocation[1]), subLength);
                locationList.add(firstLocation);
                locationList.add(secondLoction);
                locationList.add(thirdLocation);
                locationList.add(fourthLoction);
                params.put("locationList", locationList);
                String preAngleDeviceString = JSON.toJSON(preAngleDeviceList).toString();
                params.put("preAngleDeviceString", preAngleDeviceString);//两台添加箭头的设备
            } else {
                params.put("preAngleDeviceString", "");
            }
            //String preAngleDeviceString=preAngleDeviceList.
            params.put("wind", wind);
            params.put("list1", list1);
            params.put("deviceList", deviceList);
            params.put("latitudeCompany", latitudeCompany);
            params.put("longitudeCompany", longitudeCompany);
        }
        return params;
    }
}