Browse Source

Merge remote-tracking branch 'origin/master'

yzx 4 days ago
parent
commit
cf7e13f23d

+ 35 - 4
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -1194,22 +1194,56 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
 
     @Override
     public R sendRewardByFsUser(FsCourseSendRewardUParam param) {
+        // 生成锁的key,基于用户ID和视频ID确保同一用户同一视频的请求被锁定
+        String lockKey = "reward_lock:user:" + param.getUserId() + ":video:" + param.getVideoId();
+        RLock lock = redissonClient.getLock(lockKey);
+
+        try {
+            // 尝试获取锁,等待时间5秒,锁过期时间30秒
+            boolean isLocked = lock.tryLock(5, 30, TimeUnit.SECONDS);
+            if (!isLocked) {
+                logger.warn("获取锁失败,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
+                return R.error("操作频繁,请稍后再试!");
+            }
+
+            logger.info("成功获取锁,开始处理奖励发放,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
+            return executeSendRewardBusiness(param);
+
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            logger.error("获取锁被中断,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId(), e);
+            return R.error("系统繁忙,请重试!");
+        } finally {
+            // 释放锁
+            if (lock.isHeldByCurrentThread()) {
+                lock.unlock();
+                logger.info("释放锁成功,用户ID:{},视频ID:{}", param.getUserId(), param.getVideoId());
+            }
+        }
+    }
+
+
+    /**
+     * 实际的奖励发放业务逻辑
+     */
+    private R executeSendRewardBusiness(FsCourseSendRewardUParam param) {
         log.info("进入用户判断");
         FsUser user = fsUserMapper.selectFsUserByUserId(param.getUserId());
         if (user == null){
             return R.error("未识别到用户信息");
         }
+
         FsCourseWatchLog log = courseWatchLogMapper.getWatchCourseVideoByFsUser(param.getUserId(), param.getVideoId(), param.getCompanyUserId());
         if (log == null) {
             return R.error("无记录");
         }
 
         FsCourseAnswerLogs rightLog = courseAnswerLogsMapper.selectRightLogByCourseVideo(param.getVideoId(), param.getUserId(), param.getQwUserId());
-
         if (rightLog == null) {
             logger.error("未答题:{}",param.getUserId());
             return R.error("未答题");
         }
+
         if (log.getRewardType() != null ) {
             if (log.getRewardType() == 1){
                 FsCourseRedPacketLog fsCourseRedPacketLog = redPacketLogMapper.selectUserFsCourseRedPacketLog(param.getVideoId(), param.getUserId(),param.getPeriodId());
@@ -1227,11 +1261,8 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
             }else if (log.getRewardType() == 2){
                 return R.error("已领取该课程奖励,不可重复领取!");
             }
-
         }
 
-
-
         // 获取视频信息
         FsUserCourseVideo video = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoId(param.getVideoId());