Browse Source

处理红包多次领取问题

yzx 4 days ago
parent
commit
6dc392ca1b

+ 78 - 56
fs-service/src/main/java/com/fs/course/service/impl/FsUserCourseVideoServiceImpl.java

@@ -246,6 +246,7 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
 
 
 
+
     /**
      * 查询课堂视频
      *
@@ -1075,72 +1076,93 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
 //        if (isWithin10Minutes){
 //            return R.error("非有效期内,不允许领取!");
 //        }
-        // 获取用户信息
-        FsUser user = fsUserMapper.selectFsUserByUserId(param.getUserId());
+
+        RLock lock = redissonClient.getLock("sendReward");
+
+        try{
+            boolean getLock = lock.tryLock(100,10000, TimeUnit.MILLISECONDS);
+            if (!getLock) {
+
+                return R.error("服务器繁忙,请稍候再试!");
+            }
+            // 获取用户信息
+
+            FsUser user = fsUserMapper.selectFsUserByUserId(param.getUserId());
 //        if (StringUtils.isEmpty(user.getMpOpenId())){
 //            return R.error("未识别到领取信息");
 //        }
-        log.info("查询会员信息:{}", user);
-        if (user.getStatus()==0){
-            return R.error("会员被停用,无权限,请联系客服!");
-        }
-        FsCourseWatchLog watchLog = new FsCourseWatchLog();
+            log.info("查询会员信息:{}", user);
+            if (user.getStatus()==0){
+                return R.error("会员被停用,无权限,请联系客服!");
+            }
+            FsCourseWatchLog watchLog = new FsCourseWatchLog();
 
-        // 根据链接类型判断是否已发放奖励
-        watchLog = courseWatchLogMapper.getWatchCourseVideo(param.getUserId(), param.getVideoId(), param.getQwUserId(), param.getQwExternalId());
-        log.info("看课记录:{}", watchLog);
-        if (watchLog == null) {
-            return R.error("无记录");
-        }
-        if (watchLog.getLogType() != 2) {
-            return R.error("未完课");
-        }
-        if (watchLog.getRewardType() != null) {
-            FsCourseRedPacketLog packetLog = redPacketLogMapper.selectFsCourseRedPacketLogByTemporary(param.getVideoId(), param.getUserId());
-            log.info("课程红包:{}", packetLog);
-            if(packetLog != null && packetLog.getStatus() == 1) {
-                return R.error("已领取该课程奖励,不可重复领取!");
+            // 根据链接类型判断是否已发放奖励
+            watchLog = courseWatchLogMapper.getWatchCourseVideo(param.getUserId(), param.getVideoId(), param.getQwUserId(), param.getQwExternalId());
+            log.info("看课记录:{}", watchLog);
+            if (watchLog == null) {
+                return R.error("无记录");
             }
-            if(packetLog != null && packetLog.getStatus() == 0) {
-                log.info("判断领取记录");
-                if(StringUtils.isNotEmpty(packetLog.getResult())){
-                    log.info("是否有结果");
-                    R r = JSON.parseObject(packetLog.getResult(), R.class);
-                    return r;
-                } else {
-                    return R.error("操作频繁,请稍后再试!");
+            if (watchLog.getLogType() != 2) {
+                return R.error("未完课");
+            }
+            if (watchLog.getRewardType() != null) {
+                FsCourseRedPacketLog packetLog = redPacketLogMapper.selectFsCourseRedPacketLogByTemporary(param.getVideoId(), param.getUserId());
+                log.info("课程红包:{}", packetLog);
+                if(packetLog != null && packetLog.getStatus() == 1) {
+                    return R.error("已领取该课程奖励,不可重复领取!");
                 }
+                if(packetLog != null && packetLog.getStatus() == 0) {
+                    log.info("判断领取记录");
+                    if(StringUtils.isNotEmpty(packetLog.getResult())){
+                        log.info("是否有结果");
+                        R r = JSON.parseObject(packetLog.getResult(), R.class);
+                        return r;
+                    } else {
+                        return R.error("操作频繁,请稍后再试!");
+                    }
+                }
+                if(packetLog != null && packetLog.getStatus() == 2) {
+                    return R.error("请联系客服补发");
+                }
+                return R.error("奖励已发放");
             }
-            if(packetLog != null && packetLog.getStatus() == 2) {
-                return R.error("请联系客服补发");
+            // 获取视频信息
+            FsUserCourseVideo video = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoId(param.getVideoId());
+
+            // 获取配置信息
+            String json = configService.selectConfigByKey("course.config");
+            CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
+            log.info("奖励类型:{}", config.getRewardType());
+            // 根据奖励类型发放不同奖励
+            switch (config.getRewardType()) {
+                // 红包奖励
+                case 1:
+                    return sendRedPacketReward(param, user, watchLog, video, config);
+                // 积分奖励
+                case 2:
+                    return sendIntegralReward(param,user, watchLog, config);
+                // 红包+积分
+                case 3:
+                    R sendRed = sendRedPacketReward(param, user, watchLog, video, config);
+                    if (!Objects.equals(sendRed.get("code"), 200)) {
+                        return sendRed;
+                    }
+                    return sendIntegralReward(param,user, watchLog, config);
+                default:
+                    return R.error("参数错误!");
             }
-            return R.error("奖励已发放");
-        }
-        // 获取视频信息
-        FsUserCourseVideo video = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoId(param.getVideoId());
 
-        // 获取配置信息
-        String json = configService.selectConfigByKey("course.config");
-        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
-        log.info("奖励类型:{}", config.getRewardType());
-        // 根据奖励类型发放不同奖励
-        switch (config.getRewardType()) {
-            // 红包奖励
-            case 1:
-                return sendRedPacketReward(param, user, watchLog, video, config);
-            // 积分奖励
-            case 2:
-                return sendIntegralReward(param,user, watchLog, config);
-            // 红包+积分
-            case 3:
-                R sendRed = sendRedPacketReward(param, user, watchLog, video, config);
-                if (!Objects.equals(sendRed.get("code"), 200)) {
-                    return sendRed;
-                }
-                return sendIntegralReward(param,user, watchLog, config);
-            default:
-                return R.error("参数错误!");
         }
+        catch (Exception e){
+            throw new CustomException(e.getMessage());
+        }
+        finally {
+            if(null != lock && lock.isLocked() && lock.isHeldByCurrentThread()){
+                lock.unlock();
+            }
+        }
+
     }
 
 

+ 29 - 1
fs-user-app/src/main/java/com/fs/app/controller/course/CourseQwController.java

@@ -27,6 +27,8 @@ import com.hc.openapi.tool.fastjson.JSON;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
@@ -37,6 +39,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 @Api("企微看课接口")
 @RestController
@@ -68,7 +71,8 @@ public class CourseQwController extends AppBaseController {
 
     @Autowired
     private IFsIntegralGoodsService goodsService;
-
+    @Autowired
+    private RedissonClient redissonClient;
     @Autowired
     private IQwSopService qwSopService;
 
@@ -348,6 +352,30 @@ public class CourseQwController extends AppBaseController {
 //        courseVideoService.updateVideoUrl();
     }
 
+    @GetMapping("/test3")
+    @RepeatSubmit
+    public R test3() {
+        RLock lock = redissonClient.getLock("sendReward");
+        try{
+            boolean getLock = lock.tryLock(500,10000, TimeUnit.MILLISECONDS);
+            if (!getLock) {
+
+                return R.error("服务器繁忙,请稍候再试!");
+            }
+        }
+        catch (Exception e){
+
+        }
+        finally {
+            if(null != lock && lock.isLocked() && lock.isHeldByCurrentThread()){
+                lock.unlock();
+            }
+        }
+
+
+        return R.ok();
+    }
+
     @Login
     @ApiOperation("保存评论数据")
     @PostMapping("/saveMsg")

+ 2 - 2
fs-user-app/src/main/resources/application.yml

@@ -6,11 +6,11 @@ server:
 # Spring配置
 spring:
   profiles:
-#    active: dev
+    active: dev
 #    active: druid-jzzx
 #    active: druid-yzt
 #    active: druid-hdt
 #    active: druid-sxjz
 #    active: druid-qdtst
 #    active: druid-yzt
-    active: dev-jnlzjk
+#    active: dev-jnlzjk