|
|
@@ -2,6 +2,7 @@ package com.punchsettle.server.service.manager.impl;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDate;
|
|
|
+import java.time.LocalTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
@@ -11,6 +12,7 @@ import java.util.Optional;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import com.punchsettle.server.constant.PunchInStatusEnum;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -73,7 +75,7 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
return List.of();
|
|
|
}
|
|
|
|
|
|
- // 获取一周的起始日期范围,
|
|
|
+ // 获取一周的起始日期范围
|
|
|
List<LocalDate> weeklyDateRange = DateUtils.getWeeklyDateRange();
|
|
|
// 获取打卡任务ID
|
|
|
List<Long> punchInIds = punchIns.stream().map(PunchIn::getId).collect(Collectors.toList());
|
|
|
@@ -84,7 +86,7 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
query.setEndDate(weeklyDateRange.getLast().toString());
|
|
|
List<PunchInRecord> punchInRecords = punchInRecordService.listByCondition(query);
|
|
|
|
|
|
- // 按打卡任务任务分组
|
|
|
+ // 打卡任务-打卡记录 分组
|
|
|
Map<Long, List<PunchInRecord>> recordMap =
|
|
|
punchInRecords.stream().collect(Collectors.groupingBy(PunchInRecord::getPunchInId));
|
|
|
|
|
|
@@ -97,55 +99,86 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
// 构建打卡记录
|
|
|
List<PunchInWithRecordDto> punchInWithRecordDtos = new ArrayList<>();
|
|
|
for (PunchIn punchIn : punchIns) {
|
|
|
+ // 一周的打卡记录容器
|
|
|
+ List<PunchInRecordDto> weeklyRecords = new ArrayList<>();
|
|
|
+ // 打卡任务信息
|
|
|
PunchInWithRecordDto punchInWithRecordDto = new PunchInWithRecordDto();
|
|
|
BeanUtils.copyProperties(punchIn, punchInWithRecordDto);
|
|
|
punchInWithRecordDto.setPunchInId(punchIn.getId());
|
|
|
+ // 设置一个默认的页面显示值
|
|
|
+ punchInWithRecordDto.setTimeTrack(LocalTime.parse("00:00:00.000"));
|
|
|
+ punchInWithRecordDto.setCountTrack(0);
|
|
|
+ punchInWithRecordDto.setPunchInRecords(weeklyRecords);
|
|
|
+ punchInWithRecordDtos.add(punchInWithRecordDto);
|
|
|
|
|
|
// 打卡任务创建日期
|
|
|
LocalDate punchInCreationDate = LocalDate.parse(sdf.format(punchIn.getCreationTime()));
|
|
|
|
|
|
- // 获取打卡任务对应的打卡记录,并按打卡日期转为map
|
|
|
+ // 获取打卡任务对应的打卡记录,并转为打卡日期-打卡记录map
|
|
|
List<PunchInRecord> records =
|
|
|
Optional.ofNullable(recordMap.get(punchInWithRecordDto.getPunchInId())).orElse(List.of());
|
|
|
- Map<String, PunchInRecord> recordWeekMap = records.stream()
|
|
|
+ Map<String, PunchInRecord> weeklyRecordMap = records.stream()
|
|
|
.collect(Collectors.toMap(PunchInRecord::getPunchInDate, Function.identity(), (key1, key2) -> key1));
|
|
|
|
|
|
- // 设置打卡记录
|
|
|
- List<PunchInRecordDto> recordDtos = new ArrayList<>();
|
|
|
- punchInWithRecordDto.setPunchInRecords(recordDtos);
|
|
|
-
|
|
|
- // 知道任务什么时候创建
|
|
|
- // 在这之前(不包含)的记录的状态值都为0-任务未创建
|
|
|
- // 在这之后如果打卡则状态为1-任务已创建已打卡
|
|
|
- // 在这之后如果没打卡则状态为2-任务已创建但未打卡
|
|
|
- // 如果当前日期超过一周中的某天则跳过
|
|
|
- for (LocalDate currentDate : weeklyDateRange) {
|
|
|
- String currentDateStr = currentDate.toString();
|
|
|
+ for (LocalDate weeklyDate : weeklyDateRange) {
|
|
|
+ String weeklyDateStr = weeklyDate.toString();
|
|
|
+
|
|
|
+ // 获取打卡记录
|
|
|
+ PunchInRecord punchInRecord = weeklyRecordMap.get(weeklyDateStr);
|
|
|
+
|
|
|
+ // 根据过往打卡记录设置打卡状态
|
|
|
+ PunchInStatusViewEnum punchInStatus = judgePunchInStatus(today, weeklyDate, punchInCreationDate, punchIn, punchInRecord);
|
|
|
+
|
|
|
// 设置打卡记录
|
|
|
PunchInRecordDto punchInRecordDto = new PunchInRecordDto();
|
|
|
- punchInRecordDto.setPunchInDate(currentDateStr);
|
|
|
- recordDtos.add(punchInRecordDto);
|
|
|
-
|
|
|
- // 还没到当前日期则跳过,根据过往打卡记录设置打卡状态
|
|
|
- if (currentDate.isAfter(today)) {
|
|
|
- punchInRecordDto.setPunchInStatus(PunchInStatusViewEnum.FUTURE_TIME);
|
|
|
- } else if (currentDate.isBefore(punchInCreationDate)) {
|
|
|
- punchInRecordDto.setPunchInStatus(PunchInStatusViewEnum.UNCREATED);
|
|
|
- } else if (recordWeekMap.containsKey(currentDateStr)) {
|
|
|
- punchInRecordDto.setPunchInStatus(PunchInStatusViewEnum.PUNCH_IN);
|
|
|
- } else if (currentDate.isEqual(today) && !recordWeekMap.containsKey(currentDateStr)) {
|
|
|
- punchInRecordDto.setPunchInStatus(PunchInStatusViewEnum.TODAY_UNKNOWN);
|
|
|
- } else {
|
|
|
- punchInRecordDto.setPunchInStatus(PunchInStatusViewEnum.UN_PUNCH_IN);
|
|
|
+ punchInRecordDto.setPunchInDate(weeklyDateStr);
|
|
|
+ punchInRecordDto.setPunchInStatus(punchInStatus);
|
|
|
+ weeklyRecords.add(punchInRecordDto);
|
|
|
+
|
|
|
+ // 如果是今天的打卡记录,设置计数/计时属性
|
|
|
+ if (!Objects.isNull(punchInRecord) && today.isEqual(weeklyDate)) {
|
|
|
+ punchInWithRecordDto.setTimeTrack(punchInRecord.getTimeTrack());
|
|
|
+ punchInWithRecordDto.setCountTrack(punchInRecord.getCountTrack());
|
|
|
+ punchInWithRecordDto.setPunchInStatus(punchInStatus);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- punchInWithRecordDtos.add(punchInWithRecordDto);
|
|
|
}
|
|
|
|
|
|
return punchInWithRecordDtos;
|
|
|
}
|
|
|
|
|
|
+ private PunchInStatusViewEnum judgePunchInStatus(LocalDate today, LocalDate weeklyDate, LocalDate punchInCreationDate, PunchIn punchIn, PunchInRecord punchInRecord) {
|
|
|
+ // 一周某天还没到,无法打卡
|
|
|
+ if (weeklyDate.isAfter(today)) {
|
|
|
+ return PunchInStatusViewEnum.FUTURE_TIME;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 一周某天早于任务创建时间,无法打卡
|
|
|
+ if (weeklyDate.isBefore(punchInCreationDate)) {
|
|
|
+ return PunchInStatusViewEnum.UNCREATED;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 一周某天在今天之前存在打卡记录,则需要判断是否完成打卡
|
|
|
+ if (!Objects.isNull(punchInRecord) && weeklyDate.isBefore(today)) {
|
|
|
+ if (PunchInStatusEnum.FINISH.equals(punchInRecord.getPunchInStatus())
|
|
|
+ ||PunchInStatusEnum.REMAKE_FINISH.equals(punchInRecord.getPunchInStatus())) {
|
|
|
+ return PunchInStatusViewEnum.PUNCH_IN;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 一周的某天是今天,且存在打卡记录,则判断是否完成打卡
|
|
|
+ if (weeklyDate.isEqual(today) && !Objects.isNull(punchInRecord)) {
|
|
|
+ PunchInStatusEnum punchInStatusEnum = settleManager.judgePunchInStatus(punchIn, punchInRecord);
|
|
|
+ if (PunchInStatusEnum.FINISH.equals(punchInStatusEnum)
|
|
|
+ || PunchInStatusEnum.REMAKE_FINISH.equals(punchInStatusEnum)) {
|
|
|
+ return PunchInStatusViewEnum.PUNCH_IN;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 不符合任何情况则是未打卡
|
|
|
+ return PunchInStatusViewEnum.UN_PUNCH_IN;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public PunchInDto queryPunchInById(Long punchInId) {
|
|
|
Assert.isNullInBusiness(punchInId, "请传入待查询的任务ID");
|
|
|
@@ -209,7 +242,7 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
|
|
|
// 打卡类型:单次打卡,需要判断是否重复打卡
|
|
|
if (PunchInCategoryEnum.SINGLE.equals(punchIn.getCategory()) && !Objects.isNull(oldPunchInRecord)) {
|
|
|
- throw BusinessException.fail("已打卡");
|
|
|
+ throw BusinessException.fail("已打卡,无须重复打卡");
|
|
|
}
|
|
|
|
|
|
// 获取或创建打卡记录
|