package com.guo.learningprogresstracker.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.guo.learningprogresstracker.entity.StudyExpectationsEntity; import com.guo.learningprogresstracker.entity.StudySessionsEntity; import com.guo.learningprogresstracker.exception.ErrorParameterException; import com.guo.learningprogresstracker.mapper.StudyExpectationsMapper; import com.guo.learningprogresstracker.mapper.StudySessionsMapper; import com.guo.learningprogresstracker.service.StudyExpectationsService; import java.util.Collection; import java.util.Map; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * 学习预期服务实现 */ @Slf4j @Service @RequiredArgsConstructor public class StudyExpectationsServiceImpl implements StudyExpectationsService { private final StudyExpectationsMapper studyExpectationsMapper; private final StudySessionsMapper studySessionsMapper; @Override public StudyExpectationsEntity upsertExpectation(String sessionNum, String description) throws ErrorParameterException { boolean sessionExists = studySessionsMapper.exists( Wrappers.lambdaQuery().eq(StudySessionsEntity::getSessionNum, sessionNum)); if (!sessionExists) { log.warn("学习会话[{}]不存在", sessionNum); throw new ErrorParameterException("这次学习会话不存在或已结束"); } StudyExpectationsEntity existing = getBySessionNum(sessionNum); if (existing == null) { StudyExpectationsEntity entity = new StudyExpectationsEntity(); entity.setSessionNum(sessionNum); entity.setDescription(description); studyExpectationsMapper.insert(entity); return entity; } existing.setDescription(description); studyExpectationsMapper.updateById(existing); return existing; } @Override public StudyExpectationsEntity getBySessionNum(String sessionNum) { return studyExpectationsMapper.selectOne(Wrappers.lambdaQuery() .eq(StudyExpectationsEntity::getSessionNum, sessionNum) .last("LIMIT 1")); } @Override public Map descriptionsBySessionNums(Collection sessionNums) { if (sessionNums == null || sessionNums.isEmpty()) { return Map.of(); } return studyExpectationsMapper .selectList(Wrappers.lambdaQuery() .in(StudyExpectationsEntity::getSessionNum, sessionNums)) .stream() .filter(e -> e.getDescription() != null) .collect(Collectors.toMap( StudyExpectationsEntity::getSessionNum, StudyExpectationsEntity::getDescription, (first, second) -> first)); } @Override public void deleteBySessionNum(String sessionNum) { studyExpectationsMapper.delete( Wrappers.lambdaQuery().eq(StudyExpectationsEntity::getSessionNum, sessionNum)); } }