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

---
 ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TStudyController.java |  141 +++++++++++++++++++++++++++++++++++-----------
 1 files changed, 106 insertions(+), 35 deletions(-)

diff --git a/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TStudyController.java b/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TStudyController.java
index 77cc262..3ce58ac 100644
--- a/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TStudyController.java
+++ b/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TStudyController.java
@@ -850,28 +850,64 @@
         TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userid)
                 .eq(TUserStudy::getDisabled, 0).one();
         if (null == userStudy) {
-            throw new GlobalException("登录用户学习记录不存在!");
+            userStudy = createUserStudy(userid);
         }
+        Integer quarter = exitLearn.getQuarter();
+        Integer week = exitLearn.getWeek();
+        Integer day = exitLearn.getDay();
         if (userStudy.getWeek().equals(exitLearn.getWeek()) && userStudy.getDay().equals(exitLearn.getDay())) {
             // 计算完成率
-            int completionRate = exitLearn.getTopicIds().split(",").length * 5;
+            List<String> teamList = Arrays.stream(exitLearn.getTeamIds().split(",")).collect(Collectors.toList());
             Integer type = exitLearn.getType();
             // 更新用户学习完成率
-            if (Constants.ONE.equals(type) && userStudy.getListen() < completionRate) {
+            if (Constants.ONE.equals(type)) {
                 // 听音选图
-                userStudy.setListen(completionRate);
-            } else if (Constants.TWO.equals(type) && userStudy.getLook() < completionRate) {
+                List<TStudyListen> studyListens = studyListenService.listenSelectPicture(quarter, week, day);
+                int item = 0;
+                for (TStudyListen studyListen : studyListens) {
+                    item += studyListen.getSubject().split(",").length;
+                }
+                int i = exitLearn.getTopicIds().split(",").length / item;
+                if (i > userStudy.getListen()) {
+                    userStudy.setListen(100 - i);
+                }
+            } else if (Constants.TWO.equals(type)) {
                 // 看图选音
-                userStudy.setLook(completionRate);
-            } else if (Constants.THREE.equals(type) && userStudy.getInduction() < completionRate) {
+                List<TStudyLook> lookList = studyLookService.pictureSelectVoice(quarter, week, day);
+                if (!lookList.isEmpty()) {
+                    int item = lookList.size();
+                    int i = exitLearn.getSchedule() / item;
+                    if (i > userStudy.getLook()) {
+                        userStudy.setLook(100 - i);
+                    }
+                }
+            } else if (Constants.THREE.equals(type)) {
                 // 归纳排除
-                userStudy.setInduction(completionRate);
-            } else if (Constants.FOUR.equals(type) && userStudy.getAnswer() < completionRate) {
+                List<TStudyInduction> inductionList = studyInductionService.induceExclude(quarter, week, day);
+                if (!inductionList.isEmpty()) {
+                    int item = inductionList.size();
+                    int i = exitLearn.getSchedule() / item;
+                    if (i > userStudy.getInduction()) {
+                        userStudy.setInduction(100 - i);
+                    }
+                }
+            } else if (Constants.FOUR.equals(type)) {
                 // 有问有答
-                userStudy.setAnswer(completionRate);
-            } else if (Constants.FIVE.equals(type) && userStudy.getPair() < completionRate) {
+                List<TStudyAnswer> answerList = studyAnswerService.questionsAndAnswers(quarter, week, day);
+                if (answerList.size() % Constants.TWO == Constants.ZERO) {
+                    int i = exitLearn.getSchedule() / (answerList.size() / 2);
+                    if (i > userStudy.getAnswer()) {
+                        userStudy.setAnswer(100 - i);
+                    }
+                }
+            } else if (Constants.FIVE.equals(type)) {
                 // 音图相配
-                userStudy.setPair(completionRate);
+                List<TStudyPair> pairList = studyPairService.pictureMateVoice(quarter, week, day);
+                int item = pairList.size();
+                int i = exitLearn.getSchedule() / item;
+                if (i > userStudy.getPair()) {
+                    userStudy.setPair(100 - i);
+                }
             }
         }
         // 学习时长更新
@@ -1097,7 +1133,12 @@
             update = update && studyRecordService.save(record);
         }
         // 更改学习记录
-        Boolean updateStudyRecord = userStudyService.exchangeStudyRecord(userId, completeStudy);
+        TUserStudy userStudyRecord = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userId)
+                .eq(TUserStudy::getDisabled, 0).one();
+        if (null == userStudyRecord) {
+            userStudyRecord = createUserStudy(userId);
+        }
+        Boolean updateStudyRecord = userStudyService.exchangeStudyRecord(userStudyRecord, userId, completeStudy);
         return R.ok(update && updateStudyRecord);
     }
 
@@ -1294,12 +1335,36 @@
         // 学习时长更新
         TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userid)
                 .eq(TUserStudy::getDisabled, 0).one();
-        userStudy.setTotalStudy(userStudy.getTotalStudy() + completeStudy.getUseTime());
-        userStudy.setTodayStudy(userStudy.getTodayStudy() + completeStudy.getUseTime());
-        userStudy.setWeekStudy(userStudy.getWeekStudy() + completeStudy.getUseTime());
-        userStudy.setMonthStudy(userStudy.getMonthStudy() + completeStudy.getUseTime());
+        if (null == userStudy) {
+            userStudy = createUserStudy(userid);
+            userStudy.setTotalStudy(userStudy.getTotalStudy() + completeStudy.getUseTime());
+            userStudy.setTodayStudy(userStudy.getTodayStudy() + completeStudy.getUseTime());
+            userStudy.setWeekStudy(userStudy.getWeekStudy() + completeStudy.getUseTime());
+            userStudy.setMonthStudy(userStudy.getMonthStudy() + completeStudy.getUseTime());
+        }
         boolean update = userStudyService.updateById(userStudy);
         return R.ok(add && update);
+    }
+
+    private TUserStudy createUserStudy(Integer userid) {
+        TUserStudy userStudy = new TUserStudy();
+        userStudy.setUserId(userid);
+        // 学习周目
+        TStudy tStudy = studyService.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;
     }
 
     private List<String> getSubjectId(Integer week) {
@@ -1406,10 +1471,13 @@
         // 学习时长更新
         TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userId)
                 .eq(TUserStudy::getDisabled, 0).one();
-        userStudy.setTotalStudy(userStudy.getTotalStudy() + studyTime);
-        userStudy.setTodayStudy(userStudy.getTodayStudy() + studyTime);
-        userStudy.setWeekStudy(userStudy.getWeekStudy() + studyTime);
-        userStudy.setMonthStudy(userStudy.getMonthStudy() + studyTime);
+        if (null == userStudy) {
+            userStudy = createUserStudy(userId);
+            userStudy.setTotalStudy(userStudy.getTotalStudy() + studyTime);
+            userStudy.setTodayStudy(userStudy.getTodayStudy() + studyTime);
+            userStudy.setWeekStudy(userStudy.getWeekStudy() + studyTime);
+            userStudy.setMonthStudy(userStudy.getMonthStudy() + studyTime);
+        }
         boolean update = userStudyService.updateById(userStudy);
         if (!update) {
             throw new GlobalException("学习时长更新失败!");
@@ -1471,23 +1539,26 @@
         // 学习记录
         TUserStudy studyRecord = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userId)
                 .eq(TUserStudy::getDisabled, 0).one();
-        // 学习时长格式转换
-        Integer todayStudy = studyRecord.getTodayStudy();
-        studyRecord.setTodayStudy(Math.round((float) todayStudy / 3600));
-        Integer weekStudy = studyRecord.getWeekStudy();
-        studyRecord.setWeekStudy(Math.round((float) weekStudy / 3600));
-        Integer monthStudy = studyRecord.getMonthStudy();
-        studyRecord.setMonthStudy(Math.round((float) monthStudy / 3600));
         // 总时长还需计算上游戏测试成绩时长
         List<TGameRecord> gameRecordList = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userId)
                 .eq(TGameRecord::getDisabled, 0).list();
-        int sum = gameRecordList.stream().map(TGameRecord::getUseTime).mapToInt(Integer::intValue).sum();
-        Integer totalStudy = studyRecord.getTotalStudy();
-        studyRecord.setTotalStudy(Math.round((float) (totalStudy + sum) / 3600));
-        // 剩余周目
-        int size = studyService.list(new QueryWrapper<TStudy>()
-                .eq("type", 1)).size();
-        studyRecord.setSurplus(size - studyRecord.getWeek());
+        if (null != studyRecord) {
+            // 学习时长格式转换
+            Integer todayStudy = studyRecord.getTodayStudy();
+            studyRecord.setTodayStudy(Math.round((float) todayStudy / 3600));
+            Integer weekStudy = studyRecord.getWeekStudy();
+            studyRecord.setWeekStudy(Math.round((float) weekStudy / 3600));
+            Integer monthStudy = studyRecord.getMonthStudy();
+            studyRecord.setMonthStudy(Math.round((float) monthStudy / 3600));
+            // 游戏总时长
+            int sum = gameRecordList.stream().map(TGameRecord::getUseTime).mapToInt(Integer::intValue).sum();
+            Integer totalStudy = studyRecord.getTotalStudy();
+            studyRecord.setTotalStudy(Math.round((float) (totalStudy + sum) / 3600));
+            // 剩余周目
+            int size = studyService.list(new QueryWrapper<TStudy>()
+                    .eq("type", 1)).size();
+            studyRecord.setSurplus(size - studyRecord.getWeek());
+        }
         return R.ok(new StudyRecordResultVO(studyRecord, gameRecordList));
     }
 

--
Gitblit v1.7.1