refactor: 优化用户可见错误提示

This commit is contained in:
2026-08-03 23:08:26 +08:00
parent b434520eff
commit 6678c2b8c0
16 changed files with 91 additions and 50 deletions
@@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException; import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.exception.ErrorParameterException; import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException; import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import com.guo.learningprogresstracker.exception.OperationFailedException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -21,16 +22,25 @@ public class GlobalExceptionHandler {
@ExceptionHandler({ErrorParameterException.class}) @ExceptionHandler({ErrorParameterException.class})
public CommonResult errorParameterException(ErrorParameterException ex) { public CommonResult errorParameterException(ErrorParameterException ex) {
log.warn("参数异常: {}", ex.getMessage(), ex);
return CommonResult.error(ex.getMessage()); return CommonResult.error(ex.getMessage());
} }
@ExceptionHandler({NotFindEntitiesException.class}) @ExceptionHandler({NotFindEntitiesException.class})
public CommonResult notFindEntitiesException(NotFindEntitiesException ex) { 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()); return CommonResult.error(ex.getMessage());
} }
@ExceptionHandler({AppException.class}) @ExceptionHandler({AppException.class})
public CommonResult AppException(AppException ex) { public CommonResult AppException(AppException ex) {
log.error("业务异常: {}", ex.getMessage(), ex);
return CommonResult.serverError(ex.getMessage()); return CommonResult.serverError(ex.getMessage());
} }
@@ -49,7 +59,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public CommonResult Exception(Exception ex) { public CommonResult Exception(Exception ex) {
log.error("系统异常", ex); log.error("系统异常", ex);
return CommonResult.error(ex.getMessage()); return CommonResult.error("操作没有成功,请稍后再试");
} }
} }
@@ -57,7 +57,7 @@ public class StudySessionController {
* 通过sessionNum获取一个【学习会话】 * 通过sessionNum获取一个【学习会话】
*/ */
@GetMapping("/{sessionNum}") @GetMapping("/{sessionNum}")
public CommonResult<StudySessionResponse> getStudySessionBySessionNum(@NotEmpty(message = "sessionNum不可为空") @PathVariable String sessionNum) throws ErrorParameterException { public CommonResult<StudySessionResponse> getStudySessionBySessionNum(@NotEmpty(message = "请提供学习会话编号") @PathVariable String sessionNum) throws ErrorParameterException {
// 通过 service 层获取,包含归属校验 // 通过 service 层获取,包含归属校验
StudySessionResponse response = studySessionsServiceImpl.getStudySessionBySessionNum(sessionNum); StudySessionResponse response = studySessionsServiceImpl.getStudySessionBySessionNum(sessionNum);
return CommonResult.success(response); return CommonResult.success(response);
@@ -132,7 +132,7 @@ public class TaskController {
* 开始或继续一个【学习会话】 * 开始或继续一个【学习会话】
*/ */
@GetMapping("/{taskNum}/study-sessions/start-or-continue") @GetMapping("/{taskNum}/study-sessions/start-or-continue")
public CommonResult<StudySessionResponse> startOrContinueStudySession(@NotEmpty(message = "taskNum不可为空") public CommonResult<StudySessionResponse> startOrContinueStudySession(@NotEmpty(message = "请提供任务编号")
@PathVariable String taskNum) throws NotFindEntitiesException, ServiceException { @PathVariable String taskNum) throws NotFindEntitiesException, ServiceException {
StudySessionResponse response = studySessionsServiceImpl.startOrContinueStudySession(taskNum); StudySessionResponse response = studySessionsServiceImpl.startOrContinueStudySession(taskNum);
return CommonResult.success(response); return CommonResult.success(response);
@@ -143,7 +143,7 @@ public class TaskController {
* 通过学习任务num获取该任务的未结束会话 * 通过学习任务num获取该任务的未结束会话
*/ */
@GetMapping("/{taskNum}/not-ended-study-session") @GetMapping("/{taskNum}/not-ended-study-session")
public CommonResult<StudySessionResponse> getNotEndedStudySessionByTaskNum(@NotEmpty(message = "taskNum不可为空") public CommonResult<StudySessionResponse> getNotEndedStudySessionByTaskNum(@NotEmpty(message = "请提供任务编号")
@PathVariable String taskNum) throws ErrorParameterException { @PathVariable String taskNum) throws ErrorParameterException {
StudySessionResponse response = studySessionsServiceImpl.getNotEndedStudySessionByTaskNum(taskNum); StudySessionResponse response = studySessionsServiceImpl.getNotEndedStudySessionByTaskNum(taskNum);
return CommonResult.success(response); return CommonResult.success(response);
@@ -16,6 +16,6 @@ public class CreateFragmentsRequest {
/** /**
* 残片内容,学习内容的描述 * 残片内容,学习内容的描述
*/ */
@NotBlank(message = "啊?无字天书?") @NotBlank(message = "请填写学习内容")
private String content; private String content;
} }
@@ -8,7 +8,7 @@ public class CreateTaskApplicationRequest {
private String taskNum; private String taskNum;
@NotBlank(message = "应用项目标题不可为空") @NotBlank(message = "请填写应用项目标题")
private String title; private String title;
private String description; private String description;
@@ -8,6 +8,6 @@ import lombok.Data;
*/ */
@Data @Data
public class EndedStudySessionRequest { public class EndedStudySessionRequest {
@NotBlank(message = "啊?搞无字天书是吧?报告内容不可为空") @NotBlank(message = "请填写学习报告内容")
private String content; private String content;
} }
@@ -9,7 +9,7 @@ import lombok.Data;
@Data @Data
public class RecallCompareRequest { public class RecallCompareRequest {
@NotBlank(message = "回忆大纲不可为空") @NotBlank(message = "请先填写回忆大纲")
private String recallOutline; private String recallOutline;
/** 复习起点节点路径(以 / 分隔),null 为任务维度 */ /** 复习起点节点路径(以 / 分隔),null 为任务维度 */
@@ -15,16 +15,16 @@ import lombok.Data;
public class TaskRequest { public class TaskRequest {
public interface Create{} public interface Create{}
@Null(message = "创建时不可指定任务ID",groups = Ops.CreateG.class) @Null(message = "创建任务时不需要填写任务编号",groups = Ops.CreateG.class)
@NotNull(message = "更新时必须指定任务ID",groups = Ops.UpdateG.class) @NotNull(message = "缺少要更新的任务信息,请刷新后重试",groups = Ops.UpdateG.class)
private Integer id; private Integer id;
/** /**
* 学习任务的名称 * 学习任务的名称
*/ */
@NotEmpty(message = "必须指定非空的任务名称", groups = Ops.CreateG.class) @NotEmpty(message = "请填写任务名称", groups = Ops.CreateG.class)
private String taskName; private String taskName;
@NotEmpty(message = "必须指定非空的任务描述", groups = Ops.CreateG.class) @NotEmpty(message = "请填写任务描述", groups = Ops.CreateG.class)
private String taskDescription; private String taskDescription;
/** /**
@@ -35,41 +35,41 @@ public class TaskRequest {
/** /**
* 用户设置的任务紧急性 * 用户设置的任务紧急性
*/ */
@NotNull(message = "必须指定【任务紧急性】指标", groups = Ops.CreateG.class) @NotNull(message = "请设置任务紧急性", groups = Ops.CreateG.class)
@Min(value = 0,message = "urgency参数值必须大于等于0") @Min(value = 0,message = "任务紧急性不能小于 0")
@Max(value = 5,message = "urgency参数值必须小于等于5") @Max(value = 5,message = "任务紧急性不能大于 5")
private Integer urgency; private Integer urgency;
/** /**
* 用户设置的任务重要性 * 用户设置的任务重要性
*/ */
@NotNull(message = "必须指定【任务重要性】指标", groups = Ops.CreateG.class) @NotNull(message = "请设置任务重要性", groups = Ops.CreateG.class)
@Min(value = 0,message = "importance参数值必须大于等于0") @Min(value = 0,message = "任务重要性不能小于 0")
@Max(value = 5,message = "importance参数值必须小于等于5") @Max(value = 5,message = "任务重要性不能大于 5")
private Integer importance; private Integer importance;
/** /**
* 任务的内容难度 * 任务的内容难度
*/ */
@NotNull(message = "必须指定【内容难度】指标", groups = Ops.CreateG.class) @NotNull(message = "请设置内容难度", groups = Ops.CreateG.class)
@Min(value = 0,message = "contentDifficulty参数值必须大于等于0") @Min(value = 0,message = "内容难度不能小于 0")
@Max(value = 5,message = "contentDifficulty参数值必须小于等于5") @Max(value = 5,message = "内容难度不能大于 5")
private Integer contentDifficulty; private Integer contentDifficulty;
/** /**
* 任务的未来价值 * 任务的未来价值
*/ */
@NotNull(message = "必须指定【未来价值】指标", groups = Ops.CreateG.class) @NotNull(message = "请设置未来价值", groups = Ops.CreateG.class)
@Min(value = 0,message = "futureValue参数值必须大于等于0") @Min(value = 0,message = "未来价值不能小于 0")
@Max(value = 5,message = "futureValue参数值必须小于等于5") @Max(value = 5,message = "未来价值不能大于 5")
private Integer futureValue; private Integer futureValue;
/** /**
* 用户对任务的主观优先级 * 用户对任务的主观优先级
*/ */
@NotNull(message = "必须指定【主观优先级】指标", groups = Ops.CreateG.class) @NotNull(message = "请设置主观优先级", groups = Ops.CreateG.class)
@Min(value = 0,message = "subjectivePriority参数值必须大于等于0") @Min(value = 0,message = "主观优先级不能小于 0")
@Max(value = 5,message = "subjectivePriority参数值必须小于等于5") @Max(value = 5,message = "主观优先级不能大于 5")
private Integer subjectivePriority; private Integer subjectivePriority;
} }
@@ -11,6 +11,6 @@ public class UpdateFragmentsRequest {
/** /**
* 残片内容,学习内容的描述 * 残片内容,学习内容的描述
*/ */
@NotBlank(message = "啊?无字天书?") @NotBlank(message = "请填写学习内容")
private String content; private String content;
} }
@@ -9,6 +9,6 @@ import lombok.Data;
@Data @Data
public class UpdateStandardMindMapRequest { public class UpdateStandardMindMapRequest {
@NotBlank(message = "思维导图大纲不可为空") @NotBlank(message = "请填写思维导图大纲")
private String outline; private String outline;
} }
@@ -6,7 +6,7 @@ import lombok.Data;
@Data @Data
public class UpdateTaskApplicationRequest { public class UpdateTaskApplicationRequest {
@NotBlank(message = "应用项目标题不可为空") @NotBlank(message = "请填写应用项目标题")
private String title; private String title;
private String description; private String description;
@@ -9,6 +9,6 @@ import lombok.Data;
@Data @Data
public class UpsertExpectationRequest { public class UpsertExpectationRequest {
@NotBlank(message = "学习预期不可为空") @NotBlank(message = "请填写学习预期")
private String description; private String description;
} }
@@ -8,11 +8,13 @@ import com.guo.learningprogresstracker.mapper.StudyExpectationsMapper;
import com.guo.learningprogresstracker.mapper.StudySessionsMapper; import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
import com.guo.learningprogresstracker.service.StudyExpectationsService; import com.guo.learningprogresstracker.service.StudyExpectationsService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* 学习预期服务实现 * 学习预期服务实现
*/ */
@Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class StudyExpectationsServiceImpl implements StudyExpectationsService { public class StudyExpectationsServiceImpl implements StudyExpectationsService {
@@ -26,7 +28,8 @@ public class StudyExpectationsServiceImpl implements StudyExpectationsService {
Wrappers.<StudySessionsEntity>lambdaQuery() Wrappers.<StudySessionsEntity>lambdaQuery()
.eq(StudySessionsEntity::getSessionNum, sessionNum)); .eq(StudySessionsEntity::getSessionNum, sessionNum));
if (!sessionExists) { if (!sessionExists) {
throw new ErrorParameterException("学习会话[" + sessionNum + "]不存在"); log.warn("学习会话[{}]不存在", sessionNum);
throw new ErrorParameterException("这次学习会话不存在或已结束");
} }
StudyExpectationsEntity existing = getBySessionNum(sessionNum); StudyExpectationsEntity existing = getBySessionNum(sessionNum);
@@ -12,6 +12,7 @@ import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
import com.guo.learningprogresstracker.service.StudyReportFragmentsService; import com.guo.learningprogresstracker.service.StudyReportFragmentsService;
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper; import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -20,6 +21,7 @@ import java.util.List;
/** /**
* 针对表【study_report_fragments(记录学习过程中的学习内容报告残片)】的数据库操作Service实现 * 针对表【study_report_fragments(记录学习过程中的学习内容报告残片)】的数据库操作Service实现
*/ */
@Slf4j
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class StudyReportFragmentsServiceImpl extends ServiceImpl<StudyReportFragmentsMapper, StudyReportFragmentsEntity> public class StudyReportFragmentsServiceImpl extends ServiceImpl<StudyReportFragmentsMapper, StudyReportFragmentsEntity>
@@ -35,7 +37,8 @@ public class StudyReportFragmentsServiceImpl extends ServiceImpl<StudyReportFrag
Wrappers.lambdaQuery(StudySessionsEntity.class) Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, entity.getSessionNum())); .eq(StudySessionsEntity::getSessionNum, entity.getSessionNum()));
if (session == null) { if (session == null) {
throw new NotFindEntitiesException(String.format("未能找到学习会话sessionNum[%s]", entity.getSessionNum())); log.warn("未能找到学习会话sessionNum[{}]", entity.getSessionNum());
throw new NotFindEntitiesException("这次学习会话不存在或已结束");
} }
this.save(entity); this.save(entity);
} }
@@ -44,7 +47,8 @@ public class StudyReportFragmentsServiceImpl extends ServiceImpl<StudyReportFrag
public void updateFragments(Integer id, UpdateFragmentsRequest request) throws NotFindEntitiesException { public void updateFragments(Integer id, UpdateFragmentsRequest request) throws NotFindEntitiesException {
StudyReportFragmentsEntity existing = this.getById(id); StudyReportFragmentsEntity existing = this.getById(id);
if (existing == null) { if (existing == null) {
throw new NotFindEntitiesException(String.format("未能找到学习残片id[%d]", id)); log.warn("未能找到学习残片id[{}]", id);
throw new NotFindEntitiesException("这条学习残片不存在或已被删除");
} }
StudyReportFragmentsEntity entity = FragmentsConvert.MAPPER.toFragmentsEntity(request); StudyReportFragmentsEntity entity = FragmentsConvert.MAPPER.toFragmentsEntity(request);
entity.setId(id); entity.setId(id);
@@ -50,7 +50,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException, ServiceException { public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException, ServiceException {
TaskEntity taskEntity = tasksServiceImpl.getOneOpt( TaskEntity taskEntity = tasksServiceImpl.getOneOpt(
Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum)) Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在", taskNum))); .orElseThrow(() -> {
log.warn("[{}]不存在", taskNum);
return new NotFindEntitiesException("这个任务不存在或已被删除");
});
StudySessionsEntity session = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery() StudySessionsEntity session = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery()
.eq(StudySessionsEntity::getTaskNum, taskNum) .eq(StudySessionsEntity::getTaskNum, taskNum)
@@ -88,10 +91,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException, ServiceException { public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException, ServiceException {
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(() -> sessionNotFound(sessionNum));
if (StudySessionStateEnum.PAUSED.name().equals(studySessionsEntity.getSessionState())) { if (StudySessionStateEnum.PAUSED.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复暂停"); log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复暂停");
throw new ErrorParameterException("会话[" + sessionNum + "]已经暂停,请勿重复暂停"); throw new ErrorParameterException("这次学习会话已经暂停了,不用重复暂停");
} }
studySessionsEntity.calculatePointerPosition(); studySessionsEntity.calculatePointerPosition();
studySessionsEntity.pausedStudySession(endTime); studySessionsEntity.pausedStudySession(endTime);
@@ -104,7 +107,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public String endedStudySession(String sessionNum, String content) throws ErrorParameterException { public String endedStudySession(String sessionNum, String content) 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(() -> sessionNotFound(sessionNum));
boolean wasOngoing = StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState()); boolean wasOngoing = StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState());
studySessionsEntity.endedStudySession(); studySessionsEntity.endedStudySession();
this.updateById(studySessionsEntity); this.updateById(studySessionsEntity);
@@ -122,9 +125,12 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public StudySessionResponse getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException { public StudySessionResponse getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException {
TaskEntity task = tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class) TaskEntity task = tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class)
.eq(TaskEntity::getTaskNum, taskNum)) .eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在")); .orElseThrow(() -> taskNotFound(taskNum));
StudySessionsDto dto = Optional.ofNullable(studyReportsMapper.getNotEndedStudySessionDtoByTaskNum(taskNum)) StudySessionsDto dto = Optional.ofNullable(studyReportsMapper.getNotEndedStudySessionDtoByTaskNum(taskNum))
.orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在进行中或暂停中的会话")); .orElseThrow(() -> {
log.warn("任务[{}]不存在进行中或暂停中的会话", taskNum);
return new ErrorParameterException("这个任务当前没有进行中或已暂停的学习会话");
});
return StudySessionConvert.MAPPER.toStudySessionResponse(dto); return StudySessionConvert.MAPPER.toStudySessionResponse(dto);
} }
@@ -136,7 +142,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
.eq(StudyReportFragmentsEntity::getSessionNum, sessionNum)) .eq(StudyReportFragmentsEntity::getSessionNum, sessionNum))
.stream().map(StudyReportFragmentsEntity::getContent).collect(Collectors.toCollection(ArrayList::new)); .stream().map(StudyReportFragmentsEntity::getContent).collect(Collectors.toCollection(ArrayList::new));
} else { } else {
throw new ErrorParameterException("会话[" + sessionNum + "]不存在"); throw sessionNotFound(sessionNum);
} }
} }
@@ -145,13 +151,13 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public void continueStudySession(String sessionNum) throws ErrorParameterException, ServiceException { public void continueStudySession(String sessionNum) throws ErrorParameterException, ServiceException {
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(() -> sessionNotFound(sessionNum));
if (StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState())) { if (StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复开始"); log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复开始");
throw new ErrorParameterException("会话[" + sessionNum + "]已经开始,请勿重复开始"); throw new ErrorParameterException("这次学习会话已经开始了,不用重复开始");
} else if (StudySessionStateEnum.ENDED.name().equals(studySessionsEntity.getSessionState())) { } else if (StudySessionStateEnum.ENDED.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]处于结束状态,不可开始该会话!"); log.error("会话[" + studySessionsEntity.getSessionNum() + "]处于结束状态,不可开始该会话!");
throw new ErrorParameterException("会话[" + sessionNum + "]处于结束状态,不可开始该会话!"); throw new ErrorParameterException("这次学习会话已经结束,无法再次开始");
} }
studySessionsEntity.continueStudySession(); studySessionsEntity.continueStudySession();
this.updateById(studySessionsEntity); this.updateById(studySessionsEntity);
@@ -163,7 +169,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public StudySessionResponse getStudySessionBySessionNum(String sessionNum) throws ErrorParameterException { public StudySessionResponse getStudySessionBySessionNum(String sessionNum) throws ErrorParameterException {
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class) StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum)) .eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在")); .orElseThrow(() -> sessionNotFound(sessionNum));
return StudySessionConvert.MAPPER.toStudySessionResponse(session); return StudySessionConvert.MAPPER.toStudySessionResponse(session);
} }
@@ -174,7 +180,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public String generateReportDraft(String sessionNum) throws ErrorParameterException { public String generateReportDraft(String sessionNum) throws ErrorParameterException {
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class) StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum)) .eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在")); .orElseThrow(() -> sessionNotFound(sessionNum));
ArrayList<String> fragments = getAllFragments(sessionNum); ArrayList<String> fragments = getAllFragments(sessionNum);
if (fragments.isEmpty()) { if (fragments.isEmpty()) {
@@ -262,4 +268,14 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
return response; return response;
} }
private ErrorParameterException sessionNotFound(String sessionNum) {
log.warn("会话[{}]不存在", sessionNum);
return new ErrorParameterException("这次学习会话不存在或已结束");
}
private ErrorParameterException taskNotFound(String taskNum) {
log.warn("任务[{}]不存在", taskNum);
return new ErrorParameterException("这个任务不存在或已被删除");
}
} }
@@ -60,7 +60,8 @@ public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
if (this.exists(Wrappers.lambdaQuery(TaskEntity.class) if (this.exists(Wrappers.lambdaQuery(TaskEntity.class)
.eq(TaskEntity::getTaskName, task.getTaskName()))) { .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")); task.setTaskNum(GenerateNumTool.generateNum("TASK"));
@@ -132,7 +133,10 @@ public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
@Override @Override
public TaskApplicationEntity updateApplication(Integer id, UpdateTaskApplicationRequest request) throws NotFindEntitiesException { public TaskApplicationEntity updateApplication(Integer id, UpdateTaskApplicationRequest request) throws NotFindEntitiesException {
TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id)) TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id))
.orElseThrow(() -> new NotFindEntitiesException("应用场景[" + id + "]不存在")); .orElseThrow(() -> {
log.warn("应用场景[{}]不存在", id);
return new NotFindEntitiesException("这个应用场景不存在或已被删除");
});
existing.setTitle(request.getTitle()); existing.setTitle(request.getTitle());
existing.setDescription(request.getDescription()); existing.setDescription(request.getDescription());
existing.setResourceUrl(request.getResourceUrl()); existing.setResourceUrl(request.getResourceUrl());
@@ -144,14 +148,18 @@ public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
@Override @Override
public void deleteApplication(Integer id) throws NotFindEntitiesException { public void deleteApplication(Integer id) throws NotFindEntitiesException {
TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id)) TaskApplicationEntity existing = Optional.ofNullable(taskApplicationMapper.selectById(id))
.orElseThrow(() -> new NotFindEntitiesException("应用场景[" + id + "]不存在")); .orElseThrow(() -> {
log.warn("应用场景[{}]不存在", id);
return new NotFindEntitiesException("这个应用场景不存在或已被删除");
});
taskApplicationMapper.deleteById(existing.getId()); taskApplicationMapper.deleteById(existing.getId());
} }
private void ensureTaskExists(String taskNum) throws NotFindEntitiesException { private void ensureTaskExists(String taskNum) throws NotFindEntitiesException {
if (!StringUtils.hasText(taskNum) || !tasksMapper.exists( if (!StringUtils.hasText(taskNum) || !tasksMapper.exists(
Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, taskNum))) { Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, taskNum))) {
throw new NotFindEntitiesException("任务[" + taskNum + "]不存在"); log.warn("任务[{}]不存在", taskNum);
throw new NotFindEntitiesException("这个任务不存在或已被删除");
} }
} }