LotteryDraw.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Core\Request;
  4. use KIF\Page\Page;
  5. use KIF\Verify;
  6. use Cas\Dao\LotteryEvents;
  7. use KIF\Core\Config;
  8. use KIF\Image;
  9. use Cas\Dao\LotteryPrize;
  10. use KIF\Cache\Memcached;
  11. use KIF\Dao\SqlHelper;
  12. use Cas\Dao\LotteryPrizeVirtualData;
  13. use Cas\Dao\LotteryData;
  14. use Cas\Module\LotteryLog;
  15. use Cas\Dao\LotteryEventsLog as DLog;
  16. /**
  17. *
  18. *
  19. * 抽奖 模型
  20. *
  21. * @author lihuanchun
  22. */
  23. class LotteryDraw {
  24. private $objMemcached;
  25. private $objRank;
  26. private $uid;
  27. private $eventsId;
  28. private $objDLotteryEvents;
  29. private $objMlottery;
  30. private $objDLotteryPrizeVirtualData;
  31. private $objMLotteryEventsLimitations;
  32. private $objDLotteryData;
  33. private $objMLotteryLog;
  34. private $version;
  35. private $thisTime;
  36. public function __construct($uid=null , $eventsId=null ) {
  37. $this->objMemcached = new Memcached();
  38. $this->objRank = new Rank();
  39. $this->uid = $uid;
  40. $this->eventsId = $eventsId;
  41. $this->objDLotteryEvents = new LotteryEvents();
  42. $this->objDLotteryPrize = new LotteryPrize();
  43. $this->objDLotteryPrizeVirtualData = new LotteryPrizeVirtualData();
  44. $this->objDLotteryData = new LotteryData();
  45. $this->objMlottery = new Lottery();
  46. $this->objMLotteryEventsLimitations = new LotteryEventsLimitations();
  47. $this->objMLotteryLog = new LotteryLog($uid,$eventsId);
  48. $this->version = '0.1';
  49. $this->thisTime = time();
  50. }
  51. public function verifyPrize(){
  52. $eventsAndPrize = $this->objMlottery->getOneLotteryEventsAndPrize($this->eventsId);
  53. # 活动是否开始
  54. if($this->thisTime < $eventsAndPrize['events']['begin_time'] || $this->thisTime > $eventsAndPrize['events']['end_time']){
  55. return false;
  56. }
  57. # 活动限制
  58. if(!$this->objMLotteryEventsLimitations->ckEventRestrictions($this->uid,Request::ip(), $this->eventsId)){
  59. return false;
  60. }
  61. # 排列顺序
  62. $newPrize = array();
  63. foreach($eventsAndPrize['prize'] as $prize){
  64. $prize_id = $prize['id'];
  65. $begin_hour = $prize['begin_hour'];
  66. $end_hour = $prize['end_hour'];
  67. $this_hour = date("H",$this->thisTime);
  68. # 获奖是否在此抽奖时间段
  69. if(!($this_hour >=$begin_hour && $this_hour<$end_hour)){
  70. continue;
  71. }
  72. # 验证当前奖项是否显示
  73. if($prize['display']==LotteryPrize::PRIZE_DISPLAY_HIDE){
  74. continue;
  75. }
  76. # 验证当前将奖项 库存数量
  77. if($prize['prize_num']-$prize['prize_winning_num'] <= 0){
  78. continue;
  79. }
  80. # 验证是否在 黑 & 白 名单中
  81. if($this->ckBlackAndWhiteList($prize_id) == false){
  82. continue;
  83. }
  84. # 获取下一次的中奖时间 【开始抽奖逻辑】
  85. $nextWinTime = $this->getNextWinTime($prize_id);
  86. $newPrize[$nextWinTime] = $prize_id;
  87. }
  88. # 中奖顺序
  89. ksort($newPrize,SORT_NUMERIC);
  90. foreach($newPrize as $key => $this_prize_id){
  91. $newPrize[$key] = $eventsAndPrize['prize'][$this_prize_id];
  92. }
  93. foreach($newPrize as $prize){
  94. $prize_id = $prize['id'];
  95. $begin_hour = $prize['begin_hour'];
  96. $end_hour = $prize['end_hour'];
  97. $this_hour = date("H",$this->thisTime);
  98. # 获奖是否在此抽奖时间段
  99. if(!($this_hour >=$begin_hour && $this_hour<$end_hour)){
  100. continue;
  101. }
  102. # 验证当前奖项是否显示
  103. if($prize['display']==LotteryPrize::PRIZE_DISPLAY_HIDE){
  104. continue;
  105. }
  106. # 验证当前将奖项 库存数量
  107. if($prize['prize_num']-$prize['prize_winning_num'] <= 0){
  108. continue;
  109. }
  110. # 验证是否在 黑 & 白 名单中
  111. if($this->ckBlackAndWhiteList($prize_id) == false){
  112. continue;
  113. }
  114. # 获取下一次的中奖时间 【开始抽奖逻辑】
  115. $nextWinTime = $this->getNextWinTime($prize_id);
  116. if (!$this->lock($prize_id)) return false;
  117. if (!$nextWinTime) {
  118. $isWin = false;
  119. $this->setNextWinTime($prize_id);
  120. } elseif ($nextWinTime <= $this->thisTime) {
  121. $isWin = true;
  122. # 发奖
  123. $lotteryDataId = $this->awards($prize_id,$nextWinTime);
  124. $this->setNextWinTime($prize_id);
  125. $this->unlock($prize_id);
  126. return $lotteryDataId;
  127. break;
  128. }
  129. # 检查是否百分之百中奖用户名单中
  130. if($this->ckHundredPercentList($prize_id)){
  131. $isWin = true;
  132. # 发奖
  133. $lotteryDataId = $this->awards($prize_id,$this->thisTime);
  134. $this->setNextWinTime($prize_id);
  135. $this->unlock($prize_id);
  136. return $lotteryDataId;
  137. break;
  138. }
  139. $this->unlock($prize_id);
  140. continue;
  141. }
  142. return false;
  143. }
  144. /**
  145. * 清除下次中奖时间 并且动态计算下次中奖时间
  146. */
  147. public function clear_draw_next_time($prize_id) {
  148. $memkey = $this->getCacheKeyOfTimeBucketWinTimes($prize_id);
  149. $this->objMemcached->delete($memkey);
  150. $memkey = $this->getCacheKeyOfNextWinTime($prize_id);
  151. $this->objMemcached->delete($memkey);
  152. $this->setNextWinTime($prize_id);
  153. }
  154. /**
  155. * 检查是否是百分百中奖名单中的用户
  156. */
  157. public function ckHundredPercentList($prize_id){
  158. // 查看百分百中奖名单中的用户
  159. $hundred_percent_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_hundred_percent_list');
  160. if(!empty($hundred_percent_list)){
  161. if(isset($hundred_percent_list[$this->uid])){
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * 检查白名单 & 黑名单
  169. */
  170. public function ckBlackAndWhiteList($prize_id){
  171. // 查看黑名单
  172. $black_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_black_list');
  173. if(!empty($black_list)){
  174. if(isset($black_list[$this->uid])){
  175. return false;
  176. }
  177. }
  178. // 查看白名单
  179. $white_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_white_list');
  180. if(!empty($white_list)){
  181. if(!isset($white_list[$this->uid])){
  182. return false;
  183. }
  184. }
  185. return true;
  186. }
  187. /**
  188. * 发奖逻辑
  189. */
  190. public function awards($prize_id,$winTime){
  191. $eventsAndPrize = $this->objMlottery->getOneLotteryEventsAndPrize($this->eventsId);
  192. $info = array(
  193. 'events_id' => $this->eventsId,
  194. 'prize_id' => $prize_id,
  195. 'uid' => $this->uid,
  196. 'winners_time' => $winTime,
  197. 'winning_time' => $this->thisTime,
  198. 'run_time_data' => $eventsAndPrize,
  199. );
  200. $prize = $eventsAndPrize['prize'][$prize_id];
  201. if($prize['type'] == LotteryPrize::TYPE_ENTITY){
  202. $virtual_condition = "prize_id = {$prize_id} AND lottery_data_id = 0 " ;
  203. $LotteryPrizeVirtualDataID = $this->objDLotteryPrizeVirtualData->findIdsBy ( $virtual_condition, '0,1' ,'id desc');
  204. $LotteryPrizeVirtualDataID = array_pop($LotteryPrizeVirtualDataID);
  205. $prizeVirtualData = $this->objDLotteryPrizeVirtualData->get($LotteryPrizeVirtualDataID);
  206. $info['virtual_data'] = $prizeVirtualData['data'];
  207. $info['create_time'] = $this->thisTime;
  208. }
  209. $lotteryDataId = $this->objDLotteryData -> add($info);
  210. # 判断是否是 虚拟类
  211. if($prize['type'] == LotteryPrize::TYPE_ENTITY){
  212. $virtualInfo = array(
  213. 'uid' => $this->uid,
  214. 'lottery_data_id' => $lotteryDataId,
  215. 'events_id' => $this->eventsId,
  216. );
  217. $this->objDLotteryPrizeVirtualData->modify($virtualInfo,array('id' => $LotteryPrizeVirtualDataID));
  218. }
  219. $this->objDLotteryPrize -> upPrizeWinningNum($prize_id, 1);
  220. # 用户记录
  221. $this->objMLotteryEventsLimitations->setUserWinningNum($this->uid, $this->eventsId);
  222. # 记录一条获奖日志
  223. $other = $info;
  224. $this->objMLotteryLog ->addLog(DLog::LOG_TYPE_RECEIVE, $other);
  225. return $lotteryDataId;
  226. }
  227. /**
  228. * 开启抽奖锁
  229. * @return boolean | null
  230. */
  231. public function lock($prize_id) {
  232. return $this->objMemcached->add($this->getCacheKeyOfLock($prize_id), true, 10);
  233. }
  234. /**
  235. * 关闭抽奖锁
  236. * @return boolean | null
  237. */
  238. public function unlock($prize_id) {
  239. return $this->objMemcached->delete($this->getCacheKeyOfLock($prize_id));
  240. }
  241. /**
  242. * 获取抽奖锁 cache key
  243. * @return string
  244. */
  245. public function getCacheKeyOfLock($prize_id) {
  246. return "lottery_draw_lock_{$prize_id}";
  247. }
  248. /**
  249. * 设置当天下次中奖时间
  250. * @return boolean | null
  251. */
  252. public function setNextWinTime($prize_id) {
  253. $hasWinTotal = $this->objDLotteryData->getTodayHasWinTotal($prize_id);
  254. $timeBucketWinTimes = $this->getTimeBucketWinTimes($prize_id);
  255. $next_win_time = $this->objRank->winnerCal($timeBucketWinTimes, $hasWinTotal);
  256. return $this->objMemcached->set($this->getCacheKeyOfNextWinTime($prize_id), $next_win_time, 60*60*24);
  257. }
  258. /**
  259. * 获取当天每个时间断中奖次数
  260. * @return array
  261. */
  262. public function getTimeBucketWinTimes($prize_id) {
  263. $memkey = $this->getCacheKeyOfTimeBucketWinTimes($prize_id);
  264. $timeBucketWinTimes = $this->objMemcached->get($memkey);
  265. if (!$timeBucketWinTimes) {
  266. $replyInfo = $this->objDLotteryPrize->get ( $prize_id ); // 获取当前奖项详情
  267. $eventsData = $this->objDLotteryEvents->get($this->eventsId);
  268. $gift_amount = $replyInfo['prize_num'];
  269. if ($gift_amount <= 0) {
  270. return false;
  271. }
  272. $start_time = $eventsData['start_time'];
  273. $end_time = $eventsData['end_time'];
  274. $today_time = mktime(0,0,0);
  275. $overTotal = ceil(($end_time - $today_time) / (60*60*24));//剩余天数
  276. $hasWinTimes = $replyInfo['prize_winning_num'];
  277. $todayHasWinTimes = $this->objDLotteryData->getTodayHasWinTotal($prize_id);
  278. $needWinTimes = ceil(($gift_amount - $hasWinTimes) / $overTotal) - $todayHasWinTimes;
  279. $timeBucketWinTimes = $this->objRank->getTimeValue($needWinTimes, date('G'), $replyInfo['end_hour']-1);
  280. $this->objMemcached->set($memkey, $timeBucketWinTimes, 60 * 60 * 24);
  281. }
  282. return $timeBucketWinTimes;
  283. }
  284. /**
  285. * 获取当天中奖数
  286. */
  287. public function getCacheKeyOfTimeBucketWinTimes($prize_id){
  288. return date('Y_m_d',$this->thisTime).'_'.$prize_id.'_winning_num';
  289. }
  290. /**
  291. * 获取下次中奖时间
  292. * @return string
  293. */
  294. public function getNextWinTime($prize_id) {
  295. $next_win_time = $this->objMemcached->get($this->getCacheKeyOfNextWinTime($prize_id));
  296. return $next_win_time;
  297. }
  298. /**
  299. * cache中下次中奖时间的key
  300. */
  301. public function getCacheKeyOfNextWinTime($prize_id){
  302. return date('Y_m_d',$this->thisTime).'_'.$prize_id.'_next_time';
  303. }
  304. }