From 233ab439e38d3040d556f3f6d04492be2a3a6c8d Mon Sep 17 00:00:00 2001
From: hjl <1657978663@qq.com>
Date: 星期三, 19 六月 2024 18:17:14 +0800
Subject: [PATCH] fix: 学习端bug

---
 ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TStudyController.java |   81 +++++++++++++++++++++++++++++-----------
 1 files changed, 58 insertions(+), 23 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..57d48e6 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
@@ -1097,7 +1097,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 +1299,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 +1435,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 +1503,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