refactor: 优化其他 Service 实现和 Controller 代码

This commit is contained in:
2026-05-27 21:23:50 +08:00
parent 950c91791f
commit 24d871b430
4 changed files with 55 additions and 47 deletions
@@ -38,12 +38,14 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
implements StudySessionsService {
public static final int WORK_DURATION = 25;
private final TasksServiceImpl tasksServiceImpl;
private final StudyReportFragmentsServiceImpl studyReportFragmentsServiceImpl;
private final StudyReportFragmentsMapper studyReportFragmentsMapper;
private final StudyReportsMapper studyReportsMapper;
public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException, ServiceException {
// 拦截器已自动注入 created_by 条件,只有当前用户的 task 才能查到
TaskEntity taskEntity = tasksServiceImpl.getOneOpt(
Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在", taskNum)));
@@ -80,6 +82,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException, ServiceException {
// 拦截器自动校验归属,查不到即说明无权访问
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
@@ -95,12 +98,13 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
public void endedStudySession(String sessionNum, String content) throws ErrorParameterException {
// 拦截器自动校验归属
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
studySessionsEntity.endedStudySession();
this.updateById(studySessionsEntity);
// 创建学习报告
// 创建学习报告created_by 由 MetaObjectHandler 自动填充)
StudyReportsEntity studyReportsEntity = new StudyReportsEntity();
studyReportsEntity.setSessionNum(sessionNum);
studyReportsEntity.setContent(content);
@@ -109,6 +113,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
public StudySessionResponse getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException {
// 拦截器自动注入 created_by,task 归属校验已内置
TaskEntity task = tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class)
.eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在"));
StudySessionsDto dto = Optional.ofNullable(studyReportsMapper.getNotEndedStudySessionDtoByTaskNum(taskNum))
.orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在进行中或暂停中的会话"));
return StudySessionConvert.MAPPER.toStudySessionResponse(dto);
@@ -116,9 +124,9 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
public ArrayList<String> getAllFragments(String sessionNum) throws ErrorParameterException {
// 拦截器自动校验归属
if (this.exists(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))) {
// todo guo 后续可在此补充更高级的整理方式
return studyReportFragmentsMapper.selectList(Wrappers.lambdaQuery(StudyReportFragmentsEntity.class)
.eq(StudyReportFragmentsEntity::getSessionNum, sessionNum))
.stream().map(StudyReportFragmentsEntity::getContent).collect(Collectors.toCollection(ArrayList::new));
@@ -129,6 +137,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
@Override
public void continueStudySession(String sessionNum) throws ErrorParameterException, ServiceException {
// 拦截器自动校验归属
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
@@ -142,8 +151,15 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
studySessionsEntity.continueStudySession();
this.updateById(studySessionsEntity);
}
/**
* 通过 sessionNum 获取学习会话(归属由拦截器自动校验)
*/
public StudySessionResponse getStudySessionBySessionNum(String sessionNum) throws ErrorParameterException {
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
return StudySessionConvert.MAPPER.toStudySessionResponse(session);
}
}