AttendanceServiceImpl.java
39.1 KB
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
package com.ruoyi.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.global.Result;
import com.ruoyi.common.global.ResultCode;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.driver.mapper.DriverMapper;
import com.ruoyi.num.domain.RuleNum;
import com.ruoyi.num.service.IRuleNumService;
import com.ruoyi.pojo.dto.*;
import com.ruoyi.pojo.request.AgainCreateRequestVo;
import com.ruoyi.pojo.vo.*;
import com.ruoyi.scheduling.domain.RuleScheduling;
import com.ruoyi.scheduling.service.IRuleSchedulingService;
import com.ruoyi.scheduling.service.impl.RuleSchedulingServiceImpl;
import com.ruoyi.service.*;
import com.ruoyi.utils.ConstDateUtil;
import com.ruoyi.utils.ToolUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.ruoyi.common.RuleSchedulingProperties.*;
import static com.ruoyi.common.WeekEnum.FREE;
import static com.ruoyi.service.impl.RuleNumSettingServiceImpl.getFreeRuleSchedulingDto;
/**
* @author 20412
*/
@Service
public class AttendanceServiceImpl implements AttendanceService {
/**
* 中文星期声明
*/
private static final String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
@Autowired
private DriverMapper driverMapper;
@Autowired
private IRuleSchedulingService ruleSchedulingService;
@Autowired
private RuleNumSettingService ruleNumSettingService;
@Autowired
private RuleSettingDriverService settingDriverService;
@Autowired
private RuleSettingSchedulingService settingSchedulingService;
@Autowired
private RuleAttendanceMainService attendanceMainService;
@Autowired
private RuleAttendanceMainHelpService attendanceMainHelpService;
@Autowired
private IRuleSchedulingService schedulingService;
@Autowired
private RuleAttendanceMainHelpService helpService;
@Autowired
private IRuleNumService ruleNumService;
@Override
public List<PeopleResponseVo> getDriverInfo(Long id) {
List<PeopleResponseVo> vos;
if (id != null) {
vos = driverMapper.queryAttendanceInfoById(id);
} else {
// 获取所有为设置考勤的人员
vos = driverMapper.queryAttendanceInfoAll();
}
return vos;
}
@Override
public List<SchedulingResponseVo> getSchedulingList(SchedulingRequestVo vo) {
// 查询排班设置表
PageUtils.startPage();
List<SchedulingResponseVo> vos = ruleNumSettingService.getSchedulingList(vo);
if (CollectionUtil.isEmpty(vos)) {
return vos;
}
List<Integer> settingIdList = vos.stream().map(SchedulingResponseVo::getId).collect(Collectors.toList());
// 查询对应的规则
List<DriverDto> driverList = settingDriverService.queryDriverListBySettingId(settingIdList);
List<RuleSchedulingDto> schedulingList = settingSchedulingService.querySchedulingListBySettingId(settingIdList);
handleSchedulingResponse(vos, driverList, schedulingList);
return vos;
}
@Override
public List<RuleNumTemplateVo> getTemplate(Long id) {
RuleNum ruleNum = ruleNumService.selectRuleNumById(id);
if (Objects.isNull(ruleNum)) {
return null;
}
// 当为固定规则时
if (RULE_TYPE_FIXED.equals(ruleNum.getRuleType())) {
return handleFixedTemplate(ruleNum);
}
// 处理按星期
else {
return handleWeekTemplate(ruleNum);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void saveSchedulingSetting(SchedulingSettingDto dto) {
RuleNumSetting setting = getRuleNumSetting(dto);
ruleNumSettingService.save(setting);
List<RuleSettingScheduling> schedulingList = getSettingSchedulingList(dto, setting.getId());
settingSchedulingService.saveBatch(schedulingList);
if (CollectionUtil.isNotEmpty(dto.getPeopleList())) {
List<RuleSettingDriver> peopleList = getSettingDriverList(dto, setting.getId());
settingDriverService.remove(new QueryWrapper<RuleSettingDriver>().lambda().in(RuleSettingDriver::getJobCode, peopleList.stream().map(RuleSettingDriver::getJobCode).collect(Collectors.toList())));
settingDriverService.saveBatch(peopleList);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateSchedulingSetting(SchedulingDto dto) {
RuleNumSetting setting = new RuleNumSetting();
getRumSettingByDto(dto, setting);
ruleNumSettingService.updateById(setting);
// 更新规则
settingSchedulingService.remove(new QueryWrapper<RuleSettingScheduling>().lambda().eq(RuleSettingScheduling::getSettingId, dto.getId()));
settingSchedulingService.saveBatch(dto.getRuleSchedulingList());
// 更新人员
settingDriverService.remove(new QueryWrapper<RuleSettingDriver>()
.lambda()
.eq(RuleSettingDriver::getSettingId, dto.getId()));
settingDriverService.saveBatch(dto.getPeopleList());
}
@Override
public void deleteSchedulingSetting(Integer settingId) {
ruleNumSettingService.removeById(settingId);
// 更新规则
settingSchedulingService.remove(new QueryWrapper<RuleSettingScheduling>().lambda().eq(RuleSettingScheduling::getSettingId, settingId));
// 更新人员
settingDriverService.remove(new QueryWrapper<RuleSettingDriver>()
.lambda()
.eq(RuleSettingDriver::getSettingId, settingId));
}
@Override
public List<RuleAttendanceMain> queryAttendanceMain(RuleAttendanceMainRequestVo vo) {
PageUtils.startPage();
List<RuleAttendanceMain> list = attendanceMainService.list(new QueryWrapper<RuleAttendanceMain>()
.lambda()
.eq(RuleAttendanceMain::getSchedulingDate, vo.getDate())
.eq(StringUtils.isNotEmpty(vo.getJobCode()), RuleAttendanceMain::getJobCode, vo.getJobCode()));
return list;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateAttendance(UpdateAttendanceVo vo) {
RuleAttendanceMain main = new RuleAttendanceMain();
RuleScheduling ruleScheduling = ruleSchedulingService.selectRuleSchedulingById(vo.getRuleId().longValue());
ToolUtils.copyRule(ruleScheduling, main);
List<RuleAttendanceMain> mainList = handleRuleUpdate(vo, main, ruleScheduling);
attendanceMainService.updateByJobCodeTotalQuantity(vo, mainList);
}
private static List<RuleAttendanceMain> handleRuleUpdate(UpdateAttendanceVo vo, RuleAttendanceMain main, RuleScheduling ruleScheduling) {
List<RuleAttendanceMain> mainList = new ArrayList<>();
LocalDate startDate = LocalDate.parse(vo.getStartDate());
LocalDate endDate = LocalDate.parse(vo.getEndDate());
while (startDate.compareTo(endDate) <= 0) {
handleSegmentation(main, ruleScheduling, mainList, startDate);
startDate = startDate.plusDays(1);
}
return mainList;
}
private static void handleSegmentation(RuleAttendanceMain main, RuleScheduling ruleScheduling, List<RuleAttendanceMain> mainList, LocalDate startDate) {
RuleAttendanceMain nowMain = new RuleAttendanceMain();
BeanUtils.copyProperties(main, nowMain);
nowMain.setFirstWorkSignInTime(ConstDateUtil.dateAddition(startDate.toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getFirstWorkSignInTime())));
if (HAVE_SEGMENTATION.equals(ruleScheduling.getSecondFlag())) {
nowMain.setFirstQuittingSignInTime(ConstDateUtil.dateAddition(startDate.toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getFirstQuittingSignInTime())));
nowMain.setSecondWorkSignInTime(ConstDateUtil.dateAddition(startDate.toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getSecondWorkSignInTime())));
// 处理隔天
if (TOMORROW_YES.equals(ruleScheduling.getFirstSignInDayTomorrow())) {
nowMain.setSecondQuittingSignInTime(ConstDateUtil.dateAddition(startDate.plusDays(1).toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getSecondQuittingSignInTime())));
} else {
nowMain.setSecondQuittingSignInTime(ConstDateUtil.dateAddition(startDate.toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getSecondQuittingSignInTime())));
}
} else {
// 处理隔天
if (TOMORROW_YES.equals(ruleScheduling.getFirstSignInDayTomorrow())) {
nowMain.setFirstQuittingSignInTime(ConstDateUtil.dateAddition(startDate.plusDays(1).toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getFirstQuittingSignInTime())));
} else {
nowMain.setFirstQuittingSignInTime(ConstDateUtil.dateAddition(startDate.toString(), ConstDateUtil.formatDate("HH:mm:ss", nowMain.getFirstQuittingSignInTime())));
}
}
nowMain.setWorkFlag(ruleScheduling.getId() == 0 ? FREE_FLAG : WORK_FLAG);
nowMain.setSchedulingDate(Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
mainList.add(nowMain);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteAttendance(DeleteAttendanceVo vo) {
attendanceMainService.deleteByJobCodeTotalRecord(vo);
}
@Override
public TableDataInfo getTableMonth(PeopleRequestVo vo) {
PageUtils.startPage();
List<RuleAttendanceMainHelp> helpList = attendanceMainService.queryPeopleList(vo);
long total = new PageInfo(helpList).getTotal();
List<String> jobCodes = helpList.stream().map(RuleAttendanceMainHelp::getJobCode).collect(Collectors.toList());
List<RuleAttendanceMain> mainList = attendanceMainService.queryNowMonthDate(vo, jobCodes);
List<TableMonthResponseVo> voList = handleTableMonthResponseVo(vo.getDate(), helpList, mainList);
return getDataTable(voList, total);
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({"rawtypes", "unchecked"})
protected TableDataInfo getDataTable(List<?> list, Long total) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setMsg("查询成功");
rspData.setRows(list);
rspData.setTotal(total);
return rspData;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateSchedulingByUser(UpdatePeopleAttendanceRequestVo vo) {
if (NOW_RULE.equals(vo.getType())) {
ruleCopyToOtherUser(vo);
} else if (OTHER_RULE.equals(vo.getType())) {
handleOtherRule(vo);
}
}
@Override
public List<PeopleResponseVo> getTodayFreeUser(String date) {
LocalDate localDate = LocalDate.parse(date);
QueryWrapper<RuleAttendanceMain> qw = new QueryWrapper<>();
qw.lambda()
.eq(RuleAttendanceMain::getSchedulingDate, localDate)
.eq(RuleAttendanceMain::getWorkFlag, FREE_FLAG);
List<RuleAttendanceMain> mainList = attendanceMainService.list(qw);
List<PeopleResponseVo> peopleList = mainTransformPeopleResponseVo(mainList);
return peopleList;
}
@Override
public void createAttendance(String month) {
if (StringUtils.isEmpty(month) || !StringUtils.contains(month, "-")) {
return;
}
// 不能生成以前的排班
if (ConstDateUtil.formatDate("yyyy-MM").compareTo(month) > 0) {
throw new RuntimeException("只能生成未来的排班");
}
String[] split = month.split("-");
// 处理规则
List<LocalDate> dateList = ConstDateUtil.getDateSetFromTheCurrentDayToTheEndOfTheMonth(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
List<AttendanceDto> peopleList = ruleNumSettingService.getPeopleListByMonth(month);
createScheduleMethod(dateList, peopleList);
}
public void createScheduleMethod(List<LocalDate> dateList, List<AttendanceDto> peopleList) {
List<Integer> settingList = peopleList.stream().map(AttendanceDto::getSettingId).distinct().collect(Collectors.toList());
// 获取规则集合
List<RuleSchedulingDto> ruleList = schedulingService.selectRuleSchedulingListVoBySettingIds(settingList);
// 获取班次
List<RuleNumDto> ruleNumDtoList = ruleNumSettingService.selectRuleNumBySettingId(settingList);
List<RuleAttendanceMain> attendanceMains = handleAttendanceMain(peopleList, ruleList, ruleNumDtoList, dateList);
attendanceMainService.saveBatch(attendanceMains);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void againCreateAttendance(AgainCreateRequestVo vo) {
// 删除排班和生成日志
deleteScheduleByRule(vo);
//生成排班
createAttendanceByStartDate(vo);
}
@Override
public Result<?> checkScheduleRule(String ruleId) {
// TODO 规则是否绑定
LambdaQueryWrapper<RuleSettingScheduling> qw = new LambdaQueryWrapper<>();
qw.eq(RuleSettingScheduling::getRuleSchedulingId,ruleId);
List<RuleSettingScheduling> list = settingSchedulingService.list(qw);
if (CollectionUtil.isNotEmpty(list)) {
String ruleDictName = ruleNumSettingService.listByIds(list.stream()
.map(RuleSettingScheduling::getSettingId).collect(Collectors.toList()))
.stream().map(RuleNumSetting::getRuleDictName).distinct().collect(Collectors.joining(","));
return Result.ERROR(ResultCode.CODE_202,"该规则已经与" + ruleDictName + "等班次绑定!");
}
return Result.OK();
}
private void createAttendanceByStartDate(AgainCreateRequestVo vo) {
List<AttendanceDto> peopleList = ruleSchedulingService.getPeopleListByJobCodes(vo.getJobCode());
LocalDate parse = LocalDate.parse(vo.getDate());
int day = Integer.parseInt(vo.getDate().split("-")[2]);
List<LocalDate> dateList = ConstDateUtil.getDateSetFromTheCurrentDayToTheEndOfTheMonth(parse.getYear(), parse.getMonthValue(), day);
createScheduleMethod(dateList, peopleList);
}
private void deleteScheduleByRule(AgainCreateRequestVo vo) {
String strDate = LocalDate.now().toString();
String month = vo.getMonth();
String nowMonth = strDate.substring(0, strDate.lastIndexOf("-"));
int compare = month.compareTo(nowMonth);
// 删除生成日志
attendanceMainHelpService.deleteNowMathCreateLog(vo.getMonth(), vo.getJobCode());
if (compare > 0) {
LocalDate endDay;
endDay = getEndDay(vo.getMonth());
vo.setDate(vo.getMonth() + "-" + "01");
ruleSchedulingService.deletePeopleSchedule(vo.getDate(), endDay.toString(), vo.getJobCode());
} else if (compare == 0) {
LocalDate endDay = getEndDay(vo.getMonth());
LocalDate localDate = LocalDate.now().plusDays(1);
vo.setDate(localDate.toString());
ruleSchedulingService.deletePeopleSchedule(vo.getDate(), endDay.toString(), vo.getJobCode());
} else {
throw new RuntimeException("过去的日期排班无法改变!!!");
}
}
private LocalDate getEndDay(@NotBlank String endDayStr) {
String[] split = endDayStr.split("-");
if (Integer.parseInt(split[1]) == 12) {
return LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), 31);
} else {
return LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]) + 1, 1).minusDays(1);
}
}
public List<RuleAttendanceMain> handleAttendanceMain(List<AttendanceDto> peopleList, List<RuleSchedulingDto> ruleList, List<RuleNumDto> ruleNumDtoList, List<LocalDate> dateList) {
if (CollectionUtil.isEmpty(peopleList) || CollectionUtil.isEmpty(ruleList) || CollectionUtil.isEmpty(ruleNumDtoList)) {
return null;
}
Map<Integer, List<AttendanceDto>> peopleMap = transformMapByPeople(peopleList);
Map<Integer, RuleNumDto> ruleNumMap = transformMapByRuleNum(ruleNumDtoList);
Map<Integer, List<RuleSchedulingDto>> ruleMap = transformMapByRule(ruleList);
List<RuleAttendanceMain> ruleAttendanceMainList = new ArrayList<>();
for (Map.Entry<Integer, List<AttendanceDto>> entry : peopleMap.entrySet()) {
Integer settingId = entry.getKey();
List<AttendanceDto> attendanceDtoList = entry.getValue();
RuleNumDto dto = ruleNumMap.get(settingId);
List<RuleAttendanceMain> mainList = new ArrayList<>(entry.getValue().size() * dateList.size());
for (AttendanceDto attendanceDto : attendanceDtoList) {
for (LocalDate date : dateList) {
// 处理固定规则翻班
if (RULE_TYPE_FIXED.equals(dto.getRuleType())) {
handleFixed(date, mainList, ruleMap.get(settingId), attendanceDto, dto.getRuleDictName());
}
// 处理星期翻班
else {
handleWeek(date, mainList, ruleMap.get(settingId), attendanceDto, dto.getRuleDictName());
}
}
}
ruleAttendanceMainList.addAll(mainList);
}
// 记录日志
backCreatePeopleListLog(peopleList, ruleNumMap, dateList);
return ruleAttendanceMainList;
}
private void handleFixed(LocalDate nowDate, List<RuleAttendanceMain> mainList, List<RuleSchedulingDto> ruleSchedulingDtoList, AttendanceDto attendanceDto, String ruleDictName) {
RuleAttendanceMain main = new RuleAttendanceMain();
main.setPosts(attendanceDto.getPosts());
main.setRuleDictName(ruleDictName);
main.setSchedulingDate(Date.from(nowDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
main.setFleetName(attendanceDto.getFleetName());
Date startDate = attendanceDto.getStartDate();
// 匹配当天应设置的规则
RuleSchedulingDto dto = getNowRuleByFixed(nowDate, startDate, ruleSchedulingDtoList);
main.setWorkFlag(dto.getId() == 0 ? FREE_FLAG : WORK_FLAG);
ToolUtils.copyRule(dto, main);
main.setName(attendanceDto.getName());
main.setJobCode(attendanceDto.getJobCode());
// 处理是否分段
handleSegmentationByDate(nowDate, main, dto);
mainList.add(main);
}
private RuleSchedulingDto getNowRuleByFixed(LocalDate nowDate, Date startDate, List<RuleSchedulingDto> ruleSchedulingDtoList) {
LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int daysDifference = new Long(ChronoUnit.DAYS.between(start, nowDate)).intValue();
if (daysDifference < 0) {
return getFreeRuleSchedulingDto(startDate);
}
int length = ruleSchedulingDtoList.size();
return ruleSchedulingDtoList.get(daysDifference % length);
}
public static void handleSegmentationByDate(LocalDate date, RuleAttendanceMain main, RuleSchedulingDto dto) {
main.setFirstWorkSignInTime(ConstDateUtil.dateAddition(date.toString(), ConstDateUtil.formatDate("HH:mm:ss", main.getFirstWorkSignInTime())));
// 不存在分段
if (NO_SEGMENTATION.equals(dto.getSecondFlag())) {
if (TOMORROW_YES.equals(dto.getFirstSignInDayTomorrow())) {
main.setFirstQuittingSignInTime(handleTimeTomorrowByRule(date, main.getFirstQuittingSignInTime()));
} else {
main.setFirstQuittingSignInTime(ConstDateUtil.dateAddition(date.toString(), ConstDateUtil.formatDate("HH:mm:ss", main.getFirstQuittingSignInTime())));
}
}
// 存在分段
else {
main.setFirstQuittingSignInTime(ConstDateUtil.dateAddition(date.toString(), ConstDateUtil.formatDate("HH:mm:ss", main.getFirstQuittingSignInTime())));
main.setSecondWorkSignInTime(ConstDateUtil.dateAddition(date.toString(), ConstDateUtil.formatDate("HH:mm:ss", main.getSecondWorkSignInTime())));
if (TOMORROW_YES.equals(dto.getFirstSignInDayTomorrow())) {
main.setSecondQuittingSignInTime(handleTimeTomorrowByRule(date, main.getSecondQuittingSignInTime()));
} else {
main.setSecondQuittingSignInTime(ConstDateUtil.dateAddition(date.toString(), ConstDateUtil.formatDate("HH:mm:ss", main.getSecondQuittingSignInTime())));
}
}
}
private static Date handleTimeTomorrowByRule(LocalDate date, Date time) {
// 隔天日期需要加1
return ConstDateUtil.dateAddition(date.plusDays(1).toString(), ConstDateUtil.formatDate("HH:mm:ss", time));
}
private void handleWeek(LocalDate date, List<RuleAttendanceMain> mainList, List<RuleSchedulingDto> ruleSchedulingDtoList, AttendanceDto attendanceDto, String ruleDictName) {
RuleAttendanceMain main = new RuleAttendanceMain();
main.setSchedulingDate(Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
main.setPosts(attendanceDto.getPosts());
main.setRuleDictName(ruleDictName);
main.setFleetName(attendanceDto.getFleetName());
RuleSchedulingDto dto = null;
dto = getRuleByWeek(main, ruleSchedulingDtoList, date, attendanceDto.getStartDate());
main.setWorkFlag(dto.getId() == 0 ? FREE_FLAG : WORK_FLAG);
ToolUtils.copyRule(dto, main);
main.setName(attendanceDto.getName());
main.setJobCode(attendanceDto.getJobCode());
handleSegmentationByDate(date, main, dto);
mainList.add(main);
}
private static RuleSchedulingDto getRuleByWeek(RuleAttendanceMain main, List<RuleSchedulingDto> ruleSchedulingDtoList, LocalDate nowDate, Date startDate) {
RuleSchedulingDto dto = null;
switch (nowDate.getDayOfWeek().toString()) {
case "MONDAY":
dto = ruleSchedulingDtoList.get(0);
break;
case "TUESDAY":
dto = ruleSchedulingDtoList.get(1);
break;
case "WEDNESDAY":
dto = ruleSchedulingDtoList.get(2);
break;
case "THURSDAY":
dto = ruleSchedulingDtoList.get(3);
break;
case "FRIDAY":
dto = ruleSchedulingDtoList.get(4);
break;
case "SATURDAY":
dto = ruleSchedulingDtoList.get(5);
break;
case "SUNDAY":
dto = ruleSchedulingDtoList.get(6);
break;
}
LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int daysDifference = new Long(ChronoUnit.DAYS.between(start, nowDate)).intValue();
if (daysDifference < 0) {
return getFreeRuleSchedulingDto(startDate);
}
return dto;
}
private Map<Integer, List<AttendanceDto>> transformMapByPeople(List<AttendanceDto> peopleList) {
Map<Integer, List<AttendanceDto>> map = new HashMap<>(16);
for (AttendanceDto dto : peopleList) {
List<AttendanceDto> dtoList = map.get(dto.getSettingId());
if (CollectionUtil.isEmpty(dtoList)) {
map.put(dto.getSettingId(), new ArrayList<>(Arrays.asList(dto)));
} else {
dtoList.add(dto);
}
}
return map;
}
private Map<Integer, RuleNumDto> transformMapByRuleNum(List<RuleNumDto> ruleNumDtoList) {
Map<Integer, RuleNumDto> map = new HashMap<>(16);
for (RuleNumDto ruleNumDto : ruleNumDtoList) {
map.put(ruleNumDto.getSettingId(), ruleNumDto);
}
return map;
}
private Map<Integer, List<RuleSchedulingDto>> transformMapByRule(List<RuleSchedulingDto> ruleList) {
Map<Integer, List<RuleSchedulingDto>> map = new HashMap<>(16);
for (RuleSchedulingDto dto : ruleList) {
List<RuleSchedulingDto> dtoList = map.get(dto.getSettingId());
if (CollectionUtil.isEmpty(dtoList)) {
map.put(dto.getSettingId(), new ArrayList<>(Arrays.asList(dto)));
} else {
dtoList.add(dto);
}
}
for (Map.Entry<Integer, List<RuleSchedulingDto>> entry : map.entrySet()) {
entry.getValue().sort(Comparator.comparing(RuleSchedulingDto::getSort));
}
return map;
}
public void backCreatePeopleListLog(List<AttendanceDto> peopleList, Map<Integer, RuleNumDto> ruleNumDtoList, List<LocalDate> dateList) {
if (CollectionUtil.isEmpty(dateList)) {
return;
}
String month = dateList.get(0).getYear() + "-" + dateList.get(0).getMonthValue();
List<RuleAttendanceMainHelp> monthPeople = helpService.queryNowMonthPeopleByJobCodes(peopleList, month);
Map<String, Object> map = transFormMonthPeople(monthPeople);
peopleList = peopleList.stream().filter(item -> Objects.isNull(map.get(item.getJobCode()))).collect(Collectors.toList());
List<RuleAttendanceMainHelp> helpList = new ArrayList<>(monthPeople.size());
LocalDate localDate = dateList.get(0);
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
for (AttendanceDto dto : peopleList) {
RuleAttendanceMainHelp help = new RuleAttendanceMainHelp();
BeanUtils.copyProperties(dto, help);
help.setDateFlag(date);
help.setRuleDictName(ruleNumDtoList.get(dto.getSettingId()).getRuleDictName());
helpList.add(help);
}
helpService.insertHelpList(helpList);
}
private Map<String, Object> transFormMonthPeople(List<RuleAttendanceMainHelp> monthPeople) {
Map<String, Object> map = new HashMap<>(monthPeople.size());
for (RuleAttendanceMainHelp help : monthPeople) {
map.put(help.getJobCode(), help);
}
return map;
}
private List<PeopleResponseVo> mainTransformPeopleResponseVo(List<RuleAttendanceMain> mainList) {
List<PeopleResponseVo> vos = new ArrayList<>(mainList.size());
for (RuleAttendanceMain main : mainList) {
PeopleResponseVo vo = new PeopleResponseVo();
BeanUtils.copyProperties(main, vo);
vos.add(vo);
}
return vos;
}
private void handleOtherRule(UpdatePeopleAttendanceRequestVo vo) {
RuleAttendanceMain main = getRuleAttendanceMain(vo.getJobCode(), vo);
RuleAttendanceMain otherMain = getRuleAttendanceMain(vo.getOtherJobCode(), vo);
RuleScheduling ruleScheduling = ruleSchedulingService.selectRuleSchedulingById(vo.getRuleId().longValue());
// 更新休息
BeanUtils.copyProperties(otherMain, main, "id", "name", "posts", "fleetName", "ruleDictName", "jobCode");
attendanceMainService.updateById(main);
// 更新顶班人员规则
handleOtherMain(ruleScheduling, otherMain, vo.getDate());
attendanceMainService.updateById(otherMain);
}
private void handleOtherMain(RuleScheduling ruleScheduling, RuleAttendanceMain otherMain, String date) {
RuleSchedulingDto dto = new RuleSchedulingDto();
BeanUtils.copyProperties(ruleScheduling, dto);
ToolUtils.copyRule(dto, otherMain);
RuleNumSettingServiceImpl.handleSegmentationByDate(LocalDate.parse(date), otherMain, dto);
otherMain.setWorkFlag(dto.getId() == 0 ? FREE_FLAG : WORK_FLAG);
}
private void ruleCopyToOtherUser(UpdatePeopleAttendanceRequestVo vo) {
RuleAttendanceMain main = getRuleAttendanceMain(vo.getJobCode(), vo);
RuleAttendanceMain otherMain = getRuleAttendanceMain(vo.getOtherJobCode(), vo);
RuleAttendanceMain backMain = new RuleAttendanceMain();
BeanUtils.copyProperties(otherMain, backMain);
BeanUtils.copyProperties(main, otherMain, "id", "name", "posts", "fleetName", "ruleDictName", "jobCode");
attendanceMainService.updateById(otherMain);
// 更新休息
BeanUtils.copyProperties(backMain, main, "id", "name", "posts", "fleetName", "ruleDictName", "jobCode");
attendanceMainService.updateById(main);
}
private RuleAttendanceMain getRuleAttendanceMain(String vo, UpdatePeopleAttendanceRequestVo vo1) {
QueryWrapper<RuleAttendanceMain> qw = new QueryWrapper<>();
qw.lambda().eq(RuleAttendanceMain::getJobCode, vo)
.eq(RuleAttendanceMain::getSchedulingDate, vo1.getDate());
RuleAttendanceMain main = attendanceMainService.getOne(qw);
return main;
}
private List<TableMonthResponseVo> handleTableMonthResponseVo(@NotBlank String date, List<RuleAttendanceMainHelp> helpList, List<RuleAttendanceMain> mainList) {
Map<String, List<RuleAttendanceMain>> mainMap = transformMapByRuleAttendanceMain(mainList);
List<TableMonthResponseVo> vos = new ArrayList<>(10);
for (RuleAttendanceMainHelp help : helpList) {
TableMonthResponseVo vo = new TableMonthResponseVo();
BeanUtils.copyProperties(help, vo);
vo.setMonthList(handleMonthWorkList(date, mainMap.get(help.getJobCode())));
vos.add(vo);
}
return vos;
}
private List<TableMonthResponseVo.WorkItemVo> handleMonthWorkList(@NotBlank String date, List<RuleAttendanceMain> mains) {
List<TableMonthResponseVo.WorkItemVo> vos = new ArrayList<>(30);
for (RuleAttendanceMain main : mains) {
TableMonthResponseVo.WorkItemVo itemVo = new TableMonthResponseVo.WorkItemVo();
itemVo.setSchedulingDate(main.getSchedulingDate());
itemVo.setRangeTime(handleRangeTime(main));
itemVo.setDay(ConstDateUtil.formatDate("MM月dd日", main.getSchedulingDate()));
itemVo.setWeek(handleNowWeek(main.getSchedulingDate()));
vos.add(itemVo);
}
// 为未生成的日期进行补充
String[] split = date.split(TOMORROW_NO_STRING);
int year = Integer.parseInt(split[0]);
int month = Integer.parseInt(split[1]);
List<LocalDate> dateList = ConstDateUtil.getAllDatesOfTheMonth(year, month);
if (vos.size() != dateList.size()) {
int diff = DateUtils.differentDaysByMillisecond(Date.from(dateList.get(0).atStartOfDay(ZoneId.systemDefault()).toInstant()), vos.get(0).getSchedulingDate());
if (diff > 0) {
for (int i = 0; i < diff; i++) {
TableMonthResponseVo.WorkItemVo itemVo = new TableMonthResponseVo.WorkItemVo();
LocalDate localDate = dateList.get(i);
Date nowDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
itemVo.setSchedulingDate(nowDate);
itemVo.setDay(ConstDateUtil.formatDate("MM月dd日", nowDate));
itemVo.setWeek(handleNowWeek(nowDate));
vos.add(itemVo);
}
vos.sort(Comparator.comparing(TableMonthResponseVo.WorkItemVo::getSchedulingDate));
}
}
return vos;
}
private String handleNowWeek(Date date) {
Calendar cal = Calendar.getInstance(); // 获得一个日历
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0) {
w = 0;
}
return weekDays[w];
}
private String handleRangeTime(RuleAttendanceMain main) {
// 处理休息
if (FREE.getValue().equals(main.getWorkFlag())) {
return FREE.getDescription();
}
String rangeTime;
// 处理不分段
if (Objects.isNull(main.getSecondQuittingSignInTime())) {
rangeTime = handleFirstRangeTime(main);
} else {
rangeTime = handleFirstRangeTime(main) + ";" + handleSecondRangeTime(main);
}
return rangeTime;
}
private String handleSecondRangeTime(RuleAttendanceMain main) {
return ConstDateUtil.formatDate("MM月dd日HH:mm", main.getSecondWorkSignInTime()) + TOMORROW_NO_STRING + ConstDateUtil.formatDate("MM月dd日HH:mm", main.getSecondQuittingSignInTime());
}
private String handleFirstRangeTime(RuleAttendanceMain main) {
return ConstDateUtil.formatDate("MM月dd日HH:mm", main.getFirstWorkSignInTime()) + TOMORROW_NO_STRING + ConstDateUtil.formatDate("MM月dd日HH:mm", main.getFirstQuittingSignInTime());
}
private Map<String, List<RuleAttendanceMain>> transformMapByRuleAttendanceMain(List<RuleAttendanceMain> mainList) {
Map<String, List<RuleAttendanceMain>> map = new HashMap<>();
for (RuleAttendanceMain main : mainList) {
List<RuleAttendanceMain> mains = map.get(main.getJobCode());
if (CollectionUtil.isEmpty(mains)) {
map.put(main.getJobCode(), new ArrayList<>(Arrays.asList(main)));
} else {
mains.add(main);
}
}
return map;
}
private static void getRumSettingByDto(SchedulingDto dto, RuleNumSetting setting) {
setting.setId(dto.getId());
setting.setUpdateTime(DateUtils.getNowDate());
setting.setUpdateBy(SecurityUtils.getUsername());
setting.setRuleNumId(dto.getRuleNumId());
setting.setRuleDictName(dto.getRuleDictName());
}
private List<RuleSettingDriver> getSettingDriverList(SchedulingSettingDto dto, Integer id) {
return dto
.getPeopleList()
.stream()
.map(item -> {
RuleSettingDriver driver = new RuleSettingDriver();
driver.setSettingId(id);
driver.setJobCode(item.getJobCode());
if (item.getStartDate() == null) {
throw new RuntimeException("非法开始时间");
}
driver.setStartDate(item.getStartDate());
return driver;
})
.collect(Collectors.toList());
}
private List<RuleSettingScheduling> getSettingSchedulingList(SchedulingSettingDto dto, Integer id) {
// TODO 规则校验
return dto.getRuleSchedulingList().stream().peek(item -> {
item.setSettingId(id);
}).collect(Collectors.toList());
}
private static RuleNumSetting getRuleNumSetting(SchedulingSettingDto dto) {
RuleNumSetting setting = new RuleNumSetting();
setting.setCreateTime(DateUtils.getNowDate());
setting.setUpdateTime(DateUtils.getNowDate());
String username = SecurityUtils.getUsername();
setting.setCreateBy(username);
setting.setUpdateBy(username);
setting.setRuleDictName(dto.getRuleDictName());
setting.setRuleNumId(dto.getRuleNumId());
return setting;
}
private List<RuleNumTemplateVo> handleWeekTemplate(RuleNum ruleNum) {
String[] strings = ruleNum.getRuleWeek().split(",");
Map<Integer, Integer> weeks = Stream.of(strings).collect(Collectors.toMap(s -> Integer.parseInt(s), s -> Integer.parseInt(s)));
List<RuleNumTemplateVo> vos = new ArrayList<>(7);
for (int i = 1; i <= 7; i++) {
Integer integer = weeks.get(i);
if (Objects.isNull(integer)) {
vos.add(getRuleNumTemplateVo(i, false));
} else {
vos.add(getRuleNumTemplateVo(integer, true));
}
}
return vos;
}
private List<RuleNumTemplateVo> handleFixedTemplate(RuleNum ruleNum) {
int countDay = ruleNum.getWorkDay() + ruleNum.getFreeDay();
List<RuleNumTemplateVo> vos = new ArrayList<>(countDay);
for (int i = 1; i <= countDay; i++) {
vos.add(getRuleNumTemplateVo(i, i <= ruleNum.getWorkDay()));
}
return vos;
}
private RuleNumTemplateVo getRuleNumTemplateVo(Integer serialNumber, Boolean workFlag) {
RuleNumTemplateVo vo = new RuleNumTemplateVo();
vo.setSerialNumber(serialNumber);
vo.setType(workFlag ? WORK_FLAG : FREE_FLAG);
return vo;
}
private void handleSchedulingResponse(List<SchedulingResponseVo> vos, List<DriverDto> driverList, List<RuleSchedulingDto> schedulingList) {
Map<Integer, List<DriverDto>> driverMap = transFromMapByDriver(driverList);
Map<Integer, List<RuleScheduling>> schedulingMap = transFromMapByScheduling(schedulingList);
for (SchedulingResponseVo vo : vos) {
vo.setPeopleList(handlePeopleList(vo.getId(), driverMap.get(vo.getId())));
vo.setRuleList(handleRuleList(vo.getId(), schedulingMap.get(vo.getId())));
}
}
private Map<Integer, List<RuleScheduling>> transFromMapByScheduling(List<RuleSchedulingDto> schedulingList) {
Map<Integer, List<RuleScheduling>> map = new HashMap<>(16);
for (RuleSchedulingDto scheduling : schedulingList) {
List<RuleScheduling> list = map.get(scheduling.getSettingId());
if (CollectionUtil.isEmpty(list)) {
map.put(scheduling.getSettingId(), new ArrayList<>(Arrays.asList(scheduling)));
} else {
list.add(scheduling);
}
}
return map;
}
private Map<Integer, List<DriverDto>> transFromMapByDriver(List<DriverDto> driverList) {
Map<Integer, List<DriverDto>> map = new HashMap<>(16);
for (DriverDto driver : driverList) {
List<DriverDto> list = map.get(driver.getSettingId());
if (CollectionUtil.isEmpty(list)) {
map.put(driver.getSettingId(), new ArrayList<>(Arrays.asList(driver)));
} else {
list.add(driver);
}
}
return map;
}
private List<String> handleRuleList(Integer id, List<RuleScheduling> schedulingList) {
List<RuleSchedulingVo> list = RuleSchedulingServiceImpl.handleRuleScheduling(schedulingList);
if (CollectionUtil.isEmpty(list)) {
return new ArrayList<>();
}
return list.stream().map(item -> {
if (item.getRuleName().equals(FREE.getDescription())) {
return item.getRuleName();
}
return item.getRuleName() + "(" + item.getRangeTime() + ")";
}).collect(Collectors.toList());
}
private List<String> handlePeopleList(Integer id, List<DriverDto> driverList) {
if (CollectionUtil.isEmpty(driverList)) {
return new ArrayList<>();
}
return driverList.stream().map(item -> item.getPersonnelName() + "(" + item.getJobCode() + ")").collect(Collectors.toList());
}
}