hjl
2024-06-20 770de42c8bbb10676663f33e2748c733c993d27b
ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TStudyServiceImpl.java
@@ -1,16 +1,13 @@
package com.ruoyi.study.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.study.domain.*;
import com.ruoyi.study.dto.StudyWeekDTO;
import com.ruoyi.study.mapper.TStudyMapper;
import com.ruoyi.study.service.ITGameRecordService;
import com.ruoyi.study.service.ITStudyService;
import com.ruoyi.study.service.ITSubjectService;
import com.ruoyi.study.service.ITUserStudyService;
import com.ruoyi.study.service.*;
import com.ruoyi.study.vo.*;
import org.springframework.stereotype.Service;
@@ -36,110 +33,423 @@
    @Resource
    private ITGameRecordService gameRecordService;
    @Resource
    private TokenService tokenService;
    private ITStudyAnswerService studyAnswerService;
    @Resource
    private ITStudyInductionService studyInductionService;
    @Resource
    private ITStudyLookService studyLookService;
    @Resource
    private ITStudyListenService studyListenService;
    @Resource
    private ITStudyPairService studyPairService;
    @Resource
    private ITGameService gameService;
    @Resource
    private ITStoryListenService storyListenService;
    private final static Map<String, Integer> GAME_DIFFICULTY_MAP = new HashMap<>();
    private final static Map<Integer, Integer> GAME_DIFFICULTY_MAP = new HashMap<>();
    static {
        GAME_DIFFICULTY_MAP.put("1", 0);
        GAME_DIFFICULTY_MAP.put("2", 1);
        GAME_DIFFICULTY_MAP.put(1, 0);
        GAME_DIFFICULTY_MAP.put(2, 1);
    }
    @Override
    public List<StudyWeekDTO> weekList(Integer type, Integer quarter) {
        return baseMapper.weekList(type, quarter);
    }
    @Override
    public TUserStudy studySchedule(String userId, Integer week, Integer day) {
        return userStudyService.studySchedule(userId, week, day);
    }
    @Override
    public void checkDifficulty(Integer difficulty, Integer week, TGame game) {
        // 判断用户是否完成上一个等级
        Integer level = GAME_DIFFICULTY_MAP.get(String.valueOf(difficulty));
        if (null == level) {
            throw new GlobalException("游戏等级异常,请重试!");
    public List<StudyWeekDTO> weekList(Integer type, Integer quarter, Integer userId) {
        List<StudyWeekDTO> result = new ArrayList<>();
        // 根据季度和type查询学习配置
        List<TStudy> study = lambdaQuery().eq(TStudy::getQuarter, quarter).eq(TStudy::getType, type)
                .eq(TStudy::getDisabled, 0).orderByAsc(TStudy::getWeek).list();
        for (TStudy item : study) {
            Integer week = item.getWeek();
            String title = item.getTitle();
            Integer id = item.getId();
            // 计算总积分
            int total = 0;
            // 有问有答
            List<TStudyAnswer> answerList = studyAnswerService.lambdaQuery().eq(TStudyAnswer::getStudyId, id)
                    .eq(TStudyAnswer::getDisabled, 0).list();
            Optional<TStudyAnswer> answerOptional = answerList.stream().findAny();
            if (answerOptional.isPresent()) {
                total += answerOptional.get().getIntegral();
            }
            // 归纳判断
            List<TStudyInduction> inductionList = studyInductionService.lambdaQuery().eq(TStudyInduction::getStudyId, id)
                    .eq(TStudyInduction::getDisabled, 0).list();
            Optional<TStudyInduction> inductionOptional = inductionList.stream().findAny();
            if (inductionOptional.isPresent()) {
                total += inductionOptional.get().getIntegral();
            }
            // 看图选音
            List<TStudyLook> lookList = studyLookService.lambdaQuery().eq(TStudyLook::getStudyId, id)
                    .eq(TStudyLook::getDisabled, 0).list();
            Optional<TStudyLook> lookOptional = lookList.stream().findAny();
            if (lookOptional.isPresent()) {
                total += lookOptional.get().getIntegral();
            }
            // 听音选图
            List<TStudyListen> listenList = studyListenService.lambdaQuery().eq(TStudyListen::getStudyId, id)
                    .eq(TStudyListen::getDisabled, 0).list();
            Optional<TStudyListen> listenOptional = listenList.stream().findAny();
            if (listenOptional.isPresent()) {
                total += listenOptional.get().getIntegral();
            }
            // 音图相配
            List<TStudyPair> pairList = studyPairService.lambdaQuery().eq(TStudyPair::getStudyId, id)
                    .eq(TStudyPair::getDisabled, 0).list();
            Optional<TStudyPair> pairOptional = pairList.stream().findAny();
            if (pairOptional.isPresent()) {
                total += pairOptional.get().getIntegral();
            }
            // 自主游戏
            List<TGame> gameList = gameService.lambdaQuery().eq(TGame::getStudyId, id)
                    .eq(TGame::getDisabled, 0).list();
            // 自主游戏-超级听力
            List<String> gameIntegral = gameList.stream().map(TGame::getIntegral).collect(Collectors.toList());
            for (String s : gameIntegral) {
                List<String> list = Arrays.stream(s.split(",")).collect(Collectors.toList());
                total += list.stream().mapToInt(Integer::parseInt).sum();
            }
            // 自主游戏-框架记忆
            List<Integer> gameAnswerIntegral = gameList.stream().map(TGame::getAnswerIntegral).collect(Collectors.toList());
            for (Integer integral : gameAnswerIntegral) {
                if (null != integral) {
                    total += integral;
                }
            }
            // 自主故事
            List<TStoryListen> storyListenList = storyListenService.lambdaQuery().eq(TStoryListen::getStudyId, id)
                    .eq(TStoryListen::getDisabled, 0).list();
            // 自主故事 - 框架记忆
            total += storyListenList.stream().map(TStoryListen::getIntegral).mapToInt(Integer::intValue).sum();
            // 自主故事 - 看图配音
            total += storyListenList.stream().map(TStoryListen::getLookIntegral).mapToInt(Integer::intValue).sum();
            // 判断周目是否可以进入学习
            Boolean canStudy = checkWeekCanStudy(userId, item);
            result.add(new StudyWeekDTO(week, type, quarter, title, total, canStudy));
        }
        // 获取用户游戏进度
        Integer userId = tokenService.getLoginUserStudy().getUserid();
        List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userId).eq(TGameRecord::getGameId, game.getId()).list();
        boolean contains = list.stream().map(TGameRecord::getGameDifficulty).collect(Collectors.toList()).contains(level);
        if (!contains) {
            throw new GlobalException("请先完成上一难度再挑战当前难度!");
        return result;
    }
    /**
     * @param userId 用户id
     * @return 当前周目是否学习
     */
    private Boolean checkWeekCanStudy(Integer userId, TStudy study) {
        TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userId)
                .eq(TUserStudy::getDisabled, 0).one();
        if (null == userStudy) {
            userStudy = new TUserStudy();
            userStudy.setUserId(userId);
            // 学习周目
            TStudy tStudy = this.lambdaQuery().eq(TStudy::getQuarter, Constants.ONE)
                    .orderByAsc(TStudy::getWeek).last("limit 1").one();
            userStudy.setWeek(tStudy.getWeek());
            userStudy.setDay(Constants.ONE);
            userStudy.setTotalStudy(Constants.ZERO);
            userStudy.setTodayStudy(Constants.ZERO);
            userStudy.setWeekStudy(Constants.ZERO);
            userStudy.setMonthStudy(Constants.ZERO);
            userStudy.setListen(Constants.BURDEN_ONE);
            userStudy.setLook(Constants.BURDEN_ONE);
            userStudy.setInduction(Constants.BURDEN_ONE);
            userStudy.setAnswer(Constants.BURDEN_ONE);
            userStudy.setPair(Constants.BURDEN_ONE);
            userStudyService.save(userStudy);
        }
        // 学习记录所属周目大于当前学习周目,判断为已学习
        return userStudy.getWeek() >= study.getWeek();
    }
    @Override
    public TUserStudy studySchedule(String userId) {
        return userStudyService.studySchedule(userId);
    }
    @Override
    public void checkDifficulty(Integer difficulty, Integer week, TGame game, Integer userid) {
        // 判断用户是否完成上一个等级
        if (!Constants.ZERO.equals(difficulty)) {
            Integer level = GAME_DIFFICULTY_MAP.get(difficulty);
            if (null == level) {
                throw new GlobalException("游戏等级异常,请重试!");
            }
            // 游戏通关率
            String clearanceRate = game.getRate().split(",")[level];
            // 获取用户游戏进度
            List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userid)
                    .eq(TGameRecord::getGameId, game.getId())
                    .ge(TGameRecord::getAccuracy, clearanceRate)
                    .eq(TGameRecord::getGameDifficulty, level).list();
            if (list.isEmpty()) {
                throw new GlobalException("请先完成上一难度再挑战当前难度!");
            }
        }
    }
    @Override
    public StudyListenResultVO listenSelectPicture(Integer week, Integer day, List<TStudyListen> studyListens) {
        // 随机获取一组题
        Random rand = new Random();
        TStudyListen data = studyListens.get(rand.nextInt(studyListens.size()));
        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
        return new StudyListenResultVO(data, subjectList);
        if (studyListens.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = studyListens.stream().map(TStudyListen::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyListen data : studyListens) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyListen studyListen : studyListens) {
            List<String> subjectIds = Arrays.stream(studyListen.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> list = new ArrayList<>();
            // 图片及语音集合
            for (String id : subjectIds) {
                TSubject data = subjectService.lambdaQuery().eq(TSubject::getId, id)
                        .eq(TSubject::getDisabled, 0).one();
                list.add(data);
            }
            subjectList.add(list);
        }
        return new StudyListenResultVO(learnStudy, subjectList);
    }
    @Override
    public StudyLookResultVO pictureSelectVoice(Integer week, Integer day, List<TStudyLook> lookList) {
        // 随机获取一组题
        Random rand = new Random();
        TStudyLook data = lookList.get(rand.nextInt(lookList.size()));
        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
        return new StudyLookResultVO(data, subjectList);
        if (lookList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = lookList.stream().map(TStudyLook::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyLook data : lookList) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyLook studyLook : lookList) {
            List<String> sortList = Arrays.stream(studyLook.getSort().split(",")).collect(Collectors.toList());
            List<String> subjectIds = Arrays.stream(studyLook.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> list = new ArrayList<>();
            // 图片及语音集合
            for (int i = 0; i < subjectIds.size(); i++) {
                TSubject data = subjectService.lambdaQuery().eq(TSubject::getId, subjectIds.get(i))
                        .eq(TSubject::getDisabled, 0).one();
                data.setSort(Integer.parseInt(sortList.get(i)));
                list.add(data);
            }
            // 根据顺序排序
            list.sort(Comparator.comparingInt(TSubject::getSort));
            subjectList.add(list);
        }
        return new StudyLookResultVO(learnStudy, subjectList);
    }
    @Override
    public StudyInductionResultVO induceExclude(Integer week, Integer day, List<TStudyInduction> inductionList) {
        // 随机获取一组题
        Random rand = new Random();
        TStudyInduction data = inductionList.get(rand.nextInt(inductionList.size()));
        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
        return new StudyInductionResultVO(data, subjectList);
        if (inductionList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = inductionList.stream().map(TStudyInduction::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyInduction data : inductionList) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyInduction data : inductionList) {
            List<String> subjectIds = Arrays.stream(data.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> subjectLists = new ArrayList<>();
            for (String id : subjectIds) {
                if (id.startsWith("-")) {
                    id = id.replace("-", "");
                }
                subjectLists.add(subjectService.getById(id));
            }
            subjectList.add(subjectLists);
        }
        return new StudyInductionResultVO(learnStudy, subjectList);
    }
    @Override
    public StudyAnswerResultVO questionsAndAnswers(Integer week, Integer day, List<TStudyAnswer> answerList) {
        // 随机获取一组题
        Random rand = new Random();
        TStudyAnswer data = answerList.get(rand.nextInt(answerList.size()));
        AnswerVO one = new AnswerVO();
        BeanUtils.copyProperties(data, one);
        answerList.remove(data);
        TStudyAnswer dataTwo = answerList.get(rand.nextInt(answerList.size()));
        AnswerVO two = new AnswerVO();
        BeanUtils.copyProperties(dataTwo, two);
        // 获取问题题目 和 回答题目
        List<String> ids = new ArrayList<>();
        ids.add(String.valueOf(one.getSubject()));
        ids.add(String.valueOf(one.getAnswerSubject()));
        // 有问有答
        List<TStudyAnswer> answers = new ArrayList<>();
        answers.add(one);
        one.setSubjectList(getSubjects(ids.toArray(new String[0])));
        // 第二题信息
        List<String> twoIds = new ArrayList<>();
        answers.add(two);
        twoIds.add(String.valueOf(two.getSubject()));
        twoIds.add(String.valueOf(two.getAnswerSubject()));
        two.setSubjectList(getSubjects(twoIds.toArray(new String[0])));
        return new StudyAnswerResultVO(answers);
        if (answerList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = answerList.stream().map(TStudyAnswer::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyAnswer data : answerList) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 题目语音及图片信息
        List<List<QuestionsAnswersSubjectVO>> subjectList = new ArrayList<>();
        for (int i = 0; i < answerList.size(); i += Constants.TWO) {
            List<QuestionsAnswersSubjectVO> voList = new ArrayList<>();
            // 一组题目为四道题,
            TStudyAnswer one = answerList.get(i);
            TStudyAnswer two = answerList.get(i + 1);
            voAdd(voList, one);
            voAdd(voList, two);
            subjectList.add(voList);
        }
        return new StudyAnswerResultVO(learnStudy, subjectList);
    }
    private void voAdd(List<QuestionsAnswersSubjectVO> voList, TStudyAnswer one) {
        // 第一道题问题及答案
        Integer subject = one.getSubject();
        Integer answerSubject = one.getAnswerSubject();
        // 问题题目信息
        QuestionsAnswersSubjectVO oneVO = new QuestionsAnswersSubjectVO();
        TSubject one1 = subjectService.lambdaQuery().eq(TSubject::getId, subject)
                .eq(TSubject::getDisabled, 0).one();
        BeanUtils.copyProperties(one1, oneVO);
        // 回答题目信息
        QuestionsAnswersSubjectVO twoVO = new QuestionsAnswersSubjectVO();
        TSubject two1 = subjectService.lambdaQuery().eq(TSubject::getId, answerSubject)
                .eq(TSubject::getDisabled, 0).one();
        BeanUtils.copyProperties(two1, twoVO);
        // 判断第一组题目的问题题目及回答题目,哪个是答案
        if (Constants.ZERO.equals(one.getIsAnswer())) {
            oneVO.setIsQuestion(0);
            twoVO.setIsQuestion(1);
        } else {
            oneVO.setIsQuestion(1);
            twoVO.setIsQuestion(0);
        }
        voList.add(oneVO);
        voList.add(twoVO);
    }
    @Override
    public StudyPairResultVO pictureMateVoice(Integer week, Integer day, TStudyPair pair) {
        List<TSubject> subjectList = getSubjects(pair.getSubject().split(","));
        return new StudyPairResultVO(pair, subjectList);
    public StudyPairResultVO pictureMateVoice(Integer week, Integer day, List<TStudyPair> pair) {
        if (pair.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = pair.stream().map(TStudyPair::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyPair data : pair) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyPair data : pair) {
            List<String> subjectIds = Arrays.stream(data.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> subjectLists = new ArrayList<>();
            for (String id : subjectIds) {
                if (id.startsWith("-")) {
                    id = id.replace("-", "");
                }
                subjectLists.add(subjectService.getById(id));
            }
            subjectList.add(subjectLists);
        }
        return new StudyPairResultVO(learnStudy, subjectList);
    }
    /**
     * 根据参数id查询图片及语音
     *
     * @param ids 多个id
     * @return 图片及语音集合
     */
    private List<TSubject> getSubjects(String[] ids) {
        return subjectService.lambdaQuery().in(TSubject::getId, Arrays.asList(ids)).list();
    @Override
    public int computeSchedule(TUserStudy result, Integer week) {
        // 基础学习进度
        Integer day = result.getDay();
        // 默认进度为 0
        int defaultSchedule;
        // week以超过当前week,进度为 100%
        if (result.getWeek() > week) {
            defaultSchedule = 100;
        } else {
            // 根据day初始化学习进度
            if (Constants.ONE.equals(day)) {
                defaultSchedule = 0;
            } else if (Constants.TWO.equals(day)) {
                defaultSchedule = 20;
            } else if (Constants.THREE.equals(day)) {
                defaultSchedule = 40;
            } else if (Constants.FOUR.equals(day)) {
                defaultSchedule = 60;
            } else if (Constants.FIVE.equals(day)) {
                defaultSchedule = 80;
            } else {
                defaultSchedule = 0;
            }
        }
        return defaultSchedule;
    }
    @Override
    public int computeTotalIntegral(List<String> studyIds, Integer type, Integer accuracy) {
        int sum = 0;
        if (Constants.ONE.equals(type)) {
            List<TStudyListen> list = studyListenService.lambdaQuery().in(TStudyListen::getId, studyIds)
                    .eq(TStudyListen::getDisabled, 0).list();
            Optional<TStudyListen> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.TWO.equals(type)) {
            List<TStudyLook> list = studyLookService.lambdaQuery().in(TStudyLook::getId, studyIds)
                    .eq(TStudyLook::getDisabled, 0).list();
            Optional<TStudyLook> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.THREE.equals(type)) {
            List<TStudyInduction> list = studyInductionService.lambdaQuery().in(TStudyInduction::getId, studyIds)
                    .eq(TStudyInduction::getDisabled, 0).list();
            Optional<TStudyInduction> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.FOUR.equals(type)) {
            List<TStudyAnswer> list = studyAnswerService.lambdaQuery().in(TStudyAnswer::getId, studyIds)
                    .eq(TStudyAnswer::getDisabled, 0).list();
            Optional<TStudyAnswer> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.FIVE.equals(type)) {
            List<TStudyPair> list = studyPairService.lambdaQuery().in(TStudyPair::getId, studyIds)
                    .eq(TStudyPair::getDisabled, 0).list();
            Optional<TStudyPair> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else {
            throw new GlobalException("题目信息异常!");
        }
        return (int) (sum * ((double) accuracy / 100));
    }
}