From 2dee01ae6f13a72b1d2ba9ca3d1ffbea72ab513b Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Sat, 09 Oct 2021 08:59:28 +0800
Subject: [PATCH] screen-aqi            增加监测因子对比图接口

---
 screen-api/src/main/java/com/moral/api/service/impl/HistorySecondUavServiceImpl.java |  247 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 214 insertions(+), 33 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/HistorySecondUavServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/HistorySecondUavServiceImpl.java
index 1565dc3..cdca09d 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/HistorySecondUavServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/HistorySecondUavServiceImpl.java
@@ -1,8 +1,8 @@
 package com.moral.api.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.moral.api.entity.HistorySecondUav;
-import com.moral.api.entity.Organization;
+import com.moral.api.entity.*;
 import com.moral.api.mapper.HistorySecondUavMapper;
 import com.moral.api.pojo.dto.uav.UAVQueryTimeSlotDTO;
 import com.moral.api.pojo.form.uav.UAVQueryTimeSlotForm;
@@ -10,11 +10,20 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.moral.api.service.OrganizationService;
 import com.moral.api.service.SpecialDeviceService;
+import com.moral.constant.RedisConstants;
 import com.moral.util.DateUtils;
+import com.moral.util.GeodesyUtils;
+import com.moral.util.MathUtils;
+import com.moral.util.UnitConvertUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
+import org.springframework.util.ObjectUtils;
 
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Predicate;
 
 /**
@@ -28,12 +37,19 @@
 @Service
 public class HistorySecondUavServiceImpl extends ServiceImpl<HistorySecondUavMapper, HistorySecondUav> implements HistorySecondUavService {
 
+    /*
+     * ������������������
+     * */
+    private Double filterDistance = 2d;
+
     @Autowired
     HistorySecondUavMapper historySecondUavMapper;
     @Autowired
     OrganizationService organizationService;
     @Autowired
     SpecialDeviceService specialDeviceService;
+    @Autowired
+    RedisTemplate redisTemplate;
 
     @Override
     public List<Date> queryDate(Integer organizationId) {
@@ -46,13 +62,14 @@
             childrenId.add(child.getId());
         }
         childrenId.add(organizationId);
-        queryWrapper.in("organization_id",childrenId);
+        queryWrapper.in("organization_id", childrenId);
         //���������������������������180���
         Date endDate = new Date();
         Date startDate = DateUtils.addDays(endDate, -180);
-        queryWrapper.between("batch",startDate,endDate);
+        queryWrapper.between("batch", startDate, endDate);
         //������������������
         queryWrapper.select("DISTINCT batch");
+        queryWrapper.orderByDesc("batch");
         //������������
         List<HistorySecondUav> historySecondUavs = historySecondUavMapper.selectList(queryWrapper);
         //������������Date������
@@ -77,76 +94,240 @@
             childrenId.add(child.getId());
         }
         childrenId.add(organizationId);
-        wrapper.in("organization_id",childrenId);
+        wrapper.in("organization_id", childrenId);
         //������������batch������������������������������������������������
-        wrapper.between("batch",startDate,endDate);
+        wrapper.between("batch", startDate, endDate);
         //������������������
         wrapper.select("mac,time,batch");
         //������������
         List<HistorySecondUav> historySecondUavs = historySecondUavMapper.selectList(wrapper);
         //������batch������������
-        Map<String,List<HistorySecondUav>> batchMap = new LinkedHashMap<>();//key���batch���string
+        Map<String, List<HistorySecondUav>> batchMap = new LinkedHashMap<>();//key���batch���string
         for (HistorySecondUav historySecondUav : historySecondUavs) {
             //������batch���������������������
-            List<HistorySecondUav> list = batchMap.get(historySecondUav.getBatch().toString());
-            if(list!=null){
+            List<HistorySecondUav> list = batchMap.get(DateUtils.dateToDateString(historySecondUav.getBatch(), "yyyy-MM-dd HH:mm:ss"));
+            if (list != null) {
                 list.add(historySecondUav);
-            }else{
+            } else {
                 ArrayList<HistorySecondUav> newList = new ArrayList<>();
                 newList.add(historySecondUav);
-                batchMap.put(historySecondUav.getBatch().toString(),newList);
+                batchMap.put(DateUtils.dateToDateString(historySecondUav.getBatch(), "yyyy-MM-dd HH:mm:ss"), newList);
             }
         }
         //������������30������������������
         batchMap.values().removeIf(new Predicate<List<HistorySecondUav>>() {
             @Override
             public boolean test(List<HistorySecondUav> historySecondUavs) {
-                if(historySecondUavs.size()<=30)
+                if (historySecondUavs.size() <= 30)
                     return true;
                 return false;
             }
         });
         //������mac������������
-        Map<String,List<Map<String,List<HistorySecondUav>>>> macBatchMap = new LinkedHashMap<>();//key���mac
+        Map<String, List<Map<String, List<HistorySecondUav>>>> macBatchMap = new LinkedHashMap<>();//key���mac
         //������batchMap���mac���������������������macBatchMap���key���batch���������map���������������������value
-        batchMap.forEach((key,value)->{
+        batchMap.forEach((key, value) -> {
             String mac = value.get(0).getMac();
             List<Map<String, List<HistorySecondUav>>> maps = macBatchMap.get(mac);
-            if(maps!=null){
-                Map<String,List<HistorySecondUav>> map = new LinkedHashMap<>();
-                map.put(key,value);
+            if (maps != null) {
+                Map<String, List<HistorySecondUav>> map = new LinkedHashMap<>();
+                map.put(key, value);
                 maps.add(map);
-            }else{
-                List<Map<String,List<HistorySecondUav>>> list = new ArrayList<>();
-                Map<String,List<HistorySecondUav>> map = new LinkedHashMap<>();
-                map.put(key,value);
+            } else {
+                List<Map<String, List<HistorySecondUav>>> list = new ArrayList<>();
+                Map<String, List<HistorySecondUav>> map = new LinkedHashMap<>();
+                map.put(key, value);
                 list.add(map);
-                macBatchMap.put(value.get(0).getMac(),list);
+                macBatchMap.put(value.get(0).getMac(), list);
             }
         });
         //������������������
         List<UAVQueryTimeSlotDTO> dtos = new ArrayList<>();
-        macBatchMap.forEach((key,value)->{
+        macBatchMap.forEach((key, value) -> {
             UAVQueryTimeSlotDTO dto = new UAVQueryTimeSlotDTO();
-            List<Map<String,Date>> timeSlots = new ArrayList<>();
+            List<Map<String, Object>> timeSlots = new ArrayList<>();
             dto.setMac(key);
             //������mac������������������
-            dto.setName((String) specialDeviceService.getSpecialDeviceMapByMac(key).get("name"));
-            //���������������
-            value.forEach(listValue->{
-                listValue.forEach((mKey,mValue)->{
+            SpecialDevice specialDevice = specialDeviceService.getSpecialDeviceMapByMac(key);
+            //������������������������������������
+            if (specialDevice == null)
+                return;
+            dto.setName(specialDevice.getName());
+            //������������������batch
+            value.forEach(listValue -> {
+                listValue.forEach((mKey, mValue) -> {
                     Date slotStartDate = mValue.get(0).getTime();
-                    Date slotEndDate = mValue.get(mValue.size()-1).getTime();
-                    Map<String,Date> dateMap = new HashMap<>();
-                    dateMap.put("startTime",slotStartDate);
-                    dateMap.put("endTime",slotEndDate);
+                    Date slotEndDate = mValue.get(mValue.size() - 1).getTime();
+                    Map<String, Object> dateMap = new HashMap<>();
+                    dateMap.put("startTime", slotStartDate);
+                    dateMap.put("endTime", slotEndDate);
+                    dateMap.put("batch", mKey);
                     timeSlots.add(dateMap);
                 });
             });
             dto.setTimeSlot(timeSlots);
             dtos.add(dto);
         });
-      return dtos;
+        return dtos;
+    }
+
+    @Override
+    public List<HistorySecondUav> queryDataByBatch(String batch) {
+        //batch������������
+        Date batchDate = DateUtils.getDate(batch, "yyyy-MM-dd HH:mm:ss");
+        //������������
+        QueryWrapper<HistorySecondUav> wrapper = new QueryWrapper<>();
+        wrapper.eq("batch", batchDate);
+        wrapper.select("value,mac,time");
+        List<HistorySecondUav> datas = historySecondUavMapper.selectList(wrapper);
+        //������������������������������������
+        Double lowestHeight = 0d;
+        for (HistorySecondUav data : datas) {
+            String value = data.getValue();
+            Map<String, Object> valueMap = JSON.parseObject(value, Map.class);
+            //������������
+            Double height = Double.valueOf((String) valueMap.get("flyhig"));
+            if (height < lowestHeight)
+                lowestHeight = height;
+        }
+        //������������������������������������������������������������������0������������������������������������������������������������������������
+        lowestHeight = Math.abs(lowestHeight);
+        for (HistorySecondUav data : datas) {
+            String value = data.getValue();
+            Map<String, Object> valueMap = JSON.parseObject(value, Map.class);
+            //������������
+            Double height = Double.valueOf((String) valueMap.get("flyhig"));
+            //������������������������
+            height = MathUtils.add(height, lowestHeight);
+            //������������������
+            valueMap.put("flyhig", height);
+            String newValue = JSON.toJSONString(valueMap);
+            data.setValue(newValue);
+        }
+        //���������������������,���������������������������������2���������
+        datas = filterDatas(datas);
+        //������������
+        unitConvert(datas);
+        return datas;
+    }
+
+    /**
+     * @Description: ������������
+     * @Param: [datas]
+     * @return: java.util.List<com.moral.api.entity.HistorySecondUav>
+     * @Author: ���������
+     * @Date: 2021/9/16
+     */
+    private List<HistorySecondUav> filterDatas(List<HistorySecondUav> datas) {
+        //������������������
+        List<HistorySecondUav> result = new ArrayList<>();
+        //������������������������������������������
+        HistorySecondUav tempData = datas.get(0);
+        result.add(tempData);
+        datas.remove(0);
+        for (HistorySecondUav data : datas) {
+            Double distance = getDistance(tempData, data);
+            if (distance > filterDistance) {
+                result.add(data);
+                tempData = data;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * @Description: ���������������������������������
+     * @Param: [uav1, uav2]
+     * @return: java.lang.Double
+     * @Author: ���������
+     * @Date: 2021/9/13
+     */
+    private Double getDistance(HistorySecondUav uav1, HistorySecondUav uav2) {
+        String value1 = uav1.getValue();
+        String value2 = uav2.getValue();
+        Map<String, Object> value1Map = JSON.parseObject(value1, Map.class);
+        Map<String, Object> value2Map = JSON.parseObject(value2, Map.class);
+        //������������1���������������������
+        Double longtitude1 = Double.valueOf((String) value1Map.get("flylon"));
+        Double latitude1 = Double.valueOf((String) value1Map.get("flylat"));
+        BigDecimal c1 = (BigDecimal) value1Map.get("flyhig");
+        double height1 = c1.doubleValue();
+        //������������2���������������������
+        Double longtitude2 = Double.valueOf((String) value2Map.get("flylon"));
+        Double latitude2 = Double.valueOf((String) value2Map.get("flylat"));
+        BigDecimal c2 = (BigDecimal) value2Map.get("flyhig");
+        double height2 = c2.doubleValue();
+        //������������������������������������
+        Double planDistance = GeodesyUtils.getDistance(latitude1, longtitude1, latitude2, longtitude2);
+        //������������������������������������������������������������
+        Double heightDsitance = Math.abs(MathUtils.sub(height2, height1));
+        Double Distance = Math.sqrt(MathUtils.mul(planDistance, planDistance) + MathUtils.mul(heightDsitance, heightDsitance));
+        return Distance;
+    }
+
+    /**
+     * @Description: ������������������������������
+     * @Param: [datas]
+     * @return: java.util.List<com.moral.api.entity.HistorySecondUav>
+     * @Author: ���������
+     * @Date: 2021/9/16
+     */
+    private void unitConvert(List<HistorySecondUav> datas) {
+        //������������������
+        List<UnitConversion> unitConversions = redisTemplate.opsForList().range(RedisConstants.UNIT_CONVERSION, 0, -1);
+        //���������������������������
+        SpecialDevice specialDevice = (SpecialDevice) redisTemplate.opsForHash().get(RedisConstants.SPECIAL_DEVICE_INFO, datas.get(0).getMac());
+        //������������������������������������������
+        List<Sensor> sensors = specialDevice.getVersion().getSensors();
+        Map<String, Sensor> sensorsMap = new HashMap<>();
+        sensors.forEach(value -> sensorsMap.put(value.getCode(), value));
+        //������������������������������
+        for (HistorySecondUav data : datas) {
+            String valueStr = data.getValue();
+            ConcurrentHashMap<String, Object> valueMap = JSON.parseObject(valueStr, ConcurrentHashMap.class);
+            Set<Map.Entry<String, Object>> entries = valueMap.entrySet();
+            Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
+            //���������������������������������
+            while(iterator.hasNext()){
+                Map.Entry<String, Object> entry = iterator.next();
+                String code = entry.getKey();
+                String value = String.valueOf(entry.getValue());
+                Sensor sensor = sensorsMap.get(code);
+                if (sensor == null) {//���������������������������������������
+                    valueMap.remove(code);
+                    continue;
+                }
+                String unit = sensor.getUnit();
+                String unitKey = sensor.getUnitKey();
+                String showUnit = sensor.getShowUnit();
+                String showUnitKey = sensor.getShowUnitKey();
+                //���������������������������������������������������������
+                if (showUnitKey.equals(unitKey)) {
+                    value += " " + unit;
+                } else {
+                    String formula = sensor.getFormula();
+                    //������sensor���������������������������������������������
+                    if (ObjectUtils.isEmpty(formula)) {
+                        for (UnitConversion unitConversion : unitConversions) {
+                            if (unitConversion.getOriginalUnitKey().equals(unitKey) && unitConversion.getTargetUnitKey().equals(showUnitKey))
+                                formula = unitConversion.getFormula();
+                        }
+                    }
+                    //������������������������������
+                    String resultValue = UnitConvertUtils.calculate((String) value, formula);
+                    if (resultValue != null) {
+                        resultValue += showUnit;
+                    } else {//���������������������������null���������������������������������������������������������������
+                        resultValue = value + unit;
+                    }
+                    value = resultValue;
+                }
+                //������������������������������
+                valueMap.put(code, value);
+            }
+            String value = JSON.toJSONString(valueMap);
+            data.setValue(value);
+        }
     }
 
 }

--
Gitblit v1.8.0