超级幸运礼物-抽奖

This commit is contained in:
khalil
2024-03-20 17:01:29 +08:00
committed by liaozetao
parent 9103b45b2b
commit 59dd72a80d
13 changed files with 279 additions and 40 deletions

View File

@@ -4,7 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import com.accompany.admin.service.lucky.LuckyGiftRewardAdminService;
import com.accompany.admin.vo.lucky.LuckyGiftRewardAdminVo;
import com.accompany.admin.vo.lucky.LuckyGiftRewardTotalAdminVo;
import com.accompany.business.enums.lucky.LuckyRewardTypeEnum;
import com.accompany.business.enums.lucky.LuckyGiftRewardTypeEnum;
import com.accompany.business.model.Gift;
import com.accompany.business.model.GiftExample;
import com.accompany.business.model.lucky.LuckyGiftReward;
@@ -57,14 +57,12 @@ public class LuckyGiftRewardAdminServiceImpl implements LuckyGiftRewardAdminServ
return totalAdmin;
}
Map<Integer, Gift> giftMap = null;
List<Integer> giftIds = configs.stream().filter(v -> v.getRewardType() == LuckyRewardTypeEnum.GIFT.ordinal() && v.getRewardValue() != null).map(v -> v.getRewardValue().intValue()).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(giftIds)) {
GiftExample example = new GiftExample();
example.createCriteria().andGiftIdIn(giftIds);
List<Gift> gifts = giftMapper.selectByExample(example);
if (CollectionUtil.isNotEmpty(gifts)) {
giftMap = gifts.stream().collect(Collectors.toMap(Gift::getGiftId, Function.identity(), (v1, v2) -> v1));
}
List<Integer> giftIds = configs.stream().filter(v -> v.getRewardType() == LuckyGiftRewardTypeEnum.GIFT.ordinal() && v.getRewardValue() != null).map(v -> v.getRewardValue().intValue()).collect(Collectors.toList());
GiftExample example = new GiftExample();
example.createCriteria().andGiftIdIn(giftIds);
List<Gift> gifts = giftMapper.selectByExample(example);
if (CollectionUtil.isNotEmpty(gifts)) {
giftMap = gifts.stream().collect(Collectors.toMap(Gift::getGiftId, Function.identity(), (v1, v2) -> v1));
}
List<LuckyGiftRewardAdminVo> admins = new ArrayList<>();
for (LuckyGiftReward config : configs) {
@@ -78,12 +76,12 @@ public class LuckyGiftRewardAdminServiceImpl implements LuckyGiftRewardAdminServ
BeanUtils.copyProperties(config, admin);
Long platformValue = 0L;
String rewardName = "";
if (LuckyRewardTypeEnum.NONE.ordinal() == rewardType) {
if (LuckyGiftRewardTypeEnum.NONE.ordinal() == rewardType) {
rewardName = "";
} else if (LuckyRewardTypeEnum.DIAMOND.ordinal() == rewardType) {
} else if (LuckyGiftRewardTypeEnum.DIAMOND.ordinal() == rewardType) {
rewardName = "钻石";
platformValue = rewardValue;
} else if (LuckyRewardTypeEnum.GIFT.ordinal() == rewardType) {
} else if (LuckyGiftRewardTypeEnum.GIFT.ordinal() == rewardType) {
if (giftMap != null && rewardValue != null && giftMap.containsKey(rewardValue.intValue())) {
Gift gift = giftMap.get(rewardValue.intValue());
rewardName = gift.getGiftName();
@@ -124,7 +122,7 @@ public class LuckyGiftRewardAdminServiceImpl implements LuckyGiftRewardAdminServ
LuckyGiftReward defaultConfig = new LuckyGiftReward();
defaultConfig.setRewardValue(0L);
defaultConfig.setIsDefault(Constant.Yes1No0.YES);
defaultConfig.setRewardType(LuckyRewardTypeEnum.NONE.ordinal());
defaultConfig.setRewardType(LuckyGiftRewardTypeEnum.NONE.ordinal());
defaultConfig.setCreateTime(now);
defaultConfig.setUpdateTime(now);
luckyGiftRewardService.save(defaultConfig);

View File

@@ -2303,6 +2303,9 @@ public enum RedisKey {
super_lucky_gift_stock,
super_lucky_gift_draw_lock,
;
public String getKey() {

View File

@@ -169,6 +169,9 @@ public enum BillObjTypeEnum {
INVITE_FISSION_RECHARGE_INVITE_REAWARD((byte) 105, "邀请裂变活动直接邀请储值返点", BillTypeEnum.IN, CurrencyEnum.DIAMOND),
INVITE_FISSION_RECHARGE_INDIRECT_REAWARD((byte) 106, "邀请裂变活动间接邀请储值返点", BillTypeEnum.IN, CurrencyEnum.DIAMOND),
SUPER_LUCKY_GIFT_INCOME_ALLOT((byte) 104, "超级幸运礼物价值分成", BillTypeEnum.IN, CurrencyEnum.DIAMOND),
SUPER_LUCKY_GIFT_DIAMOND((byte) 105, "超级幸运礼物钻石奖励", BillTypeEnum.IN, CurrencyEnum.DIAMOND),
;
BillObjTypeEnum(byte value, String desc, BillTypeEnum type, CurrencyEnum currency) {

View File

@@ -13,6 +13,7 @@ public class SuperLuckyGiftIncomeAllot {
private BigDecimal totalValue;
private BigDecimal receiveTotalIncome;
private Map<Long, BigDecimal> receiveIncomeMap;
private Map<Long, Long> hallOwnerMap;
private BigDecimal hallOwnerTotalIncome;
private Map<Long, BigDecimal> hallOwnerIncomeMap;
private BigDecimal remainValue;

View File

@@ -0,0 +1,15 @@
package com.accompany.business.enums.lucky;
/**
* @author: liaozetao
* @date: 2024/3/14 14:20
* @description:
*/
public enum LuckyGiftRewardTypeEnum {
NONE,
DIAMOND,
GIFT;
}

View File

@@ -2,6 +2,9 @@ package com.accompany.business.mybatismapper.lucky;
import com.accompany.business.model.lucky.LuckyGiftReward;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
/**
* @author: liaozetao
@@ -9,4 +12,5 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @description:
*/
public interface LuckyGiftRewardMapper extends BaseMapper<LuckyGiftReward> {
void updateStockNum(@Param("drawResult") Map<LuckyGiftReward, Integer> drawResult);
}

View File

@@ -1,6 +1,5 @@
package com.accompany.business.service.gift;
import cn.hutool.core.collection.CollectionUtil;
import com.accompany.business.constant.LuckyBagGiftPoolItemTypeEnum;
import com.accompany.business.event.RoomPKEvent;
import com.accompany.business.message.GiftMessage;
@@ -820,7 +819,7 @@ public class GiftSendService extends BaseService {
} else if (gift.getGiftType() == Constant.GiftType.LUCKY_BAG_LINEAR) {
return sendLuckyBagLinearGift(sendUid, Arrays.asList(recvUids), room, gift, giftNum, msg, giftSource, sendType, usersMap);
} else if (gift.getGiftType() == Constant.GiftType.SUPER_LUCKY){
return superLuckyGiftSendService.send(sendUid, Arrays.asList(recvUids), room, gift, giftNum, msg, giftSource, sendType, usersMap);
return sendSuperLuckyGift(sendUid, recvUids, room, gift, giftNum, msg, giftSource, sendType, usersMap);
}
// 下面是赠送普通礼物
@@ -860,6 +859,19 @@ public class GiftSendService extends BaseService {
return null;
}
private BatchSendGiftVo sendSuperLuckyGift(long sendUid, Long[] recvUids, Room room, Gift gift, int giftNum,
String msg, int giftSource, byte sendType, Map<Long, Users> usersMap) {
List<Long> recvUidList = Arrays.asList(recvUids);
superLuckyGiftSendService.draw(sendUid, recvUidList, gift, giftNum);
//todo giftSendRecord
Long giftPrice = gift.getGoldPrice();
Long everyGoldNum = Math.abs(giftPrice * giftNum);
List<RoomGiftValueVo> giftValueVos = updateRoomGiftValue(room, Arrays.asList(recvUids), everyGoldNum, sendUid, true);
return buildBatchSendGiftVo(sendUid, recvUids, gift, giftNum, giftValueVos, usersMap);
}
private BatchLuckyBagGiftVo sendLuckyBagGift(long sendUid, List<Long> recvUids, Room room, Gift luckyBag, int luckyBagNum,
String msg, int giftSource, byte sendType, Map<Long, Users> usersMap) {
Date sendTime = new Date();

View File

@@ -2,6 +2,7 @@ package com.accompany.business.service.gift;
import com.accompany.business.dto.lucky.SuperLuckyGiftConfig;
import com.accompany.business.dto.lucky.SuperLuckyGiftIncomeAllot;
import com.accompany.business.enums.lucky.LuckyGiftRewardTypeEnum;
import com.accompany.business.model.Gift;
import com.accompany.business.model.lucky.LuckyGiftRangeConfig;
import com.accompany.business.model.lucky.LuckyGiftReward;
@@ -9,19 +10,23 @@ import com.accompany.business.service.lucky.LuckyGiftRangeConfigService;
import com.accompany.business.service.lucky.LuckyGiftRewardService;
import com.accompany.business.service.lucky.SuperLuckyIncomeAllotService;
import com.accompany.business.service.lucky.SuperLuckyStockService;
import com.accompany.business.service.purse.UserPurseService;
import com.accompany.business.service.record.BillRecordService;
import com.accompany.business.service.user.UserBackpackService;
import com.accompany.business.vo.BatchSendLuckyBagVo;
import com.accompany.common.constant.Constant;
import com.accompany.common.redis.RedisKey;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.RandomUtil;
import com.accompany.core.enumeration.BillObjTypeEnum;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.model.Room;
import com.accompany.core.model.Users;
import com.accompany.core.service.SysConfService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RAtomicDouble;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -29,6 +34,7 @@ import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@@ -46,11 +52,14 @@ public class SuperLuckyGiftSendService {
private LuckyGiftRangeConfigService rangeConfigService;
@Autowired
private LuckyGiftRewardService rewardService;
@Autowired
private UserPurseService userPurseService;
@Autowired
private BillRecordService billRecordService;
@Autowired
private UserBackpackService userBackpackService;
public Object send(long sendUid, List<Long> receiveList, Room room,
Gift gift, int giftNum,
String msg, int giftSource, byte sendType,
Map<Long, Users> usersMap) {
public void draw(long senderUid, List<Long> receiveList, Gift gift, int giftNum) {
LuckyGiftRangeConfig rangeConfig = rangeConfigService.getRangeConfigByNum(gift.getGiftId(), giftNum);
if (null == rangeConfig){
@@ -62,26 +71,153 @@ public class SuperLuckyGiftSendService {
throw new ServiceException(BusiStatus.ALREADY_NOTEXISTS_CONFIG);
}
Optional<LuckyGiftReward> defaultRewardOptional = rewardList.stream().filter(reward->Constant.Yes1No0.YES == reward.getIsDefault()).findAny();
if (!defaultRewardOptional.isPresent()){
throw new ServiceException(BusiStatus.ALREADY_NOTEXISTS_CONFIG);
}
SuperLuckyGiftConfig config = getConfig();
SuperLuckyGiftIncomeAllot incomeAllot = incomeAllotService.calculate(config, gift, giftNum, receiveList);
log.info("[超级幸运礼物] uid {} 发送 {} 个 {} 超级礼物 给 {} 个用户, 计算出各方收益 {}",
sendUid, giftNum, gift.getGiftName(), receiveList.size(), JSON.toJSONString(incomeAllot));
senderUid, giftNum, gift.getGiftName(), receiveList.size(), JSON.toJSONString(incomeAllot));
BigDecimal stock = stockService.addStock(config, gift.getGiftId(), incomeAllot.getRemainValue());
Map<LuckyGiftReward, Integer> drawResult = null;
if (config.getCompareStockSwitch()){
// todo lock
} else {
Map<LuckyGiftReward, Integer> drawResult = drawIgnoreStock(rewardList, giftNum);
boolean locked = false;
RLock lock = redissonClient.getLock(RedisKey.super_lucky_gift_draw_lock.getKey());
try {
locked = lock.tryLock(5, TimeUnit.SECONDS);
if (!locked){
throw new ServiceException(BusiStatus.SERVER_BUSY);
}
rewardList = rewardService.listRewardByRangeId(rangeConfig.getId());
if (CollectionUtils.isEmpty(rewardList)){
throw new ServiceException(BusiStatus.ALREADY_NOTEXISTS_CONFIG);
}
defaultRewardOptional = rewardList.stream().filter(reward->Constant.Yes1No0.YES == reward.getIsDefault()).findAny();
if (!defaultRewardOptional.isPresent()){
throw new ServiceException(BusiStatus.ALREADY_NOTEXISTS_CONFIG);
}
LuckyGiftReward defaultReward = defaultRewardOptional.get();
BigDecimal stock = stockService.addStock(incomeAllot.getRemainValue());
drawResult = config.getCompareStockSwitch()?
draw(rewardList, defaultReward, stock, giftNum) :
draw(rewardList, defaultReward, null, giftNum);
rewardService.updateStockNum(drawResult, defaultReward);
BigDecimal outputValue = BigDecimal.valueOf(drawResult.entrySet().stream().mapToLong(entry->entry.getKey().getRewardValue() * entry.getValue()).sum());
BigDecimal afterStock = stockService.subStock(outputValue);
// todo log
} catch (InterruptedException e) {
// todo log
throw new RuntimeException(e);
} finally {
if (locked){
lock.unlock();
}
}
// todo 记录
// todo 发礼物
// todo 发收益
return null;
// 道具奖励
sendReward(senderUid, drawResult);
// 分成
incomeAllotService.send(senderUid, incomeAllot.getReceiveIncomeMap());
incomeAllotService.send(senderUid, incomeAllot.getHallOwnerIncomeMap());
}
private void sendReward(long senderUid, Map<LuckyGiftReward, Integer> drawResult) {
for (Map.Entry<LuckyGiftReward, Integer> entry: drawResult.entrySet()){
LuckyGiftReward reward = entry.getKey();
if (LuckyGiftRewardTypeEnum.DIAMOND.ordinal() == reward.getRewardType()){
double diamond = (double) (reward.getRewardValue() * entry.getValue());
userPurseService.addDiamond(senderUid, diamond, 0D, BillObjTypeEnum.SUPER_LUCKY_GIFT_DIAMOND.getDesc());
billRecordService.insertGeneralBillRecord(senderUid, null, null, BillObjTypeEnum.SUPER_LUCKY_GIFT_DIAMOND, diamond);
} else if (LuckyGiftRewardTypeEnum.GIFT.ordinal() == reward.getRewardType()) {
userBackpackService.sendGiftToUser(senderUid, reward.getGiftId(), entry.getValue(), Constant.GiftType.ACTIVITY);
}
}
}
private Map<LuckyGiftReward, Integer> draw(List<LuckyGiftReward> rewardList, LuckyGiftReward defaultReward,
BigDecimal stock, int num) {
Map<LuckyGiftReward, Integer> drawResult = new HashMap<>();
BigDecimal weightSum = BigDecimal.ZERO;
SortedMap<BigDecimal, LuckyGiftReward> weightGiftMap = new TreeMap<>();
for (LuckyGiftReward reward: rewardList){
if (reward.getStockNum() <= 0){
continue;
}
BigDecimal giftValue = BigDecimal.valueOf(reward.getRewardValue());
if (null != stock && stock.compareTo(giftValue) < 0){
continue;
}
BigDecimal giftWeight = BigDecimal.valueOf(reward.getRewardRate());
if (giftWeight.compareTo(BigDecimal.ZERO) <= 0){
continue;
}
weightSum = weightSum.add(giftWeight);
weightGiftMap.put(weightSum, reward);
}
for (int i=0; i < num; i++){
LuckyGiftReward drawReward = null;
while (true){
BigDecimal drawValue = RandomUtil.randomByRange(BigDecimal.ZERO, weightSum);
BigDecimal drawWeight = weightGiftMap.tailMap(drawValue).firstKey();
drawReward = weightGiftMap.get(drawWeight);
log.info("[超级幸运礼物] 第 {} 次抽奖, 抽奖值为 {} 预抽出礼物 {}", i+1, drawValue, drawReward.getId());
BigDecimal drawRewardValue = BigDecimal.valueOf(drawReward.getRewardValue());
boolean noStockValue = null != stock && stock.compareTo(drawRewardValue) < 0;
int rewardNum = drawResult.getOrDefault(drawReward, 0) + 1;
boolean noStockNum = rewardNum > drawReward.getStockNum() && !defaultReward.equals(drawReward);
if (!noStockNum && !noStockNum){
break;
} else if (noStockValue && noStockNum) {
drawReward = defaultReward;
log.info("[超级幸运礼物] 第 {} 次抽奖, 预抽出礼物 {} 但库存数量不足 rewardNum {} stockNum {}",
i+1, drawReward.getId(), rewardNum, drawReward.getStockNum());
break;
}
log.info("[超级幸运礼物] 第 {} 次抽奖, 预抽出礼物 {} 但库存数量或者产出不足 drawRewardValue {} stock {}rewardNum {} stockNum {}",
i+1, drawReward.getId(), drawRewardValue, stock, rewardNum, drawReward.getStockNum());
}
if (null != stock){
stock = stock.subtract(BigDecimal.valueOf(drawReward.getRewardValue()));
}
int rewardNum = drawResult.getOrDefault(drawReward, 0);
drawResult.put(drawReward, ++rewardNum);
}
log.info("[超级幸运礼物] 抽了 {} 次, 抽出礼物数 {}", num, JSON.toJSONString(drawResult));
return drawResult;
}
private Map<LuckyGiftReward, Integer> drawIgnoreStock(List<LuckyGiftReward> rewardList, int num) {
@@ -90,12 +226,17 @@ public class SuperLuckyGiftSendService {
BigDecimal weightSum = BigDecimal.ZERO;
SortedMap<BigDecimal, LuckyGiftReward> weightGiftMap = new TreeMap<>();
for (LuckyGiftReward reward: rewardList){
BigDecimal giftWeight = BigDecimal.valueOf(reward.getRewardRate());
if (giftWeight.compareTo(BigDecimal.ZERO) > 0){
weightSum = weightSum.add(giftWeight);
weightGiftMap.put(weightSum, reward);
if (reward.getStockNum() <= 0){
continue;
}
BigDecimal giftWeight = BigDecimal.valueOf(reward.getRewardRate());
if (giftWeight.compareTo(BigDecimal.ZERO) <= 0){
continue;
}
weightSum = weightSum.add(giftWeight);
weightGiftMap.put(weightSum, reward);
}
for (int i=0; i < num; i++){

View File

@@ -4,6 +4,7 @@ import com.accompany.business.model.lucky.LuckyGiftReward;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
import java.util.Map;
/**
* @author: liaozetao
@@ -12,4 +13,6 @@ import java.util.List;
*/
public interface LuckyGiftRewardService extends IService<LuckyGiftReward> {
List<LuckyGiftReward> listRewardByRangeId(Long id);
void updateStockNum(Map<LuckyGiftReward, Integer> drawResult, LuckyGiftReward defaultReward);
}

View File

@@ -6,6 +6,9 @@ import com.accompany.business.model.Gift;
import com.accompany.business.model.Hall;
import com.accompany.business.model.HallMember;
import com.accompany.business.service.hall.HallService;
import com.accompany.business.service.purse.UserPurseService;
import com.accompany.business.service.record.BillRecordService;
import com.accompany.core.enumeration.BillObjTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -23,6 +26,10 @@ public class SuperLuckyIncomeAllotService {
@Autowired
private HallService hallService;
@Autowired
private UserPurseService userPurseService;
@Autowired
private BillRecordService billRecordService;
public SuperLuckyGiftIncomeAllot calculate(SuperLuckyGiftConfig config, Gift gift, int giftNum, List<Long> receiverUids) {
// 单价
@@ -35,25 +42,41 @@ public class SuperLuckyIncomeAllotService {
// 大头收益单价
BigDecimal hallOwnerIncome = giftValue.multiply(config.getHallOwnerRatio());
Map<Long, Long> hallMap = new HashMap<>();
Map<Long, Long> receiverHallOwnerMap = new HashMap<>();
Map<Long, BigDecimal> hallOwnerIncomeMap = new HashMap<>();
List<HallMember> hallMemberList = hallService.listHallMember(receiverUids);
if (!CollectionUtils.isEmpty(hallMemberList)){
for (HallMember hm: hallMemberList){
hallMap.put(hm.getUid(), hm.getHallId());
}
Map<Long, Long> hallOwnerUidMap = new HashMap<>();
Map<Long, Long> hallNumGroupByMap = hallMemberList.stream().collect(Collectors.groupingBy(HallMember::getHallId, Collectors.counting()));
List<Long> hallIdList = hallMemberList.stream().map(HallMember::getHallId).distinct().collect(Collectors.toList());
List<Hall> hallList = hallService.listHallByIds(hallIdList);
for (Hall hall: hallList){
hallOwnerUidMap.put(hall.getId(), hall.getOwnerUid());
Long num = hallNumGroupByMap.getOrDefault(hall.getId(), 0L);
BigDecimal thisHallOwnerIncome = hallOwnerIncome.multiply(BigDecimal.valueOf(num));
hallOwnerIncomeMap.put(hall.getOwnerUid(), thisHallOwnerIncome);
}
for (Map.Entry<Long, Long> entry: hallMap.entrySet()){
Long hallOwnerUid = hallOwnerUidMap.get(entry.getValue());
if (null == hallOwnerUid){
continue;
}
receiverHallOwnerMap.put(entry.getKey(), hallOwnerUid);
}
}
BigDecimal hallOwnerTotalIncome = hallOwnerIncomeMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
Map<Long, BigDecimal> receiverIncomeMap = new HashMap<>();
for (Long receiver: receiverUids){
receiverIncomeMap.put(receiver, receiverIncome);
@@ -63,7 +86,16 @@ public class SuperLuckyIncomeAllotService {
BigDecimal remainValue = totalValue.subtract(receiverTotalIncome).subtract(hallOwnerTotalIncome);
return new SuperLuckyGiftIncomeAllot(totalValue, receiverTotalIncome, receiverIncomeMap, hallOwnerTotalIncome, hallOwnerIncomeMap, remainValue);
return new SuperLuckyGiftIncomeAllot(totalValue, receiverTotalIncome, receiverIncomeMap, receiverHallOwnerMap, hallOwnerTotalIncome, hallOwnerIncomeMap, remainValue);
}
public void send(long senderUid, Map<Long, BigDecimal> incomeMap) {
for (Map.Entry<Long, BigDecimal> entry : incomeMap.entrySet()){
long receiverUid = entry.getKey();
double income = entry.getValue().doubleValue();
userPurseService.addDiamond(receiverUid, income, 0D, BillObjTypeEnum.SUPER_LUCKY_GIFT_INCOME_ALLOT.getDesc());
billRecordService.insertGeneralBillRecord(receiverUid, senderUid, null, BillObjTypeEnum.SUPER_LUCKY_GIFT_INCOME_ALLOT, income);
}
}
}

View File

@@ -3,6 +3,7 @@ package com.accompany.business.service.lucky;
import com.accompany.business.dto.lucky.SuperLuckyGiftConfig;
import com.accompany.common.redis.RedisKey;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RAtomicDouble;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.InitializingBean;
@@ -18,14 +19,18 @@ public class SuperLuckyStockService implements InitializingBean {
@Autowired
private RedissonClient redissonClient;
private RMap<Integer, BigDecimal> stockMap;
private RAtomicDouble stockCache;
public BigDecimal addStock(SuperLuckyGiftConfig config, Integer giftId, BigDecimal remainValue) {
return stockMap.addAndGet(giftId, remainValue);
public BigDecimal addStock(BigDecimal value) {
return BigDecimal.valueOf(stockCache.addAndGet(value.doubleValue()));
}
public BigDecimal subStock(BigDecimal value) {
return BigDecimal.valueOf(stockCache.addAndGet(value.negate().doubleValue()));
}
@Override
public void afterPropertiesSet() throws Exception {
stockMap = redissonClient.getMap(RedisKey.super_lucky_gift_stock.getKey());
stockCache = redissonClient.getAtomicDouble(RedisKey.super_lucky_gift_stock.getKey());
}
}

View File

@@ -6,8 +6,11 @@ import com.accompany.business.service.lucky.LuckyGiftRewardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: liaozetao
@@ -21,4 +24,17 @@ public class LuckyGiftRewardServiceImpl extends ServiceImpl<LuckyGiftRewardMappe
public List<LuckyGiftReward> listRewardByRangeId(Long rangeId) {
return lambdaQuery().eq(LuckyGiftReward::getRangeId, rangeId).list();
}
@Override
public void updateStockNum(Map<LuckyGiftReward, Integer> drawResult, LuckyGiftReward defaultReward) {
Map<LuckyGiftReward, Integer> drawResultWithoutDefault = drawResult.entrySet().stream()
.filter(entry -> !entry.getKey().equals(defaultReward))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (CollectionUtils.isEmpty(drawResult)){
return;
}
this.baseMapper.updateStockNum(drawResultWithoutDefault);
}
}

View File

@@ -2,4 +2,10 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.accompany.business.mybatismapper.lucky.LuckyGiftRewardMapper">
<update id="updateStockNum">
<foreach collection="param.entrySet()" index="item" item="num" separator=";">
update lucky_gift_reward set stock_num = stock_num - #{num} where id = #{item.id}
</foreach>
</update>
</mapper>