diff --git a/src/main/java/com/guo/learningprogresstracker/common/GlobalExceptionHandler.java b/src/main/java/com/guo/learningprogresstracker/common/GlobalExceptionHandler.java index b3eba3f..2914a63 100644 --- a/src/main/java/com/guo/learningprogresstracker/common/GlobalExceptionHandler.java +++ b/src/main/java/com/guo/learningprogresstracker/common/GlobalExceptionHandler.java @@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.entity.CommonResult; import com.guo.learningprogresstracker.exception.AppException; import com.guo.learningprogresstracker.exception.ErrorParameterException; import com.guo.learningprogresstracker.exception.NotFindEntitiesException; +import com.guo.learningprogresstracker.exception.OperationFailedException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -21,16 +22,25 @@ public class GlobalExceptionHandler { @ExceptionHandler({ErrorParameterException.class}) public CommonResult errorParameterException(ErrorParameterException ex) { + log.warn("参数异常: {}", ex.getMessage(), ex); return CommonResult.error(ex.getMessage()); } @ExceptionHandler({NotFindEntitiesException.class}) public CommonResult notFindEntitiesException(NotFindEntitiesException ex) { + log.warn("数据不存在: {}", ex.getMessage(), ex); + return CommonResult.error(ex.getMessage()); + } + + @ExceptionHandler({OperationFailedException.class}) + public CommonResult operationFailedException(OperationFailedException ex) { + log.warn("操作失败: {}", ex.getMessage(), ex); return CommonResult.error(ex.getMessage()); } @ExceptionHandler({AppException.class}) public CommonResult AppException(AppException ex) { + log.error("业务异常: {}", ex.getMessage(), ex); return CommonResult.serverError(ex.getMessage()); } @@ -49,7 +59,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public CommonResult Exception(Exception ex) { log.error("系统异常", ex); - return CommonResult.error(ex.getMessage()); + return CommonResult.error("操作没有成功,请稍后再试"); } } diff --git a/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java b/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java index d0b5bda..1a228d2 100644 --- a/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java +++ b/src/main/java/com/guo/learningprogresstracker/controller/StudySessionController.java @@ -57,7 +57,7 @@ public class StudySessionController { * 通过sessionNum获取一个【学习会话】 */ @GetMapping("/{sessionNum}") - public CommonResult getStudySessionBySessionNum(@NotEmpty(message = "sessionNum不可为空") @PathVariable String sessionNum) throws ErrorParameterException { + public CommonResult getStudySessionBySessionNum(@NotEmpty(message = "请提供学习会话编号") @PathVariable String sessionNum) throws ErrorParameterException { // 通过 service 层获取,包含归属校验 StudySessionResponse response = studySessionsServiceImpl.getStudySessionBySessionNum(sessionNum); return CommonResult.success(response); diff --git a/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java b/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java index 67d263b..82219ec 100644 --- a/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java +++ b/src/main/java/com/guo/learningprogresstracker/controller/TaskController.java @@ -132,7 +132,7 @@ public class TaskController { * 开始或继续一个【学习会话】 */ @GetMapping("/{taskNum}/study-sessions/start-or-continue") - public CommonResult startOrContinueStudySession(@NotEmpty(message = "taskNum不可为空") + public CommonResult startOrContinueStudySession(@NotEmpty(message = "请提供任务编号") @PathVariable String taskNum) throws NotFindEntitiesException, ServiceException { StudySessionResponse response = studySessionsServiceImpl.startOrContinueStudySession(taskNum); return CommonResult.success(response); @@ -143,7 +143,7 @@ public class TaskController { * 通过学习任务num获取该任务的未结束会话 */ @GetMapping("/{taskNum}/not-ended-study-session") - public CommonResult getNotEndedStudySessionByTaskNum(@NotEmpty(message = "taskNum不可为空") + public CommonResult getNotEndedStudySessionByTaskNum(@NotEmpty(message = "请提供任务编号") @PathVariable String taskNum) throws ErrorParameterException { StudySessionResponse response = studySessionsServiceImpl.getNotEndedStudySessionByTaskNum(taskNum); return CommonResult.success(response); diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java index 53756b9..891a72c 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java @@ -16,6 +16,6 @@ public class CreateFragmentsRequest { /** * 残片内容,学习内容的描述 */ - @NotBlank(message = "啊?无字天书?") + @NotBlank(message = "请填写学习内容") private String content; } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/CreateTaskApplicationRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/CreateTaskApplicationRequest.java index b66c360..808926b 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/CreateTaskApplicationRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/CreateTaskApplicationRequest.java @@ -8,7 +8,7 @@ public class CreateTaskApplicationRequest { private String taskNum; - @NotBlank(message = "应用项目标题不可为空") + @NotBlank(message = "请填写应用项目标题") private String title; private String description; diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/EndedStudySessionRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/EndedStudySessionRequest.java index dcff657..1e7313e 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/EndedStudySessionRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/EndedStudySessionRequest.java @@ -8,6 +8,6 @@ import lombok.Data; */ @Data public class EndedStudySessionRequest { - @NotBlank(message = "啊?搞无字天书是吧?报告内容不可为空") + @NotBlank(message = "请填写学习报告内容") private String content; } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java index a9e88a8..960b43e 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/RecallCompareRequest.java @@ -9,7 +9,7 @@ import lombok.Data; @Data public class RecallCompareRequest { - @NotBlank(message = "回忆大纲不可为空") + @NotBlank(message = "请先填写回忆大纲") private String recallOutline; /** 复习起点节点路径(以 / 分隔),null 为任务维度 */ diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java index f35b696..2ae83f1 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/TaskRequest.java @@ -15,16 +15,16 @@ import lombok.Data; public class TaskRequest { public interface Create{} - @Null(message = "创建时不可指定任务ID",groups = Ops.CreateG.class) - @NotNull(message = "更新时必须指定任务ID",groups = Ops.UpdateG.class) + @Null(message = "创建任务时不需要填写任务编号",groups = Ops.CreateG.class) + @NotNull(message = "缺少要更新的任务信息,请刷新后重试",groups = Ops.UpdateG.class) private Integer id; /** * 学习任务的名称 */ - @NotEmpty(message = "必须指定非空的任务名称", groups = Ops.CreateG.class) + @NotEmpty(message = "请填写任务名称", groups = Ops.CreateG.class) private String taskName; - @NotEmpty(message = "必须指定非空的任务描述", groups = Ops.CreateG.class) + @NotEmpty(message = "请填写任务描述", groups = Ops.CreateG.class) private String taskDescription; /** @@ -35,41 +35,41 @@ public class TaskRequest { /** * 用户设置的任务紧急性 */ - @NotNull(message = "必须指定【任务紧急性】指标", groups = Ops.CreateG.class) - @Min(value = 0,message = "urgency参数值必须大于等于0") - @Max(value = 5,message = "urgency参数值必须小于等于5") + @NotNull(message = "请设置任务紧急性", groups = Ops.CreateG.class) + @Min(value = 0,message = "任务紧急性不能小于 0") + @Max(value = 5,message = "任务紧急性不能大于 5") private Integer urgency; /** * 用户设置的任务重要性 */ - @NotNull(message = "必须指定【任务重要性】指标", groups = Ops.CreateG.class) - @Min(value = 0,message = "importance参数值必须大于等于0") - @Max(value = 5,message = "importance参数值必须小于等于5") + @NotNull(message = "请设置任务重要性", groups = Ops.CreateG.class) + @Min(value = 0,message = "任务重要性不能小于 0") + @Max(value = 5,message = "任务重要性不能大于 5") private Integer importance; /** * 任务的内容难度 */ - @NotNull(message = "必须指定【内容难度】指标", groups = Ops.CreateG.class) - @Min(value = 0,message = "contentDifficulty参数值必须大于等于0") - @Max(value = 5,message = "contentDifficulty参数值必须小于等于5") + @NotNull(message = "请设置内容难度", groups = Ops.CreateG.class) + @Min(value = 0,message = "内容难度不能小于 0") + @Max(value = 5,message = "内容难度不能大于 5") private Integer contentDifficulty; /** * 任务的未来价值 */ - @NotNull(message = "必须指定【未来价值】指标", groups = Ops.CreateG.class) - @Min(value = 0,message = "futureValue参数值必须大于等于0") - @Max(value = 5,message = "futureValue参数值必须小于等于5") + @NotNull(message = "请设置未来价值", groups = Ops.CreateG.class) + @Min(value = 0,message = "未来价值不能小于 0") + @Max(value = 5,message = "未来价值不能大于 5") private Integer futureValue; /** * 用户对任务的主观优先级 */ - @NotNull(message = "必须指定【主观优先级】指标", groups = Ops.CreateG.class) - @Min(value = 0,message = "subjectivePriority参数值必须大于等于0") - @Max(value = 5,message = "subjectivePriority参数值必须小于等于5") + @NotNull(message = "请设置主观优先级", groups = Ops.CreateG.class) + @Min(value = 0,message = "主观优先级不能小于 0") + @Max(value = 5,message = "主观优先级不能大于 5") private Integer subjectivePriority; } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateFragmentsRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateFragmentsRequest.java index 37acce3..c60273f 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateFragmentsRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateFragmentsRequest.java @@ -11,6 +11,6 @@ public class UpdateFragmentsRequest { /** * 残片内容,学习内容的描述 */ - @NotBlank(message = "啊?无字天书?") + @NotBlank(message = "请填写学习内容") private String content; } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateStandardMindMapRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateStandardMindMapRequest.java index bff71f0..4551912 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateStandardMindMapRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateStandardMindMapRequest.java @@ -9,6 +9,6 @@ import lombok.Data; @Data public class UpdateStandardMindMapRequest { - @NotBlank(message = "思维导图大纲不可为空") + @NotBlank(message = "请填写思维导图大纲") private String outline; } diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateTaskApplicationRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateTaskApplicationRequest.java index 17ae893..0aa336e 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateTaskApplicationRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/UpdateTaskApplicationRequest.java @@ -6,7 +6,7 @@ import lombok.Data; @Data public class UpdateTaskApplicationRequest { - @NotBlank(message = "应用项目标题不可为空") + @NotBlank(message = "请填写应用项目标题") private String title; private String description; diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/UpsertExpectationRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/UpsertExpectationRequest.java index f70cc3e..733bb65 100644 --- a/src/main/java/com/guo/learningprogresstracker/dto/request/UpsertExpectationRequest.java +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/UpsertExpectationRequest.java @@ -9,6 +9,6 @@ import lombok.Data; @Data public class UpsertExpectationRequest { - @NotBlank(message = "学习预期不可为空") + @NotBlank(message = "请填写学习预期") private String description; } diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyExpectationsServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyExpectationsServiceImpl.java index b77b8b4..692a79e 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyExpectationsServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyExpectationsServiceImpl.java @@ -8,11 +8,13 @@ import com.guo.learningprogresstracker.mapper.StudyExpectationsMapper; import com.guo.learningprogresstracker.mapper.StudySessionsMapper; import com.guo.learningprogresstracker.service.StudyExpectationsService; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * 学习预期服务实现 */ +@Slf4j @Service @RequiredArgsConstructor public class StudyExpectationsServiceImpl implements StudyExpectationsService { @@ -26,7 +28,8 @@ public class StudyExpectationsServiceImpl implements StudyExpectationsService { Wrappers.lambdaQuery() .eq(StudySessionsEntity::getSessionNum, sessionNum)); if (!sessionExists) { - throw new ErrorParameterException("学习会话[" + sessionNum + "]不存在"); + log.warn("学习会话[{}]不存在", sessionNum); + throw new ErrorParameterException("这次学习会话不存在或已结束"); } StudyExpectationsEntity existing = getBySessionNum(sessionNum); diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java index 5378613..3c3a83a 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java @@ -12,6 +12,7 @@ import com.guo.learningprogresstracker.mapper.StudySessionsMapper; import com.guo.learningprogresstracker.service.StudyReportFragmentsService; import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -20,6 +21,7 @@ import java.util.List; /** * 针对表【study_report_fragments(记录学习过程中的学习内容报告残片)】的数据库操作Service实现 */ +@Slf4j @Service @RequiredArgsConstructor public class StudyReportFragmentsServiceImpl extends ServiceImpl @@ -35,7 +37,8 @@ public class StudyReportFragmentsServiceImpl extends ServiceImpl new NotFindEntitiesException(String.format("[%s]不存在", taskNum))); + .orElseThrow(() -> { + log.warn("[{}]不存在", taskNum); + return new NotFindEntitiesException("这个任务不存在或已被删除"); + }); StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery() .eq(StudySessionsEntity::getTaskNum, taskNum) @@ -88,10 +91,10 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); + .orElseThrow(() -> sessionNotFound(sessionNum)); if (StudySessionStateEnum.PAUSED.name().equals(studySessionsEntity.getSessionState())) { log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复暂停"); - throw new ErrorParameterException("会话[" + sessionNum + "]已经暂停,请勿重复暂停!"); + throw new ErrorParameterException("这次学习会话已经暂停了,不用重复暂停哦"); } studySessionsEntity.calculatePointerPosition(); studySessionsEntity.pausedStudySession(endTime); @@ -104,7 +107,7 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); + .orElseThrow(() -> sessionNotFound(sessionNum)); boolean wasOngoing = StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState()); studySessionsEntity.endedStudySession(); this.updateById(studySessionsEntity); @@ -122,9 +125,12 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("任务[" + taskNum + "]不存在")); + .orElseThrow(() -> taskNotFound(taskNum)); StudySessionsDto dto = Optional.ofNullable(studyReportsMapper.getNotEndedStudySessionDtoByTaskNum(taskNum)) - .orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在进行中或暂停中的会话")); + .orElseThrow(() -> { + log.warn("任务[{}]不存在进行中或暂停中的会话", taskNum); + return new ErrorParameterException("这个任务当前没有进行中或已暂停的学习会话"); + }); return StudySessionConvert.MAPPER.toStudySessionResponse(dto); } @@ -136,7 +142,7 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); + .orElseThrow(() -> sessionNotFound(sessionNum)); if (StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState())) { log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复开始"); - throw new ErrorParameterException("会话[" + sessionNum + "]已经开始,请勿重复开始!"); + throw new ErrorParameterException("这次学习会话已经开始了,不用重复开始哦"); } else if (StudySessionStateEnum.ENDED.name().equals(studySessionsEntity.getSessionState())) { log.error("会话[" + studySessionsEntity.getSessionNum() + "]处于结束状态,不可开始该会话!"); - throw new ErrorParameterException("会话[" + sessionNum + "]处于结束状态,不可开始该会话!"); + throw new ErrorParameterException("这次学习会话已经结束,无法再次开始"); } studySessionsEntity.continueStudySession(); this.updateById(studySessionsEntity); @@ -163,7 +169,7 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); + .orElseThrow(() -> sessionNotFound(sessionNum)); return StudySessionConvert.MAPPER.toStudySessionResponse(session); } @@ -174,7 +180,7 @@ public class StudySessionsServiceImpl extends ServiceImpl new ErrorParameterException("会话[" + sessionNum + "]不存在")); + .orElseThrow(() -> sessionNotFound(sessionNum)); ArrayList fragments = getAllFragments(sessionNum); if (fragments.isEmpty()) { @@ -262,4 +268,14 @@ public class StudySessionsServiceImpl extends ServiceImpl if (this.exists(Wrappers.lambdaQuery(TaskEntity.class) .eq(TaskEntity::getTaskName, task.getTaskName()))) { - throw new ErrorParameterException(String.format("任务名[%s]重复", task.getTaskName())); + log.warn("任务名[{}]重复", task.getTaskName()); + throw new ErrorParameterException("已经有同名任务了,换个任务名称吧"); } task.setTaskNum(GenerateNumTool.generateNum("TASK")); @@ -132,7 +133,10 @@ public class TasksServiceImpl extends ServiceImpl @Override public TaskApplicationEntity updateApplication(Integer id, UpdateTaskApplicationRequest request) throws NotFindEntitiesException { TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id)) - .orElseThrow(() -> new NotFindEntitiesException("应用场景[" + id + "]不存在")); + .orElseThrow(() -> { + log.warn("应用场景[{}]不存在", id); + return new NotFindEntitiesException("这个应用场景不存在或已被删除"); + }); existing.setTitle(request.getTitle()); existing.setDescription(request.getDescription()); existing.setResourceUrl(request.getResourceUrl()); @@ -144,14 +148,18 @@ public class TasksServiceImpl extends ServiceImpl @Override public void deleteApplication(Integer id) throws NotFindEntitiesException { TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id)) - .orElseThrow(() -> new NotFindEntitiesException("应用场景[" + id + "]不存在")); + .orElseThrow(() -> { + log.warn("应用场景[{}]不存在", id); + return new NotFindEntitiesException("这个应用场景不存在或已被删除"); + }); taskApplicationMapper.deleteById(existing.getId()); } private void ensureTaskExists(String taskNum) throws NotFindEntitiesException { if (!StringUtils.hasText(taskNum) || !tasksMapper.exists( Wrappers.lambdaQuery().eq(TaskEntity::getTaskNum, taskNum))) { - throw new NotFindEntitiesException("任务[" + taskNum + "]不存在"); + log.warn("任务[{}]不存在", taskNum); + throw new NotFindEntitiesException("这个任务不存在或已被删除"); } }