feat:重构倒计时相关内容,将倒计时逻辑放在后端进行

This commit is contained in:
2025-07-21 09:18:28 +08:00
parent 617f1e9f1f
commit f89c570ff2
9 changed files with 142 additions and 38 deletions
@@ -2,6 +2,7 @@ package com.guo.learningprogresstracker.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.protobuf.ServiceException;
import com.guo.learningprogresstracker.dto.StudySessionsDto;
import com.guo.learningprogresstracker.dto.response.StudySessionResponse;
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
@@ -17,6 +18,7 @@ import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
import com.guo.learningprogresstracker.service.StudySessionsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@@ -29,46 +31,55 @@ import java.util.stream.Collectors;
* @description 针对表【study_sessions(记录每次学习会话的具体数据)】的数据库操作Service实现
* @createDate 2024-06-09 15:28:48
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, StudySessionsEntity>
implements StudySessionsService {
public static final int WORK_DURATION = 25;
private final TasksServiceImpl tasksServiceImpl;
private final StudyReportFragmentsServiceImpl studyReportFragmentsServiceImpl;
private final StudyReportFragmentsMapper studyReportFragmentsMapper;
private final StudyReportsMapper studyReportsMapper;
public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException {
// taskNum是否存在
TaskEntity taskEntity = tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException, ServiceException {
TaskEntity taskEntity = tasksServiceImpl.getOneOpt(
Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在", taskNum)));
// 任务taskNum下是否存在非"已结束"的学习会话
Optional<StudySessionsEntity> oneOpt = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery()
.eq(StudySessionsEntity::getTaskNum, taskNum)
.ne(StudySessionsEntity::getSessionState, StudySessionStateEnum.ENDED.name()));
StudySessionsEntity studySessionsEntity;
// 有则返回该未完成会话,否则创建新会话
if (oneOpt.isEmpty()) {
// 返回一个新的会话
studySessionsEntity = StudySessionsEntity.initNewSession();
studySessionsEntity.setTaskNum(taskNum);
studySessionsEntity.setLastStartTime(LocalDateTime.now());
this.save(studySessionsEntity);
} else {
// 返回已经存在的会话
studySessionsEntity = oneOpt.get();
// todo 是否调整任务状态,需要再思考思考
// studySessionsEntity.continueStudySession();
this.updateById(studySessionsEntity);
StudySessionsEntity session = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery()
.eq(StudySessionsEntity::getTaskNum, taskNum)
.ne(StudySessionsEntity::getSessionState, StudySessionStateEnum.ENDED.name()))
.orElseGet(() -> {
// 如果没有则创建新会话
StudySessionsEntity newSession = StudySessionsEntity.initNewSession();
newSession.setTaskNum(taskNum);
newSession.setLastStartTime(LocalDateTime.now());
this.save(newSession);
return newSession;
});
session.calculatePointerPosition();
if (session.isOverTime()) {
session.pausedStudySession(session.getLastStartTime().plusMinutes(WORK_DURATION));
this.updateById(session);
}
StudySessionResponse response = StudySessionConvert.MAPPER.toStudySessionResponse(studySessionsEntity);
StudySessionResponse response = StudySessionConvert.MAPPER.toStudySessionResponse(session);
response.setTaskName(taskEntity.getTaskName());
if (session.isOverTime()) {
response.setSystemMessage("上段学习任务已经超时25分钟,将仅计算为25分钟的有效学习时间,请注意休息!");
}
return response;
}
@Override
public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException {
public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException, ServiceException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
@@ -76,6 +87,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复暂停");
throw new ErrorParameterException("会话[" + sessionNum + "]已经暂停,请勿重复暂停!");
}
studySessionsEntity.calculatePointerPosition();
studySessionsEntity.pausedStudySession(endTime);
this.updateById(studySessionsEntity);
}
@@ -116,7 +128,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
}
@Override
public void continueStudySession(String sessionNum) throws ErrorParameterException {
public void continueStudySession(String sessionNum) throws ErrorParameterException, ServiceException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));