java編寫抽獎程序 抽獎程序源代碼



文章插圖
java編寫抽獎程序 抽獎程序源代碼

文章插圖
概述
項目開發中經常會有抽獎這樣的營銷活動的需求,例如:積分大轉盤、刮刮樂、老虎機等等多種形式,其實后臺的實現方法是一樣的,本文介紹一種常用的抽獎實現方法 。
整個抽獎過程包括以下幾個方面:
獎品獎品池抽獎算法獎品限制獎品發放獎品獎品包括獎品、獎品概率和限制、獎品記錄 。
【java編寫抽獎程序 抽獎程序源代碼】獎品表:
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 '狀態',`is_del` bit(1) DEFAULT NULL COMMENT '是否刪除',`position` int(5) DEFAULT NULL COMMENT '位置',`phase` int(10) DEFAULT NULL COMMENT '期數',`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 '獎品期數',`probability` float(4,2) DEFAULT NULL COMMENT '概率',`frozen` int(11) DEFAULT NULL COMMENT '商品抽中后的冷凍次數',`prize_day_max_times` int(11) DEFAULT NULL COMMENT '該商品平臺每天最多抽中的次數',`user_prize_month_max_times` int(11) DEFAULT NULL COMMENT '每位用戶每月最多抽中該商品的次數',`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='抽獎記錄表';獎品池
獎品池是根據獎品的概率和限制組裝成的抽獎用的池子 。主要包括獎品的總池值和每個獎品所占的池值(分為開始值和結束值)兩個維度 。最新面試題整理好了,點擊Java面試庫小程序在線刷題 。
獎品的總池值:所有獎品池值的總和 。每個獎品的池值:算法可以變通,常用的有以下兩種方式 :獎品的概率*10000(保證是整數)獎品的概率10000獎品的剩余數量
獎品池bean:
public class PrizePool implements Serializable{/*** 總池值*/private int total;/*** 池中的獎品*/private List<PrizePoolBean> poolBeanList;}池中的獎品bean:
public class PrizePoolBean implements Serializable{/*** 數據庫中真實獎品的ID*/private Long id;/*** 獎品的開始池值*/private int begin;/*** 獎品的結束池值*/private int end;}獎品池的組裝代碼:
/*** 獲取超級大富翁的獎品池* @param zillionaireProductMap 超級大富翁獎品map* @param flag true:有現金false:無現金* @return*/private PrizePool getZillionairePrizePool(Map<Long, ActivityProduct> zillionaireProductMap, boolean flag) {//總的獎品池值int total = 0;List<PrizePoolBean> poolBeanList = new ArrayList<>();for(Entry<Long, ActivityProduct> entry : zillionaireProductMap.entrySet()){ActivityProduct product = entry.getValue();//無現金獎品池,過濾掉類型為現金的獎品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;}抽獎算法
整個抽獎算法為:
隨機獎品池總池值以內的整數循環比較獎品池中的所有獎品,隨機數落到哪個獎品的池區間即為哪個獎品中獎 。
推薦一個 Spring Boot 基礎教程及實戰示例:
https://github.com/javastacks/spring-boot-best-practice