fix: 统一学习会话结算并修正结束幂等
This commit is contained in:
+27
-7
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.protobuf.ServiceException;
|
||||
import com.guo.learningprogresstracker.common.StudyTimePolicy;
|
||||
import com.guo.learningprogresstracker.dto.StudySessionsDto;
|
||||
import com.guo.learningprogresstracker.dto.response.StudySessionResponse;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||
@@ -38,8 +39,6 @@ import java.util.stream.Collectors;
|
||||
public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, StudySessionsEntity>
|
||||
implements StudySessionsService {
|
||||
|
||||
public static final int WORK_DURATION = 25;
|
||||
|
||||
/**
|
||||
* 误操作结束的确认语:用户必须输入完全一致的内容才能零数据关闭会话
|
||||
*/
|
||||
@@ -74,8 +73,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
|
||||
session.calculatePointerPosition();
|
||||
|
||||
if (session.isOverTime()) {
|
||||
session.pausedStudySession(session.getLastStartTime().plusMinutes(WORK_DURATION));
|
||||
// 只有进行中且本段尚未结算的会话才需要收敛;已暂停/已结束的会话不再重复截断
|
||||
boolean forgotten = session.isForgotten();
|
||||
if (forgotten) {
|
||||
session.pausedStudySession(null);
|
||||
this.updateById(session);
|
||||
}
|
||||
|
||||
@@ -84,8 +85,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
response.setMaterialUrl(taskEntity.getMaterialUrl());
|
||||
response.setTaskId(taskEntity.getId());
|
||||
|
||||
if (session.isOverTime()) {
|
||||
response.setSystemMessage("上段学习任务已经超时25分钟,将仅计算为25分钟的有效学习时间,请注意休息!");
|
||||
if (forgotten) {
|
||||
response.setSystemMessage(String.format(
|
||||
"上段学习已超过%d分钟没有任何操作,仅计算为%d分钟的有效学习时间,请注意休息!",
|
||||
StudyTimePolicy.FORGET_THRESHOLD_MINUTES, StudyTimePolicy.WORK_DURATION_MINUTES));
|
||||
}
|
||||
|
||||
return response;
|
||||
@@ -113,9 +116,26 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
|
||||
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
|
||||
.eq(StudySessionsEntity::getSessionNum, sessionNum))
|
||||
.orElseThrow(() -> sessionNotFound(sessionNum));
|
||||
|
||||
// 幂等:已结束的会话不再重复结算,也不会重复生成学习报告
|
||||
if (studySessionsEntity.isEnded()) {
|
||||
log.warn("会话[{}]已结束,本次结束请求按幂等处理,不重复生成学习报告", sessionNum);
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean wasOngoing = StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState());
|
||||
studySessionsEntity.endedStudySession();
|
||||
this.updateById(studySessionsEntity);
|
||||
|
||||
// 条件更新:只有数据库中仍未结束的会话才会被写入结算结果,
|
||||
// 避免重复点击/网络重试等并发请求各自插入一条学习报告
|
||||
boolean ended = this.update(studySessionsEntity, Wrappers.<StudySessionsEntity>lambdaUpdate()
|
||||
.eq(StudySessionsEntity::getId, studySessionsEntity.getId())
|
||||
.ne(StudySessionsEntity::getSessionState, StudySessionStateEnum.ENDED.name()));
|
||||
if (!ended) {
|
||||
log.warn("会话[{}]已被其他请求结束,本次不重复生成学习报告", sessionNum);
|
||||
return null;
|
||||
}
|
||||
|
||||
StudyReportsEntity studyReportsEntity = new StudyReportsEntity();
|
||||
studyReportsEntity.setSessionNum(sessionNum);
|
||||
studyReportsEntity.setContent(content);
|
||||
|
||||
Reference in New Issue
Block a user