jinpengyong
2023-10-31 69790994b403a61e92a20cef7451b7f087b50ad2
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.moral.api.config.Interceptor.UserHelper;
import com.moral.api.dto.MonitoringStationDTO;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.HistoryMonthlyMapper;
import com.moral.api.mapper.MonitorPointMapper;
import com.moral.api.mapper.SensorMapper;
import com.moral.api.pojo.form.device.MonitorPointQueryForm;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.DeviceService;
import com.moral.api.service.HistoryDailyService;
import com.moral.api.service.HistoryHourlyService;
import com.moral.api.service.HistoryMonthlyService;
import com.moral.api.service.MonitorPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import com.moral.util.ComprehensiveIndexUtils;
import com.moral.util.DateUtils;
import com.moral.util.RegionCodeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import springfox.documentation.schema.Entry;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-07-01
 */
@Service
public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, MonitorPoint> implements MonitorPointService {
 
    @Autowired
    MonitorPointMapper monitorPointMapper;
    @Autowired
    OrganizationService organizationService;
    @Autowired
    DeviceService deviceService;
 
    @Autowired
    DeviceMapper deviceMapper;
 
    @Autowired
    SensorMapper sensorMapper;
 
    @Autowired
    HistoryHourlyService historyHourlyService;
 
    @Autowired
    HistoryDailyService historyDailyService;
 
    @Autowired
    HistoryMonthlyService historyMonthlyService;
    @Autowired
    HistoryMonthlyMapper historyMonthlyMapper;
 
    @Override
    public List<MonitorPoint> queryByOrgIdAndRegionCode(MonitorPointQueryForm form) {
        //取参
        Integer organizationId = form.getOrganizationId();
        Integer regionCode = form.getRegionCode();
        String region = null;
        if (regionCode != null && organizationId!=24) {
            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
        }
//        if (regionCode != null) {
//            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
//        }
 
       /* //查询子组织
        List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId);
 
        for (Organization organization : childrenOrganization) {
            organizationIds.add(organization.getId());
        }
        organizationIds.add(organizationId);*/
        //查询站点
        QueryWrapper<MonitorPoint> queryMonitorPointsWrapper = new QueryWrapper<>();
 
        //如果region不为空,就查询当前组织,所选城市下所有站点及设备信息
        //如果region为空,则查询当前组织下所有的站点和设备
        List<Integer> organizationIds = deviceMapper.deviceOrgIdList(organizationId);
        if (region != null){
            queryMonitorPointsWrapper.eq(region, regionCode);
        }
        queryMonitorPointsWrapper.in("organization_id", organizationIds);
        queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper);
        //查询站点对应的设备
        for (MonitorPoint monitorPoint : monitorPoints) {
            List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPoint.getId());
            monitorPoint.setDevices(devices);
        }
        return monitorPoints;
    }
 
    @Override
    public List<MonitorPoint> queryAllMonitorPoints(Integer organizationId) {
        //查询子组织
        List<Organization> childrenOrganization = organizationService.getChildrenOrganizationsById(organizationId);
        List<Integer> organizationIds = new ArrayList<>();
        for (Organization organization : childrenOrganization) {
            organizationIds.add(organization.getId());
        }
        organizationIds.add(organizationId);
        //查询站点
        QueryWrapper<MonitorPoint> queryMonitorPointsWrapper = new QueryWrapper<>();
        queryMonitorPointsWrapper.select("id","name");
        queryMonitorPointsWrapper.in("organization_id",organizationIds);
        queryMonitorPointsWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(queryMonitorPointsWrapper);
        return monitorPoints;
    }
 
    @Override
    public List<Map<String, Object>> getHourlyDataByMonitorPoint(Map map) {
        int monitorPointId = Integer.parseInt(map.get("monitorPointId").toString());
        QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
        monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        monitorPointQueryWrapper.eq("id",monitorPointId);
        MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper);
        QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        deviceQueryWrapper.eq("monitor_point_id",monitorPointId);
        List<Device> devices = deviceMapper.selectList(deviceQueryWrapper);
        if (devices.size()<=0){
            return null;
        }
        String[] sensors = map.remove("sensors").toString().split(",");
        List<String> sensorsList = Arrays.asList(sensors);
        String startTime = map.get("startTime").toString();
        String endTime = map.get("endTime").toString();
        Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
        Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
        List<Map<String,Object>> resultList = new ArrayList<>();
        for (Device device:devices) {
            List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(map.get("startTime").toString(), DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(map.get("endTime").toString(), DateUtils.yyyy_MM_dd_HH_EN));
            for (String sensor:sensorsList) {
                Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                timeValueMap.put("监测站点",monitorPoint.getName());
                timeValueMap.put("name",device.getName());
                QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
                sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                sensorQueryWrapper.eq("code",sensor);
                Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper);
                timeValueMap.put("sensor",sensorEntity.getName());
                for (HistoryHourly historyHourly : hourlies) {
                    Date time = historyHourly.getTime();
                    String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH");
                    String value = historyHourly.getValue();
                    JSONObject jsonObject = JSONObject.parseObject(value);
                    if (jsonObject.containsKey(sensor)){
                        timeValueMap.put(dateStr, jsonObject.get(sensor).toString());
                    }
                }
                //补上无数据时间
                Date middleDate = startDate;
                while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){
                    if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null)
                        timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null);
                    middleDate = DateUtils.addHours(middleDate,1);
                }
                resultList.add(timeValueMap);
            }
        }
        return resultList;
    }
 
    /**
     * 监测站点数据导出
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getHourlyDataDataV3(Map<String, Object> params) {
        List<String> macs = (List<String>) params.remove("macs");
        List<String> times = (List<String>) params.remove("times");
        String type = params.get("type").toString();
        String startTime = times.get(0);
        String endTime = times.get(1);
 
//        String[] macs = params.remove("macs").toString().split(",");
//        List<String> macsList = Arrays.asList(macs);
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",Constants.NOT_DELETE);
        queryWrapper.in("mac", macs);
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        if (devices.size()<=0){
            return null;
        }
        String[] sensors = params.remove("sensors").toString().split(",");
        List<String> sensorsList = Arrays.asList(sensors);
        List<Map<String,Object>> resultList = new ArrayList<>();
 
//        List<String> sensorsList = (List<String>) params.remove("sensors");
//        String startTime = params.get("startTime").toString();
//        String endTime = params.get("endTime").toString();
        if (type.equals("hours")){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            for (Device device:devices) {
                List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(device.getMac(), DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN));
                for (String sensor:sensorsList) {
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
                    monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    monitorPointQueryWrapper.eq("id",device.getMonitorPointId());
                    MonitorPoint monitorPoint = monitorPointMapper.selectOne(monitorPointQueryWrapper);
                    timeValueMap.put("监测站点",monitorPoint.getName());
                    timeValueMap.put("name",device.getName());
                    QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
                    sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
                    sensorQueryWrapper.eq("code",sensor);
                    Sensor sensorEntity = sensorMapper.selectOne(sensorQueryWrapper);
                    timeValueMap.put("sensor",sensorEntity.getName());
                    for (HistoryHourly historyHourly : hourlies) {
                        Date time = historyHourly.getTime();
                        String dateStr = DateUtils.dateToDateString(time, "yyyy-MM-dd HH");
                        String value = historyHourly.getValue();
                        JSONObject jsonObject = JSONObject.parseObject(value);
                        if (jsonObject.containsKey(sensor)){
                            timeValueMap.put(dateStr, jsonObject.get(sensor).toString());
                        }
                    }
                    //补上无数据时间
                    Date middleDate = startDate;
                    while (DateUtils.compareDateStr(DateUtils.dateToDateString(endDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){
                        if (timeValueMap.get(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN)) == null)
                            timeValueMap.put(DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN), null);
                        middleDate = DateUtils.addHours(middleDate,1);
                    }
                    resultList.add(timeValueMap);
                }
            }
        } else  if("day".equals(type)){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate);
            Map<String,Map<String,Object>> map = resultMap(historyDailyByMacAndTimeSlot,new ArrayList<>());
            Map<Integer,String> pointMap = pointMap(devices);
            Map<String,String> sensorMap = sensorMap(sensorsList);
            for (Device device:devices) {
                for (String sensor:sensorsList) {
                    Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd");
                    Date js = DateUtils.getDateOfDay(DateUtils.getDate(endTime,"yyyy-MM-dd"),1);
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId()));
                    timeValueMap.put("name",device.getName());
                    timeValueMap.put("sensor",sensorMap.get(sensor));
                    ArrayList<Double> doubleArrayList = new ArrayList<>();
                    while (DateUtils.isTimeBefor(js,ks)){
                        String c = DateUtils.dateToDateString(ks,"yyyy-MM-dd");
                        String k = c+"_"+device.getMac();
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0);
                            if(Objects.nonNull(o)&& (!Double.valueOf(o.toString()).equals(0d))){
                                doubleArrayList.add(Double.valueOf(o.toString()));
                            }
                        }else {
                            timeValueMap.put(c,0);
                        }
                        ks = DateUtils.getDateOfDay(ks,1);
                    }
                    Double ListAvg = doubleArrayList.stream().collect(Collectors.averagingDouble(Double::doubleValue));
                    double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    timeValueMap.put("累计值",rsAvg);
                    resultList.add(timeValueMap);
                }
            }
        }else {
            QueryWrapper<HistoryMonthly> HistoryMonthlyWrapper = new QueryWrapper<>();
            HistoryMonthlyWrapper.in("mac",devices.stream().map(Device::getMac).collect(Collectors.toList()));
            HistoryMonthlyWrapper.between("time",startTime,endTime);
            List<HistoryMonthly> historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper);
            Map<String,Map<String,Object>> map = resultMap(new ArrayList<>(),historyMonthlyList);
            Map<Integer,String> pointMap = pointMap(devices);
            Map<String,String> sensorMap = sensorMap(sensorsList);
            for (Device device:devices) {
                for (String sensor:sensorsList) {
                    Date ks = DateUtils.getDate(startTime,"yyyy-MM");
                    Date js = DateUtils.addMonths(DateUtils.getDate(endTime,"yyyy-MM"),1);
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId()));
                    timeValueMap.put("name",device.getName());
                    timeValueMap.put("sensor",sensorMap.get(sensor));
                    while (DateUtils.isTimeBefor(js,ks)){
                        String c = DateUtils.dateToDateString(ks,"yyyy-MM");
                        String k = c+"_"+device.getMac();
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0);
                        }else {
                            timeValueMap.put(c,0);
                        }
                        ks = DateUtils.addMonths(ks,1);
                    }
                    resultList.add(timeValueMap);
                }
            }
        }
        return resultList;
    }
 
    @Override
    public List<Map<String, Object>> getHourlyDataDataV3Excel(Map<String, Object> params) {
        List<String> macs = (List<String>) params.remove("macs");
        List<String> times = (List<String>) params.remove("times");
        String type = params.get("type").toString();
        String startTime = times.get(0);
        String endTime = times.get(1);
 
//        String[] macs = params.remove("macs").toString().split(",");
//        List<String> macsList = Arrays.asList(macs);
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",Constants.NOT_DELETE);
        queryWrapper.in("mac", macs);
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        if (devices.size()<=0){
            return null;
        }
        String[] sensors = params.remove("sensors").toString().split(",");
        List<String> sensorsList = Arrays.asList(sensors);
        List<Map<String,Object>> resultList = new ArrayList<>();
 
//        List<String> sensorsList = (List<String>) params.remove("sensors");
//        String startTime = params.get("startTime").toString();
//        String endTime = params.get("endTime").toString();
        if (type.equals("hours")){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            List<HistoryHourly> hourlies = historyHourlyService.getValueByMacAndTime(macs, DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN), DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN));
            Map<String,Map<String,Object>> map = resultMap(hourlies);
            Map<Integer,String> pointMap = pointMap(devices);
            Map<String,String> sensorMap = sensorMap(sensorsList);
 
            for (Device device:devices) {
                Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd HH");
                Date js = DateUtils.getDateAddHour(DateUtils.getDate(endTime,"yyyy-MM-dd HH"),1);
                while (DateUtils.isTimeBefor(js,ks)){
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId()));
                    timeValueMap.put("name",device.getName());
                    String c = DateUtils.dateToDateString(ks,DateUtils.yyyy_MM_dd_HH_EN);
                    timeValueMap.put("日期",c);
                    for (String sensor:sensorsList) {
                        String k = c+"_"+device.getMac();
                        Double num = 0d;
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            num = Objects.nonNull(o)?Double.valueOf(o.toString()):0d;
                        }
                        timeValueMap.put(sensorMap.get(sensor),num);
                    }
                    resultList.add(timeValueMap);
                    ks = DateUtils.getDateAddHour(ks,1);
                }
            }
        } else  if("day".equals(type)){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate);
            Map<String,Map<String,Object>> map = resultMap(historyDailyByMacAndTimeSlot,new ArrayList<>());
            Map<Integer,String> pointMap = pointMap(devices);
            Map<String,String> sensorMap = sensorMap(sensorsList);
            for (Device device:devices) {
                Date ks = DateUtils.getDate(startTime,"yyyy-MM-dd");
                Date js = DateUtils.getDateOfDay(DateUtils.getDate(endTime,"yyyy-MM-dd"),1);
                String nameSite = pointMap.get(device.getMonitorPointId());
                String name = device.getName();
                Map<String, Object> timeValueMap = new LinkedHashMap<>();
                Map<String,ArrayList<Double>> listMap = new HashMap<>();
                int numCount = 0;
                while (DateUtils.isTimeBefor(js,ks)){
                    timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",nameSite);
                    timeValueMap.put("name",name);
                    String c = DateUtils.dateToDateString(ks,"yyyy-MM-dd");
                    timeValueMap.put("日期",c);
                    for (String sensor:sensorsList) {
                        String k = c+"_"+device.getMac();
                        Double num = 0d;
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            num = Objects.nonNull(o)?Double.valueOf(o.toString()):0d;
                            if(Objects.nonNull(num)&& !num.equals(0d)){
                                ArrayList<Double> doubleArrayList = Objects.isNull(listMap.get(numCount+"_"+sensorMap.get(sensor)))?new ArrayList<>():listMap.get(numCount+"_"+sensorMap.get(sensor));
                                doubleArrayList.add(num);
                                listMap.put(numCount+"_"+sensorMap.get(sensor),doubleArrayList);
                            }
                        }
                        timeValueMap.put(sensorMap.get(sensor),num);
                    }
                     resultList.add(timeValueMap);
                     ks = DateUtils.getDateOfDay(ks,1);
                }
                timeValueMap = new LinkedHashMap<>();
                timeValueMap.put("监测站点",nameSite);
                timeValueMap.put("name",name);
                timeValueMap.put("日期","累计值");
                for(int z = 0 ;z< sensorsList.size();z++){
                    double rsAvg = 0L;
                    String son = numCount+"_"+sensorMap.get(sensorsList.get(z));
                    if(listMap.containsKey(numCount+"_"+sensorMap.get(sensorsList.get(z)))){
                        if(son.contains("臭氧")){
                            rsAvg = percentile(listMap.get(numCount+"_"+sensorMap.get(sensorsList.get(z))),90d);
                        }else if(son.contains("一氧化碳")){
                            rsAvg = percentile(listMap.get(numCount+"_"+sensorMap.get(sensorsList.get(z))),95d);
                        }else {
                            Double ListAvg = listMap.get(numCount+"_"+sensorMap.get(sensorsList.get(z))).stream().collect(Collectors.averagingDouble(Double::doubleValue));
                            rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                        }
                    }
                    timeValueMap.put(sensorMap.get(sensorsList.get(z)),rsAvg);
                }
                resultList.add(timeValueMap);
                numCount++;
            }
 
        }else {
            QueryWrapper<HistoryMonthly> HistoryMonthlyWrapper = new QueryWrapper<>();
            HistoryMonthlyWrapper.in("mac",devices.stream().map(Device::getMac).collect(Collectors.toList()));
            HistoryMonthlyWrapper.between("time",startTime,endTime);
            List<HistoryMonthly> historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper);
            Map<String,Map<String,Object>> map = resultMap(new ArrayList<>(),historyMonthlyList);
            Map<Integer,String> pointMap = pointMap(devices);
            Map<String,String> sensorMap = sensorMap(sensorsList);
            for (Device device:devices) {
                Date ks = DateUtils.getDate(startTime,"yyyy-MM");
                Date js = DateUtils.addMonths(DateUtils.getDate(endTime,"yyyy-MM"),1);
                while (DateUtils.isTimeBefor(js,ks)){
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId()));
                    timeValueMap.put("name",device.getName());
                    String c = DateUtils.dateToDateString(ks,"yyyy-MM");
                    timeValueMap.put("日期",c);
                    for (String sensor:sensorsList) {
                        String k = c+"_"+device.getMac();
                        Double num = 0d;
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            num = Objects.nonNull(o)?Double.valueOf(o.toString()):0d;
                        }
                        timeValueMap.put(sensorMap.get(sensor),num);
                    }
                    resultList.add(timeValueMap);
                    ks = DateUtils.addMonths(ks,1);
                }
                /*for (String sensor:sensorsList) {
                    Date ks = DateUtils.getDate(startTime,"yyyy-MM");
                    Date js = DateUtils.addMonths(DateUtils.getDate(endTime,"yyyy-MM"),1);
                    Map<String, Object> timeValueMap = new LinkedHashMap<>();//key为time,value为数据的json
                    timeValueMap.put("监测站点",pointMap.get(device.getMonitorPointId()));
                    timeValueMap.put("name",device.getName());
                    timeValueMap.put("sensor",sensorMap.get(sensor));
                    while (DateUtils.isTimeBefor(js,ks)){
                        String c = DateUtils.dateToDateString(ks,"yyyy-MM");
                        String k = c+"_"+device.getMac();
                        if(map.containsKey(k)){
                            Object o = map.get(k).get(sensor);
                            timeValueMap.put(c,Objects.nonNull(o)?Double.valueOf(o.toString()):0);
                        }else {
                            timeValueMap.put(c,0);
                        }
                        ks = DateUtils.addMonths(ks,1);
                    }
                    resultList.add(timeValueMap);
                }*/
            }
        }
        return resultList;
    }
    @Override
    public List<MonitoringStationDTO> listMonitoringStationDTO(Map<String, Object> params, int reportType, String startTime, String endTime) {
       // List<String> macs = Arrays.asList(mac.split(","));
        List<String> macs = (List<String>) params.remove("mac");
        List<MonitoringStationDTO> resultList = new ArrayList<>();
 
        if(reportType == 1||reportType == 4){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN);
            List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate);
            resultList = resultList(historyDailyByMacAndTimeSlot);
        }else if(reportType == 2){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN);
            Date endDate = DateUtils.addDays(startDate,6);
            List<HistoryDaily> historyDailyByMacAndTimeSlot = historyDailyService.getHistoryDailyByMacAndTimeSlot(macs, startDate, endDate);
            resultList = resultList(historyDailyByMacAndTimeSlot);
        }else if(reportType == 3){
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_EN);
            QueryWrapper<HistoryMonthly> HistoryMonthlyWrapper = new QueryWrapper<>();
            HistoryMonthlyWrapper.in("mac",macs);
            HistoryMonthlyWrapper.between("time",startDate,endDate);
            List<HistoryMonthly> historyMonthlyList = historyMonthlyMapper.selectList(HistoryMonthlyWrapper);
            List<HistoryDaily> historyDailies = new ArrayList<>();
            for(HistoryMonthly h : historyMonthlyList) {
                HistoryDaily historyDaily = new HistoryDaily();
                historyDaily.setMac(h.getMac());
                historyDaily.setTime(h.getTime());
                historyDaily.setValue(h.getValue());
                historyDailies.add(historyDaily);
            }
            resultList = resultList(historyDailies);
        }
        return resultList;
    }
 
    /**
     * 根据用户查询站点
     *
     * @return
     */
    @Override
    public List<MonitorPoint> getOrganizations() {
        QueryWrapper<MonitorPoint> wrapper = new QueryWrapper<>();
        wrapper.select("id","name");
        QxUser user = UserHelper.getCurrentUser();
        Integer organizationId = user.getOrganizationId();
        List<Integer> monitorPointiIds = monitorPointMapper.getMonitorPointiId(organizationId);
        if (organizationId!=24){
            wrapper.in("id",monitorPointiIds);
        }
        wrapper.eq("is_delete",Constants.NOT_DELETE);
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectList(wrapper);
        for (int i = 0; i < monitorPoints.size(); i++) {
            List<Device> organizationDevice = deviceService.getOrganizationDevice(monitorPoints.get(i).getId());
            if (ObjectUtils.isEmpty(organizationDevice)){
                monitorPoints.remove(i);
                i--;
            }
        }
        return monitorPoints;
    }
 
 
    private List<MonitoringStationDTO> resultList(List<HistoryDaily> historyDailyByMacAndTimeSlot){
        List<MonitoringStationDTO> list = new ArrayList<>();
        if(CollectionUtils.isEmpty(historyDailyByMacAndTimeSlot)){
            return list;
        }
        //海城分组均值  海州 p5dnd7a0000001 -响堂 p5dnd7a0000002 -兴海 p5dnd7a0000003
        List<String> hzRoad = Arrays.asList("p5dnd7a0245491","p5dnd7a0392159","p5dnd7a0243605","p5dnd7a0392176");
        List<String> xtRoad = Arrays.asList("p5dnd7a0243603","p5dnd7a0391986","p5dnd7a0392003","p5dnd7a0392144","p5dnd7a0392167","p5dnd7a0391990");
        List<String> xhRoad = Arrays.asList("p5dnd7a0392158","p5dnd7a0392173","p5dnd7a0391975","p5dnd7a0392153","p5dnd7a0391961","p5dnd7a0243608","p5dnd7a0243618");
        List<HistoryDaily> hcHz= new ArrayList<>();
        List<HistoryDaily> xtHz= new ArrayList<>();
        List<HistoryDaily> xhHz= new ArrayList<>();
        for(HistoryDaily h : historyDailyByMacAndTimeSlot) {
            HistoryDaily historyDaily = new HistoryDaily();
            historyDaily.setTime( h.getTime());
            historyDaily.setValue(h.getValue());
            if(hzRoad.contains(h.getMac())){
                historyDaily.setMac("p5dnd7a0000001");
                hcHz.add(historyDaily);
            }else if(xtRoad.contains(h.getMac())){
                historyDaily.setMac("p5dnd7a0000002");
                xtHz.add(historyDaily);
            }else if(xhRoad.contains(h.getMac())){
                historyDaily.setMac("p5dnd7a0000003");
                xhHz.add(historyDaily);
            }
        }
        if(CollectionUtils.isNotEmpty(hcHz)){
            historyDailyByMacAndTimeSlot.addAll(hcHz);
        }
        if(CollectionUtils.isNotEmpty(xtHz)){
            historyDailyByMacAndTimeSlot.addAll(xtHz);
        }
        if(CollectionUtils.isNotEmpty(xhHz)){
            historyDailyByMacAndTimeSlot.addAll(xhHz);
        }
        Map<String,Map<String,ArrayList<Double>>> resultMap = new HashMap<>();
        for(HistoryDaily h : historyDailyByMacAndTimeSlot) {
            JSONObject jsonObject = JSONObject.parseObject(h.getValue());
            Map<String, Object> map = jsonObject.getInnerMap();
            Map<String,ArrayList<Double>> maps = Objects.nonNull(resultMap.get(h.getMac()))?resultMap.get(h.getMac()):new HashMap<>();
            if(Objects.nonNull(map.get("a34004"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("PM2_5"))?maps.get("PM2_5"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a34004").toString()));
                maps.put("PM2_5",numResult);
            }
            if(Objects.nonNull(map.get("a05024"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("O3"))?maps.get("O3"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a05024").toString()));
                maps.put("O3",numResult);
            }
            if(Objects.nonNull(map.get("a34002"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("PM10"))?maps.get("PM10"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a34002").toString()));
                maps.put("PM10",numResult);
            }
            if(Objects.nonNull(map.get("a21026"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("SO2"))?maps.get("SO2"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a21026").toString()));
                maps.put("SO2",numResult);
            }
            if(Objects.nonNull(map.get("a21004"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("NO2"))?maps.get("NO2"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a21004").toString()));
                maps.put("NO2",numResult);
            }
            if(Objects.nonNull(map.get("a21005"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("CO"))?maps.get("CO"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a21005").toString()));
                maps.put("CO",numResult);
            }
            if(Objects.nonNull(map.get("a99054"))){
                ArrayList<Double> numResult = Objects.nonNull(maps.get("TVOC"))?maps.get("TVOC"):new ArrayList<>();
                numResult.add(Double.parseDouble(map.get("a99054").toString()));
                maps.put("TVOC",numResult);
            }
            resultMap.put(h.getMac(),maps);
        }
        for(Map.Entry entry : resultMap.entrySet()){
            boolean dimension = false;
            String mapKey = (String) entry.getKey();
            Map<String,ArrayList<Double>> mapValue = (Map<String,ArrayList<Double>>)entry.getValue();
            Map<String, Object> data = new HashMap<>();
            if(mapValue.containsKey("PM2_5")){
                Double ListAvg = mapValue.get("PM2_5").stream().collect(Collectors.averagingDouble(Double::doubleValue));
                double rsAvg = new BigDecimal(ListAvg).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("PM2_5",rsAvg);
            }else {
                data.put("PM2_5",0);
                dimension = true;
            }
            if(mapValue.containsKey("PM10")){
                Double ListAvg = mapValue.get("PM10").stream().collect(Collectors.averagingDouble(Double::doubleValue));
                double rsAvg = new BigDecimal(ListAvg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("PM10",rsAvg);
            }else {
                data.put("PM10",0);
                dimension = true;
            }
            if(mapValue.containsKey("SO2")){
                Double ListAvg = mapValue.get("SO2").stream().collect(Collectors.averagingDouble(Double::doubleValue));
                double rsAvg = new BigDecimal(ListAvg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("SO2",rsAvg);
            }else {
                data.put("SO2",0);
                dimension = true;
            }
            if(mapValue.containsKey("NO2")){
                Double ListAvg = mapValue.get("NO2").stream().collect(Collectors.averagingDouble(Double::doubleValue));
                double rsAvg = new BigDecimal(ListAvg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("NO2",rsAvg);
            }else {
                data.put("NO2",0);
                dimension = true;
            }
            if(mapValue.containsKey("CO")){
                Double ListAvg = percentile(mapValue.get("CO"),95d);
                double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("CO",rsAvg);
            }else {
                data.put("CO",0);
                dimension = true;
            }
            if(mapValue.containsKey("O3")){
                Double ListAvg = percentile(mapValue.get("O3"),90d);
                double rsAvg = new BigDecimal(ListAvg).setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("O3",rsAvg);
            }else {
                data.put("O3",0);
                dimension = true;
            }
            if(mapValue.containsKey("TVOC")){
                Double ListAvg = mapValue.get("TVOC").stream().collect(Collectors.averagingDouble(Double::doubleValue));
                double rsAvg = new BigDecimal(ListAvg).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.put("TVOC",rsAvg);
            }else {
                data.put("TVOC",0);
                dimension = true;
            }
            Double resultNum = ComprehensiveIndexUtils.dailyData(data);
            if(dimension){
                resultNum = 0d;
            }
            MonitoringStationDTO monitoringStationDTO = new MonitoringStationDTO();
            monitoringStationDTO.setPM25(BigDecimal.valueOf(Double.parseDouble(data.get("PM2_5").toString())));
            monitoringStationDTO.setO3(BigDecimal.valueOf(Double.parseDouble(data.get("O3").toString())));
            monitoringStationDTO.setTovc(BigDecimal.valueOf(Double.parseDouble(data.get("TVOC").toString())));
            monitoringStationDTO.setComposite(BigDecimal.valueOf(resultNum));
            monitoringStationDTO.setSO2(BigDecimal.valueOf(Double.parseDouble(data.get("SO2").toString())));
            monitoringStationDTO.setNO2(BigDecimal.valueOf(Double.parseDouble(data.get("NO2").toString())));
            monitoringStationDTO.setPM10(BigDecimal.valueOf(Double.parseDouble(data.get("PM10").toString())));
            monitoringStationDTO.setCO(BigDecimal.valueOf(Double.parseDouble(data.get("CO").toString())));
            monitoringStationDTO.setMac(mapKey);
            list.add(monitoringStationDTO);
        }
        List<MonitoringStationDTO> compositeListResult = fun3(list,0);
        List<MonitoringStationDTO> compositeList = fun3(list,1);
        List<MonitoringStationDTO> pM25List = fun3(list,2);
        List<MonitoringStationDTO> o3List = fun3(list,3);
        List<MonitoringStationDTO> tovcList = fun3(list,4);
        List<MonitoringStationDTO> SO2List = fun3(list,5);
        List<MonitoringStationDTO> NO2cList = fun3(list,6);
        List<MonitoringStationDTO> PM10List = fun3(list,7);
        List<MonitoringStationDTO> COList = fun3(list,8);
        List<MonitoringStationDTO> resultList = new ArrayList<>();
        LambdaQueryChainWrapper<Device> wrapper = deviceService.lambdaQuery();
        wrapper.eq(Device::getIsDelete,0);
        Map<String,String> deviceMap =  new HashMap<>();
        wrapper.list().forEach(it->deviceMap.put(it.getMac(),it.getName()));
        deviceMap.put("p5dnd7a0000001","海州街道均值");
        deviceMap.put("p5dnd7a0000002","响堂街道均值");
        deviceMap.put("p5dnd7a0000003","兴海街道均值");
        for (MonitoringStationDTO m :compositeListResult ){
            MonitoringStationDTO monit = new MonitoringStationDTO();
            String mac = m.getMac();
            monit.setMac(mac);
            monit.setName(deviceMap.get(mac));
            List<String> listComposite = compositeList.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            monit.setComposite(listComposite.indexOf(mac) == -1?BigDecimal.ZERO:compositeList.get(listComposite.indexOf(mac)).getComposite());
            monit.setCompositeNum(listComposite.indexOf(mac) == -1?0:compositeList.get(listComposite.indexOf(mac)).getCompositeNum());
            List<String> listPM25 = pM25List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            List<String> listO3 = o3List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            List<String> listTovc = tovcList.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            monit.setO3(listO3.indexOf(mac) == -1?BigDecimal.ZERO:o3List.get(listO3.indexOf(mac)).getO3());
            monit.setO3Num(listO3.indexOf(mac) == -1?0:o3List.get(listO3.indexOf(mac)).getO3Num());
            monit.setPM25(listPM25.indexOf(mac) == -1?BigDecimal.ZERO:pM25List.get(listPM25.indexOf(mac)).getPM25());
            monit.setPM25Num(listPM25.indexOf(mac) == -1?0:pM25List.get(listPM25.indexOf(mac)).getPM25Num());
            monit.setTovc(listTovc.indexOf(mac) == -1?BigDecimal.ZERO:tovcList.get(listTovc.indexOf(mac)).getTovc());
            monit.setTOVCNum(listTovc.indexOf(mac) == -1?0:tovcList.get(listTovc.indexOf(mac)).getTOVCNum());
            List<String> listSO2 = SO2List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            monit.setSO2(listSO2.indexOf(mac) == -1?BigDecimal.ZERO:SO2List.get(listSO2.indexOf(mac)).getSO2());
            monit.setSO2Num(listSO2.indexOf(mac) == -1?0:SO2List.get(listSO2.indexOf(mac)).getSO2Num());
            List<String> listNO2 = NO2cList.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            monit.setNO2(listNO2.indexOf(mac) == -1?BigDecimal.ZERO:NO2cList.get(listNO2.indexOf(mac)).getNO2());
            monit.setNO2Num(listNO2.indexOf(mac) == -1?0:NO2cList.get(listNO2.indexOf(mac)).getNO2Num());
            List<String> listPM10 = PM10List.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            monit.setPM10(listPM10.indexOf(mac) == -1?BigDecimal.ZERO:PM10List.get(listPM10.indexOf(mac)).getPM10());
            monit.setPM10Num(listPM10.indexOf(mac) == -1?0:PM10List.get(listPM10.indexOf(mac)).getPM10Num());
            List<String> listCO = COList.stream().map(MonitoringStationDTO::getMac).collect(Collectors.toList());
            monit.setCO(listCO.indexOf(mac) == -1?BigDecimal.ZERO:COList.get(listCO.indexOf(mac)).getCO());
            monit.setCONum(listCO.indexOf(mac) == -1?0:COList.get(listCO.indexOf(mac)).getCONum());
 
            resultList.add(monit);
        }
        return resultList;
    }
 
    public List<MonitoringStationDTO> fun3(List<MonitoringStationDTO> monList,int type) {
        List<MonitoringStationDTO> resultList = new ArrayList<>();
        List<MonitoringStationDTO> compositeList = new ArrayList<>();
        Map<BigDecimal,List<MonitoringStationDTO>> map1 = new HashMap<>();
        if(type == 1){
            compositeList = monList.stream().filter(it->it.getComposite().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getComposite)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getComposite,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 0){
            compositeList = monList.stream().sorted(Comparator.comparing(MonitoringStationDTO::getComposite)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getComposite,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 2){
            compositeList = monList.stream().filter(it->it.getPM25().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getPM25)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getPM25,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 3){
            compositeList = monList.stream().filter(it->it.getO3().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getO3)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getO3,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 4){
            compositeList = monList.stream().filter(it->it.getTovc().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getTovc)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getTovc,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 5){
            compositeList = monList.stream().filter(it->it.getSO2().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getSO2)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getSO2,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 6){
            compositeList = monList.stream().filter(it->it.getNO2().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getNO2)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getNO2,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 7){
            compositeList = monList.stream().filter(it->it.getPM10().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getPM10)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getPM10,LinkedHashMap::new,Collectors.toList()));
        }else if(type == 8){
            compositeList = monList.stream().filter(it->it.getCO().compareTo(BigDecimal.ZERO)!=0).sorted(Comparator.comparing(MonitoringStationDTO::getCO)).collect(Collectors.toList());
            map1 = compositeList.stream().collect(Collectors.groupingBy(MonitoringStationDTO::getCO,LinkedHashMap::new,Collectors.toList()));
        }
        int index = 1;
        for (Map.Entry entry : map1.entrySet()) {
            List<MonitoringStationDTO> mapValue = (List<MonitoringStationDTO>)entry.getValue();
            for(MonitoringStationDTO m : mapValue){
                MonitoringStationDTO stationDTO = new MonitoringStationDTO();
                stationDTO.setMac(m.getMac());
                if(type == 1){
                    stationDTO.setComposite(m.getComposite());
                    stationDTO.setCompositeNum(index);
                }else if(type == 0){
                    stationDTO.setComposite(m.getComposite());
                    stationDTO.setCompositeNum(index);
                }else if(type == 2){
                    stationDTO.setPM25(m.getPM25());
                    stationDTO.setPM25Num(index);
                }else if(type == 3){
                    stationDTO.setO3(m.getO3());
                    stationDTO.setO3Num(index);
                }else if(type == 4){
                    stationDTO.setTovc(m.getTovc());
                    stationDTO.setTOVCNum(index);
                }else if(type == 5){
                    stationDTO.setSO2(m.getSO2());
                    stationDTO.setSO2Num(index);
                }else if(type == 6){
                    stationDTO.setNO2(m.getNO2());
                    stationDTO.setNO2Num(index);
                }else if(type == 7){
                    stationDTO.setPM10(m.getPM10());
                    stationDTO.setPM10Num(index);
                }else if(type == 8){
                    stationDTO.setCO(m.getCO());
                    stationDTO.setCONum(index);
                }
                resultList.add(stationDTO);
            }
            index = index +mapValue.size();
        }
        return resultList;
    }
 
    private Map<String,Map<String,Object>> resultMap(List<HistoryDaily> list , List<HistoryMonthly> historyMonthlyList){
        Map<String,Map<String,Object>> map = new HashMap<>();
        for(HistoryDaily h : list){
            String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM-dd")+"_"+h.getMac();
            Map<String,Object> jsonMap = new HashMap<>();
            JSONObject jsonObject = JSONObject.parseObject(h.getValue());
            jsonMap = jsonObject.getInnerMap();
            map.put(dateStr,jsonMap);
        }
        for(HistoryMonthly h : historyMonthlyList){
            String dateStr = DateUtils.dateToDateString( h.getTime(), "yyyy-MM")+"_"+h.getMac();
            Map<String,Object> jsonMap = new HashMap<>();
            JSONObject jsonObject = JSONObject.parseObject(h.getValue());
            jsonMap = jsonObject.getInnerMap();
            map.put(dateStr,jsonMap);
        }
 
        return map;
    }
    private Map<String,Map<String,Object>> resultMap(List<HistoryHourly> list){
        Map<String,Map<String,Object>> map = new HashMap<>();
        for(HistoryHourly h : list){
            String dateStr = DateUtils.dateToDateString( h.getTime(), DateUtils.yyyy_MM_dd_HH_EN)+"_"+h.getMac();
            Map<String,Object> jsonMap = new HashMap<>();
            JSONObject jsonObject = JSONObject.parseObject(h.getValue());
            jsonMap = jsonObject.getInnerMap();
            map.put(dateStr,jsonMap);
        }
 
        return map;
    }
    private Map<String,String> sensorMap(List<String> sensorsList){
        Map<String,String> map = new HashMap<>();
        QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
        sensorQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        sensorQueryWrapper.in("code",sensorsList);
         sensorMapper.selectList(sensorQueryWrapper).forEach(it->map.put(it.getCode(),it.getName()));
        return map;
    }
 
    private Map<Integer,String> pointMap(List<Device> devices){
        Map<Integer,String> map = new HashMap<>();
        QueryWrapper<MonitorPoint> monitorPointQueryWrapper = new QueryWrapper<>();
        monitorPointQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        monitorPointQueryWrapper.in("id",devices.stream().map(Device::getMonitorPointId).collect(Collectors.toList()));
        monitorPointMapper.selectList(monitorPointQueryWrapper).forEach(it->map.put(it.getId(),it.getName()));
        return map;
    }
    private Double percentile(List<Double> date,double num){
        if(CollectionUtils.isNotEmpty(date)&&date.size() == 1){
            return date.get(0);
        }
        Collections.sort(date);
        double position = (num / 100) * (date.size() - 1);
        int index = (int) position;
        // 获取小数部分的位置索引
        double fraction = position - index;
 
        // 获取百分位值
        double percentileValue = date.get(index) + fraction * (date.get(index + 1) - date.get(index));
        return new BigDecimal(percentileValue).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
}