|
@@ -4,6 +4,7 @@ import java.util.*;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -143,7 +144,7 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public boolean claimRedPacket(RedPO red) {
|
|
|
+ public R claimRedPacket(RedPO red) {
|
|
|
String lockKey = REDPACKET_REMAININGLOTS_KEY + red.getRedId();
|
|
|
List<String> keyList = new ArrayList<>();
|
|
|
keyList.add(lockKey);
|
|
@@ -151,23 +152,23 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
|
|
|
try {
|
|
|
Integer remaining = getRemaining(red.getRedId());
|
|
|
if (remaining <= 0) {
|
|
|
- return false;
|
|
|
+ return R.error("手慢了,红包已被抢完~");
|
|
|
}
|
|
|
//获取红包锁
|
|
|
if (!tryLock(lockKey, red.getUserId().toString(), 5)) {
|
|
|
- return false;
|
|
|
+ return R.error("您已经领取过红包了!");
|
|
|
}
|
|
|
|
|
|
LiveRedConf conf = baseMapper.selectLiveRedConfByRedId(red.getRedId());
|
|
|
if (conf == null || conf.getRedStatus() != 1) {
|
|
|
- return false;
|
|
|
+ return R.error("手慢了,红包已被抢完~");
|
|
|
}
|
|
|
//redis剩余红包数
|
|
|
// 平均分 暂时不适用redis 记录红包数
|
|
|
|
|
|
Long integral = calculateIntegralAverage(conf);
|
|
|
if (0L == integral) {
|
|
|
- return false;
|
|
|
+ return R.error("手慢了,红包已被抢完~");
|
|
|
}
|
|
|
Date now = new Date();
|
|
|
//剩余金额
|
|
@@ -178,12 +179,13 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
|
|
|
conf.setRemaining(Math.toIntExact(conf.getTotalLots() - conf.getTotalSend()));
|
|
|
conf.setUpdateTime(now);
|
|
|
|
|
|
+ // todo yhq 更新用户芳华币数量
|
|
|
|
|
|
|
|
|
// 更新数据库和缓存
|
|
|
baseMapper.updateLiveRedConf(conf);
|
|
|
decreaseRemainingLotsIfPossible(red.getRedId());
|
|
|
- //decreaseRemainingNumIfPossible(red.getRedId(),integral);
|
|
|
+ decreaseRemainingNumIfPossible(red.getRedId(),integral);
|
|
|
|
|
|
// 记录用户红包
|
|
|
LiveUserRedRecord record = new LiveUserRedRecord();
|
|
@@ -198,7 +200,7 @@ public class LiveRedConfServiceImpl extends ServiceImpl<LiveRedConfMapper, LiveR
|
|
|
//String msg = String.format("用户 %d 抢到了红包 %d,获得 %d 芳华币", userId, redId, integral);
|
|
|
//WebSocketServer.notifyUsers(msg);
|
|
|
|
|
|
- return true;
|
|
|
+ return R.ok();
|
|
|
} finally {
|
|
|
releaseLock(keyList, red.getUserId().toString());
|
|
|
}
|