手把手教你實現(xiàn)一個抽獎系統(tǒng)(Java版)
閱讀本文大概需要 5?分鐘。
來自:blog.csdn.net/wang258533488/article/details/78901303
1 概述
獎品 獎品池 抽獎算法 獎品限制 獎品發(fā)放
2 獎品
CREATE?TABLE?`points_luck_draw_prize`?(
??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
??`name`?varchar(50)?DEFAULT?NULL?COMMENT?'獎品名稱',
??`url`?varchar(50)?DEFAULT?NULL?COMMENT?'圖片地址',
??`value`?varchar(20)?DEFAULT?NULL,
??`type`?tinyint(4)?DEFAULT?NULL?COMMENT?'類型1:紅包2:積分3:體驗金4:謝謝惠顧5:自定義',
??`status`?tinyint(4)?DEFAULT?NULL?COMMENT?'狀態(tài)',
??`is_del`?bit(1)?DEFAULT?NULL?COMMENT?'是否刪除',
??`position`?int(5)?DEFAULT?NULL?COMMENT?'位置',
??`phase`?int(10)?DEFAULT?NULL?COMMENT?'期數(shù)',
??`create_time`?datetime?DEFAULT?NULL,
??`update_time`?datetime?DEFAULT?NULL,
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?AUTO_INCREMENT=164?DEFAULT?CHARSET=utf8mb4?COMMENT='獎品表';
CREATE?TABLE?`points_luck_draw_probability`?(
??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
??`points_prize_id`?bigint(20)?DEFAULT?NULL?COMMENT?'獎品ID',
??`points_prize_phase`?int(10)?DEFAULT?NULL?COMMENT?'獎品期數(shù)',
??`probability`?float(4,2)?DEFAULT?NULL?COMMENT?'概率',
??`frozen`?int(11)?DEFAULT?NULL?COMMENT?'商品抽中后的冷凍次數(shù)',
??`prize_day_max_times`?int(11)?DEFAULT?NULL?COMMENT?'該商品平臺每天最多抽中的次數(shù)',
??`user_prize_month_max_times`?int(11)?DEFAULT?NULL?COMMENT?'每位用戶每月最多抽中該商品的次數(shù)',
??`create_time`?datetime?DEFAULT?NULL,
??`update_time`?datetime?DEFAULT?NULL,
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?AUTO_INCREMENT=114?DEFAULT?CHARSET=utf8mb4?COMMENT='抽獎概率限制表';
CREATE?TABLE?`points_luck_draw_record`?(
??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
??`member_id`?bigint(20)?DEFAULT?NULL?COMMENT?'用戶ID',
??`member_mobile`?varchar(11)?DEFAULT?NULL?COMMENT?'中獎用戶手機號',
??`points`?int(11)?DEFAULT?NULL?COMMENT?'消耗積分',
??`prize_id`?bigint(20)?DEFAULT?NULL?COMMENT?'獎品ID',
??`result`?smallint(4)?DEFAULT?NULL?COMMENT?'1:中獎?2:未中獎',
??`month`?varchar(10)?DEFAULT?NULL?COMMENT?'中獎月份',
??`daily`?date?DEFAULT?NULL?COMMENT?'中獎日期(不包括時間)',
??`create_time`?datetime?DEFAULT?NULL,
??`update_time`?datetime?DEFAULT?NULL,
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?AUTO_INCREMENT=3078?DEFAULT?CHARSET=utf8mb4?COMMENT='抽獎記錄表';
3 獎品池
獎品的總池值:所有獎品池值的總和。 每個獎品的池值:算法可以變通,常用的有以下兩種方式 : 獎品的概率*10000(保證是整數(shù))獎品的概率10000獎品的剩余數(shù)量
public?class?PrizePool?implements?Serializable{
????/**
?????*?總池值
?????*/
????private?int?total;
????/**
?????*?池中的獎品
?????*/
????private?List?poolBeanList;
}
public?class?PrizePoolBean?implements?Serializable{
????/**
?????*?數(shù)據(jù)庫中真實獎品的ID
?????*/
????private?Long?id;
????/**
?????*?獎品的開始池值
?????*/
????private?int?begin;
????/**
?????*?獎品的結束池值
?????*/
????private?int?end;
}
/**
?????*?獲取超級大富翁的獎品池
?????*?@param?zillionaireProductMap?超級大富翁獎品map
?????*?@param?flag?true:有現(xiàn)金??false:無現(xiàn)金
?????*?@return
?????*/
????private?PrizePool?getZillionairePrizePool(Map?zillionaireProductMap,?boolean?flag) ?{
????????//總的獎品池值
????????int?total?=?0;
????????List?poolBeanList?=?new?ArrayList<>();
????????for(Entry?entry?:?zillionaireProductMap.entrySet()){
????????????ActivityProduct?product?=?entry.getValue();
????????????//無現(xiàn)金獎品池,過濾掉類型為現(xiàn)金的獎品
????????????if(!flag?&&?product.getCategoryId()?==?ActivityPrizeTypeEnums.XJ.getType()){
????????????????continue;
????????????}
????????????//組裝獎品池獎品
????????????PrizePoolBean?prizePoolBean?=?new?PrizePoolBean();
????????????prizePoolBean.setId(product.getProductDescriptionId());
????????????prizePoolBean.setBengin(total);
????????????total?=?total?+?product.getEarnings().multiply(new?BigDecimal("10000")).intValue();
????????????prizePoolBean.setEnd(total);
????????????poolBeanList.add(prizePoolBean);
????????}
????????PrizePool?prizePool?=?new?PrizePool();
????????prizePool.setTotal(total);
????????prizePool.setPoolBeanList(poolBeanList);
????????return?prizePool;
????}
4 抽獎算法
隨機獎品池總池值以內的整數(shù) 循環(huán)比較獎品池中的所有獎品,隨機數(shù)落到哪個獎品的池區(qū)間即為哪個獎品中獎。
public?static?PrizePoolBean?getPrize(PrizePool?prizePool){
????????//獲取總的獎品池值
????????int?total?=?prizePool.getTotal();
????????//獲取隨機數(shù)
????????Random?rand=new?Random();
????????int?random=rand.nextInt(total);
????????//循環(huán)比較獎品池區(qū)間
????????for(PrizePoolBean?prizePoolBean?:?prizePool.getPoolBeanList()){
????????????if(random?>=?prizePoolBean.getBengin()?&&?random?????????????????return?prizePoolBean;
????????????}
????????}
????????return?null;
????}
5 獎品限制
限制的獎品比較少,通常不多于3個:這種情況我們可以再組裝獎品池的時候就把不符合條件的獎品過濾掉,這樣抽中的獎品都是符合條件的。例如,在上面的超級大富翁抽獎代碼中,我們規(guī)定現(xiàn)金獎品一天只能被抽中5次,那么我們可以根據(jù)判斷條件分別組裝出有現(xiàn)金的獎品和沒有現(xiàn)金的獎品。 限制的獎品比較多,這樣如果要采用第一種方式,就會導致組裝獎品非常繁瑣,性能低下,我們可以采用抽中獎品后校驗抽中的獎品是否符合條件,如果不符合條件則返回一個固定的獎品即可。
6 獎品發(fā)放
/**
?????*?異步分發(fā)獎品
?????*?@param?prizeList
?????*?@throws?Exception
?????*/
????@Async("myAsync")
????@Transactional(rollbackFor?=?Exception.class,?propagation?=?Propagation.REQUIRED)
????public?Future?sendPrize(Long?memberId,?List {?prizeList)
????????try?{
????????????for(PrizeDto?prizeDto?:?prizeList){
????????????????//過濾掉謝謝惠顧的獎品
????????????????if(prizeDto.getType()?==?PointsLuckDrawTypeEnum.XXHG.getType()){
????????????????????continue;
????????????????}
????????????????//根據(jù)獎品類型從工廠中獲取獎品發(fā)放類
????????????????SendPrizeProcessor?sendPrizeProcessor?=?sendPrizeProcessorFactory.getSendPrizeProcessor(
????????????????????PointsLuckDrawTypeEnum.getPointsLuckDrawTypeEnumByType(prizeDto.getType()));
????????????????if(ObjectUtil.isNotNull(sendPrizeProcessor)){
????????????????????//發(fā)放獎品
????????????????????sendPrizeProcessor.send(memberId,?prizeDto);
????????????????}
????????????}
????????????return?new?AsyncResult<>(Boolean.TRUE);
????????}catch?(Exception?e){
????????????//獎品發(fā)放失敗則記錄日志
????????????saveSendPrizeErrorLog(memberId,?prizeList);
????????????LOGGER.error("積分抽獎發(fā)放獎品出現(xiàn)異常",?e);
????????????return?new?AsyncResult<>(Boolean.FALSE);
????????}
????}
@Component
public?class?SendPrizeProcessorFactory?implements?ApplicationContextAware{
????private?ApplicationContext?applicationContext;
????@Override
????public?void?setApplicationContext(ApplicationContext?applicationContext)?throws?BeansException?{
????????this.applicationContext?=?applicationContext;
????}
????public?SendPrizeProcessor?getSendPrizeProcessor(PointsLuckDrawTypeEnum?typeEnum){
????????String?processorName?=?typeEnum.getSendPrizeProcessorName();
????????if(StrUtil.isBlank(processorName)){
????????????return?null;
????????}
????????SendPrizeProcessor?processor?=?applicationContext.getBean(processorName,?SendPrizeProcessor.class);
????????if(ObjectUtil.isNull(processor)){
????????????throw?new?RuntimeException("沒有找到名稱為【"?+?processorName?+?"】的發(fā)送獎品處理器");
????????}
????????return?processor;
????}
}
/**
?*?紅包獎品發(fā)放類
?*/
@Component("sendHbPrizeProcessor")
public?class?SendHbPrizeProcessor?implements?SendPrizeProcessor{
????private?Logger?LOGGER?=?LoggerFactory.getLogger(SendHbPrizeProcessor.class);
????@Resource
????private?CouponService?couponService;
????@Resource
????private?MessageLogService?messageLogService;
????@Override
????public?void?send(Long?memberId,?PrizeDto?prizeDto)?throws?Exception?{
????????//?發(fā)放紅包
????????Coupon?coupon?=?couponService.receiveCoupon(memberId,?Long.parseLong(prizeDto.getValue()));
????????//發(fā)送站內信
????????messageLogService.insertActivityMessageLog(memberId,
????????????"你參與積分抽大獎活動抽中的"?+?coupon.getAmount()?+?"元理財紅包已到賬,謝謝參與",
????????????"積分抽大獎中獎通知");
????????//輸出log日志
????????LOGGER.info(memberId?+?"在積分抽獎中抽中的"?+?prizeDto.getPrizeName()?+?"已經發(fā)放!");
????}
}
推薦閱讀:
2021 互聯(lián)網公司時薪排行榜出爐!微軟、美團很強!
為什么不建議你使用實數(shù)作為 HashMap 的key?
最近面試BAT,整理一份面試資料《Java面試BATJ通關手冊》,覆蓋了Java核心技術、JVM、Java并發(fā)、SSM、微服務、數(shù)據(jù)庫、數(shù)據(jù)結構等等。
朕已閱?

