From b1358eaca0275b36dda568e3c80f79d0af9740d1 Mon Sep 17 00:00:00 2001 From: guo <1716967236@qq.com> Date: Thu, 11 Jul 2024 22:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=B6=E6=9E=84=E6=80=A7=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E3=80=90=E6=87=92=E5=BE=97=E5=8C=BA=E5=88=86=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E5=93=88=E5=93=88=E5=93=88=E5=93=88=E3=80=91=EF=BC=8C=E4=B8=BB?= =?UTF-8?q?=E8=A6=81=E6=98=AF=E8=B7=91=E9=80=9A=E4=BA=86=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8A=9F=E8=83=BD=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 49 +++++++++-- .../config/AutoTableField.java | 41 ++++++++++ .../controller/StudySessionController.java | 9 +++ .../controller/TaskController.java | 45 +++++++++-- .../dto/PriorityDto.java | 31 +++++++ .../dto/request/TaskRequest.java | 53 ++++++++++++ .../dto/response/TaskInfoResponse.java | 23 ++++++ .../entity/BaseEntity.java | 8 +- .../entity/StudyExpectationsEntity.java | 23 ------ .../entity/StudyReportFragmentsEntity.java | 23 ------ .../entity/StudyReportsEntity.java | 23 ------ .../entity/StudySessionsEntity.java | 23 ------ .../entity/TaskEntity.java | 13 +-- .../entity/UserTaskEntity.java | 23 ------ .../mapStruct/RequestConvert.java | 17 ++++ .../mapStruct/TaskConvert.java | 20 +++++ .../mapper/TasksMapper.java | 4 +- .../service/TasksService.java | 10 ++- .../service/impl/TasksServiceImpl.java | 30 ++++++- .../utils/CalculatedPriorityTool.java | 40 +++++++++ .../utils/GenerateNumTool.java | 15 +++- src/main/resources/application.yml | 2 +- src/main/resources/mapper/TasksMapper.xml | 2 +- .../controller/TaskControllerTest.java | 81 +++++++++++++++++++ 24 files changed, 460 insertions(+), 148 deletions(-) create mode 100644 src/main/java/com/guo/learningprogresstracker/config/AutoTableField.java create mode 100644 src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java create mode 100644 src/main/java/com/guo/learningprogresstracker/dto/PriorityDto.java create mode 100644 src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java create mode 100644 src/main/java/com/guo/learningprogresstracker/dto/response/TaskInfoResponse.java create mode 100644 src/main/java/com/guo/learningprogresstracker/mapStruct/RequestConvert.java create mode 100644 src/main/java/com/guo/learningprogresstracker/mapStruct/TaskConvert.java create mode 100644 src/main/java/com/guo/learningprogresstracker/utils/CalculatedPriorityTool.java create mode 100644 src/test/java/com/guo/learningprogresstracker/controller/TaskControllerTest.java diff --git a/pom.xml b/pom.xml index bcd8df9..b88887a 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,9 @@ LearningProgressTracker 17 + 1.5.5.Final + 1.18.30 + 0.2.0 @@ -31,8 +34,9 @@ org.projectlombok lombok - true + ${lombok.version} + org.springframework.boot spring-boot-starter-test @@ -51,6 +55,7 @@ org.flywaydb flyway-mysql + mysql @@ -63,11 +68,23 @@ 1.2.8 + + org.mapstruct + mapstruct + ${mapstruct.version} + + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + com.baomidou mybatis-plus-boot-starter - + org.mybatis mybatis-spring @@ -99,15 +116,31 @@ - org.springframework.boot - spring-boot-maven-plugin + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 - - + true + 17 + 17 + --enable-preview + + org.projectlombok lombok - - + ${lombok.version} + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + org.projectlombok + lombok-mapstruct-binding + ${lombok-mapstruct-binding.version} + + diff --git a/src/main/java/com/guo/learningprogresstracker/config/AutoTableField.java b/src/main/java/com/guo/learningprogresstracker/config/AutoTableField.java new file mode 100644 index 0000000..1d60b5e --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/config/AutoTableField.java @@ -0,0 +1,41 @@ +package com.guo.learningprogresstracker.config; + +import cn.dev33.satoken.stp.StpUtil; +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import com.guo.learningprogresstracker.entity.UserEntity; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.reflection.MetaObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Configuration; + +import java.time.LocalDateTime; +import java.util.Date; + +/** + * 用于配置 + * + * @author guo + */ +@Configuration +@Slf4j +public class AutoTableField implements MetaObjectHandler { + + + @Override + public void insertFill(MetaObject metaObject) { + log.info("insert autoField"); + this.strictInsertFill(metaObject, "createdBy", StpUtil::getLoginIdAsString, String.class); + this.strictInsertFill(metaObject, "createdTime", LocalDateTime::now, LocalDateTime.class); + this.strictInsertFill(metaObject, "lastModifiedBy", StpUtil::getLoginIdAsString, String.class); + this.strictInsertFill(metaObject, "lastModifiedTime", LocalDateTime::now, LocalDateTime.class); + } + + + @Override + public void updateFill(MetaObject metaObject) { + this.strictInsertFill(metaObject, "lastModifiedBy", StpUtil::getLoginIdAsString, String.class); + this.strictInsertFill(metaObject, "lastModifiedTime", LocalDateTime::now, LocalDateTime.class); + } + +} diff --git a/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java b/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java new file mode 100644 index 0000000..af80476 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java @@ -0,0 +1,9 @@ +package com.guo.learningprogresstracker.controller; + +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class StudySessionController { + + +} diff --git a/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java b/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java index 934d36b..b77132a 100644 --- a/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java +++ b/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java @@ -1,26 +1,59 @@ package com.guo.learningprogresstracker.controller; import com.guo.learningprogresstracker.entity.CommonResult; +import com.guo.learningprogresstracker.dto.request.TaskRequest; import com.guo.learningprogresstracker.service.TasksService; -import com.guo.learningprogresstracker.service.impl.TasksServiceImpl; -import org.springframework.stereotype.Controller; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.config.Task; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; 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.RestController; /** * 任务控制层 + * @author guo */ -@Controller +@RestController +@RequiredArgsConstructor public class TaskController { private final TasksService tasksService; - public TaskController(TasksServiceImpl tasksService) { - this.tasksService = tasksService; + @PostMapping("/tasks") + @Operation(summary = "添加新任务") + public CommonResult addTask(@RequestBody TaskRequest taskRequest) { + return CommonResult.success(tasksService.addTask(taskRequest)); } + @Operation(summary = "任务详情API-未完成") @GetMapping("/tasks/{taskId}") - public CommonResult showTask(@PathVariable("taskId") String taskId) { + public CommonResult showTask(@PathVariable("taskId") String taskId) { return CommonResult.success(tasksService.showTask(taskId)); } + + @PutMapping("/tasks/{taskId}") + @Operation(summary = "更新任务详情") + public CommonResult updateTask(@PathVariable("taskId") String taskId, @RequestBody Task updatedTask) { +// return CommonResult.success(tasksService.updateTask(taskId, updatedTask)); + return null; + } + + @DeleteMapping("/tasks/{taskId}") + @Operation(summary = "删除指定任务") + public CommonResult deleteTask(@PathVariable("taskId") String taskId) { +// return CommonResult.success(tasksService.deleteTask(taskId)); + return null; + } + + @GetMapping("/tasks") + @Operation(summary = "获取所有任务列表") + public CommonResult listTasks() { +// return CommonResult.success(tasksService.listTasks()); + return null; + } } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/PriorityDto.java b/src/main/java/com/guo/learningprogresstracker/dto/PriorityDto.java new file mode 100644 index 0000000..89f8bee --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/dto/PriorityDto.java @@ -0,0 +1,31 @@ +package com.guo.learningprogresstracker.dto; + +import lombok.Data; + +@Data +public class PriorityDto { + /** + * 用户设置的任务紧急性 + */ + private Integer urgency; + + /** + * 用户设置的任务重要性 + */ + private Integer importance; + + /** + * 任务的内容难度 + */ + private Integer contentDifficulty; + + /** + * 任务的未来价值 + */ + private Integer futureValue; + + /** + * 用户对任务的主观优先级 + */ + private Integer subjectivePriority; +} diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java new file mode 100644 index 0000000..7421d09 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java @@ -0,0 +1,53 @@ +package com.guo.learningprogresstracker.dto.request; + +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +/** + * 学习任务-请求参数-创建 + */ +@Data +public class TaskRequest { + static class Create{} + /** + * 学习任务的名称 + */ + @NotEmpty(message = "必须指定非空的任务名称", groups = TaskRequest.Create.class) + private String taskName; + + /** + * 学习材料的存储URL + */ + private String materialUrl; + + /** + * 用户设置的任务紧急性 + */ + @NotEmpty(message = "必须指定【任务紧急性】指标", groups = Create.class) + private Integer urgency; + + /** + * 用户设置的任务重要性 + */ + @NotEmpty(message = "必须指定【任务重要性】指标", groups = Create.class) + private Integer importance; + + /** + * 任务的内容难度 + */ + @NotEmpty(message = "必须指定【内容难度】指标", groups = Create.class) + private Integer contentDifficulty; + + /** + * 任务的未来价值 + */ + @NotEmpty(message = "必须指定【未来价值】指标", groups = Create.class) + private Integer futureValue; + + /** + * 用户对任务的主观优先级 + */ + @NotEmpty(message = "必须指定【主观优先级】指标", groups = Create.class) + private Integer subjectivePriority; + +} diff --git a/src/main/java/com/guo/learningprogresstracker/dto/response/TaskInfoResponse.java b/src/main/java/com/guo/learningprogresstracker/dto/response/TaskInfoResponse.java new file mode 100644 index 0000000..bc74080 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/dto/response/TaskInfoResponse.java @@ -0,0 +1,23 @@ +package com.guo.learningprogresstracker.dto.response; + +import lombok.Data; + +import java.util.List; + +@Data +public class TaskInfoResponse { + // 任务列表 + private List taskList; + + + static class TaskInfo{ + // 任务名称 + private String taskName; + // 任务描述 + private String taskDescription; + // 任务优先级 + private Integer taskPriority; + // 上次该任务学习情况 + private String lastLearningStatus; + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/entity/BaseEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/BaseEntity.java index c7671ad..4eb2222 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/BaseEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/BaseEntity.java @@ -15,12 +15,12 @@ public class BaseEntity { @TableField(fill = FieldFill.INSERT) private LocalDateTime createdTime; - @TableField(fill = FieldFill.INSERT_UPDATE) - private LocalDateTime updateTime; - @TableField(fill = FieldFill.INSERT) private String createdBy; @TableField(fill = FieldFill.INSERT_UPDATE) - private String updatedBy; + private LocalDateTime lastModifiedTime; + + @TableField(fill = FieldFill.INSERT_UPDATE) + private String lastModifiedBy; } diff --git a/src/main/java/com/guo/learningprogresstracker/entity/StudyExpectationsEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/StudyExpectationsEntity.java index 1132b2c..76fd27a 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/StudyExpectationsEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/StudyExpectationsEntity.java @@ -33,29 +33,6 @@ public class StudyExpectationsEntity extends BaseEntity implements Serializable @TableField(value = "description") private String description; - /** - * - */ - @TableField(value = "created_time") - private LocalDateTime createdTime; - - /** - * - */ - @TableField(value = "created_by") - private String createdBy; - - /** - * - */ - @TableField(value = "last_modified_time") - private LocalDateTime lastModifiedTime; - - /** - * - */ - @TableField(value = "last_modified_by") - private String lastModifiedBy; @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java index f8776f4..be35614 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java @@ -33,29 +33,6 @@ public class StudyReportFragmentsEntity extends BaseEntity implements Serializab @TableField(value = "content") private String content; - /** - * 创建时间 - */ - @TableField(value = "created_time") - private LocalDateTime createdTime; - - /** - * - */ - @TableField(value = "created_by") - private String createdBy; - - /** - * - */ - @TableField(value = "last_modified_time") - private LocalDateTime lastModifiedTime; - - /** - * - */ - @TableField(value = "last_modified_by") - private String lastModifiedBy; @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/guo/learningprogresstracker/entity/StudyReportsEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/StudyReportsEntity.java index 2dbea5f..f104de3 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/StudyReportsEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/StudyReportsEntity.java @@ -33,29 +33,6 @@ public class StudyReportsEntity extends BaseEntity implements Serializable { @TableField(value = "content") private String content; - /** - * 创建时间 - */ - @TableField(value = "created_time") - private LocalDateTime createdTime; - - /** - * - */ - @TableField(value = "created_by") - private String createdBy; - - /** - * - */ - @TableField(value = "last_modified_time") - private LocalDateTime lastModifiedTime; - - /** - * - */ - @TableField(value = "last_modified_by") - private String lastModifiedBy; @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java index 5dae95a..03c464a 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/StudySessionsEntity.java @@ -63,29 +63,6 @@ public class StudySessionsEntity extends BaseEntity implements Serializable { @TableField(value = "session_state") private String sessionState; - /** - * - */ - @TableField(value = "created_by") - private String createdBy; - - /** - * - */ - @TableField(value = "last_modified_time") - private LocalDateTime lastModifiedTime; - - /** - * - */ - @TableField(value = "last_modified_by") - private String lastModifiedBy; - - /** - * - */ - @TableField(value = "created_time") - private LocalDateTime createdTime; @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/guo/learningprogresstracker/entity/TaskEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/TaskEntity.java index 87b5b13..cc80c0b 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/TaskEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/TaskEntity.java @@ -6,9 +6,8 @@ import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.Null; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 存储学习任务的基本信息,包括优先级的多维度计算 @@ -17,11 +16,15 @@ import lombok.Data; @TableName(value ="tasks") @Data public class TaskEntity extends BaseEntity implements Serializable { + + @TableId(value = "id",type = IdType.AUTO) + private int id; + /** - * + * 任务编码 */ - @TableId(value = "task_id", type = IdType.AUTO) - private Integer taskId; + @TableField(value = "task_num") + private String taskNum; /** * 学习任务的名称 diff --git a/src/main/java/com/guo/learningprogresstracker/entity/UserTaskEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/UserTaskEntity.java index e529aff..7f7c9b4 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/UserTaskEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/UserTaskEntity.java @@ -31,29 +31,6 @@ public class UserTaskEntity extends BaseEntity implements Serializable { @TableField(value = "task_id") private Integer taskId; - /** - * - */ - @TableField(value = "created_time") - private LocalDateTime createdTime; - - /** - * - */ - @TableField(value = "created_by") - private String createdBy; - - /** - * - */ - @TableField(value = "last_modified_time") - private LocalDateTime lastModifiedTime; - - /** - * - */ - @TableField(value = "last_modified_by") - private String lastModifiedBy; @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/guo/learningprogresstracker/mapStruct/RequestConvert.java b/src/main/java/com/guo/learningprogresstracker/mapStruct/RequestConvert.java new file mode 100644 index 0000000..ea55d2b --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/mapStruct/RequestConvert.java @@ -0,0 +1,17 @@ +package com.guo.learningprogresstracker.mapStruct; + +import com.guo.learningprogresstracker.dto.PriorityDto; +import com.guo.learningprogresstracker.dto.request.TaskRequest; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; + +/** + * @author guo + */ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface RequestConvert { + RequestConvert MAPPER = Mappers.getMapper(RequestConvert.class); + + PriorityDto taskRequestToPriorityDto(TaskRequest taskRequest); +} diff --git a/src/main/java/com/guo/learningprogresstracker/mapStruct/TaskConvert.java b/src/main/java/com/guo/learningprogresstracker/mapStruct/TaskConvert.java new file mode 100644 index 0000000..83ef819 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/mapStruct/TaskConvert.java @@ -0,0 +1,20 @@ +package com.guo.learningprogresstracker.mapStruct; + + +import com.guo.learningprogresstracker.dto.request.TaskRequest; +import com.guo.learningprogresstracker.entity.TaskEntity; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; + +/** + * 任务相关转换 + * @author guo + */ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface TaskConvert { + TaskConvert MAPPER = Mappers.getMapper(TaskConvert.class); + + TaskEntity taskRequestToTaskEntity(TaskRequest taskRequest); + +} diff --git a/src/main/java/com/guo/learningprogresstracker/mapper/TasksMapper.java b/src/main/java/com/guo/learningprogresstracker/mapper/TasksMapper.java index e9e9b3c..eb45cca 100644 --- a/src/main/java/com/guo/learningprogresstracker/mapper/TasksMapper.java +++ b/src/main/java/com/guo/learningprogresstracker/mapper/TasksMapper.java @@ -1,6 +1,6 @@ package com.guo.learningprogresstracker.mapper; -import com.guo.learningprogresstracker.entity.TasksEntity; +import com.guo.learningprogresstracker.entity.TaskEntity; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; @@ -11,7 +11,7 @@ import org.apache.ibatis.annotations.Mapper; * @Entity com.guo.learningprogresstracker.entity.TasksEntity */ @Mapper -public interface TasksMapper extends BaseMapper { +public interface TasksMapper extends BaseMapper { } diff --git a/src/main/java/com/guo/learningprogresstracker/service/TasksService.java b/src/main/java/com/guo/learningprogresstracker/service/TasksService.java index 6f480c3..11d09ea 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/TasksService.java +++ b/src/main/java/com/guo/learningprogresstracker/service/TasksService.java @@ -1,6 +1,8 @@ package com.guo.learningprogresstracker.service; -import com.guo.learningprogresstracker.entity.TasksEntity; +import com.guo.learningprogresstracker.dto.request.TaskRequest; +import com.guo.learningprogresstracker.dto.response.TaskInfoResponse; +import com.guo.learningprogresstracker.entity.TaskEntity; import com.baomidou.mybatisplus.extension.service.IService; /** @@ -8,6 +10,10 @@ import com.baomidou.mybatisplus.extension.service.IService; * @description 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service * @createDate 2024-06-09 15:28:48 */ -public interface TasksService extends IService { +public interface TasksService extends IService { + + TaskInfoResponse showTask(String taskId); + + String addTask(TaskRequest taskRequest); } diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java index 08c9bb2..61ae9de 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/TasksServiceImpl.java @@ -1,9 +1,17 @@ package com.guo.learningprogresstracker.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.guo.learningprogresstracker.entity.TasksEntity; +import com.guo.learningprogresstracker.dto.PriorityDto; +import com.guo.learningprogresstracker.dto.request.TaskRequest; +import com.guo.learningprogresstracker.dto.response.TaskInfoResponse; +import com.guo.learningprogresstracker.entity.TaskEntity; +import com.guo.learningprogresstracker.mapStruct.RequestConvert; +import com.guo.learningprogresstracker.mapStruct.TaskConvert; import com.guo.learningprogresstracker.service.TasksService; import com.guo.learningprogresstracker.mapper.TasksMapper; +import com.guo.learningprogresstracker.utils.CalculatedPriorityTool; +import com.guo.learningprogresstracker.utils.GenerateNumTool; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; /** @@ -12,9 +20,27 @@ import org.springframework.stereotype.Service; * @createDate 2024-06-09 15:28:48 */ @Service -public class TasksServiceImpl extends ServiceImpl +@RequiredArgsConstructor +public class TasksServiceImpl extends ServiceImpl implements TasksService{ + + @Override + public TaskInfoResponse showTask(String taskId) { + + TaskInfoResponse result = new TaskInfoResponse(); + return result; + } + + @Override + public String addTask(TaskRequest taskRequest) { + TaskEntity task= TaskConvert.MAPPER.taskRequestToTaskEntity(taskRequest); + task.setTaskNum(GenerateNumTool.generateNum("TASK")); + PriorityDto priorityDto = RequestConvert.MAPPER.taskRequestToPriorityDto(taskRequest); + task.setCalculatedPriority(CalculatedPriorityTool.calculatedPriority(priorityDto)); + this.save(task); + return task.getTaskNum(); + } } diff --git a/src/main/java/com/guo/learningprogresstracker/utils/CalculatedPriorityTool.java b/src/main/java/com/guo/learningprogresstracker/utils/CalculatedPriorityTool.java new file mode 100644 index 0000000..58dfe8c --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/utils/CalculatedPriorityTool.java @@ -0,0 +1,40 @@ +package com.guo.learningprogresstracker.utils; + +import com.guo.learningprogresstracker.dto.PriorityDto; + +/** + * 加权优先级计算工具 + * + * @author guo + */ +public class CalculatedPriorityTool { + + /** + * 通过用户选择的5个选项,结合定义的权重计算任务的加权优先级。 + * 急迫性 (Urgency) + * 急迫性代表任务的紧急程度。权重:0.35 + *

+ * 重要性 (Importance) + * 重要性指示任务的重要程度。权重:0.25 + *

+ * 内容难度 (Content Difficulty) + * 内容难度衡量任务内容的复杂性。权重:0.20 + *

+ * 未来价值 (Future Value) + * 未来价值估计完成任务的长期价值。权重:0.10 + *

+ * 主观优先级 (Subjective Priority) + * 主观优先级是用户对任务优先级的个人评估。权重:0.10 + * + * @param priorityDto + * @return + */ + public static Double calculatedPriority(PriorityDto priorityDto) { + // 暂时写死权重,也许可以考虑让用户自行配置权重 + return (priorityDto.getUrgency() * 0.35) + + (priorityDto.getImportance() * 0.25) + + (priorityDto.getContentDifficulty() * 0.20) + + (priorityDto.getFutureValue() * 0.10) + + (priorityDto.getSubjectivePriority() * 0.10); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/utils/GenerateNumTool.java b/src/main/java/com/guo/learningprogresstracker/utils/GenerateNumTool.java index e34d658..afab60d 100644 --- a/src/main/java/com/guo/learningprogresstracker/utils/GenerateNumTool.java +++ b/src/main/java/com/guo/learningprogresstracker/utils/GenerateNumTool.java @@ -12,7 +12,7 @@ import java.util.Date; */ @RequiredArgsConstructor public class GenerateNumTool { - public static int getNextSequence() { + private static int getNextSequence() { // 获取当前时间的毫秒级时间戳 long timestamp = System.currentTimeMillis(); @@ -27,7 +27,18 @@ public class GenerateNumTool { * @param separator * @return */ - public String generateNum(String prefix, String separator) { + public static String generateNum(String prefix, String separator) { + String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date()); + + // 唯一的后缀 + int sequence = getNextSequence(); + + // 返回完整的 taskNum + return prefix + separator + currentDate + separator + sequence; + } + + public static String generateNum(String prefix) { + String separator = "-"; String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date()); // 唯一的后缀 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3a1defc..64e4227 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,7 +9,7 @@ spring: password: flyway: # 是否启动 flyway - enabled: true + enabled: false # 文件编码 encoding: UTF-8 # SQL 脚本位置,默认位于 classpath 下的 db/migration 目录 diff --git a/src/main/resources/mapper/TasksMapper.xml b/src/main/resources/mapper/TasksMapper.xml index 0f6d723..75dcb9f 100644 --- a/src/main/resources/mapper/TasksMapper.xml +++ b/src/main/resources/mapper/TasksMapper.xml @@ -4,7 +4,7 @@ "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + diff --git a/src/test/java/com/guo/learningprogresstracker/controller/TaskControllerTest.java b/src/test/java/com/guo/learningprogresstracker/controller/TaskControllerTest.java new file mode 100644 index 0000000..cb82336 --- /dev/null +++ b/src/test/java/com/guo/learningprogresstracker/controller/TaskControllerTest.java @@ -0,0 +1,81 @@ +package com.guo.learningprogresstracker.controller; + +import cn.dev33.satoken.stp.StpUtil; +import com.guo.learningprogresstracker.dto.request.TaskRequest; +import com.guo.learningprogresstracker.dto.response.TaskInfoResponse; +import com.guo.learningprogresstracker.entity.CommonResult; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +class TaskControllerTest { + + @Autowired + private TaskController taskController; + + @BeforeEach + void setUp() { + StpUtil.login("测试用户", "test_driver"); + } + @Test + void addTask() { + TaskRequest taskRequest = mock(TaskRequest.class); + // 设置学习任务的名称 + when(taskRequest.getTaskName()).thenReturn("测试任务名称"); + + // 设置学习材料的存储URL + when(taskRequest.getMaterialUrl()).thenReturn("http://example.com/material"); + + // 设置用户设置的任务紧急性 + when(taskRequest.getUrgency()).thenReturn(5); + + // 设置用户设置的任务重要性 + when(taskRequest.getImportance()).thenReturn(3); + + // 设置任务的内容难度 + when(taskRequest.getContentDifficulty()).thenReturn(4); + + // 设置任务的未来价值 + when(taskRequest.getFutureValue()).thenReturn(5); + + // 设置用户对任务的主观优先级 + when(taskRequest.getSubjectivePriority()).thenReturn(2); + CommonResult commonResult = taskController.addTask(taskRequest); + + // 验证 addTask 是否被调用一次 + assertEquals(commonResult.getCode(), 200,"测试数据插入失败"); + + } + + @Test + void showTask() { + String taskId = mock(String.class); + TaskInfoResponse mockData = mock(TaskInfoResponse.class); + + CommonResult commonResult = taskController.showTask(taskId); + assertEquals(commonResult.getCode(),200,"获取数据失败"); + assertNotNull(commonResult.getData(),"返回值中不存在数据"); + assertEquals(commonResult.getData(),mockData,"返回值携带数据不正确"); + } + + @Test + void updateTask() { + } + + @Test + void deleteTask() { + } + + @Test + void listTasks() { + } +} \ No newline at end of file