From 28923b3d67c58b032bc4ff128aa1d5e62d73dba1 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Fri, 02 Feb 2024 10:07:58 +0800
Subject: [PATCH] fix:提交实时在线率接口

---
 screen-api/src/main/java/com/moral/api/service/impl/HistorySecondUavServiceImpl.java |  276 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 234 insertions(+), 42 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 df440a9..0f2b128 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,27 +1,33 @@
 package com.moral.api.service.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 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.SpecialDevice;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.moral.api.entity.*;
 import com.moral.api.mapper.HistorySecondUavMapper;
 import com.moral.api.pojo.dto.uav.UAVQueryTimeSlotDTO;
+import com.moral.api.pojo.enums.SysDictTypeEnum;
 import com.moral.api.pojo.form.uav.UAVQueryTimeSlotForm;
 import com.moral.api.service.HistorySecondUavService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.moral.api.service.OrganizationService;
 import com.moral.api.service.SpecialDeviceService;
+import com.moral.api.service.SysDictTypeService;
+import com.moral.api.utils.UnitConvertUtils;
+import com.moral.constant.RedisConstants;
 import com.moral.util.DateUtils;
 import com.moral.util.GeodesyUtils;
 import com.moral.util.MathUtils;
 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;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -45,33 +51,36 @@
     OrganizationService organizationService;
     @Autowired
     SpecialDeviceService specialDeviceService;
+    @Autowired
+    private SysDictTypeService sysDictTypeService;
+    @Autowired
+    RedisTemplate redisTemplate;
 
     @Override
     public List<Date> queryDate(Integer organizationId) {
         //������������������
         QueryWrapper<HistorySecondUav> queryWrapper = new QueryWrapper<>();
+        //List<Integer> childrenId = new ArrayList<>();
+        List<Integer> children = organizationService.orgIdList(organizationId);
         //���������������id
-        List<Organization> children = organizationService.getChildrenOrganizationsById(organizationId);
+        /*List<Organization> children = organizationService.getChildrenOrganizationsById(organizationId);
         List<Integer> childrenId = new ArrayList<>();
         for (Organization child : children) {
             childrenId.add(child.getId());
         }
-        childrenId.add(organizationId);
-        queryWrapper.in("organization_id", childrenId);
+        childrenId.add(organizationId);*/
+        queryWrapper.in("organization_id", children);
         //���������������������������180���
         Date endDate = new Date();
         Date startDate = DateUtils.addDays(endDate, -180);
         queryWrapper.between("batch", startDate, endDate);
         //������������������
-        queryWrapper.select("DISTINCT batch");
+        queryWrapper.select("DISTINCT DATE_FORMAT(batch,'%Y-%m-%d') batch ");
         queryWrapper.orderByDesc("batch");
         //������������
         List<HistorySecondUav> historySecondUavs = historySecondUavMapper.selectList(queryWrapper);
         //������������Date������
-        List<Date> result = new ArrayList<>();
-        for (HistorySecondUav historySecondUav : historySecondUavs) {
-            result.add(historySecondUav.getBatch());
-        }
+        List<Date> result = historySecondUavs.stream().map(HistorySecondUav::getBatch).collect(Collectors.toList());
         return result;
     }
 
@@ -79,16 +88,16 @@
     public List<UAVQueryTimeSlotDTO> queryTimeSlot(UAVQueryTimeSlotForm form) {
         //������
         Integer organizationId = form.getOrganizationId();
+
+        List<Integer> integerList = sysDictTypeService.dateValueList(SysDictTypeEnum.SYS_SECOND_UAV_RANGE.getValue());
         Date startDate = form.getStartDate();
         Date endDate = form.getEndDate();
         QueryWrapper<HistorySecondUav> wrapper = new QueryWrapper<>();
         //���������������id
-        List<Organization> children = organizationService.getChildrenOrganizationsById(organizationId);
+        List<Integer> children = organizationService.orgIdList(organizationId);
         List<Integer> childrenId = new ArrayList<>();
-        for (Organization child : children) {
-            childrenId.add(child.getId());
-        }
-        childrenId.add(organizationId);
+        childrenId.addAll(children);
+        childrenId.addAll(integerList);
         wrapper.in("organization_id", childrenId);
         //������������batch������������������������������������������������
         wrapper.between("batch", startDate, endDate);
@@ -144,18 +153,20 @@
             dto.setMac(key);
             //������mac������������������
             SpecialDevice specialDevice = specialDeviceService.getSpecialDeviceMapByMac(key);
-            if(specialDevice==null)
+            //������������������������������������
+            if (specialDevice == null)
                 return;
-            dto.setName((String) specialDeviceService.getSpecialDeviceMapByMac(key).getName());
+            dto.setName(specialDevice.getName());
             //������������������batch
             value.forEach(listValue -> {
                 listValue.forEach((mKey, mValue) -> {
-                    Date slotStartDate = mValue.    get(0).getTime();
+                    Date slotStartDate = mValue.get(0).getTime();
                     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);
+                    dateMap.put("total",mValue.size());
                     timeSlots.add(dateMap);
                 });
             });
@@ -172,13 +183,17 @@
         //������������
         QueryWrapper<HistorySecondUav> wrapper = new QueryWrapper<>();
         wrapper.eq("batch", batchDate);
-        wrapper.select("value");
+        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);
+            //������value���������������������
+            if (!valueMap.containsKey("flyhig")|| !valueMap.containsKey("flylat")|| !valueMap.containsKey("flylon")){
+                continue;
+            }
             //������������
             Double height = Double.valueOf((String) valueMap.get("flyhig"));
             if (height < lowestHeight)
@@ -189,6 +204,10 @@
         for (HistorySecondUav data : datas) {
             String value = data.getValue();
             Map<String, Object> valueMap = JSON.parseObject(value, Map.class);
+            //������value���������������������
+            if (!valueMap.containsKey("flyhig")|| !valueMap.containsKey("flylat")|| !valueMap.containsKey("flylon")){
+                continue;
+            }
             //������������
             Double height = Double.valueOf((String) valueMap.get("flyhig"));
             //������������������������
@@ -198,10 +217,23 @@
             String newValue = JSON.toJSONString(valueMap);
             data.setValue(newValue);
         }
-        //���������������������,���������������������������������3���������
-        return filterDatas(datas);
+        //���������������������,���������������������������������2���������
+        datas = filterDatas(datas);
+        if (datas.size()<2){
+            return null;
+        }
+        //������������
+        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<>();
@@ -211,7 +243,10 @@
         datas.remove(0);
         for (HistorySecondUav data : datas) {
             Double distance = getDistance(tempData, data);
-            if(distance>filterDistance){
+            if (distance==null){
+                continue;
+            }
+            if (distance > filterDistance) {
                 result.add(data);
                 tempData = data;
             }
@@ -220,33 +255,190 @@
     }
 
     /**
-    * @Description: ���������������������������������
-            * @Param: [uav1, uav2]
-            * @return: java.lang.Double
-            * @Author: ���������
-            * @Date: 2021/9/13
-            */
-    private Double getDistance(HistorySecondUav uav1 , HistorySecondUav uav2){
+     * @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);
+        Map<String, Object> value1Map = JSON.parseObject(value1, Map.class);
+        Map<String, Object> value2Map = JSON.parseObject(value2, Map.class);
+        //���������������������������������������������
+        if (!value1Map.containsKey("flylon")||!value1Map.containsKey("flylat")||!value1Map.containsKey("flyhig")||
+                !value2Map.containsKey("flylon")||!value2Map.containsKey("flylat")||!value2Map.containsKey("flyhig")){
+            return null;
+        }
         //������������1���������������������
-        Double longtitude1 = Double.valueOf((String)value1Map.get("flylon"));
-        Double latitude1 = Double.valueOf((String)value1Map.get("flylat"));
-        BigDecimal c1 = (BigDecimal)value1Map.get("flyhig");
+        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 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));
+        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);
+        }
+    }
+
+    @Override
+    public boolean UAVTest(BigDecimal lat, BigDecimal lon, String batch) {
+        List<HistorySecondUav> list = new ArrayList<>();
+
+        QueryWrapper<HistorySecondUav> wrapper = new QueryWrapper<>();
+        wrapper.eq("batch", batch);
+        wrapper.select("value,mac,time,organization_id");
+        wrapper.orderByAsc("time");
+        List<HistorySecondUav> datas = historySecondUavMapper.selectList(wrapper);
+        Date time = new Date();
+        //String bat = "2023-08-08 14:42:20";
+        for(HistorySecondUav g : datas){
+            HistorySecondUav historySecondUav = new HistorySecondUav();
+            historySecondUav.setTime(DateUtils.addDays(g.getTime(),0));
+            historySecondUav.setMac(g.getMac());
+            historySecondUav.setBatch(time);
+            historySecondUav.setOrganizationId(g.getOrganizationId());
+            Map<String, Object> data = JSON.parseObject(g.getValue(), Map.class);
+            Map<String, Object> dataResult = new HashMap<>();
+            dataResult = data;
+            dataResult.put("flylat",Objects.isNull(data.get("flylat"))||data.get("flylat").toString().equals("0")?0d:Double.parseDouble(String.format("%.10f",Double.parseDouble(data.get("flylat").toString())+lat.doubleValue())));
+            dataResult.put("flylon",Objects.isNull(data.get("flylon"))||data.get("flylon").toString().equals("0")?0d:Double.parseDouble(String.format("%.10f",Double.parseDouble(data.get("flylon").toString())+lon.doubleValue())));
+            /*if(Objects.nonNull(data.get("a21026"))&& !data.get("a21026").toString().equals("0")){
+                Double a21026D = Double.parseDouble(data.get("a21026").toString());
+                if( a21026D>15){
+                    a21026D = a21026D -10;
+                }
+                dataResult.put("a21026",Double.parseDouble(String.format("%.4f",a21026D)));
+            }
+            if(Objects.nonNull(data.get("a21004"))&& !data.get("a21004").toString().equals("0")){
+                Double a21024D = Double.parseDouble(data.get("a21004").toString());
+                if( a21024D>100){
+                    a21024D = a21024D -100;
+                }
+                dataResult.put("a21004",Double.parseDouble(String.format("%.4f",a21024D)));
+            }*/
+            historySecondUav.setValue(JSONObject.toJSONString(dataResult));
+            list.add(historySecondUav);
+        }
+        this.saveBatch(list);
+        return true;
+    }
+
+    @Override
+    public boolean UAVUpdateTest(String batch) {
+        List<HistorySecondUav> list = new ArrayList<>();
+        QueryWrapper<HistorySecondUav> wrapper = new QueryWrapper<>();
+        wrapper.eq("batch", batch);
+        wrapper.select("id,value,mac,time,organization_id");
+        wrapper.orderByAsc("time");
+        List<HistorySecondUav> datas = historySecondUavMapper.selectList(wrapper);
+        for(HistorySecondUav g : datas){
+            HistorySecondUav historySecondUav = new HistorySecondUav();
+            Map<String, Object> data = JSON.parseObject(g.getValue(), Map.class);
+            Map<String, Object> dataResult = new HashMap<>();
+            dataResult = data;
+
+            Double a34004 = Double.parseDouble(data.get("a34004").toString());
+            Double a34002 = Double.parseDouble(data.get("a34002").toString());
+            dataResult.put("a34002",Double.parseDouble(String.format("%.4f",a34004)));
+            dataResult.put("a34004",Double.parseDouble(String.format("%.4f",a34002)));
+
+           /* if(a21026D>11){
+               // Integer digit = a21026D % 10;
+                Integer digit = new Random().nextInt(5)+8;
+                dataResult.put("a21026",Double.parseDouble(digit.toString()));
+            }else {
+                continue;
+            }*/
+            historySecondUav.setId(g.getId());
+            historySecondUav.setValue(JSONObject.toJSONString(dataResult));
+            list.add(historySecondUav);
+        }
+        this.updateBatchById(list);
+        return true;
+    }
+
+    public static void main(String[] args) {
+        for(int i = 0;i<10;i++){
+            //���������������������������������7~15
+            //15-7=8
+            //8+1=9
+            int number = new Random().nextInt(5)+8;
+            //������������������������������������������
+            System.out.println(number);
+        }
+    }
 }

--
Gitblit v1.8.0