66 lines
3.1 KiB
Java
66 lines
3.1 KiB
Java
package com.guo.learningprogresstracker.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.guo.learningprogresstracker.dto.response.StudySessionResponce;
|
|
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
|
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
|
|
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
|
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
|
import com.guo.learningprogresstracker.mapStruct.StudySessionConvert;
|
|
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
|
import com.guo.learningprogresstracker.service.StudySessionsService;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* @author guo
|
|
* @description 针对表【study_sessions(记录每次学习会话的具体数据)】的数据库操作Service实现
|
|
* @createDate 2024-06-09 15:28:48
|
|
*/
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, StudySessionsEntity>
|
|
implements StudySessionsService{
|
|
|
|
private final TasksServiceImpl tasksServiceImpl;
|
|
|
|
public StudySessionResponce startOrContinueStudySession(String taskNum) throws NotFindEntitiesException {
|
|
// taskNum是否存在
|
|
tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
|
|
.orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在",taskNum)));
|
|
// 任务taskNum下是否存在非"已结束"的学习会话
|
|
Optional<StudySessionsEntity> oneOpt = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery()
|
|
.eq(StudySessionsEntity::getTaskNum, taskNum)
|
|
.ne(StudySessionsEntity::getSessionState, StudySessionStateEnum.ENDED.name()));
|
|
// 有则返回该未完成会话,否则创建新的会话
|
|
if (oneOpt.isEmpty()) {
|
|
// 返回一个新的会话
|
|
StudySessionsEntity studySessionsEntity = StudySessionsEntity.initNewSession();
|
|
studySessionsEntity.setTaskNum(taskNum);
|
|
this.save(studySessionsEntity);
|
|
return StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity);
|
|
}else {
|
|
// 返回已经存在的会话
|
|
StudySessionsEntity studySessionsEntity = oneOpt.get();
|
|
studySessionsEntity.continueStudySession();
|
|
this.updateById(studySessionsEntity);
|
|
return StudySessionConvert.MAPPER.toStudySessionResponce(studySessionsEntity);
|
|
}
|
|
}
|
|
|
|
public void pauseStudySession(String sessionId) throws ErrorParameterException {
|
|
StudySessionsEntity studySessionsEntity = this.getOptById(sessionId)
|
|
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionId + "]不存在"));
|
|
studySessionsEntity.pausedStudySession();
|
|
this.updateById(studySessionsEntity);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|