feat:重构倒计时相关内容,将倒计时逻辑放在后端进行
This commit is contained in:
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.google.protobuf.ServiceException;
|
||||
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
|
||||
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
||||
import lombok.Data;
|
||||
@@ -14,6 +15,8 @@ import java.io.Serializable;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static com.guo.learningprogresstracker.service.impl.StudySessionsServiceImpl.WORK_DURATION;
|
||||
|
||||
/**
|
||||
* 记录每次学习会话的具体数据
|
||||
* @TableName study_sessions
|
||||
@@ -75,6 +78,12 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
|
||||
private String sessionState;
|
||||
|
||||
|
||||
/**
|
||||
* 计时器指针位置,时间戳形式存储(毫秒)
|
||||
*/
|
||||
@TableField(value = "pointer_position")
|
||||
private Long pointerPosition;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -94,10 +103,11 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
|
||||
/**
|
||||
* 继续会话
|
||||
*/
|
||||
public void continueStudySession(){
|
||||
public void continueStudySession() throws ServiceException {
|
||||
if (this.getSessionState().equals(StudySessionStateEnum.PAUSED.name())){
|
||||
this.setSessionState(StudySessionStateEnum.ONGOING.name());
|
||||
this.setLastStartTime(LocalDateTime.now());
|
||||
this.calculatePointerPosition();
|
||||
} else if (this.getSessionState().equals(StudySessionStateEnum.ONGOING.name())) {
|
||||
log.info("进行中的学习会话({})被重新开始",this.getSessionNum());
|
||||
} else{
|
||||
@@ -134,4 +144,75 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
|
||||
this.setSessionState(StudySessionStateEnum.ENDED.name());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 第一次运行
|
||||
* @return
|
||||
*/
|
||||
public boolean isFirstRun() {
|
||||
if (ObjectUtils.isEmpty(this.lastStartTime)) {
|
||||
return false;
|
||||
} else if (this.lastStartTime.equals(this.startTime)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算当前倒计时指针的位置(单位:毫秒)
|
||||
*
|
||||
* 规则说明:
|
||||
* - 如果是第一次运行(lastStartTime为null),则倒计时从25分钟开始
|
||||
* - 如果是暂停状态,返回上次保存的pointerPosition
|
||||
* - 如果是进行中状态,返回 pointerPosition -(当前时间 - lastStartTime)
|
||||
* 即从上次开始后经历的时间,从之前保存的pointerPosition中扣除
|
||||
*
|
||||
* @return 指针当前位置,单位为毫秒
|
||||
*/
|
||||
public void calculatePointerPosition() throws ServiceException {
|
||||
if (isFirstRun()) {
|
||||
this.pointerPosition = WORK_DURATION * 60 * 1000L; // 毫秒
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
StudySessionStateEnum state = StudySessionStateEnum.valueOf(sessionState);
|
||||
|
||||
switch (state) {
|
||||
case PAUSED:
|
||||
// 暂停状态,不修改 pointerPosition
|
||||
return;
|
||||
|
||||
case ONGOING:
|
||||
long elapsedMillis = Duration.between(lastStartTime, LocalDateTime.now()).toMillis();
|
||||
long newPointerPosition = pointerPosition - elapsedMillis;
|
||||
if (newPointerPosition <= 0) {
|
||||
// 如果循环已经结束,则需要重置指针位置
|
||||
this.pointerPosition = WORK_DURATION * 1000L;
|
||||
} else {
|
||||
this.pointerPosition = newPointerPosition;
|
||||
}
|
||||
return;
|
||||
|
||||
case ENDED:
|
||||
throw new ServiceException("已经结束的任务不应再计算倒计时起始位置");
|
||||
|
||||
default:
|
||||
log.error("未知学习会话状态: {}", this.sessionState);
|
||||
throw new RuntimeException("未知学习会话状态,无法计算倒计时起始位置");
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("无效的学习会话状态: {}", sessionState, e);
|
||||
throw new ServiceException("无效的学习会话状态: " + sessionState, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isOverTime() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
long minutes = Duration.between(ObjectUtils.isEmpty(lastStartTime) ? LocalDateTime.now() : lastStartTime, now).toMinutes();
|
||||
return minutes >= WORK_DURATION * 2;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user