Commit 4911674e by 吕明尚

修改抽奖逻辑

parent 1e1d4dc8
package share.system.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
......@@ -24,6 +26,7 @@ public class LotteryRecordsLog extends BaseEntity {
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
......
......@@ -60,4 +60,6 @@ public interface PrizeMapper extends BaseMapper<Prize> {
* @return 结果
*/
public int deletePrizeByIds(Long[] ids);
void updateCurrentNum(Prize prize);
}
......@@ -60,4 +60,6 @@ public interface PrizeService extends IService<Prize> {
* @return 结果
*/
public int deletePrizeById(Long id);
void updateCurrentNum(Prize prize);
}
package share.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import share.system.domain.Prize;
import share.system.domain.WheelGame;
import java.util.List;
......@@ -60,5 +61,5 @@ public interface WheelGameService extends IService<WheelGame> {
*/
public int deleteWheelGameById(Long id);
String draw(WheelGame wheelGame);
Prize draw(WheelGame wheelGame);
}
......@@ -596,9 +596,18 @@ public class ConsumerWalletServiceImpl extends ServiceImpl<ConsumerWalletMapper,
giftAmountLog.setOperationType(YesNoEnum.yes.getIndex());
giftAmountLog.setOperationTime(new Date());
giftAmountLog.setCreateTime(new Date());
BalanceLog balanceLog = new BalanceLog();
balanceLog.setConsumerId(consumerWallet.getConsumerId());
balanceLog.setCurrentBalance(oldConsumerWallet.getBalance());
balanceLog.setVariableAmount(consumerWallet.getGiftAmount());
balanceLog.setOperationType(YesNoEnum.yes.getIndex());
balanceLog.setOperationTime(new Date());
balanceLog.setCreateTime(new Date());
oldConsumerWallet.setGiftAmount(oldConsumerWallet.getGiftAmount().add(consumerWallet.getGiftAmount()));
oldConsumerWallet.setBalance(oldConsumerWallet.getRechargeAmount().add(oldConsumerWallet.getGiftAmount()));
giftAmountLogService.save(giftAmountLog);
balanceLogService.save(balanceLog);
consumerWalletService.updateById(oldConsumerWallet);
return 1;
} else {
......@@ -615,6 +624,14 @@ public class ConsumerWalletServiceImpl extends ServiceImpl<ConsumerWalletMapper,
giftAmountLog.setOperationTime(new Date());
giftAmountLog.setCreateTime(new Date());
BalanceLog balanceLog = new BalanceLog();
balanceLog.setConsumerId(consumerWallet.getConsumerId());
balanceLog.setCurrentBalance(BigDecimal.ZERO);
balanceLog.setVariableAmount(consumerWallet.getGiftAmount());
balanceLog.setOperationType(YesNoEnum.yes.getIndex());
balanceLog.setOperationTime(new Date());
balanceLog.setCreateTime(new Date());
newConsumerWallet.setBalance(consumerWallet.getGiftAmount());
newConsumerWallet.setGiftAmount(consumerWallet.getGiftAmount());
newConsumerWallet.setRemainingDuration(BigDecimal.ZERO);
......@@ -634,6 +651,7 @@ public class ConsumerWalletServiceImpl extends ServiceImpl<ConsumerWalletMapper,
newConsumerMember.setCreateTime(new Date());
consumerWalletService.save(newConsumerWallet);
giftAmountLogService.save(giftAmountLog);
balanceLogService.save(balanceLog);
consumerMemberService.save(newConsumerMember);
return 1;
}
......
......@@ -89,4 +89,9 @@ public class PrizeServiceImpl extends ServiceImpl<PrizeMapper, Prize> implements
public int deletePrizeById(Long id) {
return prizeMapper.deletePrizeById(id);
}
@Override
public void updateCurrentNum(Prize prize) {
prizeMapper.updateCurrentNum(prize);
}
}
package share.system.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
......@@ -16,7 +17,6 @@ import share.common.enums.YesNoEnum;
import share.common.exception.base.BaseException;
import share.common.utils.DateUtils;
import share.system.domain.*;
import share.system.domain.vo.FrontTokenComponent;
import share.system.mapper.WheelGameMapper;
import share.system.service.*;
......@@ -24,7 +24,6 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;
/**
......@@ -48,6 +47,9 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
private LotteryRecordsLogService lotteryRecordsLogService;
@Autowired
private ConsumerWalletService consumerWalletService;
@Autowired
private SConsumerService sConsumerService;
/**
* 查询转盘游戏
......@@ -118,12 +120,12 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
}
@Override
public String draw(WheelGame wheelGame) {
public Prize draw(WheelGame wheelGame) {
if (wheelGame == null || wheelGame.getId() == null) {
throw new IllegalArgumentException("无效的游戏对象或游戏ID为空");
}
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
SConsumer user = sConsumerService.getInfo();
if (ObjectUtil.isNull(user)) {
throw new BaseException("您的登录已过期,请先登录");
}
......@@ -156,6 +158,10 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
log.setHitPrize(prize.getPrizeName() + ":" + give.getName());
log.setSendMsg(PrizeTypeEnum.COUPON.getName() + ":" + give.getName() + "以发放");
lotteryRecordsLogService.save(log);
prize.setCurrentNum(prize.getCurrentNum() + YesNoEnum.yes.getIndex());
prizeService.updateCurrentNum(prize);
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
break;
case GOODS:
log.setIsHit(YesNoEnum.yes.getIndex());
......@@ -163,6 +169,10 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
log.setHitPrize(PrizeTypeEnum.GOODS.getName() + ":" + prize.getPrizeName());
log.setSendMsg(PrizeTypeEnum.GOODS.getName() + ":" + prize.getPrizeName() + "以发放");
lotteryRecordsLogService.save(log);
prize.setCurrentNum(prize.getCurrentNum() + YesNoEnum.yes.getIndex());
prizeService.updateCurrentNum(prize);
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
break;
case INTEGRAL:
ConsumerWallet consumerWallet = new ConsumerWallet();
......@@ -172,8 +182,12 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
log.setIsHit(YesNoEnum.yes.getIndex());
log.setIsSend(YesNoEnum.yes.getIndex());
log.setHitPrize(PrizeTypeEnum.INTEGRAL.getName());
log.setSendMsg(prize.getPrizeValue() + ":" + PrizeTypeEnum.INTEGRAL.getName() + "以发放");
log.setSendMsg(PrizeTypeEnum.INTEGRAL.getName() + ":" + prize.getPrizeValue() + "以发放");
lotteryRecordsLogService.save(log);
prize.setCurrentNum(prize.getCurrentNum() + YesNoEnum.yes.getIndex());
prizeService.updateCurrentNum(prize);
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
break;
case TIME:
ConsumerWallet durationrWallet = new ConsumerWallet();
......@@ -183,8 +197,12 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
log.setIsHit(YesNoEnum.yes.getIndex());
log.setIsSend(YesNoEnum.yes.getIndex());
log.setHitPrize(PrizeTypeEnum.TIME.getName());
log.setSendMsg(prize.getPrizeValue() + ":" + PrizeTypeEnum.TIME.getName() + "以发放");
log.setSendMsg(PrizeTypeEnum.TIME.getName() + ":" + prize.getPrizeValue() + "以发放");
lotteryRecordsLogService.save(log);
prize.setCurrentNum(prize.getCurrentNum() + YesNoEnum.yes.getIndex());
prizeService.updateCurrentNum(prize);
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
break;
case MONEY:
ConsumerWallet giveAmountWallet = new ConsumerWallet();
......@@ -194,8 +212,12 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
log.setIsHit(YesNoEnum.yes.getIndex());
log.setIsSend(YesNoEnum.yes.getIndex());
log.setHitPrize(PrizeTypeEnum.MONEY.getName());
log.setSendMsg(prize.getPrizeValue() + ":" + PrizeTypeEnum.MONEY.getName() + "以发放");
log.setSendMsg(PrizeTypeEnum.MONEY.getName() + ":" + prize.getPrizeValue() + "以发放");
lotteryRecordsLogService.save(log);
prize.setCurrentNum(prize.getCurrentNum() + YesNoEnum.yes.getIndex());
prizeService.updateCurrentNum(prize);
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
break;
case THANK:
log.setIsHit(YesNoEnum.no.getIndex());
......@@ -203,6 +225,10 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
log.setHitPrize("谢谢参与");
log.setSendMsg("谢谢参与");
lotteryRecordsLogService.save(log);
prize.setCurrentNum(prize.getCurrentNum() + YesNoEnum.yes.getIndex());
prizeService.updateCurrentNum(prize);
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
break;
default:
throw new BaseException("奖品类型异常");
......@@ -215,14 +241,16 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
throw new RuntimeException(e);
}
} else {
user.setPrizeDrawNumbr(user.getPrizeDrawNumbr() - YesNoEnum.yes.getIndex());
sConsumerService.updateById(user);
log.setIsHit(YesNoEnum.no.getIndex());
log.setIsSend(YesNoEnum.no.getIndex());
log.setHitPrize("谢谢参与");
log.setSendMsg("谢谢参与");
lotteryRecordsLogService.save(log);
return "谢谢参与";
return new Prize();
}
return null;
return prize;
}
private Prize luckDraw(List<Prize> prizeList, Long gameId) {
......@@ -242,10 +270,9 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
newList.add(newEntity);
}
// 生成一个随机数
Random random = new Random();
Double userSelect = random.nextDouble() * 10000;
for (Prize prize : prizeList) {
// 生成一个0到sum随机数
double userSelect = RandomUtil.randomDouble(0, sum);
for (Prize prize : newList) {
// 随机数小于中奖几率,则中奖
if (userSelect < prize.getRatio()) {
// 最大中奖数(0:代表不限制次数)
......@@ -260,7 +287,7 @@ public class WheelGameServiceImpl extends ServiceImpl<WheelGameMapper, WheelGame
}
}
// 谢谢参与
List<Prize> prize = prizeList.stream().filter(item -> item.getPrizeType() == 4).collect(Collectors.toList());
List<Prize> prize = prizeList.stream().filter(item -> item.getPrizeType() == PrizeTypeEnum.THANK.getCode()).collect(Collectors.toList());
if (prize.size() > 0) {
return prize.get(0);
}
......
......@@ -130,6 +130,11 @@
</trim>
where id = #{id}
</update>
<update id="updateCurrentNum">
update s_prize
set current_num = #{currentNum}
where id = #{id}
</update>
<delete id="deletePrizeById" parameterType="Long">
delete
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment