|
|
@@ -1,15 +1,17 @@
|
|
|
package com.punchsettle.server.service.manager.impl;
|
|
|
|
|
|
-import java.sql.Timestamp;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
@@ -18,6 +20,8 @@ import com.punchsettle.server.atomic.entity.PunchInRecord;
|
|
|
import com.punchsettle.server.atomic.service.IPunchInRecordService;
|
|
|
import com.punchsettle.server.atomic.service.IPunchInService;
|
|
|
import com.punchsettle.server.common.exception.BusinessException;
|
|
|
+import com.punchsettle.server.common.utils.Assert;
|
|
|
+import com.punchsettle.server.constant.PunchInStatusEnum;
|
|
|
import com.punchsettle.server.dto.PunchInDto;
|
|
|
import com.punchsettle.server.dto.PunchInRecordDto;
|
|
|
import com.punchsettle.server.dto.PunchInRecordQuery;
|
|
|
@@ -35,6 +39,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
* @date 2024/11/25 15:12
|
|
|
*/
|
|
|
@Slf4j
|
|
|
+@Service
|
|
|
public class PunchInManagerImpl implements IPunchInManager {
|
|
|
|
|
|
@Autowired
|
|
|
@@ -45,10 +50,10 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
|
|
|
@Override
|
|
|
public List<PunchInWithRecordDto> queryPunchInAndRecord() {
|
|
|
- Long currentUserId = UserUtils.getCurrentUserId();
|
|
|
- if (Objects.isNull(currentUserId)) {
|
|
|
- throw BusinessException.fail("无法获取当前用户信息,无法查询打卡任务");
|
|
|
- }
|
|
|
+ // 获取当前用户ID
|
|
|
+ Long currentUserId = Optional.ofNullable(UserUtils.getCurrentUserId())
|
|
|
+ .orElseThrow(() -> BusinessException.fail("无法获取当前用户信息,无法查询打卡任务"));
|
|
|
+
|
|
|
// 查询打卡任务
|
|
|
List<PunchIn> punchIns = punchInService.listByCreatedBy(currentUserId);
|
|
|
if (CollectionUtils.isEmpty(punchIns)) {
|
|
|
@@ -56,46 +61,75 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
return List.of();
|
|
|
}
|
|
|
|
|
|
- List<PunchInWithRecordDto> punchInWithRecordDtos = punchIns.stream().map(v -> {
|
|
|
- PunchInWithRecordDto dto = new PunchInWithRecordDto();
|
|
|
- BeanUtils.copyProperties(v, dto);
|
|
|
- dto.setPunchInId(v.getId());
|
|
|
- return dto;
|
|
|
- }).collect(Collectors.toList());
|
|
|
-
|
|
|
- // 获取一周的起始日期范围,找出范围内的打卡记录
|
|
|
- List<String> weeklyDateRange = DateUtils.getWeeklyDateRangeStr();
|
|
|
- String startDate = weeklyDateRange.getFirst();
|
|
|
- String endDate = weeklyDateRange.getLast();
|
|
|
+ // 获取一周的起始日期范围,
|
|
|
+ List<LocalDate> weeklyDateRange = DateUtils.getWeeklyDateRange();
|
|
|
// 获取打卡任务ID
|
|
|
List<Long> punchInIds = punchIns.stream().map(PunchIn::getId).collect(Collectors.toList());
|
|
|
-
|
|
|
+ // 找出范围内的打卡记录
|
|
|
PunchInRecordQuery query = new PunchInRecordQuery();
|
|
|
query.setPunchInIds(punchInIds);
|
|
|
- query.setStartDate(startDate);
|
|
|
- query.setEndDate(endDate);
|
|
|
+ query.setStartDate(weeklyDateRange.getFirst().toString());
|
|
|
+ query.setEndDate(weeklyDateRange.getLast().toString());
|
|
|
List<PunchInRecord> punchInRecords = punchInRecordService.listByCondition(query);
|
|
|
|
|
|
// 按打卡任务任务分组
|
|
|
Map<Long, List<PunchInRecord>> recordMap =
|
|
|
punchInRecords.stream().collect(Collectors.groupingBy(PunchInRecord::getPunchInId));
|
|
|
- // 构建打卡记录
|
|
|
+
|
|
|
+ // 日期格式化
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- for (PunchInWithRecordDto dto : punchInWithRecordDtos) {
|
|
|
- List<PunchInRecord> records = Optional.ofNullable(recordMap.get(dto.getPunchInId())).orElse(List.of());
|
|
|
- Map<Object, Integer> punchInDateMap =
|
|
|
- records.stream().collect(Collectors.toMap(record -> sdf.format(record.getPunchInTime()),
|
|
|
- PunchInRecord::getPunchInStatus, (key1, key2) -> key1));
|
|
|
- // 构建打卡记录
|
|
|
- List<PunchInRecordDto> punchInRecordDtoList = weeklyDateRange.stream().map(punchInDate -> {
|
|
|
- PunchInRecordDto punchInRecordDto = new PunchInRecordDto();
|
|
|
- Integer punchInStatus = punchInDateMap.get(punchInDate);
|
|
|
- punchInRecordDto.setPunchInDate(punchInDate);
|
|
|
- punchInRecordDto.setPunchInStatus(punchInStatus);
|
|
|
- return punchInRecordDto;
|
|
|
- }).collect(Collectors.toList());
|
|
|
|
|
|
- dto.setPunchInRecords(punchInRecordDtoList);
|
|
|
+ // 当前日期
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+
|
|
|
+ // 构建打卡记录
|
|
|
+ List<PunchInWithRecordDto> punchInWithRecordDtos = new ArrayList<>();
|
|
|
+ for (PunchIn punchIn : punchIns) {
|
|
|
+ PunchInWithRecordDto punchInWithRecordDto = new PunchInWithRecordDto();
|
|
|
+ BeanUtils.copyProperties(punchIn, punchInWithRecordDto);
|
|
|
+ punchInWithRecordDto.setPunchInId(punchIn.getId());
|
|
|
+
|
|
|
+ // 打卡任务创建日期
|
|
|
+ LocalDate punchInCreationDate = LocalDate.parse(sdf.format(punchIn.getCreationTime()));
|
|
|
+
|
|
|
+ // 获取打卡任务对应的打卡记录,并按打卡日期转为map
|
|
|
+ List<PunchInRecord> records =
|
|
|
+ Optional.ofNullable(recordMap.get(punchInWithRecordDto.getPunchInId())).orElse(List.of());
|
|
|
+ Map<String, PunchInRecord> recordWeekMap = 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();
|
|
|
+ // 设置打卡记录
|
|
|
+ PunchInRecordDto punchInRecordDto = new PunchInRecordDto();
|
|
|
+ punchInRecordDto.setPunchInDate(currentDateStr);
|
|
|
+ recordDtos.add(punchInRecordDto);
|
|
|
+
|
|
|
+ // 还没到当前日期则跳过
|
|
|
+ if (currentDate.isAfter(today)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据过往打卡记录设置打卡状态
|
|
|
+ if (currentDate.isBefore(punchInCreationDate)) {
|
|
|
+ punchInRecordDto.setPunchInStatus(PunchInStatusEnum.UNCREATED.getValue());
|
|
|
+ } else if (recordWeekMap.containsKey(currentDateStr)) {
|
|
|
+ punchInRecordDto.setPunchInStatus(PunchInStatusEnum.PUNCHIN.getValue());
|
|
|
+ } else {
|
|
|
+ punchInRecordDto.setPunchInStatus(PunchInStatusEnum.UNPUNCHIN.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ punchInWithRecordDtos.add(punchInWithRecordDto);
|
|
|
}
|
|
|
|
|
|
return punchInWithRecordDtos;
|
|
|
@@ -103,33 +137,28 @@ public class PunchInManagerImpl implements IPunchInManager {
|
|
|
|
|
|
@Override
|
|
|
public void saveOrUpdatePunchIn(PunchInDto dto) {
|
|
|
- if (Objects.isNull(dto)) {
|
|
|
- throw BusinessException.fail("请传入任务信息");
|
|
|
- }
|
|
|
+ Assert.isNullInBusiness(dto.getId(), "请传入任务信息");
|
|
|
+
|
|
|
PunchIn punchIn = new PunchIn();
|
|
|
BeanUtils.copyProperties(dto, punchIn);
|
|
|
+ punchIn.setArchiveFlag(false);
|
|
|
punchInService.insertOrUpdate(punchIn);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deletePunchIn(Long punchInId) {
|
|
|
- if (Objects.isNull(punchInId)) {
|
|
|
- throw BusinessException.fail("请传入待删除的任务");
|
|
|
- }
|
|
|
+ Assert.isNullInBusiness(punchInId, "请传入待删除的任务");
|
|
|
punchInService.delete(punchInId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void doPunchIn(Long punchInId) {
|
|
|
- if (Objects.isNull(punchInId)) {
|
|
|
- throw BusinessException.fail("请传入待打卡的任务");
|
|
|
- }
|
|
|
-
|
|
|
+ Assert.isNullInBusiness(punchInId, "请传入待打卡的任务");
|
|
|
// 创建打卡记录
|
|
|
PunchInRecord punchInRecord = new PunchInRecord();
|
|
|
punchInRecord.setPunchInId(punchInId);
|
|
|
- punchInRecord.setPunchInTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ punchInRecord.setPunchInDate(LocalDate.now().toString());
|
|
|
punchInRecordService.insert(punchInRecord);
|
|
|
}
|
|
|
}
|