whyclxw
6 天以前 138dead9b8a35d843a74a799e2cfbe1b12b51189
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
package com.whyc.service;
 
import com.whyc.constant.BattSingalIdEnum;
import com.whyc.constant.BattCapperformanceEnum;
import com.whyc.constant.DevAlarmEnum;
import com.whyc.constant.PowerAlarmEnum;
import com.whyc.dto.*;
import com.whyc.factory.BattCapFactory;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.db_batt_testdata.BatttestdataId;
import com.whyc.pojo.db_batt_testdata.BatttestdataInf;
import com.whyc.pojo.db_param.AppParam;
import com.whyc.pojo.db_station.BattInf;
import com.whyc.pojo.db_station.PowerInf;
import com.whyc.pojo.db_station.StationInf;
import com.whyc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
 
import java.time.LocalDateTime;
import java.util.List;
 
import static com.whyc.util.DateUtil.convertToLocalDateTime;
 
 
@Service
public class LeaderHomeService {
 
    @Autowired
    private PowerInfService  powerInfService;
 
    @Autowired
    private BattInfService battInfService;
 
    @Autowired
    private StationInfService stationInfService;
 
    @Autowired
    private BatttestdataInfService battTestDataInfService;
 
    @Autowired
    private PwrdevAlarmService powerAlarmService;
 
    @Autowired
    private DevalarmDataService devAlarmService;
 
    @Autowired
    private BattalarmDataService battAlarmService;
 
    @Autowired
    private AppParamService appParamService;
 
    @Autowired
    private BatttestdataIdService battTestDataIdService;
 
 
    public Response getAll(Integer userId) {
        Response response = new Response();
        Map<String,Object> map = new HashMap<>();
 
        CountDownLatch latch = new CountDownLatch(6);
        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
 
        //站点信息统计,
        poolExecutor.execute(() -> {
            stationInfoStatistics(userId, map);
            latch.countDown();
        });
 
        //电源信息(品牌及电压)统计
        poolExecutor.execute(() -> {
            powerInfoStatistics(userId, map);
            latch.countDown();
        });
        //蓄电池信息(品牌及电压)统计
        poolExecutor.execute(() -> {
            batteryInfoStatistics(userId, map);
            latch.countDown();
        });
        //测试数据信息统计
        poolExecutor.execute(() -> {
            testDataInfoStatistics(userId, map);
            latch.countDown();
        });
        //10类基础资产信息统计
        poolExecutor.execute(() -> {
            deviceInfoStatistics(userId, map);
            latch.countDown();
        });
 
        /**
         * 实时告警信息统计
         * 逻辑更新:
         * 电池里面的单体(单体1,单体2..)单项,可以合并. 电源里面的整流器(整流器1,整流器2..)告警单项可以合并
         * 合并后时间采用最先产生报警的那个时间
         */
        poolExecutor.execute(() -> {
            alarmRealtimeStatistics(userId, map);
            latch.countDown();
        });
 
        return response;
    }
 
    private void deviceInfoStatistics(Integer userId, Map<String, Object> map) {
        NameValueIntDto baseDto = new NameValueIntDto();
        int powerCount = powerInfService.getListByUserId(userId).size();
        map.put("powerCount", powerCount);
        List<BattInf> battInfList = battInfService.getListByUserId(userId);
        int battCount = battInfList.size();
        Map<Integer, List<BattInf>> devIdMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getDevId));
        map.put("devCount", devIdMap.size());
        map.put("battCount",battCount);
        int monCountTotal = 0;
        for (int i = 0; i < battCount; i++) {
            int monCount = battInfList.get(i).getMoncount().intValue();
            monCountTotal += monCount;
        }
        map.put("monCount", monCountTotal);
 
        LocalDateTime now = LocalDateTime.now();
        //本年第一天
        LocalDateTime yearStart = now.withDayOfYear(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        Date yearStartTime = DateUtil.convertToDate(yearStart);
        //去年的第一天
        LocalDateTime lastYearStart = now.minusYears(1).withDayOfYear(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        //去年的最后一天
        int lastYear = now.minusYears(1).getYear();
        LocalDate lastDayOfLastYear = LocalDate.of(lastYear, 12, 31);
        LocalDateTime lastYearEnd = lastDayOfLastYear.atTime(LocalTime.MAX);
        Date lastYearStartTime = DateUtil.convertToDate(lastYearStart);
        Date lastYearEndTime = DateUtil.convertToDate(lastYearEnd);
 
        //本年已放电电池数量
        //本年未放电电池数量
        //去年已放电电池数量
        //去年未放电电池数量
        int hrDisBattCount = battTestDataInfService.getHrDisCount(userId,yearStartTime,null);
        int hrNoDisBattCount = battCount - hrDisBattCount;
        int hrDisBattCountLastYear = battTestDataInfService.getHrDisCount(userId,lastYearStartTime,lastYearEndTime);
        int hrNoDisBattCountLastYear = battCount - hrDisBattCountLastYear;
        map.put("hrDisBattCount", hrDisBattCount);
        map.put("hrNoDisBattCount", hrNoDisBattCount);
        map.put("hrDisBattCountLastYear", hrDisBattCountLastYear);
        map.put("hrNoDisBattCountLastYear", hrNoDisBattCountLastYear);
 
        //本年优良劣化损坏数量
        List<BattCapInfoDto> battCapInfoList = getBattCapInfo(battInfList,yearStartTime,null);
        long battGoodCount = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_1.getStateName())).count();
        long battBadCount = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName())).count();
        long battDamageCount = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName())).count();
        //去年优良劣化损坏数量
        List<BattCapInfoDto> battCapInfoListLastYear = getBattCapInfo(battInfList,lastYearStartTime,lastYearEndTime);
        long battGoodCountLastYear = battCapInfoListLastYear.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_1.getStateName())).count();
        long battBadCountLastYear = battCapInfoListLastYear.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName())).count();
        long battDamageCountLastYear = battCapInfoListLastYear.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName())).count();
        map.put("battGoodCount", battGoodCount);
        map.put("battBadCount", battBadCount);
        map.put("battDamageCount", battDamageCount);
        map.put("battGoodCountLastYear", battGoodCountLastYear);
        map.put("battBadCountLastYear", battBadCountLastYear);
        map.put("battDamageCountLastYear", battDamageCountLastYear);
        //TODO 优良电源数量
        map.put("powerGoodCount","优良判定标准待确定" );
    }
 
 
    private void alarmRealtimeStatistics(Integer userId, Map<String, Object> map) {
        //电池告警信息统计
        List<AlarmDto> battAlarmListNew = new ArrayList<>();
        List<AlarmDto> battAlarmList = battAlarmService.getListByUserId(userId, null);
        //合并单体告警的告警信息,对AlarmDto中AlmId 在17~28之间的进行合并.
        Map<String, List<AlarmDto>> mergeMap = battAlarmList.stream()
                .filter(alarm -> alarm.getAlmId() >= 17 && alarm.getAlmId() <= 28)
                .collect(Collectors.groupingBy(temp -> temp.getBattGroupId()+"_"+temp.getAlmId()));
        mergeMap.forEach((almId, list) -> {
            //对list内的字段 单体编号进行合并. 时间取最小的
            List<AlarmDto> mergeList = list.stream()
                    .sorted(Comparator.comparing(AlarmDto::getAlmStartTime))
                    .collect(Collectors.toList());
            AlarmDto alarmDto = mergeList.get(0);
            //按单体编号排序
            mergeList.sort(Comparator.comparing(AlarmDto::getMonNum));
            alarmDto.setMonNumStr(mergeList.stream()
                    .map(AlarmDto::getMonNum)
                    .map(String::valueOf)
                    .collect(Collectors.joining(",")));
            alarmDto.setMonNum(null);
            battAlarmListNew.add(alarmDto);
        });
 
        List<AlarmDto> battAlarmListOther = battAlarmList.stream().filter(alarm -> alarm.getAlmId() < 17).collect(Collectors.toList());
        battAlarmListNew.addAll(battAlarmListOther);
        //对告警名称赋值
        battAlarmListNew.forEach(alarmDto -> {
            alarmDto.setAlmName(BattSingalIdEnum.getValue(alarmDto.getAlmId()));
        });
 
        //设备告警信息统计
        List<AlarmDto> devAlarmList = devAlarmService.getListByUserId(userId,null);
        //对告警名称赋值
        devAlarmList.forEach(alarmDto -> {
            alarmDto.setAlmName(DevAlarmEnum.getValue(alarmDto.getAlmId()));
        });
 
        //电源告警信息统计
        List<AlarmDto> powerAlarmListNew = new ArrayList<>();
        List<AlarmDto> powerAlarmList = powerAlarmService.getListByUserId(userId,null);
        //整流器总故障合并
        //ALARM_3300001(3300001,"整流器1总故障告警"),
        //    ALARM_3300002(3300002,"整流器2总故障告警"),
        //    ALARM_3300003(3300003,"整流器3总故障告警"),
        //    ALARM_3300004(3300004,"整流器4总故障告警"),
        //    ALARM_3300005(3300005,"整流器5总故障告警"),
        //    ALARM_3300006(3300006,"整流器6总故障告警"),
        //    ALARM_3300007(3300007,"整流器7总故障告警"),
        //    ALARM_3300008(3300008,"整流器8总故障告警"),
        //    ALARM_3300009(3300009,"整流器9总故障告警"),
        //    ALARM_3300010(3300010,"整流器10总故障告警"),
        //    ALARM_3300011(3300011,"整流器11总故障告警"),
        //    ALARM_3300012(3300012,"整流器12总故障告警"),
        //    ALARM_3300013(3300013,"整流器13总故障告警"),
        //    ALARM_3300014(3300014,"整流器14总故障告警"),
        //    ALARM_3300015(3300015,"整流器15总故障告警"),
        //    ALARM_3300016(3300016,"整流器16总故障告警"),
        Map<Integer, List<AlarmDto>> powerMergeMap = powerAlarmList.stream()
                .filter(alarm -> alarm.getAlmId() >= 3300001 && alarm.getAlmId() <= 3300016)
                .collect(Collectors.groupingBy(AlarmDto::getPowerId));
        powerMergeMap.forEach((powerId, list) -> {
            //对list内的字段 整流器编号进行合并,合并到rectifierName. 时间取最小的
            List<AlarmDto> mergeList = list.stream()
                    .sorted(Comparator.comparing(AlarmDto::getAlmStartTime))
                    .collect(Collectors.toList());
            AlarmDto alarmDto = mergeList.get(0);
            //按告警id排序
            mergeList.sort(Comparator.comparing(AlarmDto::getAlmId));
            StringBuilder rectifierNameSb = new StringBuilder();
            rectifierNameSb.append("整流器");
            for (int i = 0; i < list.size(); i++) {
                int almId = list.get(i).getAlmId();
                switch (almId){
                    case 3300001:
                        rectifierNameSb.append("1,");
                        break;
                    case 3300002:
                        rectifierNameSb.append("2,");
                        break;
                    case 3300003:
                        rectifierNameSb.append("3,");
                        break;
                    case 3300004:
                        rectifierNameSb.append("4,");
                        break;
                    case 3300005:
                        rectifierNameSb.append("5,");
                        break;
                    case 3300006:
                        rectifierNameSb.append("6,");
                        break;
                    case 3300007:
                        rectifierNameSb.append("7,");
                        break;
                    case 3300008:
                        rectifierNameSb.append("8,");
                        break;
                    case 3300009:
                        rectifierNameSb.append("9,");
                        break;
                    case 3300010:
                        rectifierNameSb.append("10,");
                        break;
                    case 3300011:
                        rectifierNameSb.append("11,");
                        break;
                    case 3300012:
                        rectifierNameSb.append("12,");
                        break;
                    case 3300013:
                        rectifierNameSb.append("13,");
                        break;
                    case 3300014:
                        rectifierNameSb.append("14,");
                        break;
                    case 3300015:
                        rectifierNameSb.append("15,");
                        break;
                    case 3300016:
                        rectifierNameSb.append("16,");
                        break;
 
                }
            }
            //最后一个,去掉
            rectifierNameSb.deleteCharAt(rectifierNameSb.length()-1);
            rectifierNameSb.append("总故障告警");
            alarmDto.setRectifierName(rectifierNameSb.toString());
            powerAlarmListNew.add(alarmDto);
        });
 
        //整流器过压合并
        //ALARM_3300017(3300017,"整流器1过压告警"),
        //    ALARM_3300018(3300018,"整流器2过压告警"),
        //    ALARM_3300019(3300019,"整流器3过压告警"),
        //    ALARM_3300020(3300020,"整流器4过压告警"),
        //    ALARM_3300021(3300021,"整流器5过压告警"),
        //    ALARM_3300022(3300022,"整流器6过压告警"),
        //    ALARM_3300023(3300023,"整流器7过压告警"),
        //    ALARM_3300024(3300024,"整流器8过压告警"),
        //    ALARM_3300025(3300025,"整流器9过压告警"),
        //    ALARM_3300026(3300026,"整流器10过压告警"),
        //    ALARM_3300027(3300027,"整流器11过压告警"),
        //    ALARM_3300028(3300028,"整流器12过压告警"),
        //    ALARM_3300029(3300029,"整流器13过压告警"),
        //    ALARM_3300030(3300030,"整流器14过压告警"),
        //    ALARM_3300031(3300031,"整流器15过压告警"),
        //    ALARM_3300032(3300032,"整流器16过压告警"),
        Map<Integer, List<AlarmDto>> powerOverVolMergeMap = powerAlarmList.stream()
                .filter(alarm -> alarm.getAlmId() >= 3300017 && alarm.getAlmId() <= 3300032)
                .collect(Collectors.groupingBy(AlarmDto::getPowerId));
        powerOverVolMergeMap.forEach((powerId, list) -> {
            //对list内的字段 整流器编号进行合并,合并到rectifierName. 时间取最小的
            List<AlarmDto> mergeList = list.stream()
                    .sorted(Comparator.comparing(AlarmDto::getAlmStartTime))
                    .collect(Collectors.toList());
            AlarmDto alarmDto = mergeList.get(0);
            //按告警id排序
            mergeList.sort(Comparator.comparing(AlarmDto::getAlmId));
            StringBuilder rectifierNameSb = new StringBuilder();
            rectifierNameSb.append("整流器");
            for (int i = 0; i < list.size(); i++) {
                int almId = list.get(i).getAlmId();
                switch (almId){
                    case 3300017:
                        rectifierNameSb.append("1,");
                        break;
                    case 3300018:
                        rectifierNameSb.append("2,");
                        break;
                    case 3300019:
                        rectifierNameSb.append("3,");
                        break;
                    case 3300020:
                        rectifierNameSb.append("4,");
                        break;
                    case 3300021:
                        rectifierNameSb.append("5,");
                        break;
                    case 3300022:
                        rectifierNameSb.append("6,");
                        break;
                    case 3300023:
                        rectifierNameSb.append("7,");
                        break;
                    case 3300024:
                        rectifierNameSb.append("8,");
                        break;
                    case 3300025:
                        rectifierNameSb.append("9,");
                        break;
                    case 3300026:
                        rectifierNameSb.append("10,");
                        break;
                    case 3300027:
                        rectifierNameSb.append("11,");
                        break;
                    case 3300028:
                        rectifierNameSb.append("12,");
                        break;
                    case 3300029:
                        rectifierNameSb.append("13,");
                        break;
                    case 3300030:
                        rectifierNameSb.append("14,");
                        break;
                    case 3300031:
                        rectifierNameSb.append("15,");
                        break;
                    case 3300032:
                        rectifierNameSb.append("16,");
                        break;
                }
            }
            //最后一个,去掉
            rectifierNameSb.deleteCharAt(rectifierNameSb.length()-1);
            rectifierNameSb.append("过压告警");
            alarmDto.setRectifierName(rectifierNameSb.toString());
            powerAlarmListNew.add(alarmDto);
        });
        //整流器欠压合并
        //ALARM_3300033(3300033,"整流器1欠压告警"),
        //    ALARM_3300034(3300034,"整流器2欠压告警"),
        //    ALARM_3300035(3300035,"整流器3欠压告警"),
        //    ALARM_3300036(3300036,"整流器4欠压告警"),
        //    ALARM_3300037(3300037,"整流器5欠压告警"),
        //    ALARM_3300038(3300038,"整流器6欠压告警"),
        //    ALARM_3300039(3300039,"整流器7欠压告警"),
        //    ALARM_3300040(3300040,"整流器8欠压告警"),
        //    ALARM_3300041(3300041,"整流器9欠压告警"),
        //    ALARM_3300042(3300042,"整流器10欠压告警"),
        //    ALARM_3300043(3300043,"整流器11欠压告警"),
        //    ALARM_3300044(3300044,"整流器12欠压告警"),
        //    ALARM_3300045(3300045,"整流器13欠压告警"),
        //    ALARM_3300046(3300046,"整流器14欠压告警"),
        //    ALARM_3300047(3300047,"整流器15欠压告警"),
        //    ALARM_3300048(3300048,"整流器16欠压告警"),
        Map<Integer, List<AlarmDto>> powerBellowVolMergeMap = powerAlarmList.stream()
                .filter(alarm -> alarm.getAlmId() >= 3300033 && alarm.getAlmId() <= 3300048)
                .collect(Collectors.groupingBy(AlarmDto::getPowerId));
        powerBellowVolMergeMap.forEach((powerId, list) -> {
            //对list内的字段 整流器编号进行合并,合并到rectifierName. 时间取最小的
            List<AlarmDto> mergeList = list.stream()
                    .sorted(Comparator.comparing(AlarmDto::getAlmStartTime))
                    .collect(Collectors.toList());
            AlarmDto alarmDto = mergeList.get(0);
            //按告警id排序
            mergeList.sort(Comparator.comparing(AlarmDto::getAlmId));
            StringBuilder rectifierNameSb = new StringBuilder();
            rectifierNameSb.append("整流器");
            for (int i = 0; i < list.size(); i++) {
                int almId = list.get(i).getAlmId();
                switch (almId){
                    case 3300033:
                        rectifierNameSb.append("1,");
                        break;
                    case 3300034:
                        rectifierNameSb.append("2,");
                        break;
                    case 3300035:
                        rectifierNameSb.append("3,");
                        break;
                    case 3300036:
                        rectifierNameSb.append("4,");
                        break;
                    case 3300037:
                        rectifierNameSb.append("5,");
                        break;
                    case 3300038:
                        rectifierNameSb.append("6,");
                        break;
                    case 3300039:
                        rectifierNameSb.append("7,");
                        break;
                    case 3300040:
                        rectifierNameSb.append("8,");
                        break;
                    case 3300041:
                        rectifierNameSb.append("9,");
                        break;
                    case 3300042:
                        rectifierNameSb.append("10,");
                        break;
                    case 3300043:
                        rectifierNameSb.append("11,");
                        break;
                    case 3300044:
                        rectifierNameSb.append("12,");
                        break;
                    case 3300045:
                        rectifierNameSb.append("13,");
                        break;
                    case 3300046:
                        rectifierNameSb.append("14,");
                        break;
                    case 3300047:
                        rectifierNameSb.append("15,");
                        break;
                    case 3300048:
                        rectifierNameSb.append("16,");
                        break;
                }
            }
            //最后一个,去掉
            rectifierNameSb.deleteCharAt(rectifierNameSb.length()-1);
            rectifierNameSb.append("欠压告警");
            alarmDto.setRectifierName(rectifierNameSb.toString());
            powerAlarmListNew.add(alarmDto);
        });
 
        //整流器过流告警合并
        //ALARM_3300049(3300049,"整流器1过流告警"),
        //    ALARM_3300050(3300050,"整流器2过流告警"),
        //    ALARM_3300051(3300051,"整流器3过流告警"),
        //    ALARM_3300052(3300052,"整流器4过流告警"),
        //    ALARM_3300053(3300053,"整流器5过流告警"),
        //    ALARM_3300054(3300054,"整流器6过流告警"),
        //    ALARM_3300055(3300055,"整流器7过流告警"),
        //    ALARM_3300056(3300056,"整流器8过流告警"),
        //    ALARM_3300057(3300057,"整流器9过流告警"),
        //    ALARM_3300058(3300058,"整流器10过流告警"),
        //    ALARM_3300059(3300059,"整流器11过流告警"),
        //    ALARM_3300060(3300060,"整流器12过流告警"),
        //    ALARM_3300061(3300061,"整流器13过流告警"),
        //    ALARM_3300062(3300062,"整流器14过流告警"),
        //    ALARM_3300063(3300063,"整流器15过流告警"),
        //    ALARM_3300064(3300064,"整流器16过流告警"),
        Map<Integer, List<AlarmDto>> powerOverCurrentMergeMap = powerAlarmList.stream()
                .filter(alarm -> alarm.getAlmId() >= 3300049 && alarm.getAlmId() <= 3300064)
                .collect(Collectors.groupingBy(AlarmDto::getPowerId));
        powerOverCurrentMergeMap.forEach((powerId, list) -> {
            //对list内的字段 整流器编号进行合并,合并到rectifierName. 时间取最小的
            List<AlarmDto> mergeList = list.stream()
                    .sorted(Comparator.comparing(AlarmDto::getAlmStartTime))
                    .collect(Collectors.toList());
            AlarmDto alarmDto = mergeList.get(0);
            //按告警id排序
            mergeList.sort(Comparator.comparing(AlarmDto::getAlmId));
            StringBuilder rectifierNameSb = new StringBuilder();
            rectifierNameSb.append("整流器");
            for (int i = 0; i < list.size(); i++) {
                int almId = list.get(i).getAlmId();
                switch (almId){
                    case 3300049:
                        rectifierNameSb.append("1,");
                        break;
                    case 3300050:
                        rectifierNameSb.append("2,");
                        break;
                    case 3300051:
                        rectifierNameSb.append("3,");
                        break;
                    case 3300052:
                        rectifierNameSb.append("4,");
                        break;
                    case 3300053:
                        rectifierNameSb.append("5,");
                        break;
                    case 3300054:
                        rectifierNameSb.append("6,");
                        break;
                    case 3300055:
                        rectifierNameSb.append("7,");
                        break;
                    case 3300056:
                        rectifierNameSb.append("8,");
                        break;
                    case 3300057:
                        rectifierNameSb.append("9,");
                        break;
                    case 3300058:
                        rectifierNameSb.append("10,");
                        break;
                    case 3300059:
                        rectifierNameSb.append("11,");
                        break;
                    case 3300060:
                        rectifierNameSb.append("12,");
                        break;
                    case 3300061:
                        rectifierNameSb.append("13,");
                        break;
                    case 3300062:
                        rectifierNameSb.append("14,");
                        break;
                    case 3300063:
                        rectifierNameSb.append("15,");
                        break;
                    case 3300064:
                        rectifierNameSb.append("16,");
                        break;
                }
            }
            //最后一个,去掉
            rectifierNameSb.deleteCharAt(rectifierNameSb.length()-1);
            rectifierNameSb.append("过流告警");
            alarmDto.setRectifierName(rectifierNameSb.toString());
            powerAlarmListNew.add(alarmDto);
        });
 
        //整流器交流异常告警合并
        //ALARM_3300065(3300065,"整流器1交流异常告警"),
        //    ALARM_3300066(3300066,"整流器2交流异常告警"),
        //    ALARM_3300067(3300067,"整流器3交流异常告警"),
        //    ALARM_3300068(3300068,"整流器4交流异常告警"),
        //    ALARM_3300069(3300069,"整流器5交流异常告警"),
        //    ALARM_3300070(3300070,"整流器6交流异常告警"),
        //    ALARM_3300071(3300071,"整流器7交流异常告警"),
        //    ALARM_3300072(3300072,"整流器8交流异常告警"),
        //    ALARM_3300073(3300073,"整流器9交流异常告警"),
        //    ALARM_3300074(3300074,"整流器10交流异常告警"),
        //    ALARM_3300075(3300075,"整流器11交流异常告警"),
        //    ALARM_3300076(3300076,"整流器12交流异常告警"),
        //    ALARM_3300077(3300077,"整流器13交流异常告警"),
        //    ALARM_3300078(3300078,"整流器14交流异常告警"),
        //    ALARM_3300079(3300079,"整流器15交流异常告警"),
        //    ALARM_3300080(3300080,"整流器16交流异常告警"),
        Map<Integer, List<AlarmDto>> powerACMergeMap = powerAlarmList.stream()
                .filter(alarm -> alarm.getAlmId() >= 3300065 && alarm.getAlmId() <= 3300080)
                .collect(Collectors.groupingBy(AlarmDto::getPowerId));
        powerACMergeMap.forEach((powerId, list) -> {
            //对list内的字段 整流器编号进行合并,合并到rectifierName. 时间取最小的
            List<AlarmDto> mergeList = list.stream()
                    .sorted(Comparator.comparing(AlarmDto::getAlmStartTime))
                    .collect(Collectors.toList());
            AlarmDto alarmDto = mergeList.get(0);
            //按告警id排序
            mergeList.sort(Comparator.comparing(AlarmDto::getAlmId));
            StringBuilder rectifierNameSb = new StringBuilder();
            rectifierNameSb.append("整流器");
            for (int i = 0; i < list.size(); i++) {
                int almId = list.get(i).getAlmId();
                switch (almId){
                    case 3300065:
                        rectifierNameSb.append("1,");
                        break;
                    case 3300066:
                        rectifierNameSb.append("2,");
                        break;
                    case 3300067:
                        rectifierNameSb.append("3,");
                        break;
                    case 3300068:
                        rectifierNameSb.append("4,");
                        break;
                    case 3300069:
                        rectifierNameSb.append("5,");
                        break;
                    case 3300070:
                        rectifierNameSb.append("6,");
                        break;
                    case 3300071:
                        rectifierNameSb.append("7,");
                        break;
                    case 3300072:
                        rectifierNameSb.append("8,");
                        break;
                    case 3300073:
                        rectifierNameSb.append("9,");
                        break;
                    case 3300074:
                        rectifierNameSb.append("10,");
                        break;
                    case 3300075:
                        rectifierNameSb.append("11,");
                        break;
                    case 3300076:
                        rectifierNameSb.append("12,");
                        break;
                    case 3300077:
                        rectifierNameSb.append("13,");
                        break;
                    case 3300078:
                        rectifierNameSb.append("14,");
                        break;
                    case 3300079:
                        rectifierNameSb.append("15,");
                        break;
                    case 3300080:
                        rectifierNameSb.append("16,");
                        break;
                }
            }
            //最后一个,去掉
            rectifierNameSb.deleteCharAt(rectifierNameSb.length()-1);
            rectifierNameSb.append("交流异常告警");
            alarmDto.setRectifierName(rectifierNameSb.toString());
            powerAlarmListNew.add(alarmDto);
        });
 
        //过滤掉产生合并的告警
        List<AlarmDto> powerAlarmListOther = powerAlarmList.stream().filter(alarm -> alarm.getAlmId() < 3300001 || alarm.getAlmId() > 3300080).collect(Collectors.toList());
        powerAlarmListNew.addAll(powerAlarmListOther);
        //对告警名称赋值
        powerAlarmListNew.forEach(alarmDto -> {
            alarmDto.setAlmName(PowerAlarmEnum.getValue(alarmDto.getAlmId()));
        });
        //对存在RectifierName的元素,将rectifierName字段赋值给almName字段
        powerAlarmListNew.forEach(alarmDto -> {
            if(alarmDto.getRectifierName() != null){
                alarmDto.setAlmName(alarmDto.getRectifierName());
            }
        });
 
 
        //整合所有的告警
        powerAlarmListNew.addAll(devAlarmList);
        powerAlarmListNew.addAll(battAlarmListNew);
 
        //按照时间进行倒序
        powerAlarmListNew.sort(Comparator.comparing(AlarmDto::getAlmStartTime).reversed());
        map.put("alarmList",powerAlarmListNew);
    }
 
 
    protected void testDataInfoStatistics(Integer userId, Map<String, Object> map) {
        List<BatttestdataInf> testInfList = battTestDataInfService.getListByUserId(userId);
        //过滤出本月的
        LocalDateTime startOfMonth = DateUtil.getStartOfMonth();
        List<BatttestdataInf> testInfListOfMonth = testInfList.stream()
                .filter(testData -> {
                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
                    return !startTime.isBefore(startOfMonth);
                })
                .collect(Collectors.toList());
 
        //统计本月各种充放电类型及数量
        List<NameValueDto> testInfOfMonth = getChargeAndDischargeStatistics(testInfListOfMonth);
        map.put("testInf_month",testInfOfMonth);
 
        //过滤出本季度的
        LocalDateTime startOfQuarter = DateUtil.getStartOfQuarter();
        List<BatttestdataInf> testInfListOfQuarter = testInfList.stream()
                .filter(testData -> {
                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
                    return !startTime.isBefore(startOfQuarter);
                })
                .collect(Collectors.toList());
 
        //统计本季度各种充放电类型及数量
        List<NameValueDto> testInfOfQuarter = getChargeAndDischargeStatistics(testInfListOfQuarter);
        map.put("testInf_quarter",testInfOfQuarter);
 
        //过滤出本年的
        LocalDateTime startOfYear = DateUtil.getStartOfYear();
        List<BatttestdataInf> testInfListOfYear = testInfList.stream()
                .filter(testData -> {
                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
                    return !startTime.isBefore(startOfYear);
                })
                .collect(Collectors.toList());
 
        //统计本年各种充放电类型及数量
        List<NameValueDto> testInfOfYear = getChargeAndDischargeStatistics(testInfListOfYear);
        map.put("testInf_year",testInfOfYear);
    }
 
    /**
     * 统计各种充放电类型及数量
     * @param testInfListOfMonth 被统计的列表
     * @return
     */
    private List<NameValueDto> getChargeAndDischargeStatistics(List<BatttestdataInf> testInfListOfMonth) {
        List<NameValueDto> testInfOfMonth = new ArrayList<>();
        int chargeHr = 0;
        int chargeJc = 0;
        int dischargeHr = 0;
        int dischargeJc = 0;
        for (int i = 0; i < testInfListOfMonth.size(); i++) {
            BatttestdataInf testData = testInfListOfMonth.get(i);
            if (testData.getTestType() == 3) {  // 测试类型为放电
                if (testData.getTestStarttype() == 3) {  //核容放电
                    dischargeHr++;
                } else if(testData.getTestStarttype() != 4){  //监测放电
                    dischargeJc++;
                }
            } else if (testData.getTestType() == 2) {  // 测试类型为充电
                if (testData.getTestStarttype() == 3) {  //核容充电
                    chargeHr++;
                } else {  //监测充电
                    chargeJc++;
                }
            }
        }
        //核容放电,核容充电,监测放电,检测充电
        testInfOfMonth.add(new NameValueDto("核容放电",String.valueOf(dischargeHr)));
        testInfOfMonth.add(new NameValueDto("核容充电",String.valueOf(chargeHr)));
        testInfOfMonth.add(new NameValueDto("监测放电",String.valueOf(dischargeJc)));
        testInfOfMonth.add(new NameValueDto("监测充电",String.valueOf(chargeJc)));
        return testInfOfMonth;
    }
 
    protected void batteryInfoStatistics(Integer userId, Map<String, Object> map) {
        List<BattInf> battInfList = battInfService.getListByUserId(userId);
        Map<String, List<BattInf>> brandMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getProduct));
        Map<Float, List<BattInf>> volMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getMonvolstd));
 
        List<NameValueDto> brandList = new ArrayList<>();
        brandMap.forEach((k,v)->{
            NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size()));
            brandList.add(brandDto);
        });
        map.put("battGroupInfo_brand",brandList);
 
        List<NameValueDto> volList = new ArrayList<>();
        volMap.forEach((k,v)->{
            NameValueDto volDto = new NameValueDto(String.valueOf(k), String.valueOf(v.size()));
            volList.add(volDto);
        });
        map.put("battGroupInfo_vol",volList);
    }
 
    protected void powerInfoStatistics(Integer userId, Map<String, Object> map) {
        List<PowerInf> powerInfList = powerInfService.getListByUserId(userId);
        Map<String, List<PowerInf>> brandMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getCompany));
        Map<Integer, List<PowerInf>> typeMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerType));
 
        List<NameValueDto> brandList = new ArrayList<>();
        brandMap.forEach((k,v)->{
            NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size()));
            brandList.add(brandDto);
        });
        map.put("powerInfo_brand",brandList);
 
        List<NameValueDto> typeList = new ArrayList<>();
        //电源类型:1:直流,2:通讯,3:配网
        typeMap.forEach((k,v)->{
            NameValueDto typeDto = new NameValueDto();
            switch (k){
                case 1:
                    typeDto.setName("直流");
                    break;
                case 2:
                    typeDto.setName("通讯");
                    break;
                case 3:
                    typeDto.setName("配网");
                    break;
            }
            typeDto.setValue(String.valueOf(v.size()));
            typeList.add(typeDto);
        });
        map.put("powerInfo_type",typeList);
 
    }
 
    /**
     * 按站点类型和站点电压
     * 站点类型:节点/非节点
     */
    private void stationInfoStatistics(Integer userId, Map<String, Object> map) {
        List<StationInf> stationInfList =stationInfService.getListByUserId(userId);
        Map<Integer, List<StationInf>> nodeMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation));
        Map<String, List<StationInf>> volMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
 
        List<NameValueDto> stationTypeList = new ArrayList<>();
        List<NameValueDto> stationVolList = new ArrayList<>();
        nodeMap.forEach((k,v)->{
            NameValueDto stationTypeDto = new NameValueDto(k == 0 ? "非节点" : "节点", String.valueOf(v.size()));
            stationTypeList.add(stationTypeDto);
        });
        volMap.forEach((k,v)->{
            NameValueDto stationVolDto = new NameValueDto(k, String.valueOf(v.size()));
            stationVolList.add(stationVolDto);
        });
 
        map.put("stationInfo_stationType",stationTypeList);
        map.put("stationInfo_stationVol",stationVolList);
    }
 
    //同一时间同一品牌电池性能统计
    public Response getBattPerformanceOfSameTimeSameBrand(Integer userId, NameValueIntDto nameValueDto) {
        String brand = nameValueDto.getName();
        Integer year = nameValueDto.getValue();
        //投入使用时间条件筛选
        LocalDateTime startTime = LocalDateTime.now().minusYears(year);
        Date inuseTimeStart = DateUtil.convertToDate(startTime);
        BattInf battInf = new BattInf();
        battInf.setInuseTime(inuseTimeStart);
        battInf.setProduct(brand);
        //查询满足条件的蓄电池列表
        List<BattInf> battInfList = battInfService.getListByCondition(userId,battInf);
        List<BattCapInfoDto> battCapInfoList = getBattCapInfo(battInfList,null,null);
        //data,和data2分别返回 统计数量和 站点优良,劣化,损坏 前二的信息
        List<NameValueIntDto> data = new ArrayList<>();
        List<NameValueListDto> data2 = new ArrayList<>();
        //筛选出 站点优良,劣化,损坏 的电池组,并按照这个字段分组
        List<BattCapInfoDto> goodList = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_1.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap).reversed()).collect(Collectors.toList());
        if(goodList.size()==0){
            data.add(new NameValueIntDto("优良",0));
            data2.add(new NameValueListDto("优良",goodList));
        }else{
            data.add(new NameValueIntDto("优良",goodList.size()));
            data2.add(new NameValueListDto("优良",goodList.subList(0, Math.min(goodList.size(), 2))));
        }
        List<BattCapInfoDto> badList = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        if(badList.size()==0){
            data.add(new NameValueIntDto("劣化",0));
            data2.add(new NameValueListDto("劣化",badList));
        }else{
            data.add(new NameValueIntDto("劣化",badList.size()));
            data2.add(new NameValueListDto("劣化",badList.subList(0, Math.min(badList.size(), 2))));
        }
        List<BattCapInfoDto> damageList = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        if(damageList.size()==0){
            data.add(new NameValueIntDto("损坏",0));
            data2.add(new NameValueListDto("损坏",damageList));
        }else{
            data.add(new NameValueIntDto("损坏",damageList.size()));
            data2.add(new NameValueListDto("损坏",damageList.subList(0, Math.min(damageList.size(), 2))));
        }
        return new Response().setII(1,data,data2,"");
 
    }
 
    public Response getBattPerformanceOfSameTimeDiffBrand(Integer userId, int year) {
        //投入使用时间条件筛选
        LocalDateTime startTime = LocalDateTime.now().minusYears(year);
        Date inuseTimeStart = DateUtil.convertToDate(startTime);
        BattInf battInf = new BattInf();
        battInf.setInuseTime(inuseTimeStart);
        //查询满足条件的蓄电池列表
        List<BattInf> battInfList = battInfService.getListByCondition(userId,battInf);
        List<BattCapInfoDto> battCapInfoList = getBattCapInfo(battInfList,null,null);
 
        //data,根据品牌分组统计百分比
        Map<String, List<BattCapInfoDto>> brandMap = battCapInfoList.stream().collect(Collectors.groupingBy(BattCapInfoDto::getProduct));
        List<NameValueListDto<NameValueDto>> data = new ArrayList<>();
        brandMap.forEach((brand,battCapInfoListTemp)->{
            //k -品牌
            long goodCount = battCapInfoListTemp.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_1.getStateName())).count();
            long badCount = battCapInfoListTemp.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName())).count();
            long damageCount = battCapInfoListTemp.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName())).count();
            long total = goodCount  + badCount + damageCount;
            //良好,劣化,损坏的百分比.(保留0位小数)
            int goodPercent = Math.round((float) goodCount / total * 100);
            int badPercent = Math.round((float) badCount / total * 100);
            int damagePercent = Math.round((float) damageCount / total * 100);
 
            List<NameValueDto> performanceList = new ArrayList<>();
            performanceList.add(new NameValueDto("优良",String.format("%d%%",goodPercent)));
            performanceList.add(new NameValueDto("劣化",String.format("%d%%",badPercent)));
            performanceList.add(new NameValueDto("损坏",String.format("%d%%",damagePercent)));
 
            NameValueListDto brandPerformance = new NameValueListDto(brand,performanceList);
            data.add(brandPerformance);
        });
        //data2,优良里面最大的和最小的品牌
        List<NameValueListDto<NameValueDto>> data2 = new ArrayList<>();
        List<BattCapInfoDto> goodList = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_1.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        NameValueListDto<NameValueDto> goodListDto = new NameValueListDto<>();
        List<NameValueDto> goodListValue = new ArrayList<>();
        NameValueDto goodMaxDto = new NameValueDto();
        NameValueDto goodMinDto = new NameValueDto();
        goodMaxDto.setName("max");
        goodMinDto.setName("min");
        if (goodList.size()==0){
            goodMaxDto.setValue("-");
            goodMinDto.setValue("-");
        }else{
            goodMinDto.setValue(goodList.get(0).getProduct());
            goodMaxDto.setValue(goodList.get(goodList.size()-1).getProduct());
        }
        goodListValue.add(goodMaxDto);
        goodListValue.add(goodMinDto);
 
        goodListDto.setName("优良");
        goodListDto.setValue(goodListValue);
        data2.add(goodListDto);
        //劣化里面最大的和最小的品牌
        List<BattCapInfoDto> badList = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        NameValueListDto<NameValueDto> badListDto = new NameValueListDto<>();
        List<NameValueDto> badListValue = new ArrayList<>();
        NameValueDto badMaxDto = new NameValueDto();
        NameValueDto badMinDto = new NameValueDto();
        badMaxDto.setName("max");
        badMinDto.setName("min");
        if (badList.size()==0){
            badMaxDto.setValue("-");
            badMinDto.setValue("-");
        }else{
            badMinDto.setValue(badList.get(0).getProduct());
            badMaxDto.setValue(badList.get(badList.size()-1).getProduct());
        }
        badListValue.add(badMaxDto);
        badListValue.add(badMinDto);
 
        badListDto.setName("劣化");
        badListDto.setValue(badListValue);
        data2.add(badListDto);
 
        //损坏里面最大的和最小的品牌
        List<BattCapInfoDto> damageList = battCapInfoList.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
 
        NameValueListDto<NameValueDto> damageListDto = new NameValueListDto<>();
        List<NameValueDto> damageListValue = new ArrayList<>();
        NameValueDto damageMaxDto = new NameValueDto();
        NameValueDto damageMinDto = new NameValueDto();
        damageMaxDto.setName("max");
        damageMinDto.setName("min");
        if (damageList.size()==0){
            damageMaxDto.setValue("-");
            damageMinDto.setValue("-");
        }else if (damageList.size()==1){
            damageMaxDto.setValue(damageList.get(0).getProduct());
            damageMinDto.setValue(damageList.get(0).getProduct());
        }else{
            damageMinDto.setValue(damageList.get(0).getProduct());
            damageMaxDto.setValue(damageList.get(damageList.size()-1).getProduct());
        }
        damageListValue.add(damageMaxDto);
        damageListValue.add(damageMinDto);
 
        damageListDto.setName("损坏");
        damageListDto.setValue(damageListValue);
        data2.add(damageListDto);
 
        //data3. 性能最佳品牌 TODO 威尔逊置信区间法以及贝叶斯平均法,通过设置的权重,计算各个品牌在指定投运时间内的得分
        return new Response().setIII(1,data,data2,"性能最佳品牌:TODO 威尔逊置信区间法以及贝叶斯平均法,通过设置的权重,计算各个品牌在指定投运时间内的得分",null);
 
    }
 
    public Response getBattPerformanceOfDiffTimeSameBrand(Integer userId, String brand) {
        BattInf battInf = new BattInf();
        battInf.setProduct(brand);
        //查询满足条件的蓄电池列表
        List<BattInf> battInfList = battInfService.getListByCondition(userId,battInf);
        List<BattCapInfoDto> battCapInfoList = getBattCapInfo(battInfList,null,null);
 
        //data,TODO 投运时间的维度参数,先固定变量,后续从数据库获取
        //time1和time2的单位为年
        int time1 = 3;
        int time2 = 5;
 
        //筛选battCapInfoList中对象的inuseTime属性 跟当前时间对比,年份差在time1之内,time1-time2之间,time2之后的数据
        // --------------time2Limit------time1Limit-------now
        LocalDateTime time1DateLimit = LocalDateTime.now().minusYears(time1);
        Date time1Limit = DateUtil.convertToDate(time1DateLimit);
        LocalDateTime time2DateLimit = LocalDateTime.now().minusYears(time2);
        Date time2Limit = DateUtil.convertToDate(time2DateLimit);
 
        //大于time1的数据
        List<BattCapInfoDto> time1List = battCapInfoList.stream().
                filter(battCapInfo -> battCapInfo.getInuseTime().compareTo(time1Limit)>=0).collect(Collectors.toList());
        //在time1和time2之间的数据
        List<BattCapInfoDto> time12List = battCapInfoList.stream().
                filter(battCapInfo -> battCapInfo.getInuseTime().compareTo(time2Limit)>0 && battCapInfo.getInuseTime().compareTo(time1Limit)<=0).collect(Collectors.toList());
        //小于time2的数据
        List<BattCapInfoDto> time2List = battCapInfoList.stream().
                filter(battCapInfo -> battCapInfo.getInuseTime().compareTo(time2Limit)<0).collect(Collectors.toList());
 
        //data
        List<NameValueListDto<NameValueIntDto>> data = new ArrayList<>();
        NameValueListDto<NameValueIntDto> time1Dto = new NameValueListDto<>();
        time1Dto.setName(time1+"年内");
        List<BattCapInfoDto> badListOfTime1 = time1List.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        List<BattCapInfoDto> damageListOfTime1 = time1List.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        NameValueIntDto time1BadDto = new NameValueIntDto();
        time1BadDto.setName("劣化");
        time1BadDto.setValue(badListOfTime1.size());
        NameValueIntDto time1DamageDto = new NameValueIntDto();
        time1DamageDto.setName("损坏");
        time1DamageDto.setValue(damageListOfTime1.size());
        time1Dto.setValue(Arrays.asList(time1BadDto,time1DamageDto));
 
        NameValueListDto<NameValueIntDto> time12Dto = new NameValueListDto<>();
        time12Dto.setName(time1+"-"+time2+"年");
        List<BattCapInfoDto> badListOfTime12 = time12List.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        List<BattCapInfoDto> damageListOfTime12 = time12List.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        NameValueIntDto time12BadDto = new NameValueIntDto();
        time12BadDto.setName("劣化");
        time12BadDto.setValue(badListOfTime12.size());
        NameValueIntDto time12DamageDto = new NameValueIntDto();
        time12DamageDto.setName("损坏");
        time12DamageDto.setValue(damageListOfTime12.size());
        time12Dto.setValue(Arrays.asList(time12BadDto,time12DamageDto));
 
        NameValueListDto<NameValueIntDto> time2Dto = new NameValueListDto<>();
        time2Dto.setName(time2+"年以上");
        List<BattCapInfoDto> badListOfTime2 = time2List.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_2.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        List<BattCapInfoDto> damageListOfTime2 = time2List.stream().filter(battCapInfo -> battCapInfo.getPerformance().equals(BattCapperformanceEnum.BATTSTATE_3.getStateName()))
                .sorted(Comparator.comparing(BattCapInfoDto::getRealCap)).collect(Collectors.toList());
        NameValueIntDto time2BadDto = new NameValueIntDto();
        time2BadDto.setName("劣化");
        time2BadDto.setValue(badListOfTime2.size());
        NameValueIntDto time2DamageDto = new NameValueIntDto();
        time2DamageDto.setName("损坏");
        time2DamageDto.setValue(damageListOfTime2.size());
        time2Dto.setValue(Arrays.asList(time2BadDto,time2DamageDto));
 
        data.add(time1Dto);
        data.add(time12Dto);
        data.add(time2Dto);
 
        //data2
        List<NameValueListDto<NameValueObjectDto<BattCapInfoDto>>> data2 = new ArrayList<>();
        NameValueListDto<NameValueObjectDto<BattCapInfoDto>> time1ObjectDto = new NameValueListDto<>();
        time1ObjectDto.setName(time1+"年内");
        NameValueObjectDto<BattCapInfoDto> time1BadObjectDto = new NameValueObjectDto();
        time1BadObjectDto.setName("劣化");
        time1BadObjectDto.setValue(badListOfTime1.size()==0?new BattCapInfoDto():badListOfTime1.get(0));
 
        NameValueObjectDto<BattCapInfoDto> time1DamageObjectDto = new NameValueObjectDto();
        time1DamageObjectDto.setName("损坏");
        time1DamageObjectDto.setValue(damageListOfTime1.size()==0?new BattCapInfoDto():damageListOfTime1.get(0));
        time1ObjectDto.setValue(Arrays.asList(time1BadObjectDto,time1DamageObjectDto));
        data2.add(time1ObjectDto);
 
        NameValueListDto<NameValueObjectDto<BattCapInfoDto>> time12ObjectDto = new NameValueListDto<>();
        time12ObjectDto.setName(time1+"-"+time2+"年");
        NameValueObjectDto<BattCapInfoDto> time12BadObjectDto = new NameValueObjectDto();
        time12BadObjectDto.setName("劣化");
        time12BadObjectDto.setValue(badListOfTime12.size()==0?new BattCapInfoDto():badListOfTime12.get(0));
        NameValueObjectDto<BattCapInfoDto> time12DamageObjectDto = new NameValueObjectDto();
        time12DamageObjectDto.setName("损坏");
        time12DamageObjectDto.setValue(damageListOfTime12.size()==0?new BattCapInfoDto():damageListOfTime12.get(0));
        time12ObjectDto.setValue(Arrays.asList(time12BadObjectDto,time12DamageObjectDto));
        data2.add(time12ObjectDto);
 
        NameValueListDto<NameValueObjectDto<BattCapInfoDto>> time2ObjectDto = new NameValueListDto<>();
        time2ObjectDto.setName(time2+"年以上");
        NameValueObjectDto<BattCapInfoDto> time2BadObjectDto = new NameValueObjectDto();
        time2BadObjectDto.setName("劣化");
        time2BadObjectDto.setValue(badListOfTime2.size()==0?new BattCapInfoDto():badListOfTime2.get(0));
        NameValueObjectDto<BattCapInfoDto> time2DamageObjectDto = new NameValueObjectDto();
        time2DamageObjectDto.setName("损坏");
        time2DamageObjectDto.setValue(damageListOfTime2.size()==0?new BattCapInfoDto():damageListOfTime2.get(0));
        time2ObjectDto.setValue(Arrays.asList(time2BadObjectDto,time2DamageObjectDto));
        data2.add(time2ObjectDto);
 
        return new Response().setII(1,data,data2,"");
 
    }
 
    public List<BattCapInfoDto> getBattCapInfo(List<BattInf> battInfList,Date startTime, Date endTime) {
        //获取核容优劣,损坏参数
        List<AppParam> paramList=appParamService.getHrParam();
        Float badValue=0f;
        Float damageValue=0f;
        if(paramList!=null){
            for (AppParam param:paramList) {
                if(param.getParamNamePsx().equals("batt_mon_deteriorate_val")){
                    badValue=param.getParamValue();
                }
                if(param.getParamNamePsx().equals("batt_mon_damage_val")){
                    damageValue=param.getParamValue();
                }
 
            }
        }
        //1查询符合条件的电池组
        if(battInfList==null||battInfList.size()==0){
            //return new Response().set(1,false,"当前用户未管理满足条件的电池组");
        }
        List<BattCapInfoDto> capInfoList = new ArrayList<>();
 
        for (BattInf battInf:battInfList) {
            BattCapInfoDto capInfo = new BattCapInfoDto();
            capInfo.setStationName(battInf.getStationName());
            capInfo.setBattGroupName(battInf.getBattgroupName());
            capInfo.setDeviceName(battInf.getDevName());
            capInfo.setProduct(battInf.getProduct());
            capInfo.setInuseTime(battInf.getInuseTime());
 
            //2.获取电池组在给定时间段的放电记录(指定时间段的标准核容放电)
            //BatttestdataInf testInf = battTestDataInfService.getLastStandardTestDataByTime(battInf.getBattgroupId(), battInf.getInuseTime(),null);
            BatttestdataInf testInf = battTestDataInfService.getLastStandardTestDataByTime(battInf.getBattgroupId(), startTime,endTime);
            if (testInf == null) {
                capInfo.setRealCap(0f);
                capInfo.setCapPercentage("0");
                capInfo.setPerformance(BattCapperformanceEnum.BATTSTATE_4.getStateName());
                continue;
            }
            //找这次放电的最后一笔数据
            List<BatttestdataId> idDataList = battTestDataIdService.getLastDataByBattgroupId(testInf.getBattgroupId(), testInf.getTestRecordCount(), testInf.getRecordNum());
            if (idDataList == null || idDataList.size() == 0) {
                capInfo.setRealCap(0f);
                capInfo.setCapPercentage("0");
                capInfo.setPerformance(BattCapperformanceEnum.BATTSTATE_4.getStateName());
                continue;
            }
            Float moncapStd = battInf.getMoncapstd();
            int hourRate = BattCapFactory.GetHourRate(testInf.getTestCap(), testInf.getTestCurr());
            Float groupRealCap = (float) BattCapFactory.GetMonomerCap(testInf.getTestCap(), hourRate, testInf.getTestCap(), testInf.getMaxMonvol(), testInf.getMinMonvol(), testInf.getGroupVol(), BattCapFactory.CapType_Real);
            capInfo.setRealCap(groupRealCap);
            if (groupRealCap >= moncapStd * badValue) {
                capInfo.setPerformance(BattCapperformanceEnum.BATTSTATE_1.getStateName());
            }
            if (groupRealCap <= moncapStd * damageValue) {
                capInfo.setPerformance(BattCapperformanceEnum.BATTSTATE_3.getStateName());
            }
            if ((groupRealCap > moncapStd * damageValue) && (groupRealCap < moncapStd * badValue)) {
                capInfo.setPerformance(BattCapperformanceEnum.BATTSTATE_2.getStateName());
            }
            //保留5位小数
            String capPercentage = String.format("%.5f", (groupRealCap / battInf.getMoncapstd() * 100));
            capInfo.setCapPercentage(capPercentage);
 
            capInfoList.add(capInfo);
        }
        return capInfoList;
    }
 
}