Ver código fonte

1、调整看课时长
2、积分报错

yys 3 dias atrás
pai
commit
f3730e9f71

+ 29 - 15
fs-service/src/main/java/com/fs/his/service/impl/FsUserSignServiceImpl.java

@@ -121,29 +121,35 @@ public class FsUserSignServiceImpl implements IFsUserSignService
     @Override
     @Transactional
     public Long sign(FsUser user) {
+        if (user == null || user.getUserId() == null) {
+            throw new CustomException("用户不存在");
+        }
         String json=configService.selectConfigByKey("his.sign");
         if(StringUtils.isEmpty(json)) {
             throw new CustomException("请先配置签到天数");
         }
         JSONArray jsonArray= JSONUtil.parseArray(json);
         List<StoreSignConfig> signs=jsonArray.toList(StoreSignConfig.class);
+        if (signs == null || signs.isEmpty()) {
+            throw new CustomException("请先配置签到天数");
+        }
         boolean isDaySign = this.getToDayIsSign(user.getUserId());
         if(isDaySign) {
             throw new CustomException("已签到");
         }
-        Long signNumber = 0l; //积分
-        Long userSignNum = user.getSignNum(); //签到次数
+        Long signNumber = 0L; //积分
+        Long userSignNum = user.getSignNum() != null ? user.getSignNum() : 0L; //签到次数
         if(getYesterDayIsSign(user.getUserId())){
-            if(user.getSignNum() > (signs.size() - 1)){
-                userSignNum = 0l;
+            if(userSignNum > (signs.size() - 1)){
+                userSignNum = 0L;
             }
         }else{
-            userSignNum = 0l;
+            userSignNum = 0L;
         }
         int index = 0;
         for (StoreSignConfig config : signs) {
             if(index == userSignNum){
-                signNumber = config.getSignNum();
+                signNumber = config.getSignNum() != null ? config.getSignNum() : 0L;
                 break;
             }
             index++;
@@ -160,7 +166,7 @@ public class FsUserSignServiceImpl implements IFsUserSignService
         userSign.setTitle(title);
         userSign.setNumber(signNumber);
         userSign.setCreateTime(new Date());
-        userSign.setBalance(user.getIntegral().longValue());
+        userSign.setBalance(user.getIntegral() != null ? user.getIntegral() : 0L);
         fsUserSignMapper.insertFsUserSign(userSign);
 
 //        FsUserIntegralLogs logs = new FsUserIntegralLogs();
@@ -208,29 +214,35 @@ public class FsUserSignServiceImpl implements IFsUserSignService
 
     @Override
     public Long getSign(FsUser user) {
+        if (user == null || user.getUserId() == null) {
+            return 0L;
+        }
         String json=configService.selectConfigByKey("his.sign");
         if(StringUtils.isEmpty(json)) {
             throw new CustomException("请先配置签到天数");
         }
         JSONArray jsonArray= JSONUtil.parseArray(json);
         List<StoreSignConfig> signs=jsonArray.toList(StoreSignConfig.class);
+        if (signs == null || signs.isEmpty()) {
+            return 0L;
+        }
         boolean isDaySign = this.getToDayIsSign(user.getUserId());
-        Long userSignNum = user.getSignNum(); //签到次数
+        Long userSignNum = user.getSignNum() != null ? user.getSignNum() : 0L; //签到次数
         if(getYesterDayIsSign(user.getUserId())){
-            if(user.getSignNum() > (signs.size() - 1)){
+            if(userSignNum > (signs.size() - 1)){
                 if(isDaySign) {
-                    return user.getSignNum();
+                    return userSignNum;
                 }
                 else{
-                    userSignNum = 0l;
+                    userSignNum = 0L;
                 }
             }
         }else{
             if(isDaySign) {
-                return user.getSignNum();
+                return userSignNum;
             }
             else{
-                userSignNum = 0l;
+                userSignNum = 0L;
             }
 
         }
@@ -239,8 +251,10 @@ public class FsUserSignServiceImpl implements IFsUserSignService
 
     @Override
     public Boolean isDaySign(FsUser user) {
-        boolean isDaySign = this.getToDayIsSign(user.getUserId());
-        return isDaySign;
+        if (user == null || user.getUserId() == null) {
+            return false;
+        }
+        return this.getToDayIsSign(user.getUserId());
     }
 
     /**

+ 3 - 21
fs-service/src/main/java/com/fs/live/service/impl/LiveConsoleOpLogServiceImpl.java

@@ -265,16 +265,12 @@ public class LiveConsoleOpLogServiceImpl implements ILiveConsoleOpLogService {
             fillCouponType(recordVo, opLog, couponTypeMap);
             result.add(recordVo);
         }
-        result.sort(this::compareOpLogRecordByStatus);
+        result.sort(this::compareOpLogRecordByTime);
         return result;
     }
 
-    /** 已领取优先,其次待领取,最后已结束;同状态按创建时间倒序 */
-    private int compareOpLogRecordByStatus(LiveConsoleOpLogRecordVo a, LiveConsoleOpLogRecordVo b) {
-        int priorityDiff = resolveStatusSortPriority(a.getStatus()) - resolveStatusSortPriority(b.getStatus());
-        if (priorityDiff != 0) {
-            return priorityDiff;
-        }
+    /** 按创建时间倒序(最新在前) */
+    private int compareOpLogRecordByTime(LiveConsoleOpLogRecordVo a, LiveConsoleOpLogRecordVo b) {
         Date timeA = a.getCreateTime();
         Date timeB = b.getCreateTime();
         if (timeA == null && timeB == null) {
@@ -289,20 +285,6 @@ public class LiveConsoleOpLogServiceImpl implements ILiveConsoleOpLogService {
         return timeB.compareTo(timeA);
     }
 
-    private int resolveStatusSortPriority(Integer status) {
-        if (status == null) {
-            return 2;
-        }
-        switch (status) {
-            case LiveConsoleOpLogRecordVo.STATUS_CLAIMED:
-                return 0;
-            case LiveConsoleOpLogRecordVo.STATUS_PENDING:
-                return 1;
-            default:
-                return 2;
-        }
-    }
-
     private boolean isInternalSettleOpLog(LiveConsoleOpLog opLog) {
         if (opLog == null || opLog.getOpType() == null) {
             return false;

+ 29 - 11
fs-service/src/main/java/com/fs/live/service/impl/LiveWatchUserServiceImpl.java

@@ -1301,13 +1301,16 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
             return 0L;
         }
         try {
-            long liveDuration = safeOnlineSeconds(baseMapper.selectByUniqueIndex(liveId, userId, 1, 0));
-            long replayDuration = safeOnlineSeconds(baseMapper.selectByUniqueIndex(liveId, userId, 0, 1));
-            long totalDuration = liveDuration + replayDuration;
-
-            log.debug("查询总观看时长: liveId={}, userId={}, liveDuration={}, replayDuration={}, total={}",
-                    liveId, userId, liveDuration, replayDuration, totalDuration);
-
+            Map<String, Object> params = new HashMap<>();
+            params.put("liveId", liveId);
+            params.put("userId", userId);
+            List<LiveWatchUser> records = baseMapper.selectUserByLiveIdAndUserId(params);
+            if (records == null || records.isEmpty()) {
+                return 0L;
+            }
+            long totalDuration = records.stream().mapToLong(LiveWatchUserServiceImpl::safeOnlineSeconds).sum();
+            log.debug("查询总观看时长: liveId={}, userId={}, recordCount={}, total={}",
+                    liveId, userId, records.size(), totalDuration);
             return totalDuration;
         } catch (Exception e) {
             log.error("查询总观看时长失败: liveId={}, userId={}", liveId, userId, e);
@@ -1324,19 +1327,34 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
 
     @Override
     public Long getUserWatchDuration(Long liveId, Long userId) {
-        Long total = getTotalWatchDuration(liveId, userId);
-        long dbDuration = total != null ? total : 0L;
+        long duration = safeLong(getTotalWatchDuration(liveId, userId));
+        try {
+            String entryTimeKey = String.format(USER_ENTRY_TIME_KEY, liveId, userId);
+            Long entryTime = redisCache.getCacheObject(entryTimeKey);
+            if (entryTime != null) {
+                long sessionSeconds = (System.currentTimeMillis() - entryTime) / 1000;
+                if (sessionSeconds > 0) {
+                    duration += sessionSeconds;
+                }
+            }
+        } catch (Exception e) {
+            log.debug("读取 Redis 进入时间失败: liveId={}, userId={}", liveId, userId, e);
+        }
         try {
             String hashKey = "live:watch:duration:hash:" + liveId;
             Object redisValue = redisCache.hashGet(hashKey, String.valueOf(userId));
             if (redisValue != null) {
                 long redisDuration = Long.parseLong(redisValue.toString());
-                return Math.max(dbDuration, redisDuration);
+                duration = Math.max(duration, redisDuration);
             }
         } catch (Exception e) {
             log.debug("读取 Redis 观看时长失败: liveId={}, userId={}", liveId, userId, e);
         }
-        return dbDuration;
+        return duration;
+    }
+
+    private static long safeLong(Long value) {
+        return value != null ? value : 0L;
     }
 
 }

+ 8 - 1
fs-user-app/src/main/java/com/fs/app/controller/IntegralController.java

@@ -212,18 +212,25 @@ public class IntegralController extends  AppBaseController {
     @GetMapping("/getUserSign")
     public R getUserSign(HttpServletRequest request){
         FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
+        if (user == null) {
+            return R.error("用户不存在");
+        }
         //获取签到配置
         String json=configService.selectConfigByKey("his.sign");
         //判断用户昨天是否签到过
         Long signNum=userSignService.getSign(user);
         Boolean isDaySign=userSignService.isDaySign(user);
-        return R.ok().put("isDaySign", isDaySign).put("signNum", signNum).put("integral",user.getIntegral().intValue()).put("sign", json);
+        int integral = user.getIntegral() != null ? user.getIntegral().intValue() : 0;
+        return R.ok().put("isDaySign", isDaySign).put("signNum", signNum).put("integral", integral).put("sign", json);
     }
     @Login
     @ApiOperation("签到")
     @PostMapping("/sign")
     public R sign(HttpServletRequest request){
         FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
+        if (user == null) {
+            return R.error("用户不存在");
+        }
         Long integral=userSignService.sign(user);
         return R.ok("签到获得" + integral + "积分").put("data", fsUserCourseVideoService.getSignGrandGiftRules(user.getUserId()));
     }

+ 1 - 2
fs-user-app/src/main/java/com/fs/app/controller/live/LiveController.java

@@ -416,8 +416,7 @@ public class LiveController extends AppBaseController {
 	@GetMapping("/myRewardRecords")
 	public R myRewardRecords(@RequestParam Long liveId) {
 		try {
-//			Long userId = Long.valueOf(getUserId());
-			Long userId = 956627L;
+			Long userId = Long.valueOf(getUserId());
 			return R.ok().put("data", liveFacadeService.getUserRewardRecords(liveId, userId));
 		} catch (Exception e) {
 			log.error("查询奖品留存记录失败, liveId={}, userId={}", liveId, getUserId(), e);

+ 35 - 6
fs-user-app/src/main/java/com/fs/app/facade/impl/LiveFacadeServiceImpl.java

@@ -273,7 +273,11 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
         Long liveDuration = resolveLiveDuration(liveId);
         vo.setLiveDuration(liveDuration);
         Long totalWatch = liveWatchUserService.getUserWatchDuration(liveId, userId);
-        vo.setWatchDuration(totalWatch != null ? totalWatch : 0L);
+        long watchDuration = totalWatch != null ? totalWatch : 0L;
+        if (liveDuration != null && liveDuration > 0) {
+            watchDuration = Math.min(watchDuration, liveDuration);
+        }
+        vo.setWatchDuration(watchDuration);
         return vo;
     }
 
@@ -285,17 +289,42 @@ public class LiveFacadeServiceImpl extends BaseController implements LiveFacadeS
         if (live == null) {
             return 0L;
         }
+        if (live.getStartTime() != null) {
+            LocalDateTime endTime = resolveLiveDurationEndTime(live);
+            long seconds = ChronoUnit.SECONDS.between(live.getStartTime(), endTime);
+            return Math.max(seconds, 0L);
+        }
         if (live.getDuration() != null && live.getDuration() > 0) {
             return live.getDuration();
         }
         if (live.getVideoDuration() != null && live.getVideoDuration() > 0) {
             return live.getVideoDuration();
         }
-        if (live.getStartTime() != null) {
-            LocalDateTime endTime = live.getFinishTime() != null ? live.getFinishTime() : LocalDateTime.now();
-            long seconds = ChronoUnit.SECONDS.between(live.getStartTime(), endTime);
-            return Math.max(seconds, 0L);
-        }
         return 0L;
     }
+
+    /**
+     * 计算开播总时长的结束时间点。
+     * finishTime 在创建直播间时会被预填为 startTime + 视频时长,不能作为进行中的实际结束时间。
+     */
+    private LocalDateTime resolveLiveDurationEndTime(Live live) {
+        LocalDateTime now = LocalDateTime.now();
+        if (live.getStartTime() == null) {
+            return now;
+        }
+        Integer status = live.getStatus();
+        if (status != null && status == 1 && live.getStartTime().isAfter(now)) {
+            return live.getStartTime();
+        }
+        if (status != null && (status == 2 || status == 4)) {
+            return now;
+        }
+        if (status != null && status == 3 && live.getFinishTime() != null) {
+            return live.getFinishTime();
+        }
+        if (live.getFinishTime() != null && !live.getFinishTime().isAfter(now)) {
+            return live.getFinishTime();
+        }
+        return now;
+    }
 }