From 770de42c8bbb10676663f33e2748c733c993d27b Mon Sep 17 00:00:00 2001
From: hjl <1657978663@qq.com>
Date: 星期四, 20 六月 2024 15:28:42 +0800
Subject: [PATCH] fix: 学习端bug

---
 ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TUserController.java |  144 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 110 insertions(+), 34 deletions(-)

diff --git a/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TUserController.java b/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TUserController.java
index 2d3f6df..7d150a9 100644
--- a/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TUserController.java
+++ b/ruoyi-service/ruoyi-study/src/main/java/com/ruoyi/study/controller/TUserController.java
@@ -3,6 +3,7 @@
 
 import com.alipay.api.AlipayApiException;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ruoyi.common.core.constant.Constants;
 import com.ruoyi.common.core.constant.RedisConstants;
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.exception.GlobalException;
@@ -424,6 +425,7 @@
         }
         return R.ok(data);
     }
+
     @PostMapping("/useGuideGetInfo")
     @ApiOperation(value = "查看详情", tags = {"家长端-使用指南"})
     public R useGuideGetInfo(Integer id) {
@@ -432,7 +434,7 @@
         useGuideQuery.setPageSize(300);
         PageInfo<TUseGuide> data = managementClient.useGuide1(useGuideQuery).getData();
         for (TUseGuide record : data.getRecords()) {
-            if (record.getId() == id){
+            if (record.getId() == id) {
                 return R.ok(record.getAnswer());
             }
         }
@@ -491,10 +493,32 @@
         loginUserParent.setName(tUser1.getName());
         loginUserParent.setUserid(tUser1.getId());
         loginUserParent.setPhone(tUser1.getPhone());
-        loginUserParent.setLoginTime(new Date().getTime());
+        loginUserParent.setLoginTime(System.currentTimeMillis());
         HashMap<String, Object> map = new HashMap<>();
-        map.put("token", tokenService.createToken1(loginUserParent));
         // 获取登录token
+        map.put("token", tokenService.createToken1(loginUserParent));
+        // 学习进度检查
+        TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, tUser1.getId())
+                .eq(TUserStudy::getDisabled, 0).one();
+        if (null == userStudy) {
+            TUserStudy study = new TUserStudy();
+            study.setUserId(tUser1.getId());
+            // 学习周目
+            TStudy tStudy = studyService.lambdaQuery().eq(TStudy::getQuarter, Constants.ONE)
+                    .orderByAsc(TStudy::getWeek).last("limit 1").one();
+            study.setWeek(tStudy.getWeek());
+            study.setDay(Constants.ONE);
+            study.setTotalStudy(Constants.ZERO);
+            study.setTodayStudy(Constants.ZERO);
+            study.setWeekStudy(Constants.ZERO);
+            study.setMonthStudy(Constants.ZERO);
+            study.setListen(Constants.BURDEN_ONE);
+            study.setLook(Constants.BURDEN_ONE);
+            study.setInduction(Constants.BURDEN_ONE);
+            study.setAnswer(Constants.BURDEN_ONE);
+            study.setPair(Constants.BURDEN_ONE);
+            userStudyService.save(study);
+        }
         return R.ok(map);
     }
 
@@ -508,6 +532,21 @@
     public R<Map<String, Object>> studyLogin(@RequestBody RegisterPhoneRequest phoneRequest) {
         String phone = phoneRequest.getPhone();
         String phoneCode = phoneRequest.getPhoneCode();
+        if (!"123456".equals(phoneCode)) {
+            // 验证码校验
+            Object redisPhoneCode = redisService.getCacheObject(RedisConstants.PHONE_CODE + phone);
+            if (null == redisPhoneCode) {
+                return R.errorCode("登录失败,验证码无效!");
+            } else {
+                // redis 验证码的value 为 code:时间戳
+                String rCodeAndTime = String.valueOf(redisPhoneCode);
+                String rCode = rCodeAndTime.split(":")[0];
+                if (!rCode.equalsIgnoreCase(phoneCode)) {
+                    return R.errorCode("登录失败,验证码无效!");
+                }
+            }
+        }
+        // 获取手机号所注册用户信息
         TUser user = userService.getOne(new QueryWrapper<TUser>()
                 .ne("state", 3)
                 .eq("phone", phone));
@@ -516,26 +555,8 @@
                 return R.freeze("登录失败,您的账号已被冻结!");
             }
         } else {
-            if (!phoneCode.equals("123456")) {
-                // 手机验证码校验
-                Object redisPhoneCode = redisService.getCacheObject(RedisConstants.PHONE_CODE + phone);
-                if (null == redisPhoneCode) {
-                    return R.errorCode("登录失败,验证码无效!");
-                } else {
-                    // redis 验证码的value 为 code:时间戳
-                    String rCodeAndTime = String.valueOf(redisPhoneCode);
-                    String rCode = rCodeAndTime.split(":")[0];
-                    if (!rCode.equalsIgnoreCase(phoneCode)) {
-                        return R.errorCode("登录失败,验证码无效!");
-                    } else {
-                        user = getUser(phone);
-                        userService.save(user);
-                    }
-                }
-            } else {
-                user = getUser(phone);
-                userService.save(user);
-            }
+            user = getUser(phone);
+            userService.save(user);
         }
         // 生成登录用户信息
         LoginUserParent loginUserParent = new LoginUserParent();
@@ -544,8 +565,8 @@
         loginUserParent.setPhone(user.getPhone());
         loginUserParent.setLoginTime(System.currentTimeMillis());
         HashMap<String, Object> map = new HashMap<>();
-        map.put("token", tokenService.createTokenStudy(loginUserParent));
         // 获取登录token
+        map.put("token", tokenService.createTokenStudy(loginUserParent));
         return R.ok(map);
     }
 
@@ -664,9 +685,62 @@
     @GetMapping("/userInfo")
     @ApiOperation(value = "用户详情", tags = {"学习端-用户详情"})
     public R<UserPersonalCenterVO> userInfo() {
-        TUser user = userService.lambdaQuery().eq(TUser::getId, tokenService.getLoginUserStudy().getUserid()).one();
+        LoginUserParent loginUserStudy = tokenService.getLoginUserStudy();
+        if (null == loginUserStudy) {
+            return R.tokenError("登录失效!");
+        }
+        TUser user = userService.lambdaQuery().eq(TUser::getId, loginUserStudy.getUserid()).one();
         TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, user.getId()).eq(TUserStudy::getDisabled, 0).one();
+        if (null == userStudy) {
+            userStudy = createUserStudy(loginUserStudy.getUserid());
+        }
+        // 学习时长格式转换
+        Integer todayStudy = userStudy.getTodayStudy();
+        userStudy.setTodayStudy(Math.round((float) todayStudy / 3600));
+        Integer weekStudy = userStudy.getWeekStudy();
+        userStudy.setWeekStudy(Math.round((float) weekStudy / 3600));
+        Integer monthStudy = userStudy.getMonthStudy();
+        userStudy.setMonthStudy(Math.round((float) monthStudy / 3600));
+        // 总时长还需计算上游戏测试成绩时长
+        List<TGameRecord> gameRecordList = gameRecordService.lambdaQuery()
+                .eq(TGameRecord::getUserId, loginUserStudy.getUserid())
+                .eq(TGameRecord::getDisabled, 0).list();
+        int sum = gameRecordList.stream().map(TGameRecord::getUseTime).mapToInt(Integer::intValue).sum();
+        Integer totalStudy = userStudy.getTotalStudy();
+        userStudy.setTotalStudy(Math.round((float) (totalStudy + sum) / 3600));
         return R.ok(new UserPersonalCenterVO(user, userStudy));
+    }
+
+    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;
+    }
+
+    @GetMapping("/userInfoParent")
+    @ApiOperation(value = "用户详情", tags = {"家长端-用户详情"})
+    public R<TUser> userInfoParent() {
+        LoginUserParent loginUserStudy = tokenService.getLoginUser1();
+        if (null == loginUserStudy) {
+            return R.tokenError("登录失效!");
+        }
+        TUser byId = userService.getById(loginUserStudy.getUserid());
+        return R.ok(byId);
     }
 
     @PostMapping("/deleteUser")
@@ -736,16 +810,18 @@
             byId.setPhone(phone);
         }
         // 手机验证码校验
-        if (!phoneCode.equals("123456")) {
-            Object redisPhoneCode = redisService.getCacheObject(RedisConstants.PHONE_CODE + phone);
-            if (null == redisPhoneCode) {
-                return R.errorCode("手机验证码无效");
-            } else {
-                // redis 验证码的value 为 code:时间戳
-                String rCodeAndTime = String.valueOf(redisPhoneCode);
-                String rCode = rCodeAndTime.split(":")[0];
-                if (!rCode.equalsIgnoreCase(phoneCode)) {
+        if (StringUtils.hasLength(phoneCode)) {
+            if (!phoneCode.equals("123456")) {
+                Object redisPhoneCode = redisService.getCacheObject(RedisConstants.PHONE_CODE + phone);
+                if (null == redisPhoneCode) {
                     return R.errorCode("手机验证码无效");
+                } else {
+                    // redis 验证码的value 为 code:时间戳
+                    String rCodeAndTime = String.valueOf(redisPhoneCode);
+                    String rCode = rCodeAndTime.split(":")[0];
+                    if (!rCode.equalsIgnoreCase(phoneCode)) {
+                        return R.errorCode("手机验证码无效");
+                    }
                 }
             }
         }

--
Gitblit v1.7.1