From e11362ca4849da567a77d8b5e5be510df9fb0741 Mon Sep 17 00:00:00 2001
From: hjl <1657978663@qq.com>
Date: 星期一, 17 六月 2024 11:51:51 +0800
Subject: [PATCH] fix: 学习端bug

---
 ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TStudyServiceImpl.java |  429 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 332 insertions(+), 97 deletions(-)

diff --git a/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TStudyServiceImpl.java b/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TStudyServiceImpl.java
index 3fe0e68..ffcf068 100644
--- a/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TStudyServiceImpl.java
+++ b/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/service/impl/TStudyServiceImpl.java
@@ -8,10 +8,7 @@
 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;
 
@@ -38,17 +35,86 @@
     private ITGameRecordService gameRecordService;
     @Resource
     private TokenService tokenService;
+    @Resource
+    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);
+        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();
+            total += answerList.stream().map(TStudyAnswer::getIntegral).mapToInt(Integer::intValue).sum();
+            // 归纳判断
+            List<TStudyInduction> inductionList = studyInductionService.lambdaQuery().eq(TStudyInduction::getStudyId, id)
+                    .eq(TStudyInduction::getDisabled, 0).list();
+            total += inductionList.stream().map(TStudyInduction::getIntegral).mapToInt(Integer::intValue).sum();
+            // 看图选音
+            List<TStudyLook> lookList = studyLookService.lambdaQuery().eq(TStudyLook::getStudyId, id)
+                    .eq(TStudyLook::getDisabled, 0).list();
+            total += lookList.stream().map(TStudyLook::getIntegral).mapToInt(Integer::intValue).sum();
+            // 听音选图
+            List<TStudyListen> listenList = studyListenService.lambdaQuery().eq(TStudyListen::getStudyId, id)
+                    .eq(TStudyListen::getDisabled, 0).list();
+            total += listenList.stream().map(TStudyListen::getIntegral).mapToInt(Integer::intValue).sum();
+            // 音图相配
+            List<TStudyPair> pairList = studyPairService.lambdaQuery().eq(TStudyPair::getStudyId, id)
+                    .eq(TStudyPair::getDisabled, 0).list();
+            total += pairList.stream().map(TStudyPair::getIntegral).mapToInt(Integer::intValue).sum();
+            // 自主游戏
+            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();
+            result.add(new StudyWeekDTO(week, type, quarter, title, total));
+        }
+        return result;
     }
 
     @Override
@@ -60,15 +126,17 @@
     public void checkDifficulty(Integer difficulty, Integer week, TGame game) {
         // 判断用户是否完成上一个等级
         if (!Constants.ZERO.equals(difficulty)) {
-            Integer level = GAME_DIFFICULTY_MAP.get(String.valueOf(difficulty));
+            Integer level = GAME_DIFFICULTY_MAP.get(difficulty);
             if (null == level) {
                 throw new GlobalException("游戏等级异常,请重试!");
             }
             // 获取用户游戏进度
             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) {
+            List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userId)
+                    .eq(TGameRecord::getGameId, game.getId())
+                    .eq(TGameRecord::getAccuracy, 100)
+                    .eq(TGameRecord::getGameDifficulty, level).list();
+            if (list.isEmpty()) {
                 throw new GlobalException("请先完成上一难度再挑战当前难度!");
             }
         }
@@ -79,16 +147,33 @@
         if (studyListens.isEmpty()) {
             throw new GlobalException("当前学习周目题目数量不足!");
         }
-        // 随机获取一组题
-        Random rand = new Random();
-        TStudyListen data;
-        if (studyListens.size() == 1) {
-            data = studyListens.get(0);
-        } else {
-            data = studyListens.get(rand.nextInt(studyListens.size()));
+        // 题组信息
+        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();
+            }
         }
-        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
-        return new StudyListenResultVO(data, subjectList);
+        learnStudy.setIntegral(total);
+        // 语音及图片
+        List<List<TSubject>> subjectList = new ArrayList<>();
+        for (TStudyListen studyListen : studyListens) {
+            List<String> subjectIds = Arrays.stream(studyListen.getSubject().split(",")).collect(Collectors.toList());
+            Collections.shuffle(subjectIds);
+            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
@@ -96,16 +181,33 @@
         if (lookList.isEmpty()) {
             throw new GlobalException("当前学习周目题目数量不足!");
         }
-        // 随机获取一组题
-        Random rand = new Random();
-        TStudyLook data;
-        if (lookList.size() == 1) {
-            data = lookList.get(0);
-        } else {
-            data = lookList.get(rand.nextInt(lookList.size()));
+        // 题组信息
+        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();
+            }
         }
-        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
-        return new StudyLookResultVO(data, subjectList);
+        learnStudy.setIntegral(total);
+        // 语音及图片
+        List<List<TSubject>> subjectList = new ArrayList<>();
+        for (TStudyLook studyLook : lookList) {
+            List<String> subjectIds = Arrays.stream(studyLook.getSubject().split(",")).collect(Collectors.toList());
+            Collections.shuffle(subjectIds);
+            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 StudyLookResultVO(learnStudy, subjectList);
     }
 
     @Override
@@ -113,23 +215,33 @@
         if (inductionList.isEmpty()) {
             throw new GlobalException("当前学习周目题目数量不足!");
         }
-        // 随机获取一组题
-        Random rand = new Random();
-        TStudyInduction data;
-        if (inductionList.size() == 1) {
-            data = inductionList.get(0);
-        } else {
-            data = inductionList.get(rand.nextInt(inductionList.size()));
-        }
-        String[] ids = data.getSubject().split(",");
-        List<TSubject> subjectList = new ArrayList<>();
-        for (String id : ids) {
-            if (id.startsWith("-")) {
-                id = id.replace("-", "");
+        // 题组信息
+        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();
             }
-            subjectList.add(subjectService.getById(id));
         }
-        return new StudyInductionResultVO(data, subjectList);
+        learnStudy.setIntegral(total);
+        // 语音及图片
+        List<List<TSubject>> subjectList = new ArrayList<>();
+        for (TStudyInduction data : inductionList) {
+            List<String> subjectIds = Arrays.stream(data.getSubject().split(",")).collect(Collectors.toList());
+            Collections.shuffle(subjectIds);
+            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
@@ -137,37 +249,56 @@
         if (answerList.isEmpty()) {
             throw new GlobalException("当前学习周目题目数量不足!");
         }
-        // 随机获取一组题
-        Random rand = new Random();
-        TStudyAnswer data;
-        TStudyAnswer dataTwo;
-        if (answerList.size() == 1) {
-            data = answerList.get(0);
-            dataTwo = answerList.get(0);
-        } else {
-            data = answerList.get(rand.nextInt(answerList.size()));
-            dataTwo = answerList.get(rand.nextInt(answerList.size()));
+        // 题组信息
+        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();
+            }
         }
-        AnswerVO one = new AnswerVO();
-        BeanUtils.copyProperties(data, one);
-        answerList.remove(data);
-        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);
+        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
@@ -175,32 +306,136 @@
         if (pair.isEmpty()) {
             throw new GlobalException("当前学习周目题目数量不足!");
         }
-        // 随机获取一组题
-        Random rand = new Random();
-        TStudyPair data;
-        if (pair.size() == 1) {
-            data = pair.get(0);
-        } else {
-            data = pair.get(rand.nextInt(pair.size()));
+        // 题组信息
+        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();
+            }
         }
-        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
-        return new StudyPairResultVO(data, subjectList);
+        learnStudy.setIntegral(total);
+        // 语音及图片
+        List<List<TSubject>> subjectList = new ArrayList<>();
+        for (TStudyPair data : pair) {
+            List<String> subjectIds = Arrays.stream(data.getSubject().split(",")).collect(Collectors.toList());
+            Collections.shuffle(subjectIds);
+            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) {
-        List<TSubject> list = new ArrayList<>();
-        for (String id : ids) {
-            TSubject data = subjectService.lambdaQuery().eq(TSubject::getId, id)
-                    .eq(TSubject::getDisabled, 0).one();
-            list.add(data);
+    @Override
+    public int computeSchedule(TUserStudy result, Integer week) {
+        // 基础学习进度
+        Integer listen = result.getListen();
+        Integer look = result.getLook();
+        Integer induction = result.getInduction();
+        Integer answer = result.getAnswer();
+        Integer pair = result.getPair();
+        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;
+            }
+            // 进度学习完成度计算总学习进度
+            if (!Constants.ZERO.equals(listen) && !Constants.BURDEN_ONE.equals(listen)) {
+                defaultSchedule += 4 * (100 / listen);
+            }
+            if (!Constants.ZERO.equals(look) && !Constants.BURDEN_ONE.equals(look)) {
+                defaultSchedule += 4 * (100 / look);
+            }
+            if (!Constants.ZERO.equals(induction) && !Constants.BURDEN_ONE.equals(induction)) {
+                defaultSchedule += 4 * (100 / induction);
+            }
+            if (!Constants.ZERO.equals(answer) && !Constants.BURDEN_ONE.equals(answer)) {
+                defaultSchedule += 4 * (100 / answer);
+            }
+            if (!Constants.ZERO.equals(pair) && !Constants.BURDEN_ONE.equals(pair)) {
+                defaultSchedule += 4 * (100 / pair);
+            }
         }
-        return list;
+        return defaultSchedule;
+    }
+
+    @Override
+    public int computeTotalIntegral(List<String> studyIds, Integer type, Integer accuracy) {
+        int sum;
+        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 {
+                sum = 0;
+            }
+        } 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 {
+                sum = 0;
+            }
+        } 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 {
+                sum = 0;
+            }
+        } 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 {
+                sum = 0;
+            }
+        } 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 {
+                sum = 0;
+            }
+        } else {
+            throw new GlobalException("题目信息异常!");
+        }
+        return (int) (sum * ((double) accuracy / 100));
     }
 
 }

--
Gitblit v1.7.1