jinpengyong
2024-03-15 fa2d52ee4dc866fb7ae3708e8fcabd9315b306cf
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
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.config.Interceptor.UserHelper;
import com.moral.api.entity.*;
import com.moral.api.mapper.DailyMapper;
import com.moral.api.mapper.HistorySecondCruiserMapper;
import com.moral.api.mapper.SpecialDeviceMapper;
import com.moral.api.pojo.bo.ExcelBO;
import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserListDTO;
import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultCountDTO;
import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO;
import com.moral.api.pojo.enums.SysDictTypeEnum;
import com.moral.api.pojo.vo.device.AppDeviceVo;
import com.moral.api.pojo.vo.excel.DailyVo;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SpecialDeviceHistoryService;
import com.moral.api.service.SpecialDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.SysAreaService;
import com.moral.api.service.SysDictTypeService;
import com.moral.constant.Constants;
 
import com.moral.util.AmendUtils;
import com.moral.util.DateUtils;
import com.moral.util.FileUtils;
import com.moral.util.GeodesyUtils;
import com.moral.util.TokenUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-18
 */
@Service
public class SpecialDeviceServiceImpl extends ServiceImpl<SpecialDeviceMapper, SpecialDevice> implements SpecialDeviceService {
 
    @Autowired
    private HistorySecondCruiserMapper HistorySecondCruiserMapper;
 
    @Autowired
    private SysDictTypeService sysDictTypeService;
 
    @Autowired
    private SpecialDeviceHistoryService specialDeviceHistoryService;
 
    @Autowired
    OrganizationService organizationService;
 
    @Autowired
    SpecialDeviceMapper specialDeviceMapper;
 
    @Autowired
    private HistorySecondCruiserMapper historySecondCruiserMapper;
 
    @Autowired
    private SpecialDeviceService specialDeviceService;
 
    @Autowired
    private DailyMapper dailyMapper;
 
    @Autowired
    RedisTemplate redisTemplate;
 
    @Autowired
    private SysAreaService sysAreaService;
 
    private final static Double dis = 50d;
 
 
    private final static Double DIS_NEW = 100d;
 
    @Override
    public List<Map<String, Object>> selectCruisers() {
        //获取当前用户信息
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer userId = (Integer) userInfo.get("userId");
        List<Integer> integerList = new ArrayList<>();
        SysDictData listSys = sysDictTypeService.listOne(SysDictTypeEnum.SYS_SECOND_CRUISER_RANGE.getValue(),userId.toString());
        if(listSys.getDataValue().contains(",")){
            List<Integer> resultStr = Arrays.asList(listSys.getDataValue().split(",")).stream().map(Integer::parseInt).collect(Collectors.toList());
            integerList.addAll(resultStr);
        }else {
            Integer dataValue = Objects.nonNull(listSys.getDataValue())?Integer.parseInt(listSys.getDataValue()):0;
            integerList.add(dataValue);
        }
        Integer orgId = (Integer) orgInfo.get("id");
        //获取所有子组织
        List<Organization> organizations = organizationService.getChildrenOrganizationsById(orgId);
        List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList());
        orgIds.add(orgId);
        integerList.addAll(orgIds);
        QueryWrapper<SpecialDeviceHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT mac, name").lambda();
 
                //.eq("special_type", Constants.SPECIAL_DEVICE_CRUISER)
        queryWrapper.eq("is_delete", Constants.NOT_DELETE)
                .in("organization_id", integerList);
        return specialDeviceHistoryService.listMaps(queryWrapper);
    }
 
    @Override
    public List<Map<String, Object>> carTrajectory(Map<String, Object> params,boolean type) {
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
        params.put("orgId", orgId);
        //从秒数据表获取走航车数据
        List<Map<String, Object>> data = HistorySecondCruiserMapper.getCruiserData(params);
        if (ObjectUtils.isEmpty(data)) {
            return data;
        }
        data.removeIf(o -> {
            Map<String, Object> value = JSONObject.parseObject(o.remove("value").toString(), Map.class);
            Object flylon = value.get(Constants.SENSOR_CODE_LON);
            Object flylat = value.get(Constants.SENSOR_CODE_LAT);
            if (ObjectUtils.isEmpty(flylon) || ObjectUtils.isEmpty(flylat)) {
                return true;
            }
            double lon = Double.parseDouble(flylon.toString());
            double lat = Double.parseDouble(flylat.toString());
            if (lon < 70 || lon > 150 || lat < 20 || lat > 60) {
                return true;
            }
            o.putAll(value);
            return false;
        });
        /*for (Map<String, Object> map:data) {
            Object third = map.get("dustld");
            map.put("dustld",map.get("a21026"));
            map.put("a21026",String.valueOf(Double.parseDouble(third.toString())*10000));
        }*/
        /*for (Map<String, Object> map : data) {
            String a21026 = String.valueOf(map.get("a21026"));
            String a21004 = String.valueOf(map.get("a21004"));
            map.put("a21026",String.valueOf(Double.parseDouble(a21026) / 100));
            map.put("a21004",String.valueOf(Double.parseDouble(a21004) / 10));
        }*/
        return filterData(data);
    }
 
    @Override
    public List<HistorySecondCruiserResultDTO> carTrajectoryNewAvg(Map<String, Object> params) {
        double range = Double.valueOf(params.get("range").toString());
        String type = params.get("type").toString();
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Integer userId = (Integer) userInfo.get("userId");
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
        List<Integer> integerList = new ArrayList<>();
        SysDictData listSys = sysDictTypeService.listOne(SysDictTypeEnum.SYS_SECOND_CRUISER_RANGE.getValue(),userId.toString());
        if(listSys.getDataValue().contains(",")){
            List<Integer> resultStr = Arrays.asList(listSys.getDataValue().split(",")).stream().map(Integer::parseInt).collect(Collectors.toList());
            integerList.addAll(resultStr);
        }else {
            Integer dataValue = Objects.nonNull(listSys.getDataValue())?Integer.parseInt(listSys.getDataValue()):0;
            integerList.add(dataValue);
        }
        integerList.add(orgId);
        params.put("orgIds", integerList);
        params.put("type","$."+type);
        //从秒数据表获取走航车数据
        List<HistorySecondCruiserListDTO> data = HistorySecondCruiserMapper.getCruiserDataNewAvg(params);
        data.removeIf(o->{
            if (ObjectUtils.isEmpty(o.getFlyLon()) || ObjectUtils.isEmpty(o.getFlyLat())) {
                return true;
            }
            double lon = o.getFlyLon();
            double lat = o.getFlyLat();
            if (lon < 70 || lon > 150 || lat < 20 || lat > 60) {
                return true;
            }
            return false;
        });
        if(CollectionUtils.isEmpty(data)){
            return null;
        }
        List<HistorySecondCruiserResultDTO> result = filterDataNew(data,range);
        if(CollectionUtils.isNotEmpty(result)){
            SysDictData list = sysDictTypeService.listOne(SysDictTypeEnum.SYS_SECOND_CRUISER.getValue(),type);
            if(list.getDataValue().contains(",")){
                List<String> resultStr = Arrays.asList(list.getDataValue().split(","));
                if(resultStr.size() % 2 ==0){
                    result.forEach(it->{
                        it.setAvgNum(numAvg(resultStr,it.getAvgNum()));
                    });
                }
            }else {
                BigDecimal dataValue = Objects.nonNull(list.getDataValue())?BigDecimal.valueOf(Double.parseDouble(list.getDataValue())):BigDecimal.ZERO;
                result.forEach(it->{
                    it.setAvgNum(it.getAvgNum().add(dataValue));
                });
            }
 
 
        }
        return result;
    }
 
    private BigDecimal numAvg(List<String> list , BigDecimal num){
        int nums = 1;
        for (int i=0;i<list.size();i=i+2){
            if(num.compareTo(BigDecimal.valueOf(Double.parseDouble(list.get(i))))>= 0 ){
                return num.add(BigDecimal.valueOf(Double.parseDouble(list.get(i+1))));
            }
            nums+=2;
        }
        return num;
    }
 
    @Override
    public SpecialDevice getSpecialDeviceMapByMac(String mac) {
        QueryWrapper<SpecialDevice> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mac", mac);
        queryWrapper.eq("is_delete", Constants.NOT_DELETE);
        SpecialDevice specialDevice = specialDeviceMapper.selectOne(queryWrapper);
        return specialDevice;
    }
 
    @Override
    public List<String> getDatesByMac(String mac) {
        Map<String, Object> userInfo = (Map) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
 
        Map<String, Object> params = new HashMap<>();
        params.put("orgId", orgId);
        params.put("mac", mac);
        Date start = DateUtils.addMonths(new Date(), -6);
        params.put("start", DateUtils.dateToDateString(start));
        return historySecondCruiserMapper.getDates(params);
    }
 
 
 
    /**
     * 走航车日报
     * @param params
     * @return
     */
    @Override
    public ExcelBO getDaily(Map<String, Object> params, List<MultipartFile> files) throws ParseException {
        ExcelBO excelBO = new ExcelBO();
        ArrayList<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time1 = params.get("time1").toString();
        String time2 = params.get("time2").toString();
        String rsTime = getTime(time1, time2);
        list.add(rsTime);
//        List<Map<String, Object>> maps = specialDeviceService.carTrajectory(params,false);
//        List<Map<String, Object>> maps = HistorySecondCruiserMapper.getCruiserData(params);
        List<Map<String, Object>> maps = HistorySecondCruiserMapper.getAllCruiserData(params);
 
        if (ObjectUtils.isEmpty(maps)){
            return null;
        }
        if (params.containsKey("time3")){
            Map<String, Object> map = new HashMap<>();
            String time3 = params.get("time3").toString();
            String time4 = params.get("time4").toString();
            String mac = params.get("mac").toString();
            map.put("time1",time3);
            map.put("time2",time4);
            map.put("mac",mac);
//            List<Map<String, Object>> maps1 = specialDeviceService.carTrajectory(map,false);
            List<Map<String, Object>> maps1 = HistorySecondCruiserMapper.getAllCruiserData(params);
            if (ObjectUtils.isEmpty(maps1)){
                return null;
            }
            maps.addAll(maps1);
            list.add(getTime(time3, time4));
 
        }
        if (params.containsKey("time5")){
            Map<String, Object> map = new HashMap<>();
            String time5 = params.get("time5").toString();
            String time6 = params.get("time6").toString();
            String mac = params.get("mac").toString();
            map.put("time1",time5);
            map.put("time2",time6);
            map.put("mac",mac);
//            List<Map<String, Object>> maps1 = specialDeviceService.carTrajectory(map,false);
            List<Map<String, Object>> maps1 = HistorySecondCruiserMapper.getAllCruiserData(params);
            if (ObjectUtils.isEmpty(maps1)){
                return null;
            }
            maps.addAll(maps1);
            list.add(getTime(time5, time6));
 
        }
        HashMap<String, Object> rsMap = getStringObjectHashMap(maps);
 
        rsMap.put("time",list);
 
 
        //获取jar包所在目录
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String path = applicationHome.getSource().getParentFile().toString() + "/static/img";
 
 
 
        String type = params.get("type").toString();
        String code = params.get("code").toString();
        String area = params.get("area").toString();
        String mac = params.get("mac").toString();
        rsMap.put("area",area);
        rsMap.put("mac",mac.substring(9));
        String value = JSON.toJSONString(rsMap);
 
        QueryWrapper<TbDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mac",mac);
        queryWrapper.likeRight("time",time1.substring(0,10));
        TbDaily daily = dailyMapper.selectOne(queryWrapper);
        ArrayList<String> images = new ArrayList<>();
 
        if (daily==null){
            TbDaily daily1 = new TbDaily();
            //获取图片
            if (files.size()>0){
                for (MultipartFile file : files) {
                    String fileType = file.getContentType();
                    if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){
                        //获取文件名
                        String fileName = file.getOriginalFilename();
                        //获取文件后缀名
                        String suffixName = fileName.substring(fileName.lastIndexOf("."));
                        //每一次都需要
                        fileName.substring(fileName.lastIndexOf("."));
                        //重新生成文件名
                        fileName = UUID.randomUUID() + suffixName;
                        //图片上传
                        if (FileUtils.upload(file, path, fileName)) {
                            images.add(fileName);
                        }
                    }
                }
            }
 
            if (!ObjectUtils.isEmpty(images)) {
                String image = images.stream()
                        .map(String::valueOf)
                        .collect(Collectors.joining(","));
                daily1.setImages(image);
            }
            //封装数据
            daily1.setMac(mac);
            daily1.setTime(sdf.parse(time1));
            Date date = new Date();
            daily1.setDate(date);
            daily1.setValue(value);
            daily1.setType(type);
            daily1.setCode(code);
            dailyMapper.insert(daily1);
            excelBO.setId(daily1.getId());
            excelBO.setTime(sdf.parse(time1));
            excelBO.setDate(date);
        }else {
            //删除服务器中的原来图片
            String images1 = daily.getImages();
//            ObjectUtils.isEmpty(images1);
            if (!ObjectUtils.isEmpty(images1)){
                String[] splits = images1.split(",");
                for (String split : splits) {
                    String realPath = path + File.separator + split;
                    File file = new File(realPath);
                    if (file.exists() && file.isFile()) {
                        file.delete();
                    }
                }
            }
 
 
            if (files.size() > 0) {
                for (MultipartFile file : files) {
                    //判断上传文件格式
                    String fileType = file.getContentType();
                    if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)) {
                        //获取文件名
                        String fileName = file.getOriginalFilename();
                        //获取文件后缀名
                        String suffixName = fileName.substring(fileName.lastIndexOf("."));
                        //重新生成文件名
                        fileName = UUID.randomUUID() + suffixName;
                        //图片上传
                        if (FileUtils.upload(file, path, fileName)) {
                            images.add(fileName);
                        }
                    }
                }
            }
 
            if (!ObjectUtils.isEmpty(images)) {
                String image = images.stream()
                        .map(String::valueOf)
                        .collect(Collectors.joining(","));
                daily.setImages(image);
 
            }else {
                daily.setImages("");
            }
            daily.setValue(value);
            daily.setTime(sdf.parse(time1));
            daily.setTime(sdf.parse(time1));
            daily.setDate(new Date());
            daily.setType(type);
            dailyMapper.updateById(daily);
            excelBO.setId(daily.getId());
            excelBO.setTime(sdf.parse(time1));
            excelBO.setDate(new Date());
 
        }
        return excelBO;
    }
 
    private String getTime(String time1, String time2) {
        String substring1 = time1.substring(11, 13);
        String substring2= time2.substring(11, 13);
        String substring3 = time1.substring(5, 7);
        String substring4 = time1.substring(8, 10);
        return substring3 + "月" + substring4 + "日" + substring1 + "时-" + substring2+"时";
    }
 
    /**
     * 下载走航车日报
     * @param ids
     * @return
     */
    @Override
    public List<DailyVo> loadDaily(List<Integer> ids) {
        ArrayList<DailyVo> dailyVos = new ArrayList<>();
        for (Integer id : ids) {
            ArrayList<String> rsList = new ArrayList<>();
            DailyVo dailyVo = new DailyVo();
            TbDaily tbDaily = dailyMapper.selectById(id);
 
            SysArea areaByCode = sysAreaService.getAreaByCode(Integer.parseInt(tbDaily.getCode()));
            if (tbDaily==null){
                continue;
            }
            String images = tbDaily.getImages();
            if (!ObjectUtils.isEmpty(images)){
                String[] split = images.split(",");
                List<String> list1 = Arrays.asList(split);
                dailyVo.setImages(list1);
            }else {
                dailyVo.setImages(rsList);
            }
            String value = tbDaily.getValue();
            Map map = JSON.parseObject(value, Map.class);
            List<String> list = (List<String>) map.get("time");
            String join = String.join("-", list);
            map.put("time",join);
            dailyVo.setCode(map);
            dailyVo.setName(areaByCode.getAreaName());
            dailyVo.setType(tbDaily.getType().equals("car")?"走航车":"无人机");
            dailyVos.add(dailyVo);
        }
        return dailyVos;
    }
 
    /**
     * 查询日报
     * @param params
     * @return
     */
    @Override
    public List<ExcelBO> selectDaily(Map<String, Object> params) {
        ArrayList<ExcelBO> excelBOS = new ArrayList<>();
        String startTime = params.get("startTime").toString();
        String endTime = params.get("endTime").toString();
        String type = params.get("type").toString();
        String code = params.get("code").toString();
 
        Object mac = params.get("mac");
        if (!ObjectUtils.isEmpty(mac)){
            String[] split = mac.toString().split(",");
            List<String> list = Arrays.asList(split);
            List<String> macList = new ArrayList<>(new TreeSet<>(list));
            QueryWrapper<TbDaily> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type",type).eq("code",code).in("mac",macList);
            queryWrapper.between("time",startTime,endTime);
            List<TbDaily> dailies = dailyMapper.selectList(queryWrapper);
            for (TbDaily daily : dailies) {
                ExcelBO excelBO = new ExcelBO();
                BeanUtils.copyProperties(daily,excelBO);
                excelBOS.add(excelBO);
            }
            return excelBOS;
        }
        QueryWrapper<TbDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type).eq("code",code);
        queryWrapper.between("time",startTime,endTime);
        List<TbDaily> dailies = dailyMapper.selectList(queryWrapper);
        for (TbDaily daily : dailies) {
            ExcelBO excelBO = new ExcelBO();
            BeanUtils.copyProperties(daily,excelBO);
            excelBOS.add(excelBO);
        }
        return excelBOS;
    }
 
    @Override
    public List<AppDeviceVo> selectSpecialDevice() {
        QxUser user = UserHelper.getCurrentUser();
        Integer organizationId = user.getOrganizationId();
//        Integer organizationId=71;
        List<AppDeviceVo> appDeviceVos;
        if (organizationId!=24){
            appDeviceVos = specialDeviceMapper.selectSpecialDevice(organizationId);
        }else {
            appDeviceVos = specialDeviceMapper.selectSpecialDevice(null);
        }
        return appDeviceVos;
    }
 
    //计算平均值和最大值
    private HashMap<String, Object> getStringObjectHashMap(List<Map<String, Object>> maps) {
        HashMap<String, Object> rsMap = new HashMap<>();
 
        ArrayList<Double> pm25List = new ArrayList<>();
        ArrayList<Double> pm10List = new ArrayList<>();
        ArrayList<Double> COList = new ArrayList<>();
        ArrayList<Double> SO2List = new ArrayList<>();
        ArrayList<Double> NO2List = new ArrayList<>();
        ArrayList<Double> O3List = new ArrayList<>();
        ArrayList<Double> VOCList = new ArrayList<>();
 
 
        for (Map<String, Object> map : maps) {
            Map value = JSON.parseObject(map.get("value").toString(), Map.class);
            if (!ObjectUtils.isEmpty(value.get("a34004"))){
                pm25List.add(Double.parseDouble(value.get("a34004").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a34002"))){
                pm10List.add(Double.parseDouble(value.get("a34002").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a21005"))){
                COList.add(Double.parseDouble(value.get("a21005").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a21026"))){
                SO2List.add(Double.parseDouble(value.get("a21026").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a21004"))){
                NO2List.add(Double.parseDouble(value.get("a21004").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a05024"))){
                O3List.add(Double.parseDouble(value.get("a05024").toString()));
            }
            if (!ObjectUtils.isEmpty(value.get("a99054"))){
                VOCList.add(Double.parseDouble(value.get("a99054").toString()));
            }
        }
 
        //获取平均值
        double PM25 = AmendUtils.sciCal(pm25List.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
        double PM10 = AmendUtils.sciCal(pm10List.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
        double CO = AmendUtils.sciCal(COList.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
        double SO2 = AmendUtils.sciCal(SO2List.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
        double O3 = AmendUtils.sciCal(O3List.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
        double VOC= AmendUtils.sciCal(VOCList.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
        double NO2= AmendUtils.sciCal(NO2List.stream().collect(Collectors.averagingDouble(Double::doubleValue)), 2);
 
 
        rsMap.put("maxPm25", Collections.max(pm25List));
        rsMap.put("avgPm25",PM25);
        rsMap.put("maxPm10",Collections.max(pm10List));
        rsMap.put("avgPm10",PM10);
        rsMap.put("maxCO",Collections.max(COList));
        rsMap.put("avgCO",CO);
        rsMap.put("maxSO2",Collections.max(SO2List));
        rsMap.put("avgSO2",SO2);
        rsMap.put("maxO3",Collections.max(O3List));
        rsMap.put("avgO3",O3);
        rsMap.put("maxVOC",Collections.max(VOCList));
        rsMap.put("avgVOC",VOC);
        rsMap.put("maxNO2",Collections.max(NO2List));
        rsMap.put("avgNO2",NO2);
        return rsMap;
    }
 
 
 
    //根据距离筛选数据
    private List<Map<String, Object>> filterData(List<Map<String, Object>> data) {
        List<Map<String, Object>> result = new ArrayList<>();
        result.add(data.remove(0));
        for (Map<String, Object> map : data) {
            boolean flag = true;
            for (Map<String, Object> resultMap : result) {
                double lng1 = Double.parseDouble(map.get(Constants.SENSOR_CODE_LON).toString());
                double lat1 = Double.parseDouble(map.get(Constants.SENSOR_CODE_LAT).toString());
                double lng2 = Double.parseDouble(resultMap.get(Constants.SENSOR_CODE_LON).toString());
                double lat2 = Double.parseDouble(resultMap.get(Constants.SENSOR_CODE_LAT).toString());
                double distance = GeodesyUtils.getDistance(lat1, lng1, lat2, lng2);
                if (distance < dis) {
                    flag = false;
                }
            }
            if (flag) {
                result.add(map);
            }
        }
 
 
        return result;
    }
    //范围取均值
    private List<Map<String, Object>> filterData(List<Map<String, Object>> data, List<Map<String, Object>> result) {
        List<Map<String, Object>> list = new ArrayList<>();
 
        Map<String,List<Object>> map = new HashMap<>();
        result.forEach(it->map.put(it.get(Constants.SENSOR_CODE_LON).toString()+ "_"+Constants.SENSOR_CODE_LAT.toString(),Arrays.asList(it)));
        // 坐标在50 以内分组
        for (Map<String, Object> mapData : data) {
            for (Map.Entry<String, List<Object>> entry : map.entrySet()) {
                double lng1 = Double.parseDouble(mapData.get(Constants.SENSOR_CODE_LON).toString());
                double lat1 = Double.parseDouble(mapData.get(Constants.SENSOR_CODE_LAT).toString());
                String[] mapKey = entry.getKey().split("_");
                List<Object> mapValue = entry.getValue();
                double lng2 = Double.parseDouble(mapKey[0]);
                double lat2 = Double.parseDouble(mapKey[1]);
                double distance = GeodesyUtils.getDistance(lat1, lng1, lat2, lng2);
                if (distance < dis) {
                    mapValue.add(mapData);
                }
            }
        }
 
        return list;
    }
    //根据距离筛选数据
    private List<HistorySecondCruiserResultDTO> filterDataNew(List<HistorySecondCruiserListDTO> data,double range) {
        List<HistorySecondCruiserListDTO> list = new ArrayList<>();
        Map<Integer, HistorySecondCruiserResultCountDTO> map = new HashMap<>();
        Map<String, BigDecimal> mapTest = new HashMap<>();
        Integer integer = 1;
        HistorySecondCruiserResultCountDTO countDTO  = new HistorySecondCruiserResultCountDTO();
        countDTO.setSumNum(data.get(0).getNum());
        countDTO.setContNum(1);
        map.put(integer,countDTO);
        list.add(0,data.remove(0));
        mapTest.put(data.get(0).getFlyLon()+"_"+data.get(0).getFlyLat(),data.get(0).getNum());
        int k =1;
        for (HistorySecondCruiserListDTO d : data) {
            boolean flag = true;
            for (HistorySecondCruiserListDTO l : list) {
                double distance = GeodesyUtils.getDistance(d.getFlyLat(), d.getFlyLon(), l.getFlyLat(), l.getFlyLon());
                if (distance < range) {
                    flag = false;
                    if(integer.equals(list.size())&& !mapTest.containsKey(d.getFlyLon()+"_"+d.getFlyLat())){
                        HistorySecondCruiserResultCountDTO countResultDTO = Objects.isNull(map.get(integer))?new HistorySecondCruiserResultCountDTO():map.get(integer);
                        countResultDTO.setContNum(countResultDTO.getContNum()+1);
                        countResultDTO.setSumNum(countResultDTO.getSumNum().add(d.getNum()));
                        map.put(integer,countResultDTO);
                        mapTest.put(d.getFlyLon()+"_"+d.getFlyLat(),d.getNum());
                        k++;
                    }
                }
            }
            if (flag) {
                if(!list.contains(d)){
                    list.add(d);
                    integer++;
                    HistorySecondCruiserResultCountDTO countResultDTO = Objects.isNull(map.get(integer))?new HistorySecondCruiserResultCountDTO():map.get(integer);
                    countResultDTO.setSumNum(d.getNum());
                    countResultDTO.setContNum(1);
                    map.put(integer,countResultDTO);
                    mapTest.put(d.getFlyLon()+"_"+d.getFlyLat(),d.getNum());
                }
            }
        }
        if(!list.contains(data.get(data.size()-1))){
            HistorySecondCruiserListDTO result = data.get(data.size()-1);
            list.add(result);
            integer++;
            HistorySecondCruiserResultCountDTO countResultDTO = new HistorySecondCruiserResultCountDTO();
            countResultDTO.setContNum(1);
            countResultDTO.setSumNum(result.getNum());
            map.put(integer,countResultDTO);
        }
        return filterDataNe(list,map);
    }
    private List<HistorySecondCruiserResultDTO> filterDataNe(List<HistorySecondCruiserListDTO> list,Map<Integer, HistorySecondCruiserResultCountDTO> map){
        List<HistorySecondCruiserResultDTO> listResult = new ArrayList<>();
        for(Map.Entry<Integer, HistorySecondCruiserResultCountDTO> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            HistorySecondCruiserResultCountDTO mapValue = entry.getValue();
            BigDecimal avgNum = mapValue.getSumNum().divide(BigDecimal.valueOf(mapValue.getContNum()),2,BigDecimal.ROUND_HALF_UP);
            HistorySecondCruiserResultDTO resultDTO = new HistorySecondCruiserResultDTO();
            resultDTO.setTime(list.get(mapKey-1).getTime());
            resultDTO.setAvgNum(avgNum);
            resultDTO.setFlyLon(list.get(mapKey-1).getFlyLon());
            resultDTO.setFlyLat(list.get(mapKey-1).getFlyLat());
            listResult.add(resultDTO);
        }
    return listResult;
 
 
    }
 
    private List<HistorySecondCruiserResultDTO> filterDataNew(List<HistorySecondCruiserListDTO> data, List<HistorySecondCruiserListDTO> list,double range) {
        List<HistorySecondCruiserResultDTO> listResult = new ArrayList<>();
        List<HistorySecondCruiserListDTO> filter = new ArrayList<>();
        for (HistorySecondCruiserListDTO d : data) {
            for (HistorySecondCruiserListDTO l : list) {
                double distance = GeodesyUtils.getDistance(d.getFlyLat(), d.getFlyLon(), l.getFlyLat(), l.getFlyLon());
                if (filter.contains(d)) {
                    break;
                }
                if (distance < range ) {
                    List<BigDecimal> numList = new ArrayList<>(CollectionUtils.isEmpty(l.getNumList()) ? Arrays.asList(l.getNum()) : l.getNumList());
                    numList.add(d.getNum());
                    l.setNumList(numList);
                    filter.add(d);
                    break;
                }
            }
        }
        int k = 0;
        for(int j =0;j<list.size(); j++){
            HistorySecondCruiserListDTO l = list.get(j);
            HistorySecondCruiserResultDTO resultDTO = new HistorySecondCruiserResultDTO();
            resultDTO.setFlyLat(l.getFlyLat());
            resultDTO.setTime(l.getTime());
            resultDTO.setFlyLon(l.getFlyLon());
            BigDecimal num = BigDecimal.ZERO;
            int count = l.getNumList().size();
            for(int i = 0;i < count; i++) {
                num = num.add(l.getNumList().get(i));
            }
            k = k+count;
            resultDTO.setAvgNum(num.divide(BigDecimal.valueOf(count),2,BigDecimal.ROUND_HALF_UP));
            listResult.add(resultDTO);
        }
        System.out.println("sss>>>>:"+ (k++));
        System.out.println("sss>>>>:"+ (filter.size())+">>>>:"+data.size());
        int z = 0 ;
        return listResult;
 
 
    }
 
    /**
     *
     */
      private double getDivisor(String code,double num){
          double rs = 0.0;
        if (code.contains("-")){
            String data = code.substring(1);
            rs = num - Double.parseDouble(data);
        } else if (code.contains("*")){
            String data = code.substring(1);
            rs = num * Double.parseDouble(data);
        } else if (code.contains("/")){
            String data = code.substring(1);
            rs = num / Double.parseDouble(data);
        } else if (code.contains(",")){
            String[] split = code.split(",");
            for (String s : split) {
                String[] split1 = s.split("<");
                double sp1 = Double.parseDouble(split1[0]);
                double sp2 = Double.parseDouble(split1[1]);
                double sp3 = Double.parseDouble(split1[2]);
                if (num>=sp1 && num<=sp2){
                    rs = num + sp3;
                }
            }
        }else {
            rs = num + Double.parseDouble(code);
        }
        return rs;
      }
}