yangys
2024-03-28 23a939ed820ee32f9a4309f9c81b7bab5a566f30
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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
package com.qianwen.smartman.modules.cps.service.impl;
 
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import java.lang.invoke.SerializedLambda;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.smartman.common.cache.ParamCache;
import com.qianwen.smartman.common.cache.RegionCache;
import com.qianwen.smartman.common.cache.cps.TimeSliceCache;
import com.qianwen.smartman.common.constant.CalendarConstant;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.DateConstant;
import com.qianwen.smartman.common.constant.ProductionTimeConstant;
import com.qianwen.smartman.common.utils.CommonUtil;
import com.qianwen.smartman.common.utils.LocalDateTimeUtils;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.redis.cache.BladeRedis;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.utils.CollectionUtil;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.modules.cps.convert.ProductionCalendarConvert;
import com.qianwen.smartman.modules.cps.dto.CacheBuildDTO;
import com.qianwen.smartman.modules.cps.dto.CalendarCacheDTO;
import com.qianwen.smartman.modules.cps.dto.CalendarDateDTO;
import com.qianwen.smartman.modules.cps.dto.CalendarShiftDTO;
import com.qianwen.smartman.modules.cps.dto.CalendarShiftDetailDTO;
import com.qianwen.smartman.modules.cps.dto.CalendarShiftTimeSlicesDTO;
import com.qianwen.smartman.modules.cps.dto.CurShiftDTO;
import com.qianwen.smartman.modules.cps.dto.FactoryDayInfoDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftCacheDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftDetailInfoDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftIndexInfoDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftInfoDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftSlicesCalendarCodeDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftSlicesClientCalendarCodesDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftSlicesClientDTO;
import com.qianwen.smartman.modules.cps.dto.ShiftSlicesDTO;
import com.qianwen.smartman.modules.cps.dto.TimestampToProductionTimeCacheDto;
import com.qianwen.smartman.modules.cps.entity.ProductionCalendar;
import com.qianwen.smartman.modules.cps.entity.ProductionCalendarDay;
import com.qianwen.smartman.modules.cps.entity.ProductionCalendarDaytime;
import com.qianwen.smartman.modules.cps.entity.Workstation;
import com.qianwen.smartman.modules.cps.enums.WorkstationTypeEnum;
import com.qianwen.smartman.modules.cps.mapper.CalendarMapper;
import com.qianwen.smartman.modules.cps.service.ICalendarDayService;
import com.qianwen.smartman.modules.cps.service.ICalendarDaytimeService;
import com.qianwen.smartman.modules.cps.service.ICalendarService;
import com.qianwen.smartman.modules.cps.service.IShiftModelService;
import com.qianwen.smartman.modules.cps.service.IWorkstationService;
import com.qianwen.smartman.modules.cps.vo.CalendarAssociateWorkstationVO;
import com.qianwen.smartman.modules.cps.vo.CalendarCopyVO;
import com.qianwen.smartman.modules.cps.vo.CalendarDayVO;
import com.qianwen.smartman.modules.cps.vo.CalendarSaveVO;
import com.qianwen.smartman.modules.cps.vo.CalendarSimpleVO;
import com.qianwen.smartman.modules.cps.vo.CalendarUpdateVO;
import com.qianwen.smartman.modules.cps.vo.CalendarVO;
import com.qianwen.smartman.modules.cps.vo.ShiftDetailVO;
import com.qianwen.smartman.modules.cps.vo.ShiftIndexNameVO;
import com.qianwen.smartman.modules.cps.vo.ShiftRestTimeVO;
import com.qianwen.smartman.modules.cps.vo.ShiftTimeDetailVO;
import com.qianwen.smartman.modules.cps.vo.ShiftVO;
import com.qianwen.smartman.modules.mdc.dto.ShiftIndexNameDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
 
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/cps/service/impl/CalendarServiceImpl.class */
public class CalendarServiceImpl extends ServiceImpl<CalendarMapper, ProductionCalendar> implements ICalendarService {
    private static final Logger log = LoggerFactory.getLogger(CalendarServiceImpl.class);
    @Autowired
    @Lazy
    private IWorkstationService workstationService;
    @Autowired
    private IShiftModelService shiftModelService;
    @Autowired
    private ICalendarDayService calendarDayService;
    @Autowired
    private ICalendarDaytimeService calendarDaytimeService;
    @Autowired
    private BladeRedis bladeRedis;
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    @Transactional(rollbackFor = {Exception.class})
    public ProductionCalendar saveCalendar(CalendarSaveVO calendarSaveVO) {
        checkCalendar(calendarSaveVO, AuthUtil.getTenantId());
        ProductionCalendar productionCalendar = ProductionCalendarConvert.INSTANCE.conver(calendarSaveVO);
        save(productionCalendar);
        saveCalendarDayTime(calendarSaveVO.getDateDTOList(), productionCalendar);
        return productionCalendar;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public ProductionCalendar saveYearCalendar(CalendarSaveVO calendarSaveVO) {
        checkCalendar(calendarSaveVO, AuthUtil.getTenantId());
        ProductionCalendar productionCalendar = new ProductionCalendar();
        BeanUtils.copyProperties(calendarSaveVO, productionCalendar);
        save(productionCalendar);
        saveYearCalendarDayTime(calendarSaveVO.getDateDTOList(), productionCalendar);
        return productionCalendar;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public ProductionCalendar updateCalendar(CalendarUpdateVO calendarUpdateVO) {
        this.calendarDayService.deleteByCalendarId(calendarUpdateVO.getId());
        this.calendarDaytimeService.deleteByCalendarId(calendarUpdateVO.getId());
        ProductionCalendar calendar = (ProductionCalendar) getById(calendarUpdateVO.getId());
        updateCalendarDayTime(calendarUpdateVO.getDateDTOList(), calendar);
        return calendar;
    }
 
    private void saveCalendarDayTime(List<CalendarDateDTO> dateDTOList, ProductionCalendar productionCalendar) {
        LocalDate today = LocalDate.now();
        Integer thisYear = Integer.valueOf(today.getYear());
        Integer year = productionCalendar.getYear();
        LocalDate queryStartDay = LocalDate.of(year.intValue(), 1, 1);
        Long calendarId = productionCalendar.getId();
        Set<LocalDate> dateList = (Set) dateDTOList.stream().filter(c -> {
            return !Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId());
        }).map((v0) -> {
            return v0.getCalendarDate();
        }).collect(Collectors.toSet());
        Set<LocalDate> offDay = (Set) dateDTOList.stream().filter(c2 -> {
            return !Func.isNull(c2.getOffDayId());
        }).map((v0) -> {
            return v0.getCalendarDate();
        }).collect(Collectors.toSet());
        List<ProductionCalendarDaytime> productionCalendarDaytimeList = new ArrayList<>();
        List<ProductionCalendarDay> productionCalendarDayList = new ArrayList<>();
        List<Long> modelIds = (List) dateDTOList.stream().map((v0) -> {
            return v0.getModelId();
        }).distinct().collect(Collectors.toList());
        Map<Long, ShiftVO> shiftDetailMap = this.shiftModelService.getShiftDetail(modelIds);
        Snowflake snowflake = IdUtil.createSnowflake(1L, 1L);
        List<ProductionCalendarDaytime> curProductionCalendarDayTimeList = new ArrayList<>();
        LocalDate lastDayOfYear = LocalDateTimeUtils.getLastDayOfYear(queryStartDay);
        long difference = year.equals(thisYear) ? LocalDateTimeUtils.getDifference(today, lastDayOfYear).intValue() : LocalDateTimeUtils.getDayOfYear(year).intValue() - 1;
        long j = 0;
        while (true) {
            long i = j;
            if (i <= difference) {
                LocalDate needHandleDate = year.equals(thisYear) ? today.plus(difference - i, (TemporalUnit) ChronoUnit.DAYS) : queryStartDay.plus(difference - i, (TemporalUnit) ChronoUnit.DAYS);
                LocalDate nextNeedHandleDate = needHandleDate.plus(1L, (TemporalUnit) ChronoUnit.DAYS);
                if (dateList.contains(needHandleDate)) {
                    handleCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
                } else if (offDay.contains(needHandleDate)) {
                    handleOffDayCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
                } else {
                    handleUndefinedCalendarDay(calendarId, year, curProductionCalendarDayTimeList, needHandleDate);
                }
                curProductionCalendarDayTimeList.sort(Comparator.comparing((v0) -> {
                    return v0.getStartTime();
                }));
                productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
                    return v0.getStartTime();
                }));
                handleConflictCalendarDay(i, productionCalendar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate);
                if (i == difference && i + 1 == LocalDateTimeUtils.getDayOfYear(year).intValue()) {
                    productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
                        return v0.getStartTime();
                    }));
                    handleFirstDayOfYear(productionCalendar, productionCalendarDaytimeList, needHandleDate);
                }
                curProductionCalendarDayTimeList.clear();
                j = i + 1;
            } else {
                this.calendarDayService.saveBatchDay(productionCalendarDayList);
                this.calendarDaytimeService.saveBatchDaytime(productionCalendarDaytimeList);
                return;
            }
        }
    }
 
    private void handleOffDayCalendarDay(List<CalendarDateDTO> dateDTOList, Long calendarId, Integer year, List<ProductionCalendarDay> productionCalendarDayList, Map<Long, ShiftVO> shiftDetailMap, Snowflake snowflake, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate) {
        CalendarDateDTO calendarDateDTO = dateDTOList.stream().filter(dateDTO -> {
            return dateDTO.getCalendarDate().equals(needHandleDate);
        }).findFirst().orElse(new CalendarDateDTO());
        ShiftVO shiftDetail = shiftDetailMap.get(calendarDateDTO.getModelId());
        Integer week = LocalDateTimeUtils.getWeek(calendarDateDTO.getCalendarDate());
        Integer month = LocalDateTimeUtils.getMonth(calendarDateDTO.getCalendarDate());
        Integer startDate = shiftDetail.getStartTime();
        Integer endDate = shiftDetail.getEndTime();
        ProductionCalendarDay productionCalendarDay = ProductionCalendarDay.builder().calendarId(calendarId).calendarDate(calendarDateDTO.getCalendarDate()).modelId(calendarDateDTO.getModelId()).isHighPriority(calendarDateDTO.getIsHighPriority()).week(week).month(month).year(year).isOffDay(CalendarConstant.IS_OFF_DAY).offId(calendarDateDTO.getOffDayId()).startTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getStartTime().intValue(), ChronoUnit.MINUTES)).endTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getEndTime().intValue(), ChronoUnit.MINUTES)).build();
        buildCalendarOffDay(productionCalendarDay, snowflake, productionCalendarDayList, shiftDetail, calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, startDate, endDate);
    }
 
    private void buildCalendarOffDay(ProductionCalendarDay productionCalendarDay, Snowflake snowflake, List<ProductionCalendarDay> productionCalendarDayList, ShiftVO shiftDetail, Long calendarId, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, CalendarDateDTO calendarDateDTO, Integer week, Integer month, Integer year, Integer startDate, Integer endDate) {
        productionCalendarDay.setId(Long.valueOf(snowflake.nextId()));
        productionCalendarDayList.add(productionCalendarDay);
        List<ShiftDetailVO> collect = (List) shiftDetail.getShiftDetailVOList().stream().sorted(Comparator.comparing((v0) -> {
            return v0.getShiftStartTime();
        })).collect(Collectors.toList());
        for (int i = 0; i < collect.size(); i++) {
            ShiftDetailVO shiftDetailVO = collect.get(i);
            Integer startTime = shiftDetailVO.getShiftStartTime();
            Integer endTime = shiftDetailVO.getShiftEndTime();
            createUndefinedDaytime(calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, startDate, endDate, productionCalendarDay, collect, i, startTime, endTime);
            if (CollectionUtil.isNotEmpty(shiftDetailVO.getShiftRestTimeVOList())) {
                List<ShiftRestTimeVO> shiftRestTimeVOList = (List) shiftDetailVO.getShiftRestTimeVOList().stream().sorted(Comparator.comparing((v0) -> {
                    return v0.getRestStartTime();
                })).collect(Collectors.toList());
                createShiftDayTime(calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, productionCalendarDay, shiftDetailVO, startTime, endTime, shiftRestTimeVOList);
            } else {
                ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(calendarDateDTO.getCalendarDate()).year(year).isUndefined(ProductionTimeConstant.DEFINED).isHighPriority(calendarDateDTO.getIsHighPriority()).week(week).month(month).shiftIndex(shiftDetailVO.getShiftIndex()).calendarDayId(productionCalendarDay.getId()).calendarId(calendarId).startTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startTime.intValue(), ChronoUnit.MINUTES)).endTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES)).shiftType(ProductionTimeConstant.WORK_TIME).build();
                curProductionCalendarDayTimeList.add(productionCalendarDaytime);
            }
        }
        curProductionCalendarDayTimeList.removeIf(productionCalendarDaytime2 -> {
            return productionCalendarDaytime2.getStartTime().equals(productionCalendarDaytime2.getEndTime());
        });
    }
 
    private void updateCalendarDayTime(List<CalendarDateDTO> dateDTOList, ProductionCalendar calenadar) {
        Long calendarId = calenadar.getId();
        Integer year = calenadar.getYear();
        Integer currentYear = Integer.valueOf(LocalDate.now().getYear());
        Set<LocalDate> dateList = (Set) dateDTOList.stream().filter(c -> {
            return !Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId());
        }).map((v0) -> {
            return v0.getCalendarDate();
        }).collect(Collectors.toSet());
        Set<LocalDate> offDay = (Set) dateDTOList.stream().filter(c2 -> {
            return !Func.isNull(c2.getOffDayId());
        }).map((v0) -> {
            return v0.getCalendarDate();
        }).collect(Collectors.toSet());
        List<ProductionCalendarDaytime> productionCalendarDaytimeList = new ArrayList<>();
        List<ProductionCalendarDay> productionCalendarDayList = new ArrayList<>();
        List<Long> modelIds = (List) dateDTOList.stream().map((v0) -> {
            return v0.getModelId();
        }).distinct().collect(Collectors.toList());
        Map<Long, ShiftVO> shiftDetailMap = this.shiftModelService.getShiftDetail(modelIds);
        Snowflake snowflake = IdUtil.createSnowflake(1L, 1L);
        
        List<ProductionCalendarDaytime> todayDateTimeList = this.calendarDaytimeService.list(new QueryWrapper<ProductionCalendarDaytime>().lambda()
                .eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.now())
                .eq(ProductionCalendarDaytime::getCalendarId, calendarId)
                .orderByAsc(ProductionCalendarDaytime::getStartTime));
        
        /*
        List<ProductionCalendarDaytime> todayDateTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
            return v0.getCalendarDate();
        }, LocalDate.now())).eq((v0) -> {
            return v0.getCalendarId();
        }, calendarId)).orderByAsc((v0) -> {
            return v0.getStartTime();
        }));*/
        List<ProductionCalendarDaytime> curProductionCalendarDayTimeList = new ArrayList<>();
        LocalDate lastDayOfYear = LocalDateTimeUtils.getLastDayOfYear(LocalDate.now());
        long difference = year.intValue() == LocalDate.now().getYear() ? LocalDateTimeUtils.getDifference(LocalDate.now(), lastDayOfYear).intValue() : LocalDateTimeUtils.getDayOfYear(year).intValue() - 1;
        long j = 0;
        while (true) {
            long i = j;
            if (i <= difference) {
                LocalDate needHandleDate = year.equals(currentYear) ? LocalDate.now().plus(difference - i, (TemporalUnit) ChronoUnit.DAYS) : LocalDate.of(year.intValue(), 1, 1).plus(difference - i, (TemporalUnit) ChronoUnit.DAYS);
                LocalDate nextNeedHandleDate = needHandleDate.plus(1L, (TemporalUnit) ChronoUnit.DAYS);
                buildCurrentDayTime(dateDTOList, calendarId, year, dateList, offDay, productionCalendarDaytimeList, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
                handleConflietDayTime(calenadar, year, productionCalendarDaytimeList, todayDateTimeList, curProductionCalendarDayTimeList, difference, i, needHandleDate, nextNeedHandleDate);
                j = i + 1;
            } else {
                this.calendarDayService.saveBatchDay(productionCalendarDayList);
                this.calendarDaytimeService.saveBatchDaytime(productionCalendarDaytimeList);
                return;
            }
        }
    }
 
    private void handleConflietDayTime(ProductionCalendar calenadar, Integer year, List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> todayDateTimeList, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, long difference, long i, LocalDate needHandleDate, LocalDate nextNeedHandleDate) {
        if (LocalDate.now().isBefore(LocalDate.of(year.intValue(), 1, 1))) {
            handleConflictCalendarDay(i, calenadar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate);
            productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
                return v0.getStartTime();
            }));
            if (i == difference && i + 1 == LocalDateTimeUtils.getDayOfYear(year).intValue()) {
                handleFirstDayOfYear(calenadar, productionCalendarDaytimeList, needHandleDate);
            }
        } else {
            if (i <= difference - 1) {
                handleConflictCalendarDay(i, calenadar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate);
            }
            if (i == difference - 1) {
                productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
                    return v0.getStartTime();
                }));
                handleTomorrow(year, productionCalendarDaytimeList, todayDateTimeList, needHandleDate);
            }
        }
        curProductionCalendarDayTimeList.clear();
    }
 
    private void buildCurrentDayTime(List<CalendarDateDTO> dateDTOList, Long calendarId, Integer year, Set<LocalDate> dateList, Set<LocalDate> offDay, List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDay> productionCalendarDayList, Map<Long, ShiftVO> shiftDetailMap, Snowflake snowflake, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate) {
        if (dateList.contains(needHandleDate)) {
            handleCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
        } else if (offDay.contains(needHandleDate)) {
            handleOffDayCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
        } else {
            handleUndefinedCalendarDay(calendarId, year, curProductionCalendarDayTimeList, needHandleDate);
        }
        curProductionCalendarDayTimeList.sort(Comparator.comparing((v0) -> {
            return v0.getStartTime();
        }));
        productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
            return v0.getStartTime();
        }));
    }
 
    private void handleTomorrow(Integer year, List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> todayDateTimeList, LocalDate needHandleDate) {
        ProductionCalendarDaytime tomorrow = productionCalendarDaytimeList.get(0);
        if (Func.isNotEmpty(todayDateTimeList)) {
            ProductionCalendarDaytime todayDateTime = todayDateTimeList.get(todayDateTimeList.size() - 1);
            if (tomorrow.getStartTime().isBefore(todayDateTime.getEndTime()) && tomorrow.getIsUndefined().equals(ProductionTimeConstant.DEFINED)) {
                throw new ServiceException(MessageUtils.message("calendar.time.slice.of.the.day.affected", new Object[0]));
            }
            if (tomorrow.getStartTime().isBefore(todayDateTime.getEndTime()) && tomorrow.getIsUndefined().equals(ProductionTimeConstant.UNDEFINED)) {
                productionCalendarDaytimeList.removeIf(productionCalendarDaytime -> {
                    return productionCalendarDaytime.getStartTime().isBefore(todayDateTime.getEndTime()) && (productionCalendarDaytime.getEndTime().isBefore(todayDateTime.getEndTime()) || productionCalendarDaytime.getEndTime().equals(todayDateTime.getEndTime()));
                });
                productionCalendarDaytimeList.get(0).setStartTime(todayDateTime.getEndTime());
            } else if (tomorrow.getStartTime().isAfter(todayDateTime.getEndTime())) {
                ProductionCalendarDaytime productionCalendarDaytime2 = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(tomorrow.getWeek()).month(tomorrow.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(tomorrow.getCalendarId()).startTime(todayDateTime.getEndTime()).endTime(tomorrow.getStartTime()).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(tomorrow.getIsUndefined()).build();
                productionCalendarDaytimeList.add(productionCalendarDaytime2);
            }
        }
    }
 
    /* JADX WARN: Multi-variable type inference failed */
    private void handleFirstDayOfYear(ProductionCalendar productionCalendar, List<ProductionCalendarDaytime> productionCalendarDaytimeList, LocalDate needHandleDate) {
        Integer year = productionCalendar.getYear();
        List<ProductionCalendarDaytime> lastYearDateTimeList = new ArrayList<>();
        
        ProductionCalendar item = (ProductionCalendar)(this.baseMapper.selectOne((new QueryWrapper<ProductionCalendar>()).lambda()
                .eq(ProductionCalendar::getStatus, ProductionTimeConstant.ENABLE)
                .eq(ProductionCalendar::getCode, productionCalendar.getCode())
                .eq(ProductionCalendar::getYear, Integer.valueOf(year.intValue() - 1))));
        /*
        ProductionCalendar item = (ProductionCalendar) this.baseMapper.selectOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
            return v0.getStatus();
        }, ProductionTimeConstant.ENABLE)).eq((v0) -> {
            return v0.getCode();
        }, productionCalendar.getCode())).eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(year.intValue() - 1)));
        */
        if (Func.isNotEmpty(item)) {
            lastYearDateTimeList = this.calendarDaytimeService.list(new QueryWrapper<ProductionCalendarDaytime>().lambda()
                      .eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.of(productionCalendar.getYear().intValue() - 1, 12, 31))
                      .eq(ProductionCalendarDaytime::getCalendarId, item.getId())
                      .orderByAsc(ProductionCalendarDaytime::getStartTime)); 
            /*
            lastYearDateTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                return v0.getCalendarDate();
            }, LocalDate.of(productionCalendar.getYear().intValue() - 1, 12, 31))).eq((v0) -> {
                return v0.getCalendarId();
            }, item.getId())).orderByAsc((v0) -> {
                return v0.getStartTime();
            }));*/
        }
        ProductionCalendarDaytime firstDateTime = productionCalendarDaytimeList.get(0);
        if (!Func.isNotEmpty(lastYearDateTimeList)) {
            if (Func.isEmpty(lastYearDateTimeList) && firstDateTime.getStartTime().isAfter(LocalDateTime.of(needHandleDate, LocalTime.of(0, 0, 0)))) {
                ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(firstDateTime.getWeek()).month(firstDateTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(firstDateTime.getCalendarId()).startTime(LocalDateTime.of(needHandleDate, LocalTime.of(0, 0, 0))).endTime(firstDateTime.getStartTime()).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(firstDateTime.getIsUndefined()).build();
                productionCalendarDaytimeList.add(productionCalendarDaytime);
                return;
            }
            return;
        }
        ProductionCalendarDaytime lastYearDateTime = lastYearDateTimeList.get(lastYearDateTimeList.size() - 1);
        boolean useLastShift = isUseLastShift(firstDateTime, lastYearDateTime);
        boolean useThisShift = isUseThisShift(firstDateTime, lastYearDateTime);
        boolean conflictShift = isConflictShift(firstDateTime, lastYearDateTime);
        if (conflictShift) {
            throw new ServiceException(MessageUtils.message("calendar.time.slice.of.the.day.affected", new Object[0]));
        }
        if (useLastShift) {
            productionCalendarDaytimeList.removeIf(productionCalendarDaytime2 -> {
                return productionCalendarDaytime2.getStartTime().isBefore(lastYearDateTime.getEndTime()) && (productionCalendarDaytime2.getEndTime().isBefore(lastYearDateTime.getEndTime()) || productionCalendarDaytime2.getEndTime().equals(lastYearDateTime.getEndTime()));
            });
            productionCalendarDaytimeList.get(0).setStartTime(lastYearDateTime.getEndTime());
        } else if (useThisShift) {
            lastYearDateTimeList.removeIf(productionCalendarDaytime3 -> {
                return (productionCalendarDaytime3.getStartTime().isAfter(firstDateTime.getStartTime()) || productionCalendarDaytime3.getStartTime().equals(firstDateTime.getStartTime())) && productionCalendarDaytime3.getEndTime().isAfter(firstDateTime.getStartTime());
            });
            lastYearDateTimeList.get(lastYearDateTimeList.size() - 1).setEndTime(firstDateTime.getStartTime());
            this.calendarDaytimeService.deleteByCalendarDate(lastYearDateTime.getCalendarDate(), lastYearDateTime.getCalendarId());
            productionCalendarDaytimeList.addAll(lastYearDateTimeList);
        } else if (firstDateTime.getStartTime().isAfter(lastYearDateTime.getEndTime())) {
            ProductionCalendarDaytime productionCalendarDaytime4 = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(firstDateTime.getWeek()).month(firstDateTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(firstDateTime.getCalendarId()).startTime(lastYearDateTime.getEndTime()).endTime(firstDateTime.getStartTime()).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(firstDateTime.getIsUndefined()).build();
            productionCalendarDaytimeList.add(productionCalendarDaytime4);
        }
    }
 
    private boolean isConflictShift(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) {
        return lastYearDateTime.getCalendarDate().equals(LocalDate.now()) && firstDateTime.getStartTime().isBefore(lastYearDateTime.getEndTime()) && firstDateTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED);
    }
 
    private boolean isUseLastShift(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) {
        boolean useLastShift = firstDateTime.getStartTime().isBefore(lastYearDateTime.getEndTime()) && (lastYearDateTime.getIsHighPriority().equals(ProductionTimeConstant.HIGH_PRIORITY) || (lastYearDateTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED) && firstDateTime.getIsUndefined().equals(ProductionTimeConstant.UNDEFINED)));
        return useLastShift;
    }
 
    private boolean isUseThisShift(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) {
        boolean useThisShift = firstDateTime.getStartTime().isBefore(lastYearDateTime.getEndTime()) && !lastYearDateTime.getCalendarDate().equals(LocalDate.now()) && isCurrentHighPriority(firstDateTime, lastYearDateTime);
        return useThisShift;
    }
 
    private boolean isCurrentHighPriority(ProductionCalendarDaytime firstDateTime, ProductionCalendarDaytime lastYearDateTime) {
        return firstDateTime.getIsHighPriority().equals(ProductionTimeConstant.HIGH_PRIORITY) || (firstDateTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED) && lastYearDateTime.getIsHighPriority().equals(ProductionTimeConstant.LOW_PRIORITY));
    }
 
    private void handleConflictCalendarDay(long i, ProductionCalendar productionCalendar, List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate, LocalDate nextNeedHandleDate) {
        ProductionCalendarDaytime nextProductionDayTime;
        Integer year = productionCalendar.getYear();
        String code = productionCalendar.getCode();
        new ProductionCalendarDaytime();
        List<ProductionCalendarDaytime> nextYearDateTimeList = new ArrayList<>();
        if (i == 0) {
            ProductionCalendar item =this.baseMapper.selectOne(new QueryWrapper<ProductionCalendar>().lambda()
                      .eq(ProductionCalendar::getStatus, ProductionTimeConstant.ENABLE)
                      .eq(ProductionCalendar::getCode, code)
                      .eq(ProductionCalendar::getYear, Integer.valueOf(year.intValue() + 1)));
            /*
            ProductionCalendar item = (ProductionCalendar) this.baseMapper.selectOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                return v0.getStatus();
            }, ProductionTimeConstant.ENABLE)).eq((v0) -> {
                return v0.getCode();
            }, code)).eq((v0) -> {
                return v0.getYear();
            }, Integer.valueOf(year.intValue() + 1)));
            */
            if (Func.isNotEmpty(item)) {
                nextYearDateTimeList = this.calendarDaytimeService.list(new QueryWrapper<ProductionCalendarDaytime>().lambda()
                        .eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.of(year.intValue() + 1, 1, 1))
                        .eq(ProductionCalendarDaytime::getCalendarId, item.getId())
                        .orderByAsc(ProductionCalendarDaytime::getStartTime)); 
                /*
                nextYearDateTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                    return v0.getCalendarDate();
                }, LocalDate.of(year.intValue() + 1, 1, 1))).eq((v0) -> {
                    return v0.getCalendarId();
                }, item.getId())).orderByAsc((v0) -> {
                    return v0.getStartTime();
                }));*/
            }
            if (Func.isNotEmpty(nextYearDateTimeList)) {
                nextProductionDayTime = nextYearDateTimeList.get(0);
            } else {
                nextProductionDayTime = ProductionCalendarDaytime.builder().isHighPriority(ProductionTimeConstant.LOW_PRIORITY).startTime(LocalDateTime.of(year.intValue() + 1, 1, 1, 0, 0, 0)).isUndefined(ProductionTimeConstant.UNDEFINED).calendarDate(LocalDate.of(year.intValue() + 1, 1, 1)).build();
            }
        } else {
            nextProductionDayTime = productionCalendarDaytimeList.get(0);
        }
        ProductionCalendarDaytime curProductionDayTime = curProductionCalendarDayTimeList.stream().max(Comparator.comparing((v0) -> {
            return v0.getStartTime();
        })).orElse(new ProductionCalendarDaytime());
        LocalDateTime nextStartTime = nextProductionDayTime.getStartTime();
        LocalDateTime beforeZore = LocalDateTime.of(needHandleDate, LocalTime.of(23, 59, 59));
        LocalDateTime zore = needHandleDate.atStartOfDay().plus(1L, (TemporalUnit) ChronoUnit.DAYS);
        LocalDateTime afterZore = LocalDateTime.of(nextNeedHandleDate, LocalTime.of(0, 0, 1));
        LocalDateTime curEndTime = curProductionDayTime.getEndTime();
        if (curEndTime.isAfter(nextStartTime)) {
            handleConflietDayTime(i, productionCalendarDaytimeList, curProductionCalendarDayTimeList, nextProductionDayTime, nextYearDateTimeList, curProductionDayTime, nextStartTime, curEndTime);
        } else if (curEndTime.equals(nextStartTime)) {
            productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList);
        } else if (curEndTime.isBefore(nextStartTime) && curEndTime.isAfter(beforeZore)) {
            buildEndTimeAfterZero(productionCalendarDaytimeList, curProductionCalendarDayTimeList, nextNeedHandleDate, year, nextProductionDayTime, nextStartTime, curEndTime);
        } else if (curEndTime.isBefore(nextStartTime) && nextStartTime.isBefore(afterZore)) {
            buildEndTimeBeforeZero(productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, year, curProductionDayTime, nextStartTime, curEndTime);
        } else if (curEndTime.isBefore(nextStartTime) && nextStartTime.isAfter(afterZore) && curEndTime.isBefore(beforeZore)) {
            buildEndTimeBothSideZero(productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate, year, nextProductionDayTime, curProductionDayTime, nextStartTime, zore, curEndTime);
        }
    }
 
    private void handleConflietDayTime(long i, List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, ProductionCalendarDaytime nextProductionDayTime, List<ProductionCalendarDaytime> nextYearDateTimeList, ProductionCalendarDaytime curProductionDayTime, LocalDateTime nextStartTime, LocalDateTime curEndTime) {
        boolean useCurShift = curProductionDayTime.getIsHighPriority().equals(ProductionTimeConstant.HIGH_PRIORITY) || (curProductionDayTime.getIsUndefined().equals(ProductionTimeConstant.DEFINED) && nextProductionDayTime.getIsUndefined().equals(ProductionTimeConstant.UNDEFINED));
        if (useCurShift) {
            if (i == 0) {
                if (Func.isNotEmpty(nextYearDateTimeList)) {
                    nextYearDateTimeList.removeIf(productionCalendarDaytime -> {
                        return productionCalendarDaytime.getStartTime().isBefore(curEndTime) && productionCalendarDaytime.getEndTime().isBefore(curEndTime);
                    });
                    nextYearDateTimeList.get(0).setStartTime(curEndTime);
                    this.calendarDaytimeService.deleteByCalendarDate(nextProductionDayTime.getCalendarDate(), nextProductionDayTime.getCalendarId());
                    productionCalendarDaytimeList.addAll(nextYearDateTimeList);
                }
            } else {
                productionCalendarDaytimeList.removeIf(productionCalendarDaytime2 -> {
                    return productionCalendarDaytime2.getStartTime().isBefore(curEndTime) && (productionCalendarDaytime2.getEndTime().isBefore(curEndTime) || productionCalendarDaytime2.getEndTime().equals(curEndTime));
                });
                productionCalendarDaytimeList.get(0).setStartTime(curEndTime);
            }
            productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList);
            return;
        }
        List<ProductionCalendarDaytime> filterList = (List) curProductionCalendarDayTimeList.stream().filter(item -> {
            return item.getStartTime().isBefore(nextStartTime);
        }).sorted(Comparator.comparing((v0) -> {
            return v0.getStartTime();
        })).collect(Collectors.toList());
        if (Func.isNotEmpty(filterList)) {
            filterList.get(filterList.size() - 1).setEndTime(nextStartTime);
            productionCalendarDaytimeList.addAll(filterList);
        }
    }
 
    private void buildEndTimeBothSideZero(List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate, LocalDate nextNeedHandleDate, Integer year, ProductionCalendarDaytime nextProductionDayTime, ProductionCalendarDaytime curProductionDayTime, LocalDateTime nextStartTime, LocalDateTime zore, LocalDateTime curEndTime) {
        ProductionCalendarDaytime before = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).isUndefined(curProductionDayTime.getIsUndefined()).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(curProductionDayTime.getWeek()).month(curProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(curProductionDayTime.getCalendarId()).startTime(curEndTime).endTime(zore).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build();
        ProductionCalendarDaytime next = ProductionCalendarDaytime.builder().calendarDate(nextNeedHandleDate).isUndefined(nextProductionDayTime.getIsUndefined()).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(nextProductionDayTime.getWeek()).month(nextProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(nextProductionDayTime.getCalendarId()).startTime(zore).endTime(nextStartTime).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build();
        curProductionCalendarDayTimeList.add(before);
        curProductionCalendarDayTimeList.add(next);
        productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList);
    }
 
    private void buildEndTimeBeforeZero(List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate, Integer year, ProductionCalendarDaytime curProductionDayTime, LocalDateTime nextStartTime, LocalDateTime curEndTime) {
        ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).isUndefined(ProductionTimeConstant.UNDEFINED).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(curProductionDayTime.getWeek()).month(curProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(curProductionDayTime.getCalendarId()).startTime(curEndTime).endTime(nextStartTime).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build();
        curProductionCalendarDayTimeList.add(productionCalendarDaytime);
        productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList);
    }
 
    private void buildEndTimeAfterZero(List<ProductionCalendarDaytime> productionCalendarDaytimeList, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate nextNeedHandleDate, Integer year, ProductionCalendarDaytime nextProductionDayTime, LocalDateTime nextStartTime, LocalDateTime curEndTime) {
        ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(nextNeedHandleDate).isUndefined(nextProductionDayTime.getIsUndefined()).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).year(year).week(nextProductionDayTime.getWeek()).month(nextProductionDayTime.getMonth()).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(nextProductionDayTime.getCalendarId()).startTime(curEndTime).endTime(nextStartTime).shiftType(ProductionTimeConstant.UNDEFINED_TIME_OUT).build();
        curProductionCalendarDayTimeList.add(productionCalendarDaytime);
        productionCalendarDaytimeList.addAll(curProductionCalendarDayTimeList);
    }
 
    private void handleCalendarDay(List<CalendarDateDTO> dateDTOList, Long calendarId, Integer year, List<ProductionCalendarDay> productionCalendarDayList, Map<Long, ShiftVO> shiftDetailMap, Snowflake snowflake, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate) {
        CalendarDateDTO calendarDateDTO = dateDTOList.stream().filter(dateDTO -> {
            return dateDTO.getCalendarDate().equals(needHandleDate);
        }).findFirst().orElse(new CalendarDateDTO());
        ShiftVO shiftDetail = shiftDetailMap.get(calendarDateDTO.getModelId());
        Integer week = LocalDateTimeUtils.getWeek(calendarDateDTO.getCalendarDate());
        Integer month = LocalDateTimeUtils.getMonth(calendarDateDTO.getCalendarDate());
        Integer startDate = shiftDetail.getStartTime();
        Integer endDate = shiftDetail.getEndTime();
        ProductionCalendarDay productionCalendarDay = ProductionCalendarDay.builder().calendarId(calendarId).calendarDate(calendarDateDTO.getCalendarDate()).modelId(calendarDateDTO.getModelId()).isHighPriority(calendarDateDTO.getIsHighPriority()).week(week).month(month).year(year).isOffDay(CalendarConstant.NO_OFF_DAY).startTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getStartTime().intValue(), ChronoUnit.MINUTES)).endTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), shiftDetail.getEndTime().intValue(), ChronoUnit.MINUTES)).build();
        buildCalendarOffDay(productionCalendarDay, snowflake, productionCalendarDayList, shiftDetail, calendarId, curProductionCalendarDayTimeList, calendarDateDTO, week, month, year, startDate, endDate);
    }
 
    private void handleUndefinedCalendarDay(Long calendarId, Integer year, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, LocalDate needHandleDate) {
        ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(needHandleDate).year(year).week(LocalDateTimeUtils.getWeek(needHandleDate)).month(LocalDateTimeUtils.getMonth(needHandleDate)).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarId(calendarId).startTime(LocalDateTime.of(needHandleDate, LocalTime.of(0, 0, 0))).endTime(LocalDateTimeUtils.plus(needHandleDate, 1L, ChronoUnit.DAYS)).shiftType(ProductionTimeConstant.UNDEFINED_TIME_IN).isHighPriority(ProductionTimeConstant.LOW_PRIORITY).isUndefined(ProductionTimeConstant.UNDEFINED).build();
        curProductionCalendarDayTimeList.add(productionCalendarDaytime);
    }
 
    private void createUndefinedDaytime(Long calendarId, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, CalendarDateDTO calendarDateDTO, Integer week, Integer month, Integer year, Integer startDate, Integer endDate, ProductionCalendarDay productionCalendarDay, List<ShiftDetailVO> collect, int i, Integer startTime, Integer endTime) {
        ProductionCalendarDaytime undefinedDaytime = ProductionCalendarDaytime.builder().calendarDate(calendarDateDTO.getCalendarDate()).year(year).week(week).month(month).shiftIndex(ProductionTimeConstant.OUT_OF_SHIFT_TIME).calendarDayId(productionCalendarDay.getId()).isUndefined(ProductionTimeConstant.DEFINED).isHighPriority(calendarDateDTO.getIsHighPriority()).calendarDayId(productionCalendarDay.getId()).calendarId(calendarId).build();
        if (i == 0) {
            ProductionCalendarDaytime undefinedDaytime1 = new ProductionCalendarDaytime();
            BeanUtils.copyProperties(undefinedDaytime, undefinedDaytime1);
            undefinedDaytime1.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startDate.intValue(), ChronoUnit.MINUTES));
            undefinedDaytime1.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startTime.intValue(), ChronoUnit.MINUTES));
            undefinedDaytime1.setShiftType(ProductionTimeConstant.UNDEFINED_TIME_IN);
            curProductionCalendarDayTimeList.add(undefinedDaytime1);
        }
        if (i < collect.size() - 1) {
            undefinedDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES));
            undefinedDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), collect.get(i + 1).getShiftStartTime().intValue(), ChronoUnit.MINUTES));
            undefinedDaytime.setShiftType(ProductionTimeConstant.UNDEFINED_TIME_IN);
            curProductionCalendarDayTimeList.add(undefinedDaytime);
        } else if (i == collect.size() - 1) {
            undefinedDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES));
            undefinedDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endDate.intValue(), ChronoUnit.MINUTES));
            undefinedDaytime.setShiftType(ProductionTimeConstant.UNDEFINED_TIME_IN);
            curProductionCalendarDayTimeList.add(undefinedDaytime);
        }
    }
 
    private void createShiftDayTime(Long calendarId, List<ProductionCalendarDaytime> curProductionCalendarDayTimeList, CalendarDateDTO calendarDateDTO, Integer week, Integer month, Integer year, ProductionCalendarDay productionCalendarDay, ShiftDetailVO shiftDetailVO, Integer startTime, Integer endTime, List<ShiftRestTimeVO> shiftRestTimeVOList) {
        Stream.iterate(0, j -> {
            return Integer.valueOf(j.intValue() + 1);
        }).limit(shiftRestTimeVOList.size()).forEach(j2 -> {
            ProductionCalendarDaytime productionCalendarDaytime = ProductionCalendarDaytime.builder().calendarDate(calendarDateDTO.getCalendarDate()).year(year).week(week).month(month).shiftIndex(shiftDetailVO.getShiftIndex()).calendarDayId(productionCalendarDay.getId()).isUndefined(ProductionTimeConstant.DEFINED).isHighPriority(calendarDateDTO.getIsHighPriority()).calendarId(calendarId).build();
            if (j2.intValue() == 0) {
                productionCalendarDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), startTime.intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(0)).getRestStartTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime.setShiftType(ProductionTimeConstant.WORK_TIME);
                ProductionCalendarDaytime productionCalendarDaytime1 = new ProductionCalendarDaytime();
                BeanUtils.copyProperties(productionCalendarDaytime, productionCalendarDaytime1);
                productionCalendarDaytime1.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestStartTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime1.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestEndTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime1.setShiftType(ProductionTimeConstant.REST_TIME);
                curProductionCalendarDayTimeList.add(productionCalendarDaytime);
                curProductionCalendarDayTimeList.add(productionCalendarDaytime1);
            } else if (j2.intValue() > 0 && j2.intValue() < shiftRestTimeVOList.size()) {
                productionCalendarDaytime.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestStartTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestEndTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime.setShiftType(ProductionTimeConstant.REST_TIME);
                ProductionCalendarDaytime productionCalendarDaytime12 = new ProductionCalendarDaytime();
                BeanUtils.copyProperties(productionCalendarDaytime, productionCalendarDaytime12);
                productionCalendarDaytime12.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue() - 1)).getRestEndTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime12.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestStartTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime12.setShiftType(ProductionTimeConstant.WORK_TIME);
                curProductionCalendarDayTimeList.add(productionCalendarDaytime);
                curProductionCalendarDayTimeList.add(productionCalendarDaytime12);
            }
            if (j2.intValue() == shiftRestTimeVOList.size() - 1) {
                ProductionCalendarDaytime productionCalendarDaytime13 = new ProductionCalendarDaytime();
                BeanUtils.copyProperties(productionCalendarDaytime, productionCalendarDaytime13);
                productionCalendarDaytime13.setStartTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), ((ShiftRestTimeVO) shiftRestTimeVOList.get(j2.intValue())).getRestEndTime().intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime13.setEndTime(LocalDateTimeUtils.plus(calendarDateDTO.getCalendarDate(), endTime.intValue(), ChronoUnit.MINUTES));
                productionCalendarDaytime13.setShiftType(ProductionTimeConstant.WORK_TIME);
                curProductionCalendarDayTimeList.add(productionCalendarDaytime13);
            }
        });
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public CalendarVO getCalendar(Long calendarId) {
        CalendarVO calendarDetail = this.baseMapper.getCalendarDetail(calendarId);
        List<CalendarDayVO> otherCalendarDayVOList = new ArrayList<>();
        Integer year = calendarDetail.getYear();
        
        Optional.<ProductionCalendar>ofNullable(getOne(new QueryWrapper<ProductionCalendar>().lambda()
                .eq(ProductionCalendar::getCode, calendarDetail.getCode())
                .eq(ProductionCalendar::getYear, year+ 1)))
            .ifPresent(productionCalendar -> {
                List<ProductionCalendarDay> list = this.calendarDayService.list(new QueryWrapper<ProductionCalendarDay>().lambda().eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId()).in(ProductionCalendarDay::getCalendarDate, new Object[] { LocalDate.of(year.intValue() + 1, 1, 1), LocalDate.of(year.intValue() + 1, 1, 2) }));
                entityToVo(otherCalendarDayVOList, list);
             });
        
        /*
        Optional.ofNullable(getOne((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
            return v0.getCode();
        }, calendarDetail.getCode())).eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(year.intValue() + 1)))).ifPresent(productionCalendar -> {
            List<ProductionCalendarDay> list = this.calendarDayService.list((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                return v0.getCalendarId();
            }, productionCalendar.getId())).in((v0) -> {
                return v0.getCalendarDate();
            }, new Object[]{LocalDate.of(year.intValue() + 1, 1, 1), LocalDate.of(year.intValue() + 1, 1, 2)}));
            entityToVo(otherCalendarDayVOList, list);
        });*/
        
        Optional.<ProductionCalendar>ofNullable(getOne(new QueryWrapper<ProductionCalendar>().lambda()
                .eq(ProductionCalendar::getCode, calendarDetail.getCode())
                .eq(ProductionCalendar::getYear, year.intValue() - 1)))
            .ifPresent(productionCalendar -> {
                List<ProductionCalendarDay> list = this.calendarDayService.list(new QueryWrapper<ProductionCalendarDay>().lambda().eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId()).in(ProductionCalendarDay::getCalendarDate, new Object[] { LocalDate.of(year.intValue() - 1, 12, 30), LocalDate.of(year.intValue() - 1, 12, 31) }));
                entityToVo(otherCalendarDayVOList, list);
              });
        
        /*
        Optional.ofNullable(getOne((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
            return v0.getCode();
        }, calendarDetail.getCode())).eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(year.intValue() - 1)))).ifPresent(productionCalendar2 -> {
            List<ProductionCalendarDay> list = this.calendarDayService.list((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                return v0.getCalendarId();
            }, productionCalendar2.getId())).in((v0) -> {
                return v0.getCalendarDate();
            }, new Object[]{LocalDate.of(year.intValue() - 1, 12, 30), LocalDate.of(year.intValue() - 1, 12, 31)}));
            entityToVo(otherCalendarDayVOList, list);
        });
        */
        calendarDetail.setOtherCalendarDayVOList(otherCalendarDayVOList);
        return calendarDetail;
    }
 
    private void entityToVo(List<CalendarDayVO> otherCalendarDayVOList, List<ProductionCalendarDay> list) {
        if (Func.isNotEmpty(list)) {
            otherCalendarDayVOList.addAll((Collection) list.stream().map(productionCalendarDay -> {
                CalendarDayVO calendarDayVO = new CalendarDayVO();
                BeanUtils.copyProperties(productionCalendarDay, calendarDayVO);
                return calendarDayVO;
            }).collect(Collectors.toList()));
        }
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    @Transactional(rollbackFor = {Exception.class})
    public ProductionCalendar copyCalendar(CalendarCopyVO calendarCopyVO) {
        CalendarSaveVO calendarSaveVO = new CalendarSaveVO();
        BeanUtils.copyProperties(calendarCopyVO, calendarSaveVO);
        checkCalendar(calendarSaveVO, AuthUtil.getTenantId());
        CalendarVO calendar = getCalendar(calendarCopyVO.getId());
        ProductionCalendar productionCalendar = ProductionCalendar.builder().code(calendarCopyVO.getCode()).name(calendar.getName()).status(calendar.getStatus()).year(calendarCopyVO.getYear()).build();
        save(productionCalendar);
        return productionCalendar;
    }
 
    public void checkCalendar(CalendarSaveVO calendarSaveVO, String tenantId) {
         ProductionCalendar productionCalendar = getOne(Wrappers.<ProductionCalendar>lambdaQuery()
                    .eq(ProductionCalendar::getCode, calendarSaveVO.getCode())
                    .eq(ProductionCalendar::getYear, calendarSaveVO.getYear())
                    .eq(ProductionCalendar::getTenantId, tenantId));
        /*
        ProductionCalendar productionCalendar = (ProductionCalendar) getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getCode();
        }, calendarSaveVO.getCode())).eq((v0) -> {
            return v0.getYear();
        }, calendarSaveVO.getYear())).eq((v0) -> {
            return v0.getTenantId();
        }, tenantId));
        */
        Optional.ofNullable(productionCalendar).ifPresent(po -> {
            throw new ServiceException(MessageUtils.message("calendar.code.year.has.be.repeated", new Object[0]));
        });
    }
 
    private void saveYearCalendarDayTime(List<CalendarDateDTO> dateDTOList, ProductionCalendar productionCalendar) {
        Integer year = productionCalendar.getYear();
        Long calendarId = productionCalendar.getId();
        Set<LocalDate> dateList = (Set) dateDTOList.stream().filter(c -> {
            return !Func.isNull(c.getModelId()) && Func.isNull(c.getOffDayId());
        }).map((v0) -> {
            return v0.getCalendarDate();
        }).collect(Collectors.toSet());
        Set<LocalDate> offDay = (Set) dateDTOList.stream().filter(c2 -> {
            return !Func.isNull(c2.getOffDayId());
        }).map((v0) -> {
            return v0.getCalendarDate();
        }).collect(Collectors.toSet());
        List<ProductionCalendarDaytime> productionCalendarDaytimeList = new ArrayList<>();
        List<ProductionCalendarDay> productionCalendarDayList = new ArrayList<>();
        List<Long> modelIds = (List) dateDTOList.stream().map((v0) -> {
            return v0.getModelId();
        }).distinct().collect(Collectors.toList());
        Map<Long, ShiftVO> shiftDetailMap = this.shiftModelService.getShiftDetail(modelIds);
        Snowflake snowflake = IdUtil.createSnowflake(1L, 1L);
        List<ProductionCalendarDaytime> curProductionCalendarDayTimeList = new ArrayList<>();
        LocalDate lastDayOfYear = LocalDateTimeUtils.getLastDayOfYear(LocalDate.of(year.intValue(), 1, 1));
        LocalDate firstDay = LocalDate.of(LocalDate.now().getYear(), 1, 1);
        Integer thisYear = Integer.valueOf(firstDay.getYear());
        long difference = year.equals(thisYear) ? LocalDateTimeUtils.getDifference(firstDay, lastDayOfYear).intValue() : LocalDateTimeUtils.getDayOfYear(year).intValue() - 1;
        long j = 0;
        while (true) {
            long i = j;
            if (i <= difference) {
                LocalDate needHandleDate = year.equals(thisYear) ? firstDay.plus(difference - i, (TemporalUnit) ChronoUnit.DAYS) : LocalDate.of(year.intValue(), 1, 1).plus(difference - i, (TemporalUnit) ChronoUnit.DAYS);
                LocalDate nextNeedHandleDate = needHandleDate.plus(1L, (TemporalUnit) ChronoUnit.DAYS);
                if (dateList.contains(needHandleDate)) {
                    handleCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
                } else if (offDay.contains(needHandleDate)) {
                    handleOffDayCalendarDay(dateDTOList, calendarId, year, productionCalendarDayList, shiftDetailMap, snowflake, curProductionCalendarDayTimeList, needHandleDate);
                } else {
                    handleUndefinedCalendarDay(calendarId, year, curProductionCalendarDayTimeList, needHandleDate);
                }
                curProductionCalendarDayTimeList.sort(Comparator.comparing((v0) -> {
                    return v0.getStartTime();
                }));
                productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
                    return v0.getStartTime();
                }));
                handleConflictCalendarDay(i, productionCalendar, productionCalendarDaytimeList, curProductionCalendarDayTimeList, needHandleDate, nextNeedHandleDate);
                if (i == difference && i + 1 == LocalDateTimeUtils.getDayOfYear(year).intValue()) {
                    productionCalendarDaytimeList.sort(Comparator.comparing((v0) -> {
                        return v0.getStartTime();
                    }));
                    handleFirstDayOfYear(productionCalendar, productionCalendarDaytimeList, needHandleDate);
                }
                curProductionCalendarDayTimeList.clear();
                j = i + 1;
            } else {
                this.calendarDayService.saveBatchDay(productionCalendarDayList);
                this.calendarDaytimeService.saveBatchDaytime(productionCalendarDaytimeList);
                return;
            }
        }
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<TimestampToProductionTimeCacheDto> buildProductionTimeCache(CacheBuildDTO cacheBuildDTO) {
        LocalDate targetDate = cacheBuildDTO.getTargetDate();
        Set<String> tenantIds = cacheBuildDTO.getTenantIds();
        String code = cacheBuildDTO.getCalendarCode();
        
        List<ProductionCalendar> list = list(new QueryWrapper<ProductionCalendar>().lambda()
                .in(ProductionCalendar::getTenantId, tenantIds)
                .eq(ProductionCalendar::getStatus, ProductionTimeConstant.ENABLE)
                .eq(StringUtils.isNotBlank(code), ProductionCalendar::getCode, code)
                .eq(ProductionCalendar::getYear, Integer.valueOf(targetDate.getYear())));
        /*
        List<ProductionCalendar> list = list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().in((v0) -> {
            return v0.getTenantId();
        }, tenantIds)).eq((v0) -> {
            return v0.getStatus();
        }, ProductionTimeConstant.ENABLE)).eq(StringUtils.isNotBlank(code), (v0) -> {
            return v0.getCode();
        }, code).eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(targetDate.getYear())));*/
        LocalDateTime startTime = LocalDateTime.of(targetDate, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(targetDate.plusDays(1L), LocalTime.of(0, 0));
        if (Func.isNotEmpty(list)) {
            return list.stream()
                    .map(productionCalendar -> {
                        
                        TimestampToProductionTimeCacheDto timestampToProductionTimeCacheDto = TimestampToProductionTimeCacheDto.builder().TenantId(productionCalendar.getTenantId()).CalendarCode(productionCalendar.getCode()).NaturalDay(targetDate).build();
                        
                        List<ProductionCalendarDaytime> dayTimeList = this.calendarDaytimeService.list(new QueryWrapper<ProductionCalendarDaytime>().lambda()
                                .eq(ProductionCalendarDaytime::getCalendarId, productionCalendar.getId())
                                .or().and(i ->{
                                    i.between(ProductionCalendarDaytime::getStartTime, startTime, endTime).or().between(ProductionCalendarDaytime::getEndTime, startTime, endTime);
                                    //return i;
                                })
                                .orderByAsc(ProductionCalendarDaytime::getStartTime));
                        dayTimeList = dayTimeList.stream().filter(productionCalendarDaytime -> {
                            return (productionCalendarDaytime.getEndTime().equals(startTime) || productionCalendarDaytime.getStartTime().equals(endTime)) ? false : true;
                        }).collect(Collectors.toList());
                        Map<Integer, CalendarShiftTimeSlicesDTO> timeSlicesMap = new HashMap<>(1919);
                        for (int i = 0; i <= ProductionTimeConstant.MINUTE_STOP.intValue(); i++) {
                          buildTimeSlicesMap(startTime, endTime, dayTimeList, timeSlicesMap, i, targetDate); 
                        }
                        timestampToProductionTimeCacheDto.setTimeSlicesDTOMap(timeSlicesMap);
                        return timestampToProductionTimeCacheDto;
                        
                      }).collect(Collectors.toList()); 
            /*
            return (List) list.stream().map(productionCalendar -> {
                TimestampToProductionTimeCacheDto timestampToProductionTimeCacheDto = TimestampToProductionTimeCacheDto.builder().TenantId(productionCalendar.getTenantId()).CalendarCode(productionCalendar.getCode()).NaturalDay(targetDate).build();
                List<ProductionCalendarDaytime> dayTimeList = this.calendarDaytimeService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                    return v0.getCalendarId();
                }, productionCalendar.getId())).or()).and(i -> {
                    LambdaQueryWrapper lambdaQueryWrapper = (LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) i.between((v0) -> {
                        return v0.getStartTime();
                    }, startTime, endTime)).or()).between((v0) -> {
                        return v0.getEndTime();
                    }, startTime, endTime);
                })).orderByAsc((v0) -> {
                    return v0.getStartTime();
                }));
                List<ProductionCalendarDaytime> dayTimeList2 = (List) dayTimeList.stream().filter(productionCalendarDaytime -> {
                    return (productionCalendarDaytime.getEndTime().equals(startTime) || productionCalendarDaytime.getStartTime().equals(endTime)) ? false : true;
                }).collect(Collectors.toList());
                Map<Integer, CalendarShiftTimeSlicesDTO> timeSlicesMap = new HashMap<>(1919);
                for (int i2 = 0; i2 <= ProductionTimeConstant.MINUTE_STOP.intValue(); i2++) {
                    buildTimeSlicesMap(startTime, endTime, dayTimeList2, timeSlicesMap, i2, targetDate);
                }
                timestampToProductionTimeCacheDto.setTimeSlicesDTOMap(timeSlicesMap);
                return timestampToProductionTimeCacheDto;
            }).collect(Collectors.toList());
            */
        }
        return Lists.newArrayList();
    }
 
    private void buildTimeSlicesMap(LocalDateTime startTime, LocalDateTime endTime, List<ProductionCalendarDaytime> dayTimeList, Map<Integer, CalendarShiftTimeSlicesDTO> timeSlicesDTOMap, int i, LocalDate targetDate) {
        LocalDateTime localDateTime = LocalDateTime.of(targetDate, LocalTime.MIN).plusMinutes(i).plusSeconds(1L);
        ProductionCalendarDaytime productionCalendarDaytime = dayTimeList.stream().filter(dayTime -> {
            return (localDateTime.isAfter(dayTime.getStartTime()) || localDateTime.equals(dayTime.getStartTime())) && (localDateTime.isBefore(dayTime.getEndTime()) || localDateTime.equals(dayTime.getEndTime()));
        }).findFirst().orElse(null);
        if (Func.isEmpty(productionCalendarDaytime)) {
            return;
        }
        LocalDateTime shiftStartTime = productionCalendarDaytime.getStartTime();
        LocalDateTime shiftEndTime = productionCalendarDaytime.getEndTime();
        if (shiftStartTime.isBefore(startTime)) {
            shiftStartTime = startTime;
        }
        if (shiftEndTime.isAfter(endTime)) {
            shiftEndTime = endTime;
        }
        CalendarShiftTimeSlicesDTO calendarShiftTimeSlicesDTO = CalendarShiftTimeSlicesDTO.builder().shiftTimeType(productionCalendarDaytime.getShiftType().toString()).endTime(LocalDateTimeUtils.LocalDateTimeToDate(shiftEndTime)).startTime(LocalDateTimeUtils.LocalDateTimeToDate(shiftStartTime)).shiftIndex(productionCalendarDaytime.getShiftIndex()).shiftTimeType(productionCalendarDaytime.getShiftType().toString()).factoryDate(LocalDateTimeUtils.formatTimeLocalDate(productionCalendarDaytime.getCalendarDate(), DateConstant.PATTERN_DATE)).factoryMonth(productionCalendarDaytime.getMonth()).factoryWeek(productionCalendarDaytime.getWeek()).factoryYear(productionCalendarDaytime.getYear()).build();
        timeSlicesDTOMap.put(Integer.valueOf(i), calendarShiftTimeSlicesDTO);
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<CalendarSimpleVO> getCalendarList(String tenantId) {
        return list(Wrappers.<ProductionCalendar>query().lambda()
                .eq(ProductionCalendar::getTenantId, tenantId))
              .stream()
              .map(productionCalendar -> {
                  CalendarSimpleVO calendarSimpleVO = new CalendarSimpleVO();
                  BeanUtils.copyProperties(productionCalendar, calendarSimpleVO);
                  return calendarSimpleVO;
                }).collect(Collectors.toList());
        /*
        return (List) list((Wrapper) Wrappers.query().lambda().eq((v0) -> {
            return v0.getTenantId();
        }, tenantId)).stream().map(productionCalendar -> {
            CalendarSimpleVO calendarSimpleVO = new CalendarSimpleVO();
            BeanUtils.copyProperties(productionCalendar, calendarSimpleVO);
            return calendarSimpleVO;
        }).collect(Collectors.toList());
        */
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<CalendarShiftDetailDTO> getDaytimeList(String tenantId, String calendarCode, Integer year) {
        List<CalendarShiftDetailDTO> result = new ArrayList<>();
        Optional.ofNullable(getOne(new QueryWrapper<ProductionCalendar>().lambda()
                .eq(ProductionCalendar::getCode, calendarCode)
                .eq(ProductionCalendar::getTenantId, Long.valueOf(tenantId))
                .eq(ProductionCalendar::getYear, year))).ifPresent(calendar -> {
            List<ProductionCalendarDaytime> list = this.calendarDaytimeService.list(new QueryWrapper<ProductionCalendarDaytime>().lambda().eq(ProductionCalendarDaytime::getCalendarId, calendar.getId()).eq(ProductionCalendarDaytime::getCalendarDate, LocalDate.now()));
            if (Func.isNotEmpty(list)) {
                list.forEach(productionCalendarDaytime -> {
                    CalendarShiftDetailDTO calendarShiftDetailDTO = new CalendarShiftDetailDTO();
                    calendarShiftDetailDTO.setCode(calendarCode);
                    calendarShiftDetailDTO.setCalendarDay(DateUtil.format(productionCalendarDaytime.getCalendarDate(), DateConstant.PATTERN_DATE));
                    calendarShiftDetailDTO.setFactoryDate(DateUtil.format(productionCalendarDaytime.getCalendarDate(), DateConstant.PATTERN_DATE));
                    calendarShiftDetailDTO.setFactoryMonth(productionCalendarDaytime.getMonth());
                    calendarShiftDetailDTO.setFactoryWeek(productionCalendarDaytime.getWeek());
                    calendarShiftDetailDTO.setFactoryYear(productionCalendarDaytime.getYear());
                    calendarShiftDetailDTO.setShiftIndex(productionCalendarDaytime.getShiftIndex());
                    calendarShiftDetailDTO.setShiftTimeType(productionCalendarDaytime.getShiftType().toString());
                    calendarShiftDetailDTO.setStartTime(LocalDateTimeUtils.LocalDateTimeToDate(productionCalendarDaytime.getStartTime()));
                    result.add(calendarShiftDetailDTO);
                });
            }
        });
        return result;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<CalendarCacheDTO> buildCalendarShift(CacheBuildDTO cacheBuildDTO) {
        List<CalendarCacheDTO> calendarCacheDTOList = new LinkedList<>();
        LocalDate targetDate = cacheBuildDTO.getTargetDate();
        Set<String> tenantIds = cacheBuildDTO.getTenantIds();
        
        List<ProductionCalendar> list = list(new QueryWrapper<ProductionCalendar>().lambda()
                .in(ProductionCalendar::getTenantId, tenantIds)
                .eq(ProductionCalendar::getYear, targetDate.getYear()));
        /*
        List<ProductionCalendar> list = list((Wrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().in((v0) -> {
            return v0.getTenantId();
        }, tenantIds)).eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(targetDate.getYear())));
        */
        if (Func.isNotEmpty(list)) {
            Map<String, List<ProductionCalendar>> collect = list.stream().collect(Collectors.groupingBy(ProductionCalendar::getTenantId));
            /*
            Map<String, List<ProductionCalendar>> collect = (Map) list.stream().collect(Collectors.groupingBy((v0) -> {
                return v0.getTenantId();
            }));*/
            
            collect.forEach((tenantId, productionList) -> {
                productionList.forEach(productionCalendar -> {
                    log.info("处理的租户:{},处理的日历code:{}", tenantId, productionCalendar.getCode());
                    List<ProductionCalendarDaytime> dayTimeList = this.calendarDaytimeService.list(new QueryWrapper<ProductionCalendarDaytime>().lambda()
                            .eq(ProductionCalendarDaytime::getCalendarId, productionCalendar.getId())
                            .eq(ProductionCalendarDaytime::getCalendarDate, targetDate)
                            .orderByAsc(ProductionCalendarDaytime::getStartTime));
                    
                    if (Func.isNotEmpty(dayTimeList)) {
                        CalendarCacheDTO calendarCacheDTO = CalendarCacheDTO.builder().tenantId(tenantId).calendarCode(productionCalendar.getCode()).factoryDate(DateUtil.formatDate(targetDate)).factoryMonth(dayTimeList.get(0).getMonth()).factoryWeek(dayTimeList.get(0).getWeek()).factoryYear(dayTimeList.get(0).getYear()).build();
                        List<ShiftCacheDTO> shiftCacheDTOList = dayTimeList.stream().map(dayTime -> {
                            return ShiftCacheDTO.builder().shiftIndex(dayTime.getShiftIndex()).shiftTimeType(dayTime.getShiftType()).startTime(Long.valueOf(dayTime.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli())).endTime(Long.valueOf(dayTime.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli())).build();
                        }).collect(Collectors.toList());
                        calendarCacheDTO.setShiftCacheDTOS(shiftCacheDTOList);
                        calendarCacheDTOList.add(calendarCacheDTO);
                    }
                });
            });
        }
        return calendarCacheDTOList;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<CurShiftDTO> getCurShift(String tenantId, List<String> calendarCodeList) {
        List<ProductionCalendar> list = list(new QueryWrapper<ProductionCalendar>().lambda()
                .in(ProductionCalendar::getCode, calendarCodeList)
                .eq(ProductionCalendar::getYear, LocalDateTimeUtils.getYear(LocalDate.now()))
                .eq(ProductionCalendar::getTenantId, tenantId));
        /*
        List<ProductionCalendar> list = list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().in((v0) -> {
            return v0.getCode();
        }, calendarCodeList)).eq((v0) -> {
            return v0.getYear();
        }, LocalDateTimeUtils.getYear(LocalDate.now()))).eq((v0) -> {
            return v0.getTenantId();
        }, tenantId));*/
        List<CurShiftDTO> curShiftDTOS = new ArrayList<>();
        if (Func.isNotEmpty(list)) {
            list.forEach(productionCalendar -> {
                CurShiftDTO curShiftDTO = new CurShiftDTO();//ProductionCalendarDay::getCalendarDate
                curShiftDTO.setCalendarCode(productionCalendar.getCode());
                ProductionCalendarDay one = this.calendarDayService.getOne(new QueryWrapper<ProductionCalendarDay>().lambda()
                        .eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId())
                        .eq(ProductionCalendarDay::getCalendarDate, LocalDate.now()));
                if (Func.isNull(one)) {
                    curShiftDTO.setShiftIndex(-1);
                } else {
                    ShiftVO shiftDetail = this.shiftModelService.getShiftDetail(one.getModelId());
                    shiftDetail.getShiftDetailVOList().forEach(shiftDetailVO -> {
                        Integer shiftStartTime = shiftDetailVO.getShiftStartTime();
                        Integer shiftEndTime = shiftDetailVO.getShiftEndTime();
                        LocalDateTime startTime = LocalDateTimeUtils.plus(LocalDate.now(), shiftStartTime.intValue(), ChronoUnit.MINUTES);
                        LocalDateTime endTime = LocalDateTimeUtils.plus(LocalDate.now(), shiftEndTime.intValue(), ChronoUnit.MINUTES);
                        if (LocalDateTime.now().isBefore(endTime) && LocalDateTime.now().isAfter(startTime)) {
                            curShiftDTO.setShiftIndex(shiftDetailVO.getShiftIndex());
                            curShiftDTO.setIndexName(shiftDetailVO.getIndexName());
                        }
                    });
                    if (curShiftDTO.getShiftIndex() == null) {
                        curShiftDTO.setShiftIndex(-1);
                    }
                }
                curShiftDTOS.add(curShiftDTO);
            });
        }
        return curShiftDTOS;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public Map<String, FactoryDayInfoDTO> getFactoryDayInfo(LocalDateTime date, String tenantId, List<String> calendarCodeList) {
        List<FactoryDayInfoDTO> factoryDayInfoDTOList = new ArrayList<>();
        Integer minute = Integer.valueOf((date.getHour() * 60) + date.getMinute());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATE);
        String str = LocalDate.now().format(formatter);
        calendarCodeList.forEach(calendarCode -> {
            String redisKey = CommonUtil.getReallyCacheName(TimeSliceCache.CALENDAR_CACHE, TimeSliceCache.CALENDARS_CODE.concat(calendarCode).concat(":").concat(TimeSliceCache.MINUTE_POINT), str);
            CalendarShiftTimeSlicesDTO calendarShiftTimeSlicesDTO = (CalendarShiftTimeSlicesDTO) this.bladeRedis.hGet(redisKey, minute);
            if (Func.isNotEmpty(calendarShiftTimeSlicesDTO)) {
                FactoryDayInfoDTO factoryDayInfoDTO = FactoryDayInfoDTO.builder().calendarCode(calendarCode).factoryDay(LocalDate.parse(calendarShiftTimeSlicesDTO.getFactoryDate(), formatter)).build();
                factoryDayInfoDTOList.add(factoryDayInfoDTO);
            }
        });
        if (Func.isEmpty(factoryDayInfoDTOList)) {
            return new HashMap<>(16);
        }
        Map<String, Long> map = list(new QueryWrapper<ProductionCalendar>().lambda()
                .eq(ProductionCalendar::getYear, Integer.valueOf(date.getYear()))
                .in(ProductionCalendar::getCode, calendarCodeList)
                .eq(ProductionCalendar::getTenantId, tenantId))
                .stream()
                .collect(Collectors.toMap(ProductionCalendar::getCode, ProductionCalendar::getId));
        /*
        Map<String, Long> map = (Map) list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(date.getYear()))).in((v0) -> {
            return v0.getCode();
        }, calendarCodeList)).eq((v0) -> {
            return v0.getTenantId();
        }, tenantId)).stream().collect(Collectors.toMap((v0) -> {
            return v0.getCode();
        }, (v0) -> {
            return v0.getId();
        }));*/
        //ProductionCalendarDay::getCalendarId
        return factoryDayInfoDTOList.stream().peek(fac -> {
            Long id = map.get(fac.getCalendarCode());
            ProductionCalendarDay productionCalendarDay = this.calendarDayService.getOne(new QueryWrapper<ProductionCalendarDay>().lambda()
                    .eq(ProductionCalendarDay::getCalendarDate, fac.getFactoryDay())
                    .eq(ProductionCalendarDay::getCalendarId, id));
            if (Func.isNotEmpty(productionCalendarDay)) {
                fac.setStartTime(productionCalendarDay.getStartTime());
                fac.setEndTime(productionCalendarDay.getEndTime());
            }
        }).collect(Collectors.toMap(FactoryDayInfoDTO::getCalendarCode, v -> v));
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public ShiftSlicesDTO getShiftSlices(ShiftSlicesClientDTO shiftSlicesClientDTO) {
        String tenantId = shiftSlicesClientDTO.getTenantId();
        String calendarCode = shiftSlicesClientDTO.getCalendarCode();
        Integer shiftIndex = shiftSlicesClientDTO.getShiftIndex();
        LocalDate localDate = shiftSlicesClientDTO.getLocalDate();
        ShiftSlicesDTO shiftSlicesDTO = new ShiftSlicesDTO();
        Optional.ofNullable(this.shiftModelService.getShiftTime(tenantId, shiftIndex, localDate, calendarCode)).ifPresent(shiftDetail -> {
            Integer shiftStartTime = shiftDetail.getShiftStartTime();
            Integer shiftEndTime = shiftDetail.getShiftEndTime();
            LocalDateTime start = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0)).plus(shiftStartTime.intValue(), (TemporalUnit) ChronoUnit.MINUTES);
            LocalDateTime end = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0)).plus(shiftEndTime.intValue(), (TemporalUnit) ChronoUnit.MINUTES);
            shiftSlicesDTO.setStartTime(start);
            shiftSlicesDTO.setEndTime(end);
        });
        return shiftSlicesDTO;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<ShiftSlicesCalendarCodeDTO> getShiftSlicesList(ShiftSlicesClientCalendarCodesDTO shiftSlicesClientCalendarCodesDTO) {
        List<String> calendarCodes = shiftSlicesClientCalendarCodesDTO.getCalendarCodes();
        ArrayList<ShiftSlicesCalendarCodeDTO> shiftSlicesCalendarCodeDTOList = new ArrayList<>();
        calendarCodes.forEach(calendarCode -> {
            ShiftSlicesClientDTO shiftSlicesClientDTO = new ShiftSlicesClientDTO();
            shiftSlicesClientDTO.setShiftIndex(shiftSlicesClientCalendarCodesDTO.getShiftIndex());
            shiftSlicesClientDTO.setCalendarCode(calendarCode);
            shiftSlicesClientDTO.setTenantId(shiftSlicesClientCalendarCodesDTO.getTenantId());
            shiftSlicesClientDTO.setLocalDate(shiftSlicesClientCalendarCodesDTO.getLocalDate());
            ShiftSlicesDTO data = getShiftSlices(shiftSlicesClientDTO);
            ShiftSlicesCalendarCodeDTO shiftSlicesCalendarCodeDTO = new ShiftSlicesCalendarCodeDTO();
            shiftSlicesCalendarCodeDTO.setCalendarCode(calendarCode);
            shiftSlicesCalendarCodeDTO.setStartTime(data.getStartTime());
            shiftSlicesCalendarCodeDTO.setEndTime(data.getEndTime());
            shiftSlicesCalendarCodeDTOList.add(shiftSlicesCalendarCodeDTO);
        });
        return shiftSlicesCalendarCodeDTOList;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public Long getShiftTimeWithoutRest(String calendarCode) {
        ProductionCalendar productionCalendar = getOne(Wrappers.<ProductionCalendar>lambdaQuery()
                .eq(ProductionCalendar::getCode, calendarCode)
                .eq(ProductionCalendar::getYear, Calendar.getInstance().get(1)));
        /*
        ProductionCalendar productionCalendar = (ProductionCalendar) getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getCode();
        }, calendarCode)).eq((v0) -> {
            return v0.getYear();
        }, Integer.valueOf(Calendar.getInstance().get(1))));
        */
        if (Func.isEmpty(productionCalendar)) {
            return 0L;
        }
        List<ProductionCalendarDay> list = this.calendarDayService.list(Wrappers.<ProductionCalendarDay>lambdaQuery()
                .eq(ProductionCalendarDay::getCalendarId, productionCalendar.getId()));
        /*
        List<ProductionCalendarDay> list = this.calendarDayService.list((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getCalendarId();
        }, productionCalendar.getId()));*/
        List<Long> productionCalendarDayIds = list.stream().map((v0) -> {
            return v0.getId();
        }).collect(Collectors.toList());
        
        LocalDate now = LocalDate.now();
        List<ProductionCalendarDaytime> productionCalendarDaytimeList = this.calendarDaytimeService.list(Wrappers.<ProductionCalendarDaytime>lambdaQuery()
                .in(ProductionCalendarDaytime::getCalendarDayId, productionCalendarDayIds)
                .eq(ProductionCalendarDaytime::getShiftType, ProductionTimeConstant.WORK_TIME)
                .eq(ProductionCalendarDaytime::getCalendarDate, now));
        return Long.valueOf(productionCalendarDaytimeList.stream().mapToLong(productionCalendarDaytime -> {
            return LocalDateTimeUtils.betweenTwoTime(productionCalendarDaytime.getStartTime(), productionCalendarDaytime.getEndTime(), ChronoUnit.SECONDS);
        }).sum());
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public ShiftIndexNameVO getShiftIndexName() {
        return this.shiftModelService.getShiftIndexName();
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public String getShiftIndexNameByCodeAndYear(String calendarCode, CalendarShiftTimeSlicesDTO calendarShiftTimeSlicesDTO) {
        return this.baseMapper.getShiftIndexNameByCodeAndYear(calendarCode, calendarShiftTimeSlicesDTO.getFactoryYear(), calendarShiftTimeSlicesDTO.getFactoryDate(), calendarShiftTimeSlicesDTO.getShiftIndex(), AuthUtil.getTenantId());
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public String expireCalendarInfo() {
        String code = this.baseMapper.expireCalendarInfo(LocalDate.now().getYear());
        LocalDate now = LocalDate.now();
        LocalDate lastDayOfYear = LocalDate.now().with(TemporalAdjusters.lastDayOfYear());
        Integer difference = LocalDateTimeUtils.getDifference(now.minusDays(1L), lastDayOfYear);
        log.info("当前时间:[{}],最后一天:[{}]", now, lastDayOfYear);
        if (difference.intValue() <= ProductionTimeConstant.SEVEN_DAYS.intValue()) {
            return String.format("您的【%s】生产日历将在【%s】天后过期,为了能正常使用,请及时创建新的日历(如果您想更便捷使用,建议创建多年份日历)", code, difference);
        }
        return null;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    @Transactional(rollbackFor = {Exception.class})
    public Boolean deleteCalendar(Long id) {
        //Workstation::getCalendarCodeWaiting
        Optional.ofNullable(getById(id)).ifPresent(calendar -> {
            List<Workstation> list = this.workstationService.list(new QueryWrapper<Workstation>().lambda()
                    .eq(Workstation::getCalendarCode, calendar.getCode()).eq(Workstation::getStatus, CommonConstant.ENABLE).or()
                    .eq(Workstation::getCalendarCodeWaiting, calendar.getCode()));
            if (Func.isNotEmpty(list)) {
                throw new ServiceException(MessageUtils.message("calendar.has.be.used", new Object[0]));
            }
            removeById(id);
            this.calendarDayService.remove(Wrappers.<ProductionCalendarDay>lambdaQuery().eq(ProductionCalendarDay::getCalendarId, id));
            this.calendarDaytimeService.remove(Wrappers.<ProductionCalendarDaytime>lambdaQuery().eq(ProductionCalendarDaytime::getCalendarId, id));
        });
        return true;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public Long getShiftModelByFactoryDay(String calendarCode, LocalDate factoryDay) {
        return this.baseMapper.getShiftModelByFactoryDay(calendarCode, Integer.valueOf(LocalDate.now().getYear()), factoryDay);
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<ShiftTimeDetailVO> getShiftDetailDates(String calendarCode, List<LocalDate> dates) {
        
        //ProductionCalendar
        QueryWrapper<Object> wrapper = Wrappers.<Object>query()
                .eq("bpc.is_deleted", 0)
                .eq("bpcd.is_deleted", 0)
                .eq("bpc.code", calendarCode)
                .nested(q -> q.in("bpcd.calendar_date", dates))
                .orderByAsc("calendar_date", "shift_index");
        return this.baseMapper.getShiftDetailDates(wrapper);
        /*
        return this.baseMapper.getShiftDetailDates((QueryWrapper) ((QueryWrapper) ((QueryWrapper) ((QueryWrapper) ((QueryWrapper) Wrappers.query().eq("bpc.is_deleted", 0)).eq("bpcd.is_deleted", 0)).eq("bpc.code", calendarCode)).nested(q -> {
            QueryWrapper queryWrapper = (QueryWrapper) q.in("bpcd.calendar_date", dates);
        })).orderByAsc("calendar_date", new String[]{"shift_index"}));*/
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<CalendarShiftDTO> getTimeShiftAll(String calendarCode, Date startTime, Date endTime) {
        
        QueryWrapper<Object> wrapper = Wrappers.query().eq("bpc.is_deleted", 0)
                .eq("bpcd.is_deleted", 0)
                .ne("bsd.shift_index", ProductionTimeConstant.OUT_OF_SHIFT_TIME)
                .nested(q -> q.between("bpcd.calendar_date", DateUtil.formatDate(DateUtil.minusDays(startTime, 1L)), DateUtil.formatDate(DateUtil.plusDays(endTime, 1L)))
                .eq("bpc.code", calendarCode)).orderByAsc("calendar_date",  "shift_index" );
        return this.baseMapper.getTimeShiftAll(wrapper);
        /*
        return this.baseMapper.getTimeShiftAll(Wrappers.query().eq("bpc.is_deleted", 0)
                .eq("bpcd.is_deleted", 0))
                .ne("bsd.shift_index", ProductionTimeConstant.OUT_OF_SHIFT_TIME)
                .nested(q -> {
                        QueryWrapper queryWrapper = q.between("bpcd.calendar_date", DateUtil.formatDate(DateUtil.minusDays(startTime, 1L)), DateUtil.formatDate(DateUtil.plusDays(endTime, 1L)));
        })).eq("bpc.code", calendarCode)
        .orderByAsc("calendar_date", new String[]{"shift_index"}));
        */
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<ShiftIndexInfoDTO> getWorkstationShiftIndexListByDate(Long workstationId, LocalDate localDate) {
        List<ShiftIndexInfoDTO> result = Lists.newArrayList();
        
        Workstation workstation = (Workstation)this.workstationService.getOne(Wrappers.<Workstation>lambdaQuery()
                .eq(Workstation::getId, workstationId)
                .eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode())
                .isNotNull(Workstation::getCalendarCode));
        /*
        Workstation workstation = (Workstation) this.workstationService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getId();
        }, workstationId)).eq((v0) -> {
            return v0.getType();
        }, WorkstationTypeEnum.MACHINE.getCode())).isNotNull((v0) -> {
            return v0.getCalendarCode();
        }));*/
        if (Func.isNotEmpty(workstation)) {
            List<ShiftDetailInfoDTO> shiftIndexList = getWorkstationShiftIndexListByDate(workstation.getCalendarCode(), Integer.valueOf(localDate.getYear()), LocalDateTimeUtils.formatTimeLocalDate(localDate, DateConstant.PATTERN_DATE));
            shiftIndexList.forEach(x -> {
                ShiftIndexInfoDTO shiftIndexInfoDTO = new ShiftIndexInfoDTO();
                if (x.getCalendarDate().isBefore(localDate)) {
                    if (x.getShiftStartTime().intValue() < CommonConstant.MINUTES_OF_DAY.intValue() && x.getShiftEndTime().intValue() > CommonConstant.MINUTES_OF_DAY.intValue()) {
                        shiftIndexInfoDTO.setShiftIndex(x.getShiftIndex());
                        shiftIndexInfoDTO.setDateShiftIndex(DateUtil.format(x.getCalendarDate(), "yyyyMMdd") + "/" + x.getShiftIndex());
                        shiftIndexInfoDTO.setDateShiftIndexName(DateUtil.format(x.getCalendarDate(), "MM/dd") + x.getIndexName());
                        result.add(shiftIndexInfoDTO);
                        return;
                    }
                    return;
                }
                shiftIndexInfoDTO.setShiftIndex(x.getShiftIndex());
                shiftIndexInfoDTO.setDateShiftIndex(DateUtil.format(x.getCalendarDate(), "yyyyMMdd") + "/" + x.getShiftIndex());
                shiftIndexInfoDTO.setDateShiftIndexName(DateUtil.format(x.getCalendarDate(), "MM/dd") + x.getIndexName());
                result.add(shiftIndexInfoDTO);
            });
        }
        return result;
    }
 
    private List<ShiftDetailInfoDTO> getWorkstationShiftIndexListByDate(String calendarCode, Integer year, String date) {
        Integer maxShiftIndex;
        List<ShiftDetailInfoDTO> workstationShiftIndexListByDate = this.baseMapper.getWorkstationShiftIndexListByDate(calendarCode, year, date);
        String maxShiftIndexStr = ParamCache.getValue("system.shift.max");
        if (Func.isEmpty(maxShiftIndexStr)) {
            maxShiftIndex = CalendarConstant.DEFAULT_MAX_SHIFT_INDEX;
        } else {
            maxShiftIndex = Integer.valueOf(maxShiftIndexStr);
        }
        Integer num = maxShiftIndex;
        workstationShiftIndexListByDate.removeIf(s -> {
            return s.getShiftIndex().intValue() > num.intValue();
        });
        return workstationShiftIndexListByDate;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public Boolean associateWorkstation(CalendarAssociateWorkstationVO calendarAssociateWorkstationVO) {
        return this.workstationService.update(Wrappers.<Workstation>lambdaUpdate()
                  .set(Workstation::getCalendarCodeWaiting, calendarAssociateWorkstationVO.getCalendarCode())
                  .in(Workstation::getId, calendarAssociateWorkstationVO.getWorkstationIdList()));
        /*
        return Boolean.valueOf(this.workstationService.update((Wrapper) ((LambdaUpdateWrapper) Wrappers.lambdaUpdate().set((v0) -> {
            return v0.getCalendarCodeWaiting();
        }, calendarAssociateWorkstationVO.getCalendarCode())).in((v0) -> {
            return v0.getId();
        }, calendarAssociateWorkstationVO.getWorkstationIdList())));*/
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<ShiftIndexNameDTO> queryShiftIndexName(String calendarCode, Integer year, String day) {
        Integer maxShiftIndex;
        List<ShiftIndexNameDTO> shiftIndexNameDTOList = this.baseMapper.queryShiftIndexName(calendarCode, year, day);
        String maxShiftIndexStr = ParamCache.getValue("system.shift.max");
        if (Func.isEmpty(maxShiftIndexStr)) {
            maxShiftIndex = CalendarConstant.DEFAULT_MAX_SHIFT_INDEX;
        } else {
            maxShiftIndex = Integer.valueOf(maxShiftIndexStr);
        }
        Integer num = maxShiftIndex;
        shiftIndexNameDTOList.removeIf(s -> {
            return s.getShiftIndex().intValue() > num.intValue();
        });
        return shiftIndexNameDTOList;
    }
 
    @Override // org.springblade.modules.cps.service.ICalendarService
    public List<ShiftInfoDTO> listShiftInfo(List<Long> workstationIdList, LocalDate startDay, LocalDate endDay) {
        Integer maxShiftIndex;
        List<ShiftInfoDTO> shiftInfoDTOList = this.baseMapper.listShiftInfo(workstationIdList, startDay, endDay);
        String maxShiftIndexStr = ParamCache.getValue("system.shift.max");
        if (Func.isEmpty(maxShiftIndexStr)) {
            maxShiftIndex = CalendarConstant.DEFAULT_MAX_SHIFT_INDEX;
        } else {
            maxShiftIndex = Integer.valueOf(maxShiftIndexStr);
        }
        Integer num = maxShiftIndex;
        shiftInfoDTOList.removeIf(s -> {
            return s.getShiftIndex().intValue() > num.intValue();
        });
        return shiftInfoDTOList;
    }
}