|  | @@ -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);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 |