whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
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
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.constant.RoleEnum;
import com.whyc.constant.WorkflowDischargePlanEnum;
import com.whyc.constant.WorkflowEnum;
import com.whyc.dto.Response;
import com.whyc.mapper.BattDischargePlanTempMapper;
import com.whyc.pojo.*;
import com.whyc.util.ActionUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class BattDischargePlanTempService {
 
    @Resource
    private BattDischargePlanTempMapper mapper;
 
    @Resource
    private BaoJiGroupBattGroupService baoJiGroupBattGroupService;
 
    @Autowired
    private BaoJiGroupUserService baoJiGroupUserService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    @Lazy
    private BattdischargePlanService planService;
 
    @Autowired
    private HolidaysService holidaysService;
 
    @Autowired
    @Lazy
    private WorkflowMainService mainService;
 
    @Autowired
    @Lazy
    private WorkflowLinkService linkService;
 
    public void insertBatch(List<BattDischargePlanTemp> tempList) {
        mapper.insertBatchSomeColumn(tempList);
    }
 
    public Response getReplaceBattGroupList(int num) {
        List<BattDischargePlanTemp> tempList = mapper.selectList((Wrapper<BattDischargePlanTemp>) ActionUtil.objeNull);
        List<BattDischargePlanTemp> recommendList = new LinkedList<>();
        List<BattDischargePlanTemp> list = new LinkedList<>();
        BattDischargePlanTemp currentTemp = null;
        for (BattDischargePlanTemp temp : tempList) {
            if(temp.getId()==num){
                currentTemp = temp;
                break;
            }
        }
        //同类型替换,并且不能是同一天的
        for (BattDischargePlanTemp temp : tempList) {
            assert currentTemp != null;
            if(temp.getId()!=num &&temp.getDischargeStartTime().compareTo(currentTemp.getDischargeStartTime())!=0 && temp.getNodeStation().equals(currentTemp.getNodeStation())){
                if(temp.getGroupId().equals(currentTemp.getGroupId())){ //同班组,推荐
                    recommendList.add(temp);
                }else{
                    list.add(temp);
                }
            }
        }
        return new Response().setII(1,recommendList,list,"");
    }
 
    public Response getDisabledDischargeTime(int num) {
        List<BattDischargePlanTemp> tempList = mapper.selectList((Wrapper<BattDischargePlanTemp>) ActionUtil.objeNull);
        BattDischargePlanTemp currentTemp = null;
        Integer nodeStation = 0;
        for (BattDischargePlanTemp temp : tempList) {
            if(temp.getId()==num){
                currentTemp = temp;
                break;
            }
        }
        //不可用的时间,三种方式,层层筛选:1.当前电池组时间 2.其他 满足每天3个站点的时间 3.如果当前为节点站,再其他,存在节点站的时间
        List<Date> disabledDischargeTimeList = new LinkedList<>();
        Date dischargeStartTime = new Date();
        if(currentTemp!=null){
            dischargeStartTime=currentTemp.getDischargeStartTime();
        }
        disabledDischargeTimeList.add(dischargeStartTime);
        if(currentTemp!=null){
            nodeStation = currentTemp.getNodeStation();
        }
        Map<Date, List<BattDischargePlanTemp>> dischargeMap = tempList.stream().collect(Collectors.groupingBy(BattDischargePlanTemp::getDischargeStartTime));
        Set<Date> dates = dischargeMap.keySet();
        for (Date date : dates) {
            if(dischargeStartTime.compareTo(date)==0){
                continue;
            }
            List<BattDischargePlanTemp> dailyTempList = dischargeMap.get(date);
            if (dailyTempList.size()==3){
                disabledDischargeTimeList.add(date);
            }else{ //当天不足3个站点
                if(nodeStation == 1){ //如果需要插入的站点是节点站,当天存在节点站,也不可用
                    for (BattDischargePlanTemp temp : dailyTempList) {
                        if(temp.getNodeStation()==1){
                            disabledDischargeTimeList.add(date);
                            break;
                        }
                    }
                }
            }
        }
        disabledDischargeTimeList = disabledDischargeTimeList.stream().sorted().collect(Collectors.toList());
        return new Response().set(1,disabledDischargeTimeList);
    }
 
    public Response exchangeDischargeTime(int numOriginal, int numReplaced) {
        BattDischargePlanTemp original = mapper.selectById(numOriginal);
        BattDischargePlanTemp replaced = mapper.selectById(numReplaced);
 
        UpdateWrapper<BattDischargePlanTemp> update = Wrappers.update();
        update.set("discharge_starttime",replaced.getDischargeStartTime()).eq("id",numOriginal);
        mapper.update((BattDischargePlanTemp) ActionUtil.objeNull,update);
 
        UpdateWrapper<BattDischargePlanTemp> update2 = Wrappers.update();
        update2.set("discharge_starttime",original.getDischargeStartTime()).eq("id",numReplaced);
        mapper.update((BattDischargePlanTemp) ActionUtil.objeNull,update2);
        return new Response().setII(1,"放电时间互换");
    }
 
    /**
     * 原则:
     * 0.每个站点只取第一组电池参与放电计划
     * 1.每天最多1个节点站,每天最多3个放电站点
     * 2.各班组尽量扁平,尽量保证各个组完全放电完成时间接近
     * @return
     * @param resetCapPercent
     * @param startTimeStr
     */
    @Transactional
    public Response generateDischargePlan(Float resetCapPercent, String startTimeStr) throws ParseException {
        List<BattDischargePlanTemp> tempList = new LinkedList<>();
        //Date startTime = DateUtil.YYYY_MM_DD_HH_MM_SS.parse(startTimeStr);
        Date startTime = ThreadLocalUtil.parse(startTimeStr,1);
        Calendar planTime = Calendar.getInstance();
        planTime.setTime(startTime);
        //一年度只能生成一次审核通过的放电计划,校验
        int planYear = planTime.get(Calendar.YEAR);
        BattDischargePlanTemp tempDB = getValidOneByYear(planYear);
        if(tempDB!=null){
            return new Response().set(1,false,"当前年度已存在计划审批清单,无法重复生成");
        }
        //查询所有可用的组以及每个组的站点
        //planTime校验,避开节假日和周六日
        planTime = checkAndGetPlanTime(planTime);
        List<BaojiGroupBattGroup> baoJiGroupListWithinBattGroupList = baoJiGroupBattGroupService.getBaoJiGroupListWithinBattGroupList();
        //List<BattdischargePlan> planList = new LinkedList<>();
        List<Battinf> planList = new LinkedList<>();
        int prePlanSize = 3;
        int planSize = 0;
        while (prePlanSize != planSize && baoJiGroupListWithinBattGroupList.size()>0) { //当相等时,说明每天计划已经为0,计划完结
            prePlanSize = planSize;
            //List<BattdischargePlan> subPlanList =  getDischargePlanList(baoJiGroupListWithinBattGroupList,planTime);
            List<Battinf> subPlanList =  getDischargePlanList2(baoJiGroupListWithinBattGroupList,planTime);
            //添加到电池放电计划List
            planList.addAll(subPlanList);
            //baoJiGroupListWithinBattGroupList重置为当前baoJiGroupListWithinBattGroupList-battGroupList
 
            //按日递增计划时间,同时避开节假日和周六日
            planTime.add(Calendar.DAY_OF_MONTH,1);
            planTime = checkAndGetPlanTime(planTime);
            planSize = planList.size();
 
            //整理数据,为下一天做放电计划做准备
            //剔除已经不存在的班组
            for (int i = baoJiGroupListWithinBattGroupList.size() - 1; i >= 0; i--) {
                if (baoJiGroupListWithinBattGroupList.get(i).getBattGroupList().size() == 0) {
                    baoJiGroupListWithinBattGroupList.remove(i);
                }
            }
            //算法方式1:可以本次安排了放电计划的班组,排序到最后,其他瞬移.
            //算法方式2:滚动班组顺序,保证扁平.原第一顺位,变为最后顺位
            List<Integer> existsBaojiGroupList = baoJiGroupListWithinBattGroupList.stream().map(BaojiGroupBattGroup::getBaojiGroupId).collect(Collectors.toList());
            List<Integer> planedGroupIdExistsList = new LinkedList<>();
            for (Battinf battinf : subPlanList) {
                int baojiGroupId = battinf.getNum();
                if(existsBaojiGroupList.contains(baojiGroupId)) {
                    planedGroupIdExistsList.add(baojiGroupId);
                }
            }
            int planedGroupIdExistsListSize = planedGroupIdExistsList.size();
            int baoJiGroupListSize = baoJiGroupListWithinBattGroupList.size();
            if(planedGroupIdExistsListSize >0) {
                if (baoJiGroupListSize >1){//班组数大于1组时调整排序才有意义
                    //班组<=3时才可能执行的调整逻辑,且执行过上次计划分配后的班组仍都存在可分配站点
                    //上次计划分配后的班组和当时存在的班组完全相同
                    if(baoJiGroupListSize == planedGroupIdExistsListSize) {
                        //如果已经计划且还有站点需要放电的班组数量 = 需要放电的班组数量
                        baoJiGroupListWithinBattGroupList = adjustListMethod2(baoJiGroupListWithinBattGroupList);
                    }else{
                        //必然是需要放电的班组数量>已经计划且还有站点需要放电的班组数量
                        adjustListMethod1(baoJiGroupListWithinBattGroupList, planedGroupIdExistsList);
                    }
                }
            }
            //算法方式2:滚动班组顺序,保证扁平.原第一顺位,变为最后顺位
            //baoJiGroupListWithinBattGroupList = adjustListMethod2(baoJiGroupListWithinBattGroupList, firstBaojiGroupId);
 
        }
 
        Date date = new Date();
        int userId = ActionUtil.getUser().getUId().intValue();
        for (Battinf battinf : planList) {
            BattDischargePlanTemp temp = new BattDischargePlanTemp();
            temp.setBattGroupId(battinf.getBattGroupId());
            temp.setBattGroupName(battinf.getBattGroupName());
            temp.setStationId(battinf.getStationId());
            temp.setStationName(battinf.getStationName());
            temp.setNodeStation(battinf.getNodeStation());
            temp.setGroupId(battinf.getNum().longValue());
            temp.setGroupName(battinf.getInstallUser());
            temp.setDischargeStartTime(battinf.getDischargeStartTime());
            temp.setCreateTime(date);
            temp.setMonCapStd(battinf.getMonCapStd());
            temp.setMonVolStd(battinf.getMonVolStd());
            temp.setCapPercent(resetCapPercent);
            temp.setCreateUserId(userId);
            temp.setSubmitStatus(0);
            tempList.add(temp);
        }
        //存入临时表
        if(tempList.size()>0) {
            insertBatch(tempList);
            return new Response().setII(1,true,tempList,"");
        }else{
            return new Response().set(1,false,"未找到需要执行放电计划的机房");
        }
 
 
    }
 
    /**
     * 原则:
     * 0.站点只取第一组电池参与放电计划
     * 1.每天最多1个节点站,每天最多3个放电站点
     * @return
     * @param resetCapPercent
     * @param startTimeStr
     */
    @Transactional
    public Response generateDischargePlan2(Float resetCapPercent, String startTimeStr) throws ParseException {
        int userId = ActionUtil.getUser().getUId().intValue();
        List<BattDischargePlanTemp> tempList = new LinkedList<>();
        //Date startTime = DateUtil.YYYY_MM_DD_HH_MM_SS.parse(startTimeStr);
        Date startTime = ThreadLocalUtil.parse(startTimeStr,1);
        Calendar planTime = Calendar.getInstance();
        planTime.setTime(startTime);
        //校验当前用户是否为管理员且只存在于一个包机组
        int resultValue = checkUser(userId);
        if(resultValue == 2){
            return new Response().set(1,false,"当前用户不是管理员");
        }else if(resultValue == 3){
            return new Response().set(1,false,"当前用户管理多个包机组,无法生成单个包机组放电计划");
        }
 
        //一年度当前用户只能生成一次审核通过的放电计划,校验
        int planYear = planTime.get(Calendar.YEAR);
        BattDischargePlanTemp tempDB = getValidOneByYearOfUser(planYear,userId);
        if(tempDB!=null){
            return new Response().set(1,false,"当前年度已存在计划审批清单,无法重复生成");
        }
        //查询所有可用的组以及每个组的站点
        //planTime校验,避开节假日和周六日,以及已经审批占用的时间
        planTime = checkAndGetPlanTime2(planTime);
        List<BaojiGroupBattGroup> baoJiGroupListWithinBattGroupList = baoJiGroupBattGroupService.getBaoJiGroupListWithinBattGroupListOfUser(userId);
        //List<BattdischargePlan> planList = new LinkedList<>();
        List<Battinf> planList = new LinkedList<>();
        int prePlanSize = 3;
        int planSize = 0;
        while (prePlanSize != planSize && baoJiGroupListWithinBattGroupList.size()>0) { //当相等时,说明每天计划已经为0,计划完结
            prePlanSize = planSize;
            //List<BattdischargePlan> subPlanList =  getDischargePlanList(baoJiGroupListWithinBattGroupList,planTime);
            List<Battinf> subPlanList =  getDischargePlanList2(baoJiGroupListWithinBattGroupList,planTime);
            //添加到电池放电计划List
            planList.addAll(subPlanList);
            //baoJiGroupListWithinBattGroupList重置为当前baoJiGroupListWithinBattGroupList-battGroupList
 
            //按日递增计划时间,同时避开节假日和周六日
            planTime.add(Calendar.DAY_OF_MONTH,1);
            planTime = checkAndGetPlanTime2(planTime);
            planSize = planList.size();
 
            //整理数据,为下一天做放电计划做准备
            //剔除已经不存在的班组
            for (int i = baoJiGroupListWithinBattGroupList.size() - 1; i >= 0; i--) {
                if (baoJiGroupListWithinBattGroupList.get(i).getBattGroupList().size() == 0) {
                    baoJiGroupListWithinBattGroupList.remove(i);
                }
            }
            //算法方式1:可以本次安排了放电计划的班组,排序到最后,其他瞬移.
            //算法方式2:滚动班组顺序,保证扁平.原第一顺位,变为最后顺位
            List<Integer> existsBaojiGroupList = baoJiGroupListWithinBattGroupList.stream().map(BaojiGroupBattGroup::getBaojiGroupId).collect(Collectors.toList());
            List<Integer> planedGroupIdExistsList = new LinkedList<>();
            for (Battinf battinf : subPlanList) {
                int baojiGroupId = battinf.getNum();
                if(existsBaojiGroupList.contains(baojiGroupId)) {
                    planedGroupIdExistsList.add(baojiGroupId);
                }
            }
            int planedGroupIdExistsListSize = planedGroupIdExistsList.size();
            int baoJiGroupListSize = baoJiGroupListWithinBattGroupList.size();
            if(planedGroupIdExistsListSize >0) {
                if (baoJiGroupListSize >1){//班组数大于1组时调整排序才有意义
                    //班组<=3时才可能执行的调整逻辑,且执行过上次计划分配后的班组仍都存在可分配站点
                    //上次计划分配后的班组和当时存在的班组完全相同
                    if(baoJiGroupListSize == planedGroupIdExistsListSize) {
                        //如果已经计划且还有站点需要放电的班组数量 = 需要放电的班组数量
                        baoJiGroupListWithinBattGroupList = adjustListMethod2(baoJiGroupListWithinBattGroupList);
                    }else{
                        //必然是需要放电的班组数量>已经计划且还有站点需要放电的班组数量
                        adjustListMethod1(baoJiGroupListWithinBattGroupList, planedGroupIdExistsList);
                    }
                }
            }
            //算法方式2:滚动班组顺序,保证扁平.原第一顺位,变为最后顺位
            //baoJiGroupListWithinBattGroupList = adjustListMethod2(baoJiGroupListWithinBattGroupList, firstBaojiGroupId);
 
        }
 
        Date date = new Date();
        for (Battinf battinf : planList) {
            BattDischargePlanTemp temp = new BattDischargePlanTemp();
            temp.setBattGroupId(battinf.getBattGroupId());
            temp.setBattGroupName(battinf.getBattGroupName());
            temp.setStationId(battinf.getStationId());
            temp.setStationName(battinf.getStationName());
            temp.setNodeStation(battinf.getNodeStation());
            temp.setGroupId(battinf.getNum().longValue());
            temp.setGroupName(battinf.getInstallUser());
            temp.setDischargeStartTime(battinf.getDischargeStartTime());
            temp.setCreateTime(date);
            temp.setMonCapStd(battinf.getMonCapStd());
            temp.setMonVolStd(battinf.getMonVolStd());
            temp.setCapPercent(resetCapPercent);
            temp.setCreateUserId(userId);
            temp.setSubmitStatus(0);
            tempList.add(temp);
        }
        //存入临时表
        if(tempList.size()>0) {
            insertBatch(tempList);
            return new Response().setII(1,true,tempList,"");
        }else{
            return new Response().set(1,false,"未找到需要执行放电计划的机房");
        }
 
 
    }
 
    private int checkUser(int userId) {
        int roleId = ActionUtil.getUser().getURole();
        if(roleId!= RoleEnum.GROUP_LEADER.getId()){
            return 2; //当前用户不是管理员
        }
        List<BaoJiGroupUser> groupListByUserId = baoJiGroupUserService.getGroupListByUserId(userId);
        if(groupListByUserId.size()>1){
            return 3; //管理员管理多个包机组,需要检查
        }
        return 1; //通过
    }
 
    private BattDischargePlanTemp getValidOneByYearOfUser(int planYear, int userId) {
        return mapper.getValidOneByYearOfUser(planYear,userId);
    }
 
    /**查看放电临时表中审核状态有效(包含审批中,审批通过) */
    private BattDischargePlanTemp getValidOneByYear(int planYear) {
        return mapper.getValidOneByYear(planYear);
    }
 
    /**校验日期,避开节假日和周六日*/
    private Calendar checkAndGetPlanTime(Calendar planTime) throws ParseException {
        Calendar planTimeOnce = checkAndGetOnce(planTime);
        while (true){
            Calendar planTimeTwice = checkAndGetOnce(planTimeOnce);
            if(planTimeTwice.compareTo(planTimeOnce) == 0) {
                break;
            }else{
                planTimeOnce = planTimeTwice;
            }
        }
        return planTimeOnce;
    }
 
    private Calendar checkAndGetOnce(Calendar planTime) throws ParseException {
        Calendar planTimeDo = Calendar.getInstance();
        planTimeDo.setTime(planTime.getTime());
        int dayOfWeek = planTimeDo.get(Calendar.DAY_OF_WEEK);
        int saturday = Calendar.SATURDAY;
        int sunday = Calendar.SUNDAY;
        //周六日的话,就推到星期一
        if(dayOfWeek == saturday){
            planTimeDo.add(Calendar.DAY_OF_MONTH,2);
        }else if(dayOfWeek == sunday){
            planTimeDo.add(Calendar.DAY_OF_MONTH,1);
        }
        //获取节假日
        int year = planTimeDo.get(Calendar.YEAR);
        List<Holidays> holidays = holidaysService.getListByYear(year);
        List<Date> holidaysDateList = holidays.stream().map(Holidays::getDay).collect(Collectors.toList());
        while (true){
            if(holidaysDateList.contains(planTimeDo.getTime())){
                planTimeDo.add(Calendar.DAY_OF_MONTH,1);
            }else{
                break;
            }
        }
        return planTimeDo;
    }
 
    /**校验日期,避开节假日和周六日,及已经被占用的时间(审批中及审批通过的时间-天)*/
    private Calendar checkAndGetPlanTime2(Calendar planTime) throws ParseException {
        Calendar planTimeOnce = checkAndGetOnce2(planTime);
        while (true){
            Calendar planTimeTwice = checkAndGetOnce2(planTimeOnce);
            if(planTimeTwice.compareTo(planTimeOnce) == 0) {
                break;
            }else{
                planTimeOnce = planTimeTwice;
            }
        }
        return planTimeOnce;
    }
 
    private Calendar checkAndGetOnce2(Calendar planTime) throws ParseException {
        Calendar planTimeDo = Calendar.getInstance();
        planTimeDo.setTime(planTime.getTime());
        //获取未占用的最早时间
        List<Date> occupiedTime = getOccupiedTime();
        planTimeDo = getEarliestTimeNotOccupied(planTimeDo,occupiedTime);
 
        //周六日的话,就推到星期一
        int dayOfWeek = planTimeDo.get(Calendar.DAY_OF_WEEK);
        int saturday = Calendar.SATURDAY;
        int sunday = Calendar.SUNDAY;
        if(dayOfWeek == saturday){
            planTimeDo.add(Calendar.DAY_OF_MONTH,2);
        }else if(dayOfWeek == sunday){
            planTimeDo.add(Calendar.DAY_OF_MONTH,1);
        }
        //获取节假日
        int year = planTimeDo.get(Calendar.YEAR);
        List<Holidays> holidays = holidaysService.getListByYear(year);
        List<Date> holidaysDateList = holidays.stream().map(Holidays::getDay).collect(Collectors.toList());
        while (true){
            if(holidaysDateList.contains(planTimeDo.getTime())){
                planTimeDo.add(Calendar.DAY_OF_MONTH,1);
            }else{
                break;
            }
        }
        return planTimeDo;
    }
 
    private Calendar getEarliestTimeNotOccupied(Calendar planTimeDo,List<Date> occupiedTime) {
        Calendar planTimeOld = planTimeDo;
        int hour = planTimeOld.get(Calendar.HOUR_OF_DAY);
        int minute = planTimeOld.get(Calendar.MINUTE);
        int second = planTimeOld.get(Calendar.SECOND);
        // 将Calendar实例的时分秒设置为0
        planTimeDo.set(Calendar.HOUR_OF_DAY, 0);
        planTimeDo.set(Calendar.MINUTE, 0);
        planTimeDo.set(Calendar.SECOND, 0);
        while (true){
            if(occupiedTime.contains(planTimeDo.getTime())){
                planTimeDo.add(Calendar.DAY_OF_MONTH,1);
                getEarliestTimeNotOccupied(planTimeDo,occupiedTime);
            }else{
                planTimeDo.set(Calendar.HOUR_OF_DAY, hour);
                planTimeDo.set(Calendar.MINUTE, minute);
                planTimeDo.set(Calendar.SECOND, second);
                return planTimeDo;
            }
        }
    }
 
    /**申请占用的的时间-天*/
    public List<Date> getOccupiedTime() {
        List<Date> dayList = new LinkedList<>();
        List<BattDischargePlanTemp> tempList = mapper.getOccupiedTempList();
        List<Date> dateList = tempList.stream().map(BattDischargePlanTemp::getDischargeStartTime).collect(Collectors.toList());
        dateList.stream().forEach(date -> {
            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            // 将Calendar实例的时分秒设置为0
            instance.set(Calendar.HOUR_OF_DAY, 0);
            instance.set(Calendar.MINUTE, 0);
            instance.set(Calendar.SECOND, 0);
            dayList.add(instance.getTime());
        });
        return dayList;
    }
 
    /**
     * 算法方式1:可以本次安排了放电计划的班组,排序到最后,其他瞬移.
     * @param baoJiGroupListWithinBattGroupList n
     * @param planedGroupIdExistsList n
     * @return
     */
    private List<BaojiGroupBattGroup> adjustListMethod1(List<BaojiGroupBattGroup> baoJiGroupListWithinBattGroupList, List<Integer> planedGroupIdExistsList) {
        List<BaojiGroupBattGroup> planedGroupIdExistsListWithBattGroupList = new LinkedList<>();
        for (int i = baoJiGroupListWithinBattGroupList.size()-1; i >= 0; i--) {
            Integer baojiGroupId = baoJiGroupListWithinBattGroupList.get(i).getBaojiGroupId();
            if (planedGroupIdExistsList.contains(baojiGroupId)){
                planedGroupIdExistsListWithBattGroupList.add(baoJiGroupListWithinBattGroupList.get(i));
                baoJiGroupListWithinBattGroupList.remove(i);
            }
        }
        for (int i = planedGroupIdExistsListWithBattGroupList.size()-1; i >= 0; i--) {
            baoJiGroupListWithinBattGroupList.add(planedGroupIdExistsListWithBattGroupList.get(i));
        }
        return baoJiGroupListWithinBattGroupList;
    }
 
    /**
     * 算法方式2:滚动班组顺序,保证扁平.原第一顺位,变为最后顺位
     * @param baoJiGroupListWithinBattGroupList 要调整顺序的列表
     * @return
     */
    private List<BaojiGroupBattGroup> adjustListMethod2(List<BaojiGroupBattGroup> baoJiGroupListWithinBattGroupList) {
        if (baoJiGroupListWithinBattGroupList.size() > 1) { //班组数大于1组时滚动逻辑才有意义
            List<BaojiGroupBattGroup> newSequenceList = new LinkedList<>();
            for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
                if (i != baoJiGroupListWithinBattGroupList.size() - 1) {
                    newSequenceList.add(baoJiGroupListWithinBattGroupList.get(i + 1));
                } else {
                    newSequenceList.add(baoJiGroupListWithinBattGroupList.get(0));
                }
            }
            baoJiGroupListWithinBattGroupList = newSequenceList;
        }
        return baoJiGroupListWithinBattGroupList;
    }
 
    /**
     * 根据逻辑获取一天的
     * 模型1:
     *         2组2非节(包含本组情况,只是包含本组情况优先级低)
     *         1组2非节(包含本组情况,只是包含本组情况优先级低)
     * 1组1节  1组1非节(包含本组情况,只是包含本组情况优先级低)
     *         0非节
     *
     * 模型2:
     *
     *         3组3非节
     *         2组3非节
     * 0组1节  1组3非节
     *         2组2非节
     *         1组2非节
     *         1组1非节
     *
     *
     *
     * @param baoJiGroupListWithinBattGroupList
     * @param planTime
     * @return
     */
    private List<BattdischargePlan> getDischargePlanList(List<BaojiGroupBattGroup> baoJiGroupListWithinBattGroupList, Calendar planTime) {
        List<BattdischargePlan> planList = new LinkedList<>();
        int nodeStationExists = 0;
        for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
            //一条记录就是一组,组内包含了所有性质的站点
            //先确定是否存在节点,存在则加入到计划. 没有则执行没有节点的模型
            BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(i);
            List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
            for (int j = 0; j < battStationList.size(); j++) {
                Battinf battinf = battStationList.get(j);
                if(battinf.getNodeStation()==1){
                    //找到节点
                    BattdischargePlan plan = new BattdischargePlan();
                    plan.setBattgroupid(battinf.getBattGroupId());
                    plan.setBattGroupName(battinf.getBattGroupName());
                    plan.setNodeStation(1);
                    plan.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                    plan.setNote(String.valueOf(i));//记录计划内的组号对应的索引
                    planList.add(plan);
                    nodeStationExists = 1;
                    //已经加入到计划组的,从原纪录中剔除
                    battStationList.remove(j);
                    baoJiGroupListWithinBattGroupList.get(i).setBattGroupList(battStationList);
                    break; //当前组当前排站点级别降低,只要其他组能排,不会再选择当前组.
                }
            }
            //判断是否找到节点,找到节点则退出此次遍历
            if(nodeStationExists == 1){
                break;
            }
        }
 
        //判断是否找到节点,找到则执行模型1
        if(nodeStationExists == 1){
            //节点在的组优先级降低
            Integer planedBaoJiGroupId = planList.get(0).getNum();
            //模型1-1是否成立
            for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
                BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(i);
                Integer baojiGroupId = groupWithBattStationList.getBaojiGroupId();
                if(planedBaoJiGroupId.equals(baojiGroupId)){
                    continue; //先看能否满足2组2非节(非本组)
                }
                List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                for (int j = 0; j < battStationList.size(); j++) {
                    Battinf battinf = battStationList.get(j);
                    if(battinf.getNodeStation()==0){
                        BattdischargePlan plan = new BattdischargePlan();
                        plan.setBattgroupid(battinf.getBattGroupId());
                        plan.setBattGroupName(battinf.getBattGroupName());
                        plan.setNodeStation(0);
                        plan.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                        plan.setNote(String.valueOf(i));//记录计划内的组号对应的索引
                        planList.add(plan);
                        //已经加入到计划组的,从原纪录中剔除
                        battStationList.remove(j);
                        baoJiGroupListWithinBattGroupList.get(i).setBattGroupList(battStationList);
                        break; //当前组当前排站点级别降低,只要其他组能排,不会再选择当前组.
                    }
                }
                //遍历过程中,查看计划是否满足3个站点,已经满足则提前结束计划分配
                if(planList.size()==3){
                    break;
                }
            }
            //遍历完毕,查看计划是否满足3个站点
            //如果不满足,则把已排组的站点继续允许分配,优先遍历非节点站所在组
            //如果还不满足,则继续分配,允许遍历节点站所在组
            int size = planList.size();
            if(size!=3){
                //如果计划站点数是2,说明计划1为节点站,计划2为非节点站,优先计划2所在非节点站所在组去分配
                int notNodeStationExists = 0;
                if(size==2){
                    int indexNotNode = Integer.parseInt(planList.get(1).getNote());
                    BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(indexNotNode);
                    List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                    for (int j = 0; j < battStationList.size(); j++) {
                        Battinf battinf = battStationList.get(j);
                        if(battinf.getNodeStation()==0){
                            BattdischargePlan plan = new BattdischargePlan();
                            plan.setBattgroupid(battinf.getBattGroupId());
                            plan.setBattGroupName(battinf.getBattGroupName());
                            plan.setNodeStation(0);
                            plan.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                            planList.add(plan);
                            notNodeStationExists = 1;
                            //已经加入到计划组的,从原纪录中剔除
                            battStationList.remove(j);
                            baoJiGroupListWithinBattGroupList.get(indexNotNode).setBattGroupList(battStationList);
                            break;//找到可分配的,终止
                        }
                    }
                    if(notNodeStationExists != 1){ //在非节点站所在组没找到其他非节点,只能从节点站所在组去分配
                        int indexNode = Integer.parseInt(planList.get(0).getNote());
                        BaojiGroupBattGroup groupHavingNodeWithBattStationList = baoJiGroupListWithinBattGroupList.get(indexNode);
                        List<Battinf> battStationList2 = groupHavingNodeWithBattStationList.getBattGroupList();
                        for (int j = 0; j < battStationList2.size(); j++) {
                            Battinf battinf = battStationList2.get(j);
                            if(battinf.getNodeStation()==0){
                                BattdischargePlan plan = new BattdischargePlan();
                                plan.setBattgroupid(battinf.getBattGroupId());
                                plan.setBattGroupName(battinf.getBattGroupName());
                                plan.setNodeStation(0);
                                plan.setNum(groupHavingNodeWithBattStationList.getBaojiGroupId());//记录计划内的组号
                                planList.add(plan);
                                //已经加入到计划组的,从原纪录中剔除
                                battStationList.remove(j);
                                baoJiGroupListWithinBattGroupList.get(indexNotNode).setBattGroupList(battStationList);
                                break;//找到可分配的,终止
                            }
                        }
                    }
                }
                //如果计划站点数是1,说明只有1个节点站,继续从节点站所在组去分配
                else{
                    int indexNode = Integer.parseInt(planList.get(0).getNote());
                    BaojiGroupBattGroup groupHavingNodeWithBattStationList = baoJiGroupListWithinBattGroupList.get(indexNode);
                    List<Battinf> battStationList2 = groupHavingNodeWithBattStationList.getBattGroupList();
                    List<Integer> indexRemoveList = new LinkedList<>();
                    for (int j = 0; j < battStationList2.size(); j++) {
                        Battinf battinf = battStationList2.get(j);
                        if(battinf.getNodeStation()==0){
                            BattdischargePlan plan = new BattdischargePlan();
                            plan.setBattgroupid(battinf.getBattGroupId());
                            plan.setBattGroupName(battinf.getBattGroupName());
                            plan.setNodeStation(0);
                            plan.setNum(groupHavingNodeWithBattStationList.getBaojiGroupId());//记录计划内的组号
                            planList.add(plan);
                            indexRemoveList.add(j);
                            if(planList.size()==3) {
                                break;//找到可分配的,终止
                            }
                        }
                    }
                    for (int i = indexRemoveList.size()-1; i >=0; i--) {
                        //已经加入到计划组的,从原纪录中剔除
                        battStationList2.remove(indexRemoveList.get(i).intValue());
                        //baoJiGroupListWithinBattGroupList.get(indexNode).setBattGroupList(battStationList2);
                    }
                }
            }
        }
        //未找到节点,执行模型2
        else{
            for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
                BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(i);
                List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                List<Integer> indexRemoveList = new LinkedList<>();
                for (int j = 0; j < battStationList.size(); j++) {
                    Battinf battinf = battStationList.get(j);
                    BattdischargePlan plan = new BattdischargePlan();
                    plan.setBattgroupid(battinf.getBattGroupId());
                    plan.setBattGroupName(battinf.getBattGroupName());
                    plan.setNodeStation(0);
                    plan.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                    plan.setNote(String.valueOf(i));//记录计划内的组号对应的索引
                    planList.add(plan);
                    indexRemoveList.add(j);
                    if(planList.size()==3) {
                        break;//找到可分配的,终止
                    }
                }
                for (int i1 = indexRemoveList.size()-1; i1 >=0; i1--) {
                    //已经加入到计划组的,从原纪录中剔除
                    battStationList.remove(indexRemoveList.get(i1).intValue());
                    //baoJiGroupListWithinBattGroupList.get(i).setBattGroupList(battStationList);
                }
            }
            //遍历完毕,查看计划是否满足3个站点
            //如果不满足,则把已排组的站点继续允许分配,遍历非节点站所在组
            //如果存在1组,说明后续也只能从这1组获取,反复遍历;如果存在2组,那再遍历完一次后无需再遍历,因为如果存在则已经满足要求
            int size = planList.size();
            if(size!=3) {
                if(size==1) { //存在1组,那说明后续也只能从这1组中获取
                    for (int i = 0; i < planList.size(); i++) {
                        BattdischargePlan existsPlan = planList.get(i);
                        int index = Integer.parseInt(existsPlan.getNote());
                        BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(index);
                        List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                        List<Integer> indexRemoveList = new LinkedList<>();
                        for (int j = 0; j < battStationList.size(); j++) {
                            Battinf battinf = battStationList.get(j);
                            if (battinf.getNodeStation() == 0) {
                                BattdischargePlan plan = new BattdischargePlan();
                                plan.setBattgroupid(battinf.getBattGroupId());
                                plan.setBattGroupName(battinf.getBattGroupName());
                                plan.setNodeStation(0);
                                plan.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                                indexRemoveList.add(j);
                                planList.add(plan);
                                if(planList.size()==3){
                                    break;//找到可分配的,终止
                                }
                            }
                        }
                        //已经加入到计划组的,从原纪录中剔除
                        for (int i1 = indexRemoveList.size()-1; i1 >=0; i1--) {
                            battStationList.remove(indexRemoveList.get(i1).intValue());
                            baoJiGroupListWithinBattGroupList.get(index).setBattGroupList(battStationList);
                        }
                    }
                }else{ //已经存在2组
                    for (int i = 0; i < planList.size(); i++) {
                        BattdischargePlan existsPlan = planList.get(i);
                        BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(Integer.parseInt(existsPlan.getNote()));
                        List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                        for (int j = 0; j < battStationList.size(); j++) {
                            Battinf battinf = battStationList.get(j);
                            if (battinf.getNodeStation() == 0) {
                                BattdischargePlan plan = new BattdischargePlan();
                                plan.setBattgroupid(battinf.getBattGroupId());
                                plan.setBattGroupName(battinf.getBattGroupName());
                                plan.setNodeStation(0);
                                plan.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                                planList.add(plan);
                                //已经加入到计划组的,从原纪录中剔除
                                battStationList.remove(j);
                                baoJiGroupListWithinBattGroupList.get(Integer.parseInt(existsPlan.getNote())).setBattGroupList(battStationList);
                                break;//找到可分配的,终止
                            }
                        }
                    }
                    //完结
                }
            }
        }
 
        return planList;
    }
 
    private List<Battinf> getDischargePlanList2(List<BaojiGroupBattGroup> baoJiGroupListWithinBattGroupList, Calendar planTime) {
        List<Battinf> planList = new LinkedList<>();
        int nodeStationExists = 0;
        for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
            //一条记录就是一组,组内包含了所有性质的站点
            //先确定是否存在节点,存在则加入到计划. 没有则执行没有节点的模型
            BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(i);
            List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
            for (int j = 0; j < battStationList.size(); j++) {
                Battinf battinf = battStationList.get(j);
                if(battinf.getNodeStation()==1){
                    //找到节点
                    battinf.setDischargeStartTime(planTime.getTime());;
                    battinf.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                    battinf.setInstallUser(groupWithBattStationList.getBaojiGroupName());
                    battinf.setNote(String.valueOf(i));//记录计划内的组号对应的索引
                    planList.add(battinf);
                    nodeStationExists = 1;
                    //已经加入到计划组的,从原纪录中剔除
                    battStationList.remove(j);
                    baoJiGroupListWithinBattGroupList.get(i).setBattGroupList(battStationList);
                    break; //当前组当前排站点级别降低,只要其他组能排,不会再选择当前组.
                }
            }
            //判断是否找到节点,找到节点则退出此次遍历
            if(nodeStationExists == 1){
                break;
            }
        }
 
        //判断是否找到节点,找到则执行模型1
        if(nodeStationExists == 1){
            //节点在的组优先级降低
            Integer planedBaoJiGroupId = planList.get(0).getNum();
            //模型1-1是否成立
            for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
                BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(i);
                Integer baojiGroupId = groupWithBattStationList.getBaojiGroupId();
                if(planedBaoJiGroupId.equals(baojiGroupId)){
                    continue; //先看能否满足2组2非节(非本组)
                }
                List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                for (int j = 0; j < battStationList.size(); j++) {
                    Battinf battinf = battStationList.get(j);
                    if(battinf.getNodeStation()==0){
                        battinf.setDischargeStartTime(planTime.getTime());;
                        battinf.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                        battinf.setInstallUser(groupWithBattStationList.getBaojiGroupName());
                        battinf.setNote(String.valueOf(i));//记录计划内的组号对应的索引
                        planList.add(battinf);
                        //已经加入到计划组的,从原纪录中剔除
                        battStationList.remove(j);
                        baoJiGroupListWithinBattGroupList.get(i).setBattGroupList(battStationList);
                        break; //当前组当前排站点级别降低,只要其他组能排,不会再选择当前组.
                    }
                }
                //遍历过程中,查看计划是否满足3个站点,已经满足则提前结束计划分配
                if(planList.size()==3){
                    break;
                }
            }
            //遍历完毕,查看计划是否满足3个站点
            //如果不满足,则把已排组的站点继续允许分配,优先遍历非节点站所在组
            //如果还不满足,则继续分配,允许遍历节点站所在组
            int size = planList.size();
            if(size!=3){
                //如果计划站点数是2,说明计划1为节点站,计划2为非节点站,优先计划2所在非节点站所在组去分配
                int notNodeStationExists = 0;
                if(size==2){
                    int indexNotNode = Integer.parseInt(planList.get(1).getNote());
                    BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(indexNotNode);
                    List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                    for (int j = 0; j < battStationList.size(); j++) {
                        Battinf battinf = battStationList.get(j);
                        if(battinf.getNodeStation()==0){
                            battinf.setDischargeStartTime(planTime.getTime());;
                            battinf.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                            battinf.setInstallUser(groupWithBattStationList.getBaojiGroupName());
                            planList.add(battinf);
                            notNodeStationExists = 1;
                            //已经加入到计划组的,从原纪录中剔除
                            battStationList.remove(j);
                            baoJiGroupListWithinBattGroupList.get(indexNotNode).setBattGroupList(battStationList);
                            break;//找到可分配的,终止
                        }
                    }
                    if(notNodeStationExists != 1){ //在非节点站所在组没找到其他非节点,只能从节点站所在组去分配
                        int indexNode = Integer.parseInt(planList.get(0).getNote());
                        BaojiGroupBattGroup groupHavingNodeWithBattStationList = baoJiGroupListWithinBattGroupList.get(indexNode);
                        List<Battinf> battStationList2 = groupHavingNodeWithBattStationList.getBattGroupList();
                        for (int j = 0; j < battStationList2.size(); j++) {
                            Battinf battinf = battStationList2.get(j);
                            if(battinf.getNodeStation()==0){
                                battinf.setDischargeStartTime(planTime.getTime());;
                                battinf.setNum(groupHavingNodeWithBattStationList.getBaojiGroupId());//记录计划内的组号
                                battinf.setInstallUser(groupHavingNodeWithBattStationList.getBaojiGroupName());
                                planList.add(battinf);
                                //已经加入到计划组的,从原纪录中剔除
                                battStationList.remove(j);
                                baoJiGroupListWithinBattGroupList.get(indexNotNode).setBattGroupList(battStationList);
                                break;//找到可分配的,终止
                            }
                        }
                    }
                }
                //如果计划站点数是1,说明只有1个节点站,继续从节点站所在组去分配
                else{
                    int indexNode = Integer.parseInt(planList.get(0).getNote());
                    BaojiGroupBattGroup groupHavingNodeWithBattStationList = baoJiGroupListWithinBattGroupList.get(indexNode);
                    List<Battinf> battStationList2 = groupHavingNodeWithBattStationList.getBattGroupList();
                    List<Integer> indexRemoveList = new LinkedList<>();
                    for (int j = 0; j < battStationList2.size(); j++) {
                        Battinf battinf = battStationList2.get(j);
                        if(battinf.getNodeStation()==0){
                            battinf.setDischargeStartTime(planTime.getTime());;
                            battinf.setNum(groupHavingNodeWithBattStationList.getBaojiGroupId());//记录计划内的组号
                            battinf.setInstallUser(groupHavingNodeWithBattStationList.getBaojiGroupName());
                            planList.add(battinf);
                            indexRemoveList.add(j);
                            if(planList.size()==3) {
                                break;//找到可分配的,终止
                            }
                        }
                    }
                    for (int i = indexRemoveList.size()-1; i >=0; i--) {
                        //已经加入到计划组的,从原纪录中剔除
                        battStationList2.remove(indexRemoveList.get(i).intValue());
                        //baoJiGroupListWithinBattGroupList.get(indexNode).setBattGroupList(battStationList2);
                    }
                }
            }
        }
        //未找到节点,执行模型2
        else{
            for (int i = 0; i < baoJiGroupListWithinBattGroupList.size(); i++) {
                BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(i);
                List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                List<Integer> indexRemoveList = new LinkedList<>();
                for (int j = 0; j < battStationList.size(); j++) {
                    Battinf battinf = battStationList.get(j);
                    battinf.setDischargeStartTime(planTime.getTime());;
                    battinf.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                    battinf.setInstallUser(groupWithBattStationList.getBaojiGroupName());
                    battinf.setNote(String.valueOf(i));//记录计划内的组号对应的索引
                    planList.add(battinf);
                    indexRemoveList.add(j);
 
                    break;//找到可分配的,终止
                }
                for (int i1 = indexRemoveList.size()-1; i1 >=0; i1--) {
                    //已经加入到计划组的,从原纪录中剔除
                    battStationList.remove(indexRemoveList.get(i1).intValue());
                    //baoJiGroupListWithinBattGroupList.get(i).setBattGroupList(battStationList);
                }
                if(planList.size()==3){
                    break;
                }
            }
            //遍历完毕,查看计划是否满足3个站点
            //如果不满足,则把已排组的站点继续允许分配,遍历非节点站所在组
            //如果存在1组,说明后续也只能从这1组获取,反复遍历;如果存在2组,那再遍历完一次后无需再遍历,因为如果存在则已经满足要求
            int size = planList.size();
            if(size!=3) {
                if(size==1) { //存在1组,那说明后续也只能从这1组中获取
                    Battinf existsPlan = planList.get(0);
                    int index = Integer.parseInt(existsPlan.getNote());
                    BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(index);
                    List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                    List<Integer> indexRemoveList = new LinkedList<>();
                    for (int j = 0; j < battStationList.size(); j++) {
                        Battinf battinf = battStationList.get(j);
                        if (battinf.getNodeStation() == 0) {
                            battinf.setDischargeStartTime(planTime.getTime());;
                            battinf.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                            battinf.setInstallUser(groupWithBattStationList.getBaojiGroupName());
                            planList.add(battinf);
                            indexRemoveList.add(j);
                            if(planList.size()==3){
                                break;//找到可分配的,终止
                            }
                        }
                    }
                    //已经加入到计划组的,从原纪录中剔除
                    for (int i1 = indexRemoveList.size()-1; i1 >=0; i1--) {
                        battStationList.remove(indexRemoveList.get(i1).intValue());
                        baoJiGroupListWithinBattGroupList.get(index).setBattGroupList(battStationList);
                    }
 
                }else{ //已经存在2组
                    for (int i = 0; i < planList.size(); i++) {
                        Battinf existsPlan = planList.get(i);
                        BaojiGroupBattGroup groupWithBattStationList = baoJiGroupListWithinBattGroupList.get(Integer.parseInt(existsPlan.getNote()));
                        List<Battinf> battStationList = groupWithBattStationList.getBattGroupList();
                        for (int j = 0; j < battStationList.size(); j++) {
                            Battinf battinf = battStationList.get(j);
                            if (battinf.getNodeStation() == 0) {
                                battinf.setDischargeStartTime(planTime.getTime());;
                                battinf.setNum(groupWithBattStationList.getBaojiGroupId());//记录计划内的组号
                                battinf.setInstallUser(groupWithBattStationList.getBaojiGroupName());
                                planList.add(battinf);
                                //已经加入到计划组的,从原纪录中剔除
                                battStationList.remove(j);
                                baoJiGroupListWithinBattGroupList.get(Integer.parseInt(existsPlan.getNote())).setBattGroupList(battStationList);
                                break;//找到可分配的,终止
                            }
                        }
                        if(planList.size()==3){
                            break;
                        }
                    }
                    //完结
                }
            }
        }
 
        return planList;
    }
 
    /**
     * 弃用,里面approve_status数据库不存在,已经无法使用
     * @param startTimeStr
     * @return
     * @throws ParseException
     */
    @Transactional
    public Response deleteDischargePlanTemp(String startTimeStr) throws ParseException {
       // Date startTime = DateUtil.YYYY_MM_DD_HH_MM_SS.parse(startTimeStr);
        Date startTime = ThreadLocalUtil.parse(startTimeStr,1);
        Calendar planTime = Calendar.getInstance();
        planTime.setTime(startTime);
        //一年只会有一个计划,删除年度的计划. 审核通过的不能删除
        int planYear = planTime.get(Calendar.YEAR);
        UpdateWrapper<BattDischargePlanTemp> update = Wrappers.update();
        update.eq("Year(discharge_start_time)",planYear).ne("approve_status",1);
        int deleteCount = mapper.delete(update);
        if(deleteCount>0){
            return new Response().set(1,true, "删除完成");
        }else {
            return new Response().set(1,false, "删除的数据已审核通过,无法删除");
        }
    }
 
    @Transactional
    public Response deleteDischargePlanTemp2(String startTimeStr) throws ParseException {
        int userId = ActionUtil.getUser().getUId().intValue();
        //Date startTime = DateUtil.YYYY_MM_DD_HH_MM_SS.parse(startTimeStr);
        Date startTime = ThreadLocalUtil.parse(startTimeStr,1);
        Calendar planTime = Calendar.getInstance();
        planTime.setTime(startTime);
        //一年只会有一个计划,删除年度的计划. 审核通过的不能删除
        int planYear = planTime.get(Calendar.YEAR);
        UpdateWrapper<BattDischargePlanTemp> update = Wrappers.update();
        update.eq("Year(discharge_start_time)",planYear).eq("create_user_id",userId).eq("submit_status",0);
        int deleteCount = mapper.delete(update);
        if(deleteCount>0){
            return new Response().set(1,true, "删除完成");
        }else {
            return new Response().set(1,false, "不存在可以删除的数据,无法删除");
        }
    }
 
    @Transactional
    public Response updateListAndSubmit(List<BattDischargePlanTemp> list, String taskDesc) {
        int userId = ActionUtil.getUser().getUId().intValue();
        Date now = new Date();
        //1.提交到单据审批流程
        WorkflowMain main = new WorkflowMain();
        String orderId = mainService.getNextOrderId("FDJH");
        //String title = "放电计划审批单-"+DateUtil.YYYY_MM_DD_HH_MM_SS.format(now);
        String title = "放电计划审批单-"+ThreadLocalUtil.format(now,1);
        Integer mainStatus = WorkflowEnum.MAIN_STATUS_DEALING.getValue();
        Integer mainType = WorkflowEnum.MAIN_TYPE_DISCHARGE_PLAN.getValue();
        main.setOrderId(orderId);
        main.setTitle(title);
        main.setTaskDesc(taskDesc);
        main.setCreateUserId(userId);
        main.setCreateTime(now);
        main.setBeginTime(now);
        main.setStatus(mainStatus);
        main.setType(mainType);
        mainService.add(main);
        //内存中去除已插入数据库的单号
        ServletContext application = ActionUtil.getApplication();
        List<String> orderIdList = (List<String>) application.getAttribute("orderIdList");
        //校验是否去除
        boolean remove = orderIdList.remove(orderId);
        if(!remove){
            System.err.println("没有去除掉!!!!!!!!!!!!!");
        }
        application.setAttribute("orderIdList",orderIdList);
 
        //查询包机组对应的班组长,并在单据节点表添加记录
        Set<Long> baoJiGroupIds = list.stream().collect(Collectors.groupingBy(BattDischargePlanTemp::getGroupId)).keySet();
        List<UserInf> userInfList = baoJiGroupUserService.getUserListByRole(baoJiGroupIds,1);
        List<WorkflowLink> links = new LinkedList<>();
        for (UserInf userInf : userInfList) {
            WorkflowLink link = new WorkflowLink();
            link.setMainId(main.getId());
            link.setParentId(0);
            link.setProcessLevel(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevel());
            link.setProcessLevelName(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevelName());
            link.setCreateTime(now);
            link.setDealUserId(userInf.getUId().intValue());
            link.setDealType(WorkflowEnum.TYPE_DELIVER.getValue());
            link.setDealDesc(taskDesc);
            link.setStatus(WorkflowEnum.STATUS_DEALING.getValue());
 
            links.add(link);
        }
        linkService.addBatch(links);
 
        //2.更新临时表放电计划数据
        list.forEach(item-> {
            item.setCreateTime(now);
            item.setMainId(main.getId());
        });
        updateList(list);
 
        return new Response().setII(1,"提交完成");
    }
 
    @Transactional
    public Response updateListAndSubmit2(List<BattDischargePlanTemp> list, String taskDesc) {
        int userId = ActionUtil.getUser().getUId().intValue();
        Date now = new Date();
        //1.提交到单据审批流程
        WorkflowMain main = new WorkflowMain();
        String orderId = mainService.getNextOrderId("FDJH");
        //String title = "放电计划审批单-"+DateUtil.YYYY_MM_DD_HH_MM_SS.format(now);
        String title = "放电计划审批单-"+ThreadLocalUtil.format(now,1);
        Integer mainStatus = WorkflowEnum.MAIN_STATUS_DEALING.getValue();
        Integer mainType = WorkflowEnum.MAIN_TYPE_DISCHARGE_PLAN.getValue();
        main.setOrderId(orderId);
        main.setTitle(title);
        main.setTaskDesc(taskDesc);
        main.setCreateUserId(userId);
        main.setCreateTime(now);
        main.setBeginTime(now);
        main.setStatus(mainStatus);
        main.setType(mainType);
        mainService.add(main);
        //内存中去除已插入数据库的单号
        ServletContext application = ActionUtil.getApplication();
        List<String> orderIdList = (List<String>) application.getAttribute("orderIdList");
        //校验是否去除
        boolean remove = orderIdList.remove(orderId);
        if(!remove){
            System.err.println("没有去除掉!!!!!!!!!!!!!");
        }
        application.setAttribute("orderIdList",orderIdList);
 
        //在单据节点表添加记录
 
 
        WorkflowLink link = new WorkflowLink();
        link.setMainId(main.getId());
        link.setParentId(0);
        link.setProcessLevel(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevel());
        link.setProcessLevelName(WorkflowDischargePlanEnum.PROCESS_MANAGER1SIGN.getProcessLevelName());
        link.setCreateTime(now);
        link.setDealRoleId(RoleEnum.ADMIN.getId());
        link.setDealType(WorkflowEnum.TYPE_DELIVER.getValue());
        link.setDealDesc(taskDesc);
        link.setStatus(WorkflowEnum.STATUS_TAKING.getValue());
 
        linkService.add(link);
 
        //2.更新临时表放电计划数据
        list.forEach(item-> {
            item.setCreateTime(now);
            item.setMainId(main.getId());
        });
        updateList(list);
 
        return new Response().setII(1,"提交完成");
    }
 
    public void updateList(List<BattDischargePlanTemp> list) {
        //化整为零,避免sql拼接太长导致无法执行
        int step = 20;
        for (int i = 0; i < list.size(); i = i+step) {
            if(i+step<list.size()){
                mapper.updateList(list.subList(i, i + step));
            }else {
                mapper.updateList(list.subList(i, list.size()));
            }
        }
    }
 
 
    /**
     * 临时表审批状态变为已审批
     *
     * @param approveStatus
     * @param approveReason
     * @param tempList
     * @return
     */
    @Transactional
    public Response updateApproveDischargePlan(int approveStatus, String approveReason, List<BattDischargePlanTemp> tempList) {
        UserInf user = ActionUtil.getUser();
        BattDischargePlanTemp temp = tempList.get(0);
        Date createTime = temp.getCreateTime();
        Integer createUserId = temp.getCreateUserId();
        updateApproveStatus(approveStatus,approveReason,createTime,createUserId,user.getUId().intValue());
 
        if(approveStatus==1) {
            planService.insertConfirmDischargePlan(tempList);
        }
        return new Response().setII(1,"审核完成");
    }
 
 
    public void updateApproveStatus(int approveStatus, String approveReason, Date createTime, Integer createUserId, int approveUserId) {
        UpdateWrapper<BattDischargePlanTemp> update = Wrappers.update();
        update.set("approve_status",approveStatus).set("approve_reason",approveReason).set("approve_time",new Date()).set("approve_user_id",approveUserId)
                .eq("create_time",createTime).eq("create_user_id",createUserId);
        mapper.update((BattDischargePlanTemp) ActionUtil.objeNull,update);
    }
 
    public Response deleteNotSubmitted() {
        UserInf user = ActionUtil.getUser();
        UpdateWrapper<BattDischargePlanTemp> update = Wrappers.update();
        update.eq("submit_status",0).eq("create_user_id",user.getUId());
        mapper.delete(update);
        return new Response().set(1,"删除完成");
    }
 
    public List<BattDischargePlanTemp> getListByMainId(Integer mainId) {
        QueryWrapper<BattDischargePlanTemp> query = Wrappers.query();
        query.eq("main_id",mainId);
        return mapper.selectList(query);
    }
}