|
|
@@ -1,12 +1,15 @@
|
|
|
package com.fs.live.service.impl;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.fs.common.constant.LiveKeysConstant;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.core.redis.RedisUtil;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.live.domain.LiveLotteryRegistration;
|
|
|
import com.fs.live.domain.LiveRedConf;
|
|
|
import com.fs.live.domain.LiveUserRedRecord;
|
|
|
import com.fs.live.mapper.LiveRedConfMapper;
|
|
|
@@ -22,12 +25,10 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 直播红包记录配置Service业务层处理
|
|
|
@@ -180,68 +181,66 @@ public class LiveRedConfServiceImpl implements ILiveRedConfService {
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public R claimRedPacket(RedPO red) {
|
|
|
- String claimKey = REDPACKET_CLAIM_KEY + red.getRedId();
|
|
|
-
|
|
|
- try {
|
|
|
- Integer remaining = getRemaining(red.getRedId());
|
|
|
- if (remaining <= 0) {
|
|
|
- LiveRedConf liveRedConf = new LiveRedConf();
|
|
|
- liveRedConf.setRedId(red.getRedId());
|
|
|
- liveRedConf.setRedStatus(2L);
|
|
|
- baseMapper.updateLiveRedConf(liveRedConf);
|
|
|
- return R.error("手慢了,红包已被抢完~");
|
|
|
- }
|
|
|
- //获取红包锁
|
|
|
+ // String claimKey = REDPACKET_CLAIM_KEY + red.getRedId();
|
|
|
+ Object o = redisUtil.hashGet(String.format(LiveKeysConstant.LIVE_HOME_PAGE_CONFIG_RED, red.getLiveId(), red.getRedId()), String.valueOf(red.getUserId()));
|
|
|
+ if (ObjectUtil.isNotEmpty(o)) {
|
|
|
+ return R.error("您已经领取过红包了!");
|
|
|
+ }
|
|
|
+ /*try {*/
|
|
|
+ Integer remaining = getRemaining(red.getRedId());
|
|
|
+ if (remaining <= 0) {
|
|
|
+ LiveRedConf liveRedConf = new LiveRedConf();
|
|
|
+ liveRedConf.setRedId(red.getRedId());
|
|
|
+ liveRedConf.setRedStatus(2L);
|
|
|
+ baseMapper.updateLiveRedConf(liveRedConf);
|
|
|
+ return R.error("手慢了,红包已被抢完~");
|
|
|
+ }
|
|
|
+/* //获取红包锁
|
|
|
if (!tryLock(claimKey, red.getUserId().toString(), 5)) {
|
|
|
return R.error("您已经领取过红包了!");
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
- LiveRedConf conf = baseMapper.selectLiveRedConfByRedId(red.getRedId());
|
|
|
- if (conf == null || conf.getRedStatus() != 1) {
|
|
|
- return R.error("手慢了,红包已被抢完~");
|
|
|
- }
|
|
|
- //redis剩余红包数
|
|
|
- // 平均分 暂时不适用redis 记录红包数
|
|
|
+ LiveRedConf conf = baseMapper.selectLiveRedConfByRedId(red.getRedId());
|
|
|
+ if (conf == null || conf.getRedStatus() != 1) {
|
|
|
+ return R.error("手慢了,红包已被抢完~");
|
|
|
+ }
|
|
|
+ //redis剩余红包数
|
|
|
+ // 平均分 暂时不适用redis 记录红包数
|
|
|
+ Long integral = calculateIntegralAverage(conf);
|
|
|
+ if (0L == integral) {
|
|
|
+ return R.error("手慢了,红包已被抢完~");
|
|
|
+ }
|
|
|
|
|
|
- Long integral = calculateIntegralAverage(conf);
|
|
|
- if (0L == integral) {
|
|
|
- return R.error("手慢了,红包已被抢完~");
|
|
|
- }
|
|
|
- Date now = new Date();
|
|
|
- //剩余金额
|
|
|
-// Integer remainingNum = getRemainingNum(red.getRedId());
|
|
|
-
|
|
|
-
|
|
|
- conf.setTotalSend(conf.getTotalSend() + 1);
|
|
|
- conf.setRemaining(Math.toIntExact(conf.getTotalLots() - conf.getTotalSend()));
|
|
|
- conf.setUpdateTime(now);
|
|
|
-
|
|
|
- userService.incrIntegral(Collections.singletonList(red.getUserId()), integral);
|
|
|
-
|
|
|
- // 更新数据库和缓存
|
|
|
- baseMapper.updateLiveRedConf(conf);
|
|
|
- decreaseRemainingLotsIfPossible(red.getRedId());
|
|
|
-// decreaseRemainingNumIfPossible(red.getRedId(), integral);
|
|
|
-
|
|
|
- // 记录用户红包
|
|
|
- LiveUserRedRecord record = new LiveUserRedRecord();
|
|
|
- record.setRedId(red.getRedId());
|
|
|
- record.setLiveId(red.getLiveId());
|
|
|
- record.setUserId(red.getUserId());
|
|
|
- record.setIntegral(integral);
|
|
|
- record.setCreateTime(now);
|
|
|
- userRedRecordMapper.insertLiveUserRedRecord(record);
|
|
|
-
|
|
|
- // WebSocket 通知
|
|
|
- //String msg = String.format("用户 %d 抢到了红包 %d,获得 %d 芳华币", userId, redId, integral);
|
|
|
- //WebSocketServer.notifyUsers(msg);
|
|
|
- redisUtil.hashPut(String.format(LiveKeysConstant.LIVE_HOME_PAGE_CONFIG_RED, red.getLiveId(), red.getRedId()), String.valueOf(red.getUserId()), JSONUtil.toJsonStr(record));
|
|
|
- return R.ok("恭喜您成功抢到"+ integral+"芳华币");
|
|
|
- } catch (Exception e) {
|
|
|
+ // 更新数据库
|
|
|
+/*
|
|
|
+ Date now = new Date();
|
|
|
+ conf.setTotalSend(conf.getTotalSend() + 1);
|
|
|
+ conf.setRemaining(Math.toIntExact(conf.getTotalLots() - conf.getTotalSend()));
|
|
|
+ conf.setUpdateTime(now);
|
|
|
+ baseMapper.updateLiveRedConf(conf);
|
|
|
+*/
|
|
|
+
|
|
|
+ // 记录用户红包
|
|
|
+ LiveUserRedRecord record = new LiveUserRedRecord();
|
|
|
+ record.setRedId(red.getRedId());
|
|
|
+ record.setLiveId(red.getLiveId());
|
|
|
+ record.setUserId(red.getUserId());
|
|
|
+ record.setIntegral(integral);
|
|
|
+ record.setCreateTime(new Date());
|
|
|
+ // userRedRecordMapper.insertLiveUserRedRecord(record);
|
|
|
+
|
|
|
+ // 最后更新缓存
|
|
|
+ decreaseRemainingLotsIfPossible(red.getRedId());
|
|
|
+ // WebSocket 通知
|
|
|
+ //String msg = String.format("用户 %d 抢到了红包 %d,获得 %d 芳华币", userId, redId, integral);
|
|
|
+ //WebSocketServer.notifyUsers(msg);
|
|
|
+ redisUtil.hashPut(String.format(LiveKeysConstant.LIVE_HOME_PAGE_CONFIG_RED, red.getLiveId(), red.getRedId()), String.valueOf(red.getUserId()), JSONUtil.toJsonStr(record));
|
|
|
+ return R.ok("恭喜您成功抢到" + integral + "芳华币");
|
|
|
+/* } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
log.error("抢红包异常:" + e.getMessage());
|
|
|
- }
|
|
|
- return R.error("抢红包异常");
|
|
|
+ }*/
|
|
|
+ // return R.error("抢红包异常");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -269,7 +268,55 @@ public class LiveRedConfServiceImpl implements ILiveRedConfService {
|
|
|
|
|
|
@Override
|
|
|
public void finishRedStatusBySetIds(Set<String> range) {
|
|
|
- baseMapper.finishRedStatusBySetIds(range);
|
|
|
+ try {
|
|
|
+ log.info("开始结束红包状态:{}",range);
|
|
|
+ baseMapper.finishRedStatusBySetIds(range);
|
|
|
+ // 插入抽奖记录
|
|
|
+ for (String s : range) {
|
|
|
+ LiveRedConf liveRedConf = baseMapper.selectLiveRedConfByRedId(Long.valueOf(s));
|
|
|
+ // 更新数据库
|
|
|
+ Date now = new Date();
|
|
|
+ Integer remaining = getRemaining(liveRedConf.getRedId());
|
|
|
+ liveRedConf.setTotalSend(liveRedConf.getTotalLots() - remaining);
|
|
|
+ liveRedConf.setRemaining(remaining);
|
|
|
+ liveRedConf.setUpdateTime(now);
|
|
|
+ baseMapper.updateLiveRedConf(liveRedConf);
|
|
|
+ String hashKey = String.format(LiveKeysConstant.LIVE_HOME_PAGE_CONFIG_RED, liveRedConf.getLiveId(), liveRedConf.getRedId());
|
|
|
+ Map<Object, Object> hashEntries = redisUtil.hashEntries(hashKey);
|
|
|
+ List<LiveUserRedRecord> liveUserRedRecords = new ArrayList<>();
|
|
|
+ if (CollUtil.isNotEmpty(hashEntries)) {
|
|
|
+ liveUserRedRecords = hashEntries.values().stream()
|
|
|
+ .map(value -> JSONUtil.toBean(JSONUtil.parseObj(value), LiveUserRedRecord.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ userRedRecordMapper.insertLiveUserRedRecordBatch(liveUserRedRecords);
|
|
|
+ for (LiveUserRedRecord liveUserRedRecord : liveUserRedRecords) {
|
|
|
+ userService.incrIntegral(Collections.singletonList(liveUserRedRecord.getUserId()), liveUserRedRecord.getIntegral());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ redisUtil.delete(hashKey);
|
|
|
+ }
|
|
|
+ log.info("结束红包状态完成");
|
|
|
+ }catch (Exception e){
|
|
|
+ log.info("红包状态结束异常",e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void redStatusUpdate() {
|
|
|
+ LiveRedConf liveRedConf = new LiveRedConf();
|
|
|
+ liveRedConf.setRedStatus(1L);
|
|
|
+ List<LiveRedConf> conf = baseMapper.selectLiveRedConfList(liveRedConf);
|
|
|
+ for (LiveRedConf red : conf) {
|
|
|
+ // 更新数据库
|
|
|
+ Date now = new Date();
|
|
|
+ Integer remaining = getRemaining(red.getRedId());
|
|
|
+ red.setTotalSend(red.getTotalLots() - remaining);
|
|
|
+ red.setRemaining(remaining);
|
|
|
+ red.setUpdateTime(now);
|
|
|
+ baseMapper.updateLiveRedConf(red);
|
|
|
+ log.info("更新红包数据完成 {} {}",red.getRedId(),remaining);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 初始化剩余数量
|