feat: 任务开始页面,相关逻辑补充

This commit is contained in:
2025-07-08 09:20:31 +08:00
parent 72c9a0be65
commit 5df805cca7
7 changed files with 71 additions and 28 deletions
+7
View File
@@ -116,6 +116,13 @@
<version>3.5.5</version> <version>3.5.5</version>
</dependency> </dependency>
<!--支持jackson对更多类型的字段进行转换(java8中新增加的日期类型)-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -1,10 +1,12 @@
package com.guo.learningprogresstracker.config; package com.guo.learningprogresstracker.config;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.guo.learningprogresstracker.config.serializer.LocalDateTimeSerializer; import com.guo.learningprogresstracker.config.serializer.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -14,11 +16,17 @@ import java.time.LocalDateTime;
@Configuration @Configuration
public class JacksonConfig { public class JacksonConfig {
@Bean @Bean
public ObjectMapper objectMapper() { public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = builder.build();
// 注册自定义序列化器
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
mapper.registerModule(module); mapper.registerModule(module);
// 关闭序列化为时间戳(使用字符串格式)
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper; return mapper;
} }
} }
@@ -10,8 +10,8 @@ import java.time.format.DateTimeFormatter;
public class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> { public class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> {
// 定义你希望使用的日期时间格式 // 修改为ISO 8601格式,末尾带Z,表示UTC时区
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
public LocalDateTimeSerializer() { public LocalDateTimeSerializer() {
super(LocalDateTime.class); super(LocalDateTime.class);
@@ -22,9 +22,8 @@ public class LocalDateTimeSerializer extends StdSerializer<LocalDateTime> {
if (value == null) { if (value == null) {
gen.writeNull(); gen.writeNull();
} else { } else {
// 格式化LocalDateTime为字符串 // 这里假设LocalDateTime是UTC时间,直接格式化并加Z
String formattedDate = value.format(formatter); String formattedDate = value.format(formatter);
// 将格式化后的字符串写入JSON流作为一个值
gen.writeString(formattedDate); gen.writeString(formattedDate);
} }
} }
@@ -12,13 +12,9 @@ import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@@ -49,12 +45,21 @@ public class StudySessionController {
return CommonResult.success(studySessionResponse); return CommonResult.success(studySessionResponse);
} }
/**
* 开始一个【学习会话】
*/
@PostMapping("/{sessionNum}/study-sessions/continue")
public CommonResult<Void> startStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
studySessionsServiceImpl.continueStudySession(sessionNum);
return CommonResult.success();
}
/** /**
* 暂停一个【学习会话】 * 暂停一个【学习会话】
*/ */
@PostMapping("/{sessionNum}/study-sessions/pause") @PostMapping("/{sessionNum}/study-sessions/pause")
public CommonResult<Void> pauseStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException { public CommonResult<Void> pauseStudySession(@PathVariable("sessionNum") String sessionNum, @RequestParam(value = "endTime",required = false) LocalDateTime endTime) throws ErrorParameterException {
studySessionsServiceImpl.pauseStudySession(sessionNum); studySessionsServiceImpl.pauseStudySession(sessionNum,endTime);
return CommonResult.success(); return CommonResult.success();
} }
@@ -8,6 +8,7 @@ import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
import com.guo.learningprogresstracker.utils.GenerateNumTool; import com.guo.learningprogresstracker.utils.GenerateNumTool;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import java.io.Serializable; import java.io.Serializable;
import java.time.Duration; import java.time.Duration;
@@ -98,7 +99,7 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
this.setSessionState(StudySessionStateEnum.ONGOING.name()); this.setSessionState(StudySessionStateEnum.ONGOING.name());
this.setLastStartTime(LocalDateTime.now()); this.setLastStartTime(LocalDateTime.now());
} else if (this.getSessionState().equals(StudySessionStateEnum.ONGOING.name())) { } else if (this.getSessionState().equals(StudySessionStateEnum.ONGOING.name())) {
log.info("进行中的学习会话({})被重新获取",this.getSessionNum()); log.info("进行中的学习会话({})被重新开始",this.getSessionNum());
} else{ } else{
log.warn("不应出现的情况:开始了一个状态为【{}】的学习会话({})",this.getSessionState(),this.getSessionNum()); log.warn("不应出现的情况:开始了一个状态为【{}】的学习会话({})",this.getSessionState(),this.getSessionNum());
} }
@@ -107,15 +108,15 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
/** /**
* 暂停会话 * 暂停会话
*/ */
public void pausedStudySession(){ public void pausedStudySession(LocalDateTime endTime){
if (this.getSessionState().equals(StudySessionStateEnum.PAUSED.name())){ if (this.getSessionState().equals(StudySessionStateEnum.PAUSED.name())) {
log.warn("不应出现的情况:暂停了一个状态为【{}】的学习会话",this.getSessionState()); log.warn("不应出现的情况:暂停了一个状态为【{}】的学习会话({})", this.getSessionState(), this.getSessionNum());
// //
}else { } else {
this.setEndTime(LocalDateTime.now()); this.setEndTime(ObjectUtils.isEmpty(endTime) ? LocalDateTime.now() : endTime);
this.setActualTime(Duration.between(this.startTime,this.endTime).toSeconds()); this.setActualTime(Duration.between(this.startTime, this.endTime).toSeconds());
this.setEffectiveTime(this.getEffectiveTime()+Duration.between(this.lastStartTime,this.endTime).toSeconds()); this.setEffectiveTime(this.getEffectiveTime() + Duration.between(this.lastStartTime, this.endTime).toSeconds());
this.setEffectivenessRatio(this.effectiveTime/this.actualTime); this.setEffectivenessRatio(this.effectiveTime / this.actualTime);
this.setSessionState(StudySessionStateEnum.PAUSED.name()); this.setSessionState(StudySessionStateEnum.PAUSED.name());
} }
} }
@@ -128,7 +129,7 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
log.warn("不应出现的情况:结束了一个状态为【{}】的学习会话",this.getSessionState()); log.warn("不应出现的情况:结束了一个状态为【{}】的学习会话",this.getSessionState());
}else { }else {
if (this.getSessionState().equals(StudySessionStateEnum.ONGOING.name())) { if (this.getSessionState().equals(StudySessionStateEnum.ONGOING.name())) {
this.pausedStudySession(); this.pausedStudySession(endTime);
} }
this.setSessionState(StudySessionStateEnum.ENDED.name()); this.setSessionState(StudySessionStateEnum.ENDED.name());
} }
@@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.entity.StudySessionsEntity;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.guo.learningprogresstracker.exception.ErrorParameterException; import com.guo.learningprogresstracker.exception.ErrorParameterException;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@@ -20,5 +21,7 @@ public interface StudySessionsService extends IService<StudySessionsEntity> {
void endedStudySession(String sessionNum, String content) throws ErrorParameterException; void endedStudySession(String sessionNum, String content) throws ErrorParameterException;
void pauseStudySession(String sessionNum) throws ErrorParameterException; void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException;
void continueStudySession(String sessionNum) throws ErrorParameterException;
} }
@@ -58,7 +58,8 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
} else { } else {
// 返回已经存在的会话 // 返回已经存在的会话
studySessionsEntity = oneOpt.get(); studySessionsEntity = oneOpt.get();
studySessionsEntity.continueStudySession(); // todo 是否调整任务状态,需要再思考思考
// studySessionsEntity.continueStudySession();
this.updateById(studySessionsEntity); this.updateById(studySessionsEntity);
} }
StudySessionResponse response = StudySessionConvert.MAPPER.toStudySessionResponse(studySessionsEntity); StudySessionResponse response = StudySessionConvert.MAPPER.toStudySessionResponse(studySessionsEntity);
@@ -67,11 +68,15 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
} }
@Override @Override
public void pauseStudySession(String sessionNum) throws ErrorParameterException { public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class) StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum)) .eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在")); .orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
studySessionsEntity.pausedStudySession(); if (StudySessionStateEnum.PAUSED.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复暂停");
throw new ErrorParameterException("会话[" + sessionNum + "]已经暂停,请勿重复暂停!");
}
studySessionsEntity.pausedStudySession(endTime);
this.updateById(studySessionsEntity); this.updateById(studySessionsEntity);
} }
@@ -110,6 +115,21 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
} }
} }
@Override
public void continueStudySession(String sessionNum) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
if (StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复开始");
throw new ErrorParameterException("会话[" + sessionNum + "]已经开始,请勿重复开始!");
} else if (StudySessionStateEnum.ENDED.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]处于结束状态,不可开始该会话!");
throw new ErrorParameterException("会话[" + sessionNum + "]处于结束状态,不可开始该会话!");
}
studySessionsEntity.continueStudySession();
this.updateById(studySessionsEntity);
}
} }