瀏覽代碼

直播间红包功能 礼物配置等

yuhongqi 4 天之前
父節點
當前提交
3e90bba64c

+ 1 - 6
fs-admin/src/main/java/com/fs/live/controller/LiveController.java

@@ -99,12 +99,7 @@ public class LiveController extends BaseController
 
     @GetMapping("/living/{liveId}")
     public R getRoom(@PathVariable String liveId) {
-        Map<String, Object> resultMap = liveService.getLiveRoom(liveId);
-        if ("200".equals(resultMap.get("code"))) {
-            return R.ok().put("livingUrl", resultMap.get("livingUrl"));
-        } else {
-            return R.error((String) resultMap.get("msg"));
-        }
+        return liveService.getLiveRoom(liveId);
     }
 
 }

+ 0 - 1
fs-service/src/main/java/com/fs/live/mapper/LiveRedConfMapper.java

@@ -61,7 +61,6 @@ public interface LiveRedConfMapper extends BaseMapper<LiveRedConf>{
      */
     int deleteLiveRedConfByRedIds(Long[] redIds);
 
-    @Select("SELECT * FROM live_red_conf WHERE red_id = #{redId}")
     LiveRedConf selectById(Long redId);
 
     @Select("SELECT * FROM live_red_conf WHERE live_id = #{liveId}")

+ 19 - 13
fs-service/src/main/java/com/fs/live/service/impl/LiveRedConfServiceImpl.java

@@ -1,9 +1,6 @@
 package com.fs.live.service.impl;
 
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 
@@ -148,7 +145,7 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
     @Transactional
     public boolean claimRedPacket(RedPO red) {
         String lockKey = REDPACKET_REMAININGLOTS_KEY + red.getRedId();
-        List<String> keyList = Collections.emptyList();
+        List<String> keyList =  new ArrayList<>();
         keyList.add(lockKey);
 
         try {
@@ -157,25 +154,33 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
                 return false;
             }
 
-            LiveRedConf conf = baseMapper.selectById(red.getRedId());
+            LiveRedConf conf = baseMapper.selectLiveRedConfByRedId(red.getRedId());
             if (conf == null || conf.getRedStatus() != 1) {
                 return false;
             }
             //redis剩余红包数
-            Integer remaining = getRemaining(red.getRedId());
-            if (remaining <= 0) {
+            // 平均分 暂时不适用redis 记录红包数
+//            Integer remaining = getRemaining(red.getRedId());
+//            if (remaining <= 0) {
+//                return false;
+//            }
+            Long integral = calculateIntegralAverage(conf);
+            if (0L == integral) {
                 return false;
             }
+            Date now = new Date();
             //剩余金额
             //Integer remainingNum = getRemainingNum(red.getRedId());
-            conf.setRemaining(remaining);
-            Long integral = calculateIntegralAverage(conf);
+
+
             conf.setTotalSend(conf.getTotalSend() + 1);
+            conf.setRemaining(Math.toIntExact(conf.getTotalLots() - conf.getTotalSend()));
+            conf.setUpdateTime(now);
 
 
 
             // 更新数据库和缓存
-            baseMapper.update(conf);
+            baseMapper.updateLiveRedConf(conf);
             decreaseRemainingLotsIfPossible(red.getRedId());
             //decreaseRemainingNumIfPossible(red.getRedId(),integral);
 
@@ -185,7 +190,8 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
             record.setLiveId(red.getLiveId());
             record.setUserId(red.getUserId());
             record.setIntegral(integral);
-            userRedRecordMapper.insert(record);
+            record.setCreateTime(now);
+            userRedRecordMapper.insertLiveUserRedRecord(record);
 
             // WebSocket 通知
             //String msg = String.format("用户 %d 抢到了红包 %d,获得 %d 芳华币", userId, redId, integral);
@@ -251,7 +257,7 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
     }
 
     public void releaseLock(List<String> lockKey, String clientId) {
-        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
+        String script = "if redis.call('get', KEYS[1]) == ARGV then return redis.call('del', KEYS[1]) else return 0 end";
         redisCache.redisTemplate.execute(new DefaultRedisScript<>(script, Long.class), lockKey, clientId);
     }
 

+ 6 - 0
fs-service/src/main/resources/mapper/live/LiveRedConfMapper.xml

@@ -102,4 +102,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{redId}
         </foreach>
     </delete>
+
+    <select id="selectById" parameterType="Long" resultMap="LiveRedConfResult">
+        <include refid="selectLiveRedConfVo"/>
+        where red_id = #{redId}
+    </select>
+
 </mapper>