cjl
2023-10-30 f2f77f203fd6b054197ecaba0517a159e8b3ac8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
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.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.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.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
 
/**
 * <p>
 * 无人机秒数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-31
 */
@Service
public class HistorySecondUavServiceImpl extends ServiceImpl<HistorySecondUavMapper, HistorySecondUav> implements HistorySecondUavService {
 
    /*
     * 筛选数据举例
     * */
    private Double filterDistance = 2d;
 
    @Autowired
    HistorySecondUavMapper historySecondUavMapper;
    @Autowired
    OrganizationService organizationService;
    @Autowired
    SpecialDeviceService specialDeviceService;
    @Autowired
    private SysDictTypeService sysDictTypeService;
    @Autowired
    RedisTemplate redisTemplate;
 
    @Override
    public List<Date> queryDate(Integer organizationId) {
        //构造查询条件
        QueryWrapper<HistorySecondUav> queryWrapper = new QueryWrapper<>();
        //获取子组织id
        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);
        //设置查询时间范围为180天
        Date endDate = new Date();
        Date startDate = DateUtils.addDays(endDate, -180);
        queryWrapper.between("batch", startDate, endDate);
        //设置查询字段
        queryWrapper.select("DISTINCT batch");
        queryWrapper.orderByDesc("batch");
        //查询结果
        List<HistorySecondUav> historySecondUavs = historySecondUavMapper.selectList(queryWrapper);
        //结果转为Date集合
        List<Date> result = new ArrayList<>();
        for (HistorySecondUav historySecondUav : historySecondUavs) {
            result.add(historySecondUav.getBatch());
        }
        return result;
    }
 
    @Override
    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> childrenId = new ArrayList<>();
        for (Organization child : children) {
            childrenId.add(child.getId());
        }
        childrenId.add(organizationId);
        childrenId.addAll(integerList);
        wrapper.in("organization_id", childrenId);
        //查询根据batch查,因为可能会有跨天飞行的情况。
        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
        for (HistorySecondUav historySecondUav : historySecondUavs) {
            //获取batch对应的数据集合
            List<HistorySecondUav> list = batchMap.get(DateUtils.dateToDateString(historySecondUav.getBatch(), "yyyy-MM-dd HH:mm:ss"));
            if (list != null) {
                list.add(historySecondUav);
            } else {
                ArrayList<HistorySecondUav> newList = new ArrayList<>();
                newList.add(historySecondUav);
                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)
                    return true;
                return false;
            }
        });
        //根据mac进行分类
        Map<String, List<Map<String, List<HistorySecondUav>>>> macBatchMap = new LinkedHashMap<>();//key为mac
        //遍历batchMap将mac提取出来,作为macBatchMap的key,batch和数据map的数据集合放入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);
                maps.add(map);
            } 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);
            }
        });
        //封装返回数据
        List<UAVQueryTimeSlotDTO> dtos = new ArrayList<>();
        macBatchMap.forEach((key, value) -> {
            UAVQueryTimeSlotDTO dto = new UAVQueryTimeSlotDTO();
            List<Map<String, Object>> timeSlots = new ArrayList<>();
            dto.setMac(key);
            //根据mac查询设备名称
            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, Object> dateMap = new HashMap<>();
                    dateMap.put("startTime", slotStartDate);
                    dateMap.put("endTime", slotEndDate);
                    dateMap.put("batch", mKey);
                    dateMap.put("total",mValue.size());
                    timeSlots.add(dateMap);
                });
            });
            dto.setTimeSlot(timeSlots);
            dtos.add(dto);
        });
        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);
            //判断value里面有没有高度
            if (!valueMap.containsKey("flyhig")|| !valueMap.containsKey("flylat")|| !valueMap.containsKey("flylon")){
                continue;
            }
            //获取高度
            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);
            //判断value里面有没有高度
            if (!valueMap.containsKey("flyhig")|| !valueMap.containsKey("flylat")|| !valueMap.containsKey("flylon")){
                continue;
            }
            //获取高度
            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);
        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<>();
        //把第一个数据暂时作为对比数据
        HistorySecondUav tempData = datas.get(0);
        result.add(tempData);
        datas.remove(0);
        for (HistorySecondUav data : datas) {
            Double distance = getDistance(tempData, data);
            if (distance==null){
                continue;
            }
            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);
        //判断这两个数据里面有没有经纬度
        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 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);
        }
    }
 
    @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;
            int a21026D =(int) Math.round(Double.parseDouble(data.get("a21026").toString()));
            if(a21026D>10){
                Integer digit = a21026D % 10;
                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;
    }
 
}