[xingyu.guo] feat: 1. 替换所有响应值中的id为num,不将非业务字段展示给外部 2. 调整[实际使用时间]等字段类型为double,方便计算\保存与读取 3. 【获取所有任务列表】api,完善功能,并增加分页功能 4.将int类型修改为使用Integer,更加安全

This commit is contained in:
guo
2024-09-22 08:20:48 +08:00
parent 7315f02ce6
commit 6005782b14
12 changed files with 86 additions and 67 deletions
@@ -1,5 +1,6 @@
package com.guo.learningprogresstracker.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guo.learningprogresstracker.dto.response.StudySessionResponce;
import com.guo.learningprogresstracker.entity.CommonResult;
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
@@ -32,12 +33,13 @@ public class StudySessionController {
}
/**
* 通过sessionId获取一个【学习会话】
* 通过sessionNum获取一个【学习会话】
*/
@GetMapping("/{sessionId}")
public CommonResult getStudySession(@NotEmpty(message = "sessionId不可为空") @PathVariable String sessionId) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = studySessionsServiceImpl.getOptById(sessionId)
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionId + "]不存在"));
@GetMapping("/{sessionNum}")
public CommonResult getStudySession(@NotEmpty(message = "sessionId不可为空") @PathVariable String sessionNum) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = studySessionsServiceImpl.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
StudySessionResponce studySessionResponce = StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity);
return CommonResult.success(studySessionResponce);
}
@@ -45,9 +47,9 @@ public class StudySessionController {
/**
* 暂停一个【学习会话】
*/
@PostMapping("/{taskId}/studay-sessions/pause")
public CommonResult pauseStudySession(@PathVariable("sessionId") String sessionId) throws ErrorParameterException {
studySessionsServiceImpl.pauseStudySession(sessionId);
@PostMapping("/{sessionNum}/studay-sessions/pause")
public CommonResult pauseStudySession(@PathVariable("sessionNum") String sessionNum) throws ErrorParameterException {
studySessionsServiceImpl.pauseStudySession(sessionNum);
return CommonResult.success();
}
@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.rmi.ServerException;
@@ -64,8 +65,9 @@ public class TaskController {
@GetMapping("/tasks")
@Operation(summary = "获取所有任务列表")
public CommonResult tasksList() {
return CommonResult.success(tasksService.taskList());
public CommonResult tasksList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
return CommonResult.success(tasksService.taskList(pageNum, pageSize));
}
/**
@@ -73,7 +75,7 @@ public class TaskController {
*
*/
@GetMapping("/{taskNum}/studay-sessions/start-or-continue")
public CommonResult startOrContinueStudySession(@NotEmpty(message = "taskId不可为空")
public CommonResult startOrContinueStudySession(@NotEmpty(message = "taskNum不可为空")
@PathVariable String taskNum) throws NotFindEntitiesException {
StudySessionResponce responce = studySessionsServiceImpl.startOrContinueStudySession(taskNum);
return CommonResult.success(responce);
@@ -0,0 +1,20 @@
package com.guo.learningprogresstracker.dto;
import lombok.Data;
/**
* @author guo
*/
@Data
public class TaskInfo {
// 任务编码
private String taskNum;
// 任务名称
private String taskName;
// 任务描述
private String taskDescription;
// 任务优先级
private Double taskPriority;
// 上次该任务学习情况
private String lastLearningStatus;
}
@@ -8,9 +8,9 @@ import java.time.LocalDateTime;
@Data
public class StudySessionResponce {
private Integer sessionId;
private String sessionNum;
private Integer taskId;
private String taskNum;
@Schema(description = "学习开始时间")
@@ -25,11 +25,11 @@ public class StudySessionResponce {
@Schema(description = "实际使用时间(分钟)")
private Integer actualTime;
private double actualTime;
@Schema(description = "有效学习时间(分钟)")
private Integer effectiveTime;
private double effectiveTime;
@Schema(description = "有效时间比")
@@ -1,5 +1,6 @@
package com.guo.learningprogresstracker.dto.response;
import com.guo.learningprogresstracker.dto.TaskInfo;
import lombok.Data;
import java.util.List;
@@ -13,14 +14,4 @@ public class TasksListInfoResponse {
private List<TaskInfo> taskList;
static class TaskInfo{
// 任务名称
private String taskName;
// 任务描述
private String taskDescription;
// 任务优先级
private Integer taskPriority;
// 上次该任务学习情况
private String lastLearningStatus;
}
}
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
import com.guo.learningprogresstracker.utils.GenerateNumTool;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -49,22 +50,22 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
private LocalDateTime endTime;
/**
* 实际使用时间(分钟
* 实际使用时间(
*/
@TableField(value = "actual_time")
private Integer actualTime;
private double actualTime;
/**
* 有效学习时间(分钟
* 有效学习时间(
*/
@TableField(value = "effective_time")
private Integer effectiveTime;
private double effectiveTime;
/**
* 有效时间比
*/
@TableField(value = "effectiveness_ratio")
private Double effectivenessRatio;
private double effectivenessRatio;
/**
* 学习会话的状态(进行中、暂停、已结束)
@@ -82,6 +83,7 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
*/
public static StudySessionsEntity initNewSession(){
StudySessionsEntity studySessionsEntity = new StudySessionsEntity();
studySessionsEntity.setSessionNum(GenerateNumTool.generateNum("SESSION"));
studySessionsEntity.setStartTime(LocalDateTime.now());
studySessionsEntity.setLastModifiedTime(LocalDateTime.now());
studySessionsEntity.setSessionState(StudySessionStateEnum.ONGOING.name());
@@ -105,11 +107,13 @@ public class StudySessionsEntity extends BaseEntity implements Serializable {
*/
public void pausedStudySession(){
if (this.getSessionState().equals(StudySessionStateEnum.PAUSED.name())){
log.warn("不应出现的情况:暂停了一个状态为【{}】的学习会话",this.getSessionState());
//
}else {
this.setEndTime(LocalDateTime.now());
this.setEffectiveTime((int) Duration.between(this.lastStartTime,this.endTime).toMinutes());
this.setActualTime((int) Duration.between(this.endTime,this.startTime).toMinutes());
this.setActualTime(Duration.between(this.startTime,this.endTime).toSeconds());
this.setEffectiveTime(this.getEffectiveTime()+Duration.between(this.lastStartTime,this.endTime).toSeconds());
this.setEffectivenessRatio(this.effectiveTime/this.actualTime);
this.setSessionState(StudySessionStateEnum.PAUSED.name());
}
}
@@ -4,13 +4,13 @@ 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 java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* 存储学习任务的基本信息,包括优先级的多维度计算
* @author guo
* @TableName tasks
*/
@TableName(value ="tasks")
@@ -18,7 +18,7 @@ import lombok.EqualsAndHashCode;
public class TaskEntity extends BaseEntity implements Serializable {
@TableId(value = "id",type = IdType.AUTO)
private int id;
private Integer id;
/**
* 任务编码
@@ -1,10 +1,13 @@
package com.guo.learningprogresstracker.mapStruct;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guo.learningprogresstracker.dto.TaskInfo;
import com.guo.learningprogresstracker.dto.request.TaskRequest;
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
import com.guo.learningprogresstracker.entity.TaskEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
@@ -19,4 +22,9 @@ public interface TaskConvert {
TaskEntity taskRequestToTaskEntity(TaskRequest taskRequest);
TaskInfoResponse taskEntityToTaskInfoResponse(TaskEntity taskEntity);
@Mapping(source = "calculatedPriority",target = "taskPriority")
TaskInfo toTaskInfo(TaskEntity taskEntity);
Page<TaskInfo> toTaskInfoPage(Page<TaskEntity> page);
}
@@ -1,10 +1,11 @@
package com.guo.learningprogresstracker.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.guo.learningprogresstracker.dto.TaskInfo;
import com.guo.learningprogresstracker.dto.request.TaskRequest;
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
import com.guo.learningprogresstracker.dto.response.TasksListInfoResponse;
import com.guo.learningprogresstracker.entity.TaskEntity;
import com.baomidou.mybatisplus.extension.service.IService;
import java.rmi.ServerException;
@@ -15,7 +16,7 @@ import java.rmi.ServerException;
*/
public interface TasksService extends IService<TaskEntity> {
TasksListInfoResponse taskList();
Page<TaskInfo> taskList(Integer pageNum, Integer pageSize);
String addTask(TaskRequest taskRequest);
@@ -14,6 +14,7 @@ import com.guo.learningprogresstracker.service.StudySessionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Optional;
/**
@@ -41,6 +42,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
// 返回一个新的会话
StudySessionsEntity studySessionsEntity = StudySessionsEntity.initNewSession();
studySessionsEntity.setTaskNum(taskNum);
studySessionsEntity.setLastStartTime(LocalDateTime.now());
this.save(studySessionsEntity);
return StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity);
}else {
@@ -52,9 +54,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
}
}
public void pauseStudySession(String sessionId) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOptById(sessionId)
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionId + "]不存在"));
public void pauseStudySession(String sessionNum) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
studySessionsEntity.pausedStudySession();
this.updateById(studySessionsEntity);
}
@@ -1,13 +1,18 @@
package com.guo.learningprogresstracker.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.guo.learningprogresstracker.dto.PriorityDto;
import com.guo.learningprogresstracker.dto.TaskInfo;
import com.guo.learningprogresstracker.dto.request.TaskRequest;
import com.guo.learningprogresstracker.dto.response.TaskInfoResponse;
import com.guo.learningprogresstracker.dto.response.TasksListInfoResponse;
import com.guo.learningprogresstracker.entity.TaskEntity;
import com.guo.learningprogresstracker.mapStruct.RequestConvert;
import com.guo.learningprogresstracker.mapStruct.TaskConvert;
import com.guo.learningprogresstracker.mapStruct.TaskConvertImpl;
import com.guo.learningprogresstracker.service.TasksService;
import com.guo.learningprogresstracker.mapper.TasksMapper;
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
@@ -17,6 +22,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.rmi.ServerException;
import java.util.List;
/**
* @author guo
@@ -30,12 +36,15 @@ public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
implements TasksService{
@Override
public TasksListInfoResponse taskList() {
private final TasksMapper tasksMapper;
private final TaskConvertImpl taskConvertImpl;
TasksListInfoResponse result = new TasksListInfoResponse();
@Override
public Page<TaskInfo> taskList(Integer pageNum, Integer pageSize) {
Page<TaskEntity> page = tasksMapper.selectPage(new Page<TaskEntity>(pageNum, pageSize), Wrappers.lambdaQuery(TaskEntity.class));
//todo 补充返回内容
return result;
Page<TaskInfo> response = taskConvertImpl.MAPPER.toTaskInfoPage(page);
return response;
}
@Override
@@ -4,25 +4,4 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.guo.learningprogresstracker.mapper.StudySessionsMapper">
<resultMap id="BaseResultMap" type="com.guo.learningprogresstracker.entity.StudySessionsEntity">
<id property="sessionId" column="session_id" jdbcType="INTEGER"/>
<result property="taskId" column="task_id" jdbcType="INTEGER"/>
<result property="startTime" column="start_time" jdbcType="TIMESTAMP"/>
<result property="endTime" column="end_time" jdbcType="TIMESTAMP"/>
<result property="actualTime" column="actual_time" jdbcType="INTEGER"/>
<result property="effectiveTime" column="effective_time" jdbcType="INTEGER"/>
<result property="effectivenessRatio" column="effectiveness_ratio" jdbcType="FLOAT"/>
<result property="sessionState" column="session_state" jdbcType="VARCHAR"/>
<result property="createdBy" column="created_by" jdbcType="VARCHAR"/>
<result property="lastModifiedTime" column="last_modified_time" jdbcType="TIMESTAMP"/>
<result property="lastModifiedBy" column="last_modified_by" jdbcType="VARCHAR"/>
<result property="createdTime" column="created_time" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
session_id,task_id,start_time,
end_time,actual_time,effective_time,
effectiveness_ratio,session_state,created_by,
last_modified_time,last_modified_by,created_time
</sql>
</mapper>