SignInServiceImpl.java
32 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
package com.ruoyi.in.service.impl;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.cache.NowSchedulingCache;
import com.ruoyi.common.cache.SchedulingCache;
import com.ruoyi.common.cache.TempCache;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.driver.domain.Driver;
import com.ruoyi.driver.mapper.DriverMapper;
import com.ruoyi.driver.mapper.DriverSchedulingMapper;
import com.ruoyi.equipment.domain.Equipment;
import com.ruoyi.equipment.mapper.EquipmentMapper;
import com.ruoyi.errorScheduling.service.IErrorJobcodeService;
import com.ruoyi.pojo.DriverSignInRecommendation;
import com.ruoyi.pojo.GlobalIndex;
import com.ruoyi.domain.DriverScheduling;
import com.ruoyi.pojo.response.SignInResponseVo;
import com.ruoyi.service.SchedulingService;
import com.ruoyi.service.ThreadJobService;
import com.ruoyi.utils.ConstDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.in.mapper.SignInMapper;
import com.ruoyi.in.domain.SignIn;
import com.ruoyi.in.service.ISignInService;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import static com.ruoyi.common.ConstDriverProperties.*;
import static com.ruoyi.common.ErrorTypeProperties.*;
import static com.ruoyi.common.ConstSignInConstSignInProperties.*;
import static com.ruoyi.common.redispre.GlobalRedisPreName.REDIS_SIGN_IN_DRIVER_ALCOHOL_OVERFLOW;
/**
* 签到Service业务层处理
*
* @author guzijian
* @date 2023-07-05
*/
@Service
public class SignInServiceImpl implements ISignInService {
private Logger log = LoggerFactory.getLogger(SignInServiceImpl.class);
@Resource
private NowSchedulingCache nowSchedulingCache;
@Resource
private SchedulingService schedulingService;
@Autowired
private IErrorJobcodeService errorJobcodeService;
@Autowired
private DriverSchedulingMapper schedulingMapper;
@Resource
private SchedulingCache schedulingCache;
@Autowired
private SignInMapper signInMapper;
@Autowired
private DriverMapper driverMapper;
@Autowired
private EquipmentMapper equipmentMapper;
@Autowired
private RedisCache redisCache;
@Resource
private ThreadJobService threadJobService;
/**
* 查询签到
*
* @param id 签到主键
* @return 签到
*/
@Override
public SignIn selectSignInById(Long id) {
return signInMapper.selectSignInById(id);
}
/**
* 查询签到列表
*
* @param vo 签到
* @return 签到
*/
@Override
public List<SignInResponseVo> selectSignInList(SignInResponseVo vo) {
List<SignInResponseVo> vos = signInMapper.selectSignInList(vo);
return vos;
}
/**
* 修改签到
*
* @param signIn 签到
* @return 结果
*/
@Override
public int updateSignIn(SignIn signIn) {
signIn.setUpdateTime(DateUtils.getNowDate());
return signInMapper.updateSignIn(signIn);
}
/**
* 批量删除签到
*
* @param ids 需要删除的签到主键
* @return 结果
*/
@Override
public int deleteSignInByIds(Long[] ids) {
return signInMapper.deleteSignInByIds(ids);
}
/**
* 删除签到信息
*
* @param id 签到主键
* @return 结果
*/
@Override
public int deleteSignInById(Long id) {
return signInMapper.deleteSignInById(id);
}
@Override
public AjaxResult addSignIn(SignIn signIn) throws IOException {
// 查询员工信息
Driver driver = driverMapper.getDriverInfoByJobCode(signIn.getJobCode());
if (Objects.isNull(driver)) return AjaxResult.warn("这个工号的员工不存在!");
// 查询地址
Equipment equipment = equipmentMapper.selectEquipmentByDeviceId(signIn.getDeviceId());
SignInResponseVo vo = getSignInResponseVo(driver, signIn, equipment);
signIn.setCreateTime(new Date());
signIn.setRemark("");
GlobalIndex globalIndex = new GlobalIndex();
long now = signIn.getCreateTime().getTime();
List<DriverScheduling> dto = schedulingService.queryScheduling(signIn.getJobCode(), now);
handleSignBody(signIn, driver, globalIndex, now, dto);
if (CollectionUtils.isNotEmpty(dto)) {
int size = CollectionUtils.size(dto);
if (size > 0 && globalIndex.getIndex() < size) {
log.info("签到签退数据为:[{}]", dto.get(globalIndex.getIndex()));
}
} else {
log.info("没有签到或签退的数据");
}
if (PERSONNEL_POSTS_DRIVER.equals(driver.getPosts()) && CollectionUtils.isNotEmpty(dto) && Objects.equals(dto.get(globalIndex.getIndex()).getBcType(), BC_TYPE_OUT)) {
AjaxResult result = getAjaxResultByDriverSignInfo(signIn, vo);
if (!Objects.isNull(result)) {
String msg = "签到异常: 请使用手持式酒精测试棒进行酒测";
result.put(AjaxResult.MSG_TAG, msg);
return result;
}
}
uploadImage(signIn, vo);
signInMapper.insertSignIn(signIn);
// 更新考勤
schedulingService.computedSignInBySignIn(dto, signIn, globalIndex);
if (TempCache.checkJobCodeExist(signIn.getJobCode())) {
TempCache.updateSignStatus(signIn.getJobCode());
}
// 异常保存到异常异常中
threadJobService.asyncInsertExceptionRecord(signIn, driver, dto, globalIndex);
// 驾驶人员二次签到酒精测试异常
return handleAjaxResult(signIn, driver, vo);
}
private void handleSignBody(SignIn signIn, Driver driver, GlobalIndex globalIndex, long now, List<DriverScheduling> dto) {
// 签到检查
if (checkSignIn(now, dto, globalIndex, signIn, driver)) {
signIn.setStatus(SIGN_IN_SUCCESS);
signIn.setExType(SIGN_NO_EX_NUM);
signIn.setRemark("正常");
} else {
signIn.setStatus(SIGN_IN_FAIL);
signIn.setRemark(signIn.getRemark().replaceFirst(",$", "。"));
}
// base64转图片
signIn.setIp(IpUtils.getIpAddr());
}
private AjaxResult handleAjaxResult(SignIn signIn, Driver driver, SignInResponseVo vo) {
// 酒精测试后续恢复的需要删除异常的酒测缓存
if (SIGN_IN_SUCCESS.equals(signIn.getStatus())) {
String prompt = signIn.getRemark().replace("正常", "");
if (signIn.getType().equals(SIGN_OUT)) {
return AjaxResult.success(SIGN_OUT_SUCCESS_STRING + prompt, vo);
} else {
return AjaxResult.success(SIGN_IN_SUCCESS_STRING + prompt, vo);
}
} else {
if (signIn.getType().equals(SIGN_OUT)) {
return AjaxResult.success(SIGN_OUT_ERROR + ":" + signIn.getRemark(), vo);
}
return AjaxResult.success(SIGN_IN_ERROR + ":" + signIn.getRemark(), vo);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public String repairSignRecord(HttpServletRequest request) {
String header = request.getHeader("X-TOKEN-AUTH");
if (!"gzjUse".equals(header)) {
throw new RuntimeException("错误的认证");
}
// 获取发车班次
List<DriverScheduling> list = signInMapper.queryOutBcType();
// 获取签到数据
List<SignIn> signIns = signInMapper.querySignInData();
Map<String, DriverScheduling> transformMap = list.stream().collect(Collectors.toMap(DriverScheduling::getJobCode, item -> item));
// 更新签到数据
List<SignIn> signInList = handlerHaveScheduling(transformMap, signIns);
startRepairSignRecord(transformMap, signInList);
equipmentMapper.deleteExRecord();
return null;
}
@Override
public String repairAllSignRecord(HttpServletRequest request, String date) {
String header = request.getHeader("X-TOKEN-AUTH");
if (!"gzjUse".equals(header)) {
throw new RuntimeException("错误的认证");
}
// 恢复当天的所有签到数据
// 获取排班
List<DriverScheduling> dto = schedulingService.queryToDay(date);
Map<String, List<DriverScheduling>> resultMap = new HashMap<>(500);
NowSchedulingCache.handlerResultMap(resultMap, dto);
// 获取当天的签到数据
SignInResponseVo vo = new SignInResponseVo();
vo.setDate(transFormDate(date));
List<SignInResponseVo> inList = signInMapper.selectSignInList(vo);
vo.setDate(transFormDate(handleDate(date)));
inList.addAll(signInMapper.selectSignInList(vo));
handleAllRecord(resultMap, inList);
return null;
}
@Override
public AjaxResult supplementarySignature(SignIn signIn) {
Driver driver = driverMapper.getDriverInfoByJobCode(signIn.getJobCode());
if (Objects.isNull(driver)) return AjaxResult.warn("这个工号的员工不存在!");
GlobalIndex globalIndex = new GlobalIndex();
long now = signIn.getCreateTime().getTime();
List<DriverScheduling> dto = schedulingService.queryScheduling(signIn.getJobCode(), now);
// 只能补签当月
if (checkSupplementarySignatureTime(signIn.getCreateTime())) {
throw new RuntimeException("只能对当前月的数据进行补签!");
}
if (CollectionUtil.isEmpty(dto)) {
dto = schedulingMapper.queryToDay(ConstDateUtil.formatDate("yyyy-MM-dd", signIn.getCreateTime()), null, driver.getJobCode(), null);
}
handleSignBody(signIn, driver, globalIndex, now, dto);
signIn.setUpdateBy(SecurityUtils.getUsername());
signIn.setRemark("补签");
signInMapper.insertSignIn(signIn);
// 更新考勤
if (CollectionUtil.isNotEmpty(dto)) {
schedulingMapper.updateRoster(dto.get(globalIndex.getIndex()), signIn.getId(), signIn.getExType(), signIn.getCreateTime(), signIn.getRemark(), signIn.getType(), signIn.getAlcoholFlag(), signIn.getAlcoholIntake());
// 更新缓存
nowSchedulingCache.updateCacheByJobCode(signIn.getRemark(), ConstDateUtil.formatDate(dto.get(0).getScheduleDate()), globalIndex.getIndex(), signIn);
}
// 异常保存到异常异常中
threadJobService.asyncInsertExceptionRecord(signIn, driver, dto, globalIndex);
return AjaxResult.success("补签成功");
}
private boolean checkSupplementarySignatureTime(Date createTime) {
String date = ConstDateUtil.formatDate("yyyy-MM", createTime);
String now = ConstDateUtil.formatDate("yyyy-MM", new Date());
if (date.equals(now)) {
return false;
}
return true;
}
@Override
public List<SignInResponseVo> selectSignInByIds(List<Long> collect) {
if (CollectionUtil.isEmpty(collect)) {
return new ArrayList<>();
}
return signInMapper.selectSignInByIds(collect);
}
private String handleDate(String dateString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// 解析字符串日期为 LocalDate 对象
LocalDate parse = LocalDate.parse(dateString, formatter);
// 给日期加上一天
LocalDate nextDay = parse.plusDays(1);
// 将结果日期格式化为字符串(可选)
return nextDay.format(formatter);
}
private String transFormDate(String dateString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate parse = LocalDate.parse(dateString, formatter);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return parse.format(formatter1);
}
private void handleAllRecord(Map<String, List<DriverScheduling>> resultMap, List<SignInResponseVo> inList) {
inList = inList.stream().filter(item -> !Objects.isNull(resultMap.get(item.getJobCode()))).collect(Collectors.toList());
Map<String, List<SignInResponseVo>> recordMap = new HashMap<>(500);
handleSignIn(recordMap, inList);
// 恢复数据
repairRecord(resultMap, recordMap);
errorJobcodeService.insertBatchErrorJobcode(ThreadJobService.getErrorScheduling(resultMap));
}
private void repairRecord(Map<String, List<DriverScheduling>> resultMap, Map<String, List<SignInResponseVo>> recordMap) {
Map<String, Driver> drivers = driverMapper.getDrivers(new Driver()).stream().collect(Collectors.toMap(Driver::getJobCode, driver -> driver));
for (Map.Entry<String, List<SignInResponseVo>> entry : recordMap.entrySet()) {
List<SignInResponseVo> value = entry.getValue();
for (SignInResponseVo sign : value) {
GlobalIndex globalIndex = new GlobalIndex();
checkSignIn(sign.getCreateTime().getTime(), resultMap.get(sign.getJobCode()), globalIndex, sign, drivers.get(sign.getJobCode()));
schedulingService.computedSignInBySignIn(resultMap.get(sign.getJobCode()), sign, globalIndex);
}
}
}
private void handleSignIn(Map<String, List<SignInResponseVo>> recordMap, List<SignInResponseVo> inList) {
for (SignInResponseVo vo : inList) {
List<SignInResponseVo> voList = recordMap.get(vo.getJobCode());
if (CollectionUtil.isEmpty(voList)) {
recordMap.put(vo.getJobCode(), new ArrayList<>(Arrays.asList(vo)));
} else {
voList.add(vo);
}
}
// 排序
for (Map.Entry<String, List<SignInResponseVo>> stringListEntry : recordMap.entrySet()) {
stringListEntry.getValue().sort(Comparator.comparing(SignInResponseVo::getCreateTime));
}
}
private void startRepairSignRecord(Map<String, DriverScheduling> transformMap, List<SignIn> signInList) {
for (SignIn signIn : signInList) {
handlerExOutData(transformMap.get(signIn.getJobCode()), signIn);
schedulingService.computedSignInBySignIn(new ArrayList<>(Arrays.asList(transformMap.get(signIn.getJobCode()))), signIn, new GlobalIndex());
// // 更新缓存
nowSchedulingCache.updateCacheByJobCode(signIn.getRemark(), ConstDateUtil.formatDate(transformMap.get(signIn.getJobCode()).getScheduleDate()), 0, signIn);
signInMapper.updateSignIn(signIn);
}
}
private void handlerExOutData(DriverScheduling driverScheduling, SignIn signIn) {
LocalDateTime endTime = ConstDateUtil.getLocalDateTimeByLongTime(driverScheduling.getFcsjT());
LocalDateTime nowTime = ConstDateUtil.getLocalDateTimeByLongTime(signIn.getCreateTime().getTime());
long nowBetween = ChronoUnit.MINUTES.between(endTime, nowTime);
if (Math.abs(nowBetween) <= 60) {
handlerNoExData(signIn, driverScheduling);
return;
}
handlerHaveExData(signIn, driverScheduling);
}
private void handlerHaveExData(SignIn signIn, DriverScheduling driverScheduling) {
signIn.setExType(SIGN_TIME_OUT_EX_NUM);
signIn.setStatus(SIGN_IN_FAIL);
signIn.setUpdateTime(new Date());
signIn.setRemark(SIGN_IN_TIMEOUT);
}
private void handlerNoExData(SignIn signIn, DriverScheduling driverScheduling) {
signIn.setExType(SIGN_NO_EX_NUM);
signIn.setStatus(SIGN_IN_SUCCESS);
signIn.setUpdateTime(new Date());
signIn.setRemark("正常");
}
private List<SignIn> handlerHaveScheduling(Map<String, DriverScheduling> transformMap, List<SignIn> signIns) {
List<SignIn> signList = new ArrayList<>(500);
for (SignIn signIn : signIns) {
if (!Objects.isNull(transformMap.get(signIn.getJobCode()))) {
signList.add(signIn);
}
}
return signList;
}
private SignInResponseVo getSignInResponseVo(Driver driver, SignIn signIn, Equipment equipment) {
SignInResponseVo vo = new SignInResponseVo(signIn);
if (Objects.isNull(equipment)) {
equipment = new Equipment();
}
vo.setAddress(equipment.getAddress());
vo.setDeviceId(signIn.getDeviceId());
vo.setSiteName(equipment.getSiteName());
vo.setName(driver.getPersonnelName());
vo.setPosts(driver.getPosts());
return vo;
}
private AjaxResult getAjaxResultByDriverSignInfo(SignIn signIn, SignInResponseVo vo) {
String key = REDIS_SIGN_IN_DRIVER_ALCOHOL_OVERFLOW + ConstDateUtil.formatDate("yyyyMMdd") + ":" + signIn.getJobCode();
// 驾驶员酒精测试连续超标两次则提示换人
Integer count = redisCache.getCacheObject(key);
if (Objects.isNull(count) || count.equals(0)) {
count = 1;
redisCache.setCacheObject(key, count, 1, TimeUnit.DAYS);
} else {
redisCache.setCacheObject(key, ++count, 1, TimeUnit.DAYS);
}
if (SIGN_IN_ERROR_COUNT.compareTo(count) <= 0) {
return AjaxResult.success(SIGN_IN_ERROR + ": " + signIn.getRemark() + "酒精测试不通过" + count + "次请更换车辆驾驶员。", vo);
}
return null;
}
private boolean checkSignIn(long now, List<DriverScheduling> dto, GlobalIndex globalIndex, SignIn signIn, Driver driver) {
boolean result = true;
if (Objects.isNull(driver)) {
return true;
}
// 排班校验 和签到超时校验
result = checkWorkDay(now, dto, globalIndex, signIn, driver.getPosts());
// 酒精测试校验 确定 且员工工种是驾驶员
if (ALCOHOL_FLAG_YES.equals(signIn.getAlcoholFlag()) && PERSONNEL_POSTS_DRIVER.equals(driver.getPosts())) {
if (new BigDecimal(20).compareTo(signIn.getAlcoholIntake()) <= 0) {
threadJobService.asyncSendEmail(dto, globalIndex.getIndex(), signIn, driver);
signIn.setRemark(signIn.getRemark() + ALCOHOL_SIGN_IN_ERROR + signIn.getAlcoholIntake().toString() + "mg/100ml。");
signIn.setExType(SIGN_ALCOHOL_EX_NUM);
result = false;
} else {
// 如果驾驶员酒精测试在之前不合格 必须重测
String key = REDIS_SIGN_IN_DRIVER_ALCOHOL_OVERFLOW + ConstDateUtil.formatDate("yyyyMMdd") + ":" + driver.getJobCode();
Integer count = redisCache.getCacheObject(key);
if (!Objects.isNull(count)) {
redisCache.deleteObject(key);
}
}
}
return result;
}
/**
* 排班和超时检查
*
* @param globalIndex
* @param signIn
* @param posts
* @return
*/
private boolean checkWorkDay(long now, List<DriverScheduling> dto, GlobalIndex globalIndex, SignIn signIn, String posts) {
Boolean result = true;
switch (posts) {
case "驾驶员":
case "售票员":
String key = ConstDateUtil.formatDate(now);
// 查询工号对应的排班 司售人员
log.debug("签到key:{},排班数据:{}", key, dto);
if (CollectionUtil.isEmpty(dto)) {
signIn.setRemark(signIn.getRemark() + WORK_DAY_ERROR);
signIn.setExType(SIGN_NO_SCHEDULING_EX_NUM);
result = false;
}
break;
default:
if (CollectionUtil.isEmpty(dto)) {
signIn.setRemark(signIn.getRemark() + WORK_DAY_ERROR);
signIn.setExType(SIGN_NO_SCHEDULING_EX_NUM);
result = false;
}
break;
}
// 超时校验
if (result) {
result = checkTimeOut(globalIndex, signIn, dto, posts, now);
if (!result) {
signIn.setExType(SIGN_TIME_OUT_EX_NUM);
}
}
return result;
}
private static Date getDayKey() {
Calendar calendar = Calendar.getInstance();
// 获取当前日期时间
Date currentDate = calendar.getTime();
// 设置时间为2:30 AM
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
// 判断当前时间是否在2:30 AM之前
if (currentDate.before(calendar.getTime())) {
// 当前时间在2:30 AM之前,将日期向前推一天
calendar.add(Calendar.DAY_OF_MONTH, -1);
}
// 获取结果日期对象
return calendar.getTime();
}
private boolean checkTimeOut(GlobalIndex globalIndex, SignIn signIn, List<DriverScheduling> dto, String posts, long now) {
// 发车时间和到站时间作为开始上班的时间
return driverCheckTimeOut(globalIndex, signIn, dto, now);
}
private boolean driverCheckTimeOut(GlobalIndex globalIndex, SignIn signIn, List<DriverScheduling> dto, long now) {
try {
boolean result = true;
DriverSignInRecommendation currentScheduling = schedulingService.computedTheCurrentClosestTimestamp(dto, now, 0);
LocalDateTime endTime = ConstDateUtil.getLocalDateTimeByLongTime(currentScheduling.getTimestamps());
LocalDateTime nowTime = ConstDateUtil.getLocalDateTimeByLongTime(now);
long nowBetween = ChronoUnit.MINUTES.between(endTime, nowTime);
// 异常数据当进出场为单记录时
if (dto.size() == 1) {
return handlerExceptionRecord(dto, now, globalIndex, signIn);
}
// 不在有效时间范围内,判断当前是否有签到记录 当前应签到还是签退
if (checkTimerSign(nowBetween, currentScheduling.getBcType())) {
// 签到一个逻辑 当前无记录
if (currentScheduling.getBcType().equals(BC_TYPE_OUT) && Objects.isNull(currentScheduling.getSignInId())) {
DriverSignInRecommendation lastClosestTimestamp = schedulingService.computedTheCurrentClosestTimestamp(dto, now, -1);
// 给出签到范围
// 上一次无记|签到,type:签退 当前时间小于最近签到时间 -> 超时签退
if (nowBetween < -SIGN_OUT_TIME && Objects.isNull(lastClosestTimestamp.getSignInId()) && signIn.getType().equals(SIGN_OUT) && lastClosestTimestamp.getBcType().equals(BC_TYPE_IN)) {
signIn.setRemark(SIGN_OUT_TIMEOUT + getPrompt(lastClosestTimestamp));
globalIndex.setIndex(lastClosestTimestamp.getIndex());
result = false;
}
// 当前无记|签到,type:签退 当前时间小于最近签到时间 -> 早签 或者 超时
else {
signIn.setRemark(SIGN_IN_TIMEOUT + getPrompt(currentScheduling));
globalIndex.setIndex(currentScheduling.getIndex());
result = false;
}
}
// 签退一个逻辑 当前无记录
if (currentScheduling.getBcType().equals(BC_TYPE_IN) && Objects.isNull(currentScheduling.getSignInId())) {
DriverSignInRecommendation lastClosestTimestamp = schedulingService.computedTheCurrentClosestTimestamp(dto, now, -1);
// 上一次无记|签退,type:签到 当前时间小于最近签退时间 -> 签到异常
if (nowBetween < -SIGN_OUT_TIME && Objects.isNull(lastClosestTimestamp.getSignInId()) && signIn.getType().equals(SIGN_IN)) {
signIn.setRemark(SIGN_IN_TIMEOUT + getPrompt(lastClosestTimestamp));
globalIndex.setIndex(lastClosestTimestamp.getIndex());
result = false;
}
// 当前无记|签退 , type:签退|签到 ——> 签退异常
else {
signIn.setRemark(SIGN_OUT_TIMEOUT + getPrompt(currentScheduling));
globalIndex.setIndex(currentScheduling.getIndex());
result = false;
}
}
// 签退|签到 当前有记录
if (!Objects.isNull(currentScheduling.getSignInId())) {
String prompt;
// 超时了就到下一次签到报表记录
boolean timeOut = handleTimeOut(currentScheduling.getBcType(), nowBetween, globalIndex, currentScheduling);
if (timeOut) {
DriverSignInRecommendation nextScheduling = schedulingService.computedTheCurrentClosestTimestamp(dto, now, 1);
globalIndex.setIndex(nextScheduling.getIndex());
prompt = getPrompt(nextScheduling);
signIn.setRemark((nextScheduling.getBcType().equals(BC_TYPE_IN) ? SIGN_OUT_TIMEOUT : SIGN_IN_TIMEOUT) + prompt);
} else {
globalIndex.setIndex(currentScheduling.getIndex());
prompt = getPrompt(currentScheduling);
signIn.setRemark((currentScheduling.getBcType().equals(BC_TYPE_IN) ? SIGN_OUT_TIMEOUT : SIGN_IN_TIMEOUT) + prompt);
}
result = false;
}
}
// 有效直接保存记录
else {
globalIndex.setIndex(currentScheduling.getIndex());
result = true;
}
return result;
} catch (Exception e) {
log.info(e.getMessage());
return true;
}
}
private boolean handleTimeOut(String bcType, long nowBetween, GlobalIndex globalIndex, DriverSignInRecommendation currentScheduling) {
if ((BC_TYPE_OUT.equals(bcType) && nowBetween > 0L)
|| (BC_TYPE_IN.equals(bcType) && nowBetween > SIGN_OUT_TIME)) {
return true;
} else {
return false;
}
}
private String getPrompt(DriverSignInRecommendation currentScheduling) {
if (BC_TYPE_OUT.equals(currentScheduling.getBcType())) {
LocalDateTime time = ConstDateUtil.stringTransformLocalDateTime(ConstDateUtil.formatDate("yyyy-MM-dd HH:mm:ss", new Date(currentScheduling.getTimestamps())), "yyyy-MM-dd HH:mm:ss");
LocalDateTime subHours = time.plusHours(-1);
return "请在" + ConstDateUtil.formatDate("HH:mm", time) + "到"
+ ConstDateUtil.formatDate("HH:mm", subHours) + "之间打卡";
} else {
// 延后一小时
LocalDateTime time = ConstDateUtil.stringTransformLocalDateTime(ConstDateUtil.formatDate("yyyy-MM-dd HH:mm:ss", new Date(currentScheduling.getTimestamps())), "yyyy-MM-dd HH:mm:ss");
LocalDateTime addHours = time.plusMinutes(80);
return "请在" + ConstDateUtil.formatDate("HH:mm", time) + "到"
+ ConstDateUtil.formatDate("HH:mm", addHours) + "之间打卡";
}
}
public static boolean checkTimerSign(long nowBetween, String bcType) {
// 如果是签到就是提前一小时内有效
if (BC_TYPE_OUT.equals(bcType)) {
return !(nowBetween <= 0L && nowBetween >= -60L);
} else {
// 如果是签退就是就是延后80分钟内有效
return !(nowBetween <= SIGN_OUT_TIME && nowBetween >= 0L);
}
}
private Boolean handlerExceptionRecord(List<DriverScheduling> dto, long now, GlobalIndex globalIndex, SignIn signIn) {
DriverSignInRecommendation currentScheduling = schedulingService.computedTheCurrentClosestTimestamp(dto, now, 0);
LocalDateTime endTime = ConstDateUtil.getLocalDateTimeByLongTime(currentScheduling.getTimestamps());
LocalDateTime nowTime = ConstDateUtil.getLocalDateTimeByLongTime(now);
long nowBetween = ChronoUnit.MINUTES.between(endTime, nowTime);
globalIndex.setIndex(0);
// 单条数据 且不在范围内
if (BC_TYPE_IN.equals(dto.get(0).getBcType())) {
if (!(nowBetween < 0L && nowBetween >= -SIGN_OUT_TIME)) {
signIn.setRemark(SIGN_OUT_TIMEOUT);
return false;
} else {
return true;
}
}
if (BC_TYPE_OUT.equals(dto.get(0).getBcType())) {
if (!(Math.abs(nowBetween) <= 60)) {
signIn.setRemark(SIGN_IN_TIMEOUT);
return false;
} else {
return true;
}
}
return true;
}
private void uploadImage(SignIn signIn, SignInResponseVo vo) throws IOException {
String base64 = signIn.getImage();
// 图片路径
String filePath = RuoYiConfig.getUploadPath();
// 固定jpg文件
String fileName = "";
// 看是否带有base64前缀 有就判断文件类型 没有默认jpg
fileName = checkImageBase64Format(signIn.getImage());
fileName = extractFilename(fileName);
// 获取相对路径
String absPath = getAbsoluteFile(filePath, fileName).getAbsolutePath();
// 获取文件上传路径
String pathFileName = getPathFileName(filePath, fileName);
signIn.setImage(pathFileName);
vo.setImage(signIn.getImage());
log.info("开始上传签到图片");
// 异步上传文件
threadJobService.asyncStartUploadBase64Image(absPath, base64);
}
/**
* 获取相对路径名
*
* @param uploadDir
* @param fileName
* @return
* @throws IOException
*/
public File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
File desc = new File(uploadDir + File.separator + fileName);
if (!desc.exists()) {
if (!desc.getParentFile().exists()) {
desc.getParentFile().mkdirs();
}
}
return desc;
}
/**
* 获取扩展文件名
*
* @param extendFileName
* @return
*/
public String extractFilename(String extendFileName) {
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
FilenameUtils.getBaseName(UUID.randomUUID().toString().replace("-", "")), Seq.getId(Seq.uploadSeqType), extendFileName);
}
/**
* @param uploadDir
* @param fileName
* @return
* @throws IOException
*/
public String getPathFileName(String uploadDir, String fileName) throws IOException {
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
}
/**
* 检查文件类型
*
* @param base64ImgData
* @return
*/
public static String checkImageBase64Format(String base64ImgData) {
byte[] b = Base64.getDecoder().decode(base64ImgData);
String type = "";
if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "bmp";
} else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
type = "png";
} else {
type = "jpg";
}
// else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
// type = "jpg";
// }
return type;
}
}