objMemcached = new Memcached(); $this->objRank = new Rank(); $this->uid = $uid; $this->eventsId = $eventsId; $this->objDLotteryEvents = new LotteryEvents(); $this->objDLotteryPrize = new LotteryPrize(); $this->objDLotteryPrizeVirtualData = new LotteryPrizeVirtualData(); $this->objDLotteryData = new LotteryData(); $this->objMlottery = new Lottery(); $this->objMLotteryEventsLimitations = new LotteryEventsLimitations(); $this->objMLotteryLog = new LotteryLog($uid,$eventsId); $this->objDLotteryEventsLetters = new LotteryEventsLetters(); $this->version = '0.1'; $this->thisTime = time(); } public function verifyPrize($letters_ids=array()){ $eventsAndPrize = $this->objMlottery->getOneLotteryEventsAndPrize($this->eventsId); $eventsLettersData = $this->objDLotteryEventsLetters -> getEventData($this->eventsId); // 过滤用户抽中的 字母 foreach($letters_ids as $key => $letters_id){ unset($eventsLettersData[$letters_id]); } $keys = array_keys($eventsLettersData); shuffle($keys); $randEventsLettersData = array(); foreach($keys as $key=>$data){ $randEventsLettersData[$data] = $eventsLettersData[$data]; } foreach($randEventsLettersData as $letters){ $letters_id = $letters['id']; $begin_hour = 0; $end_hour = 24; $this_hour = date("H",$this->thisTime); # 验证当前将奖项 库存数量 if($letters['num']-$letters['winning_num'] <= 0){ continue; } # 获取下一次的中奖时间 【开始抽奖逻辑】 $nextWinTime = $this->getNextWinTime($letters_id); if (!$this->lock($letters_id)) return false; if (!$nextWinTime) { $isWin = false; $this->setNextWinTime($letters_id); } elseif ($nextWinTime <= $this->thisTime) { $isWin = true; # 发奖 $this->awards($letters_id,$nextWinTime); $this->setNextWinTime($letters_id); $this->unlock($letters_id); return $letters_id; break; } $this->unlock($letters_id); continue; } return false; } /** * 清除下次中奖时间 并且动态计算下次中奖时间 */ public function clear_draw_next_time($letters_id) { $memkey = $this->getCacheKeyOfTimeBucketWinTimes($letters_id); $this->objMemcached->delete($memkey); $memkey = $this->getCacheKeyOfNextWinTime($letters_id); $this->objMemcached->delete($memkey); $this->setNextWinTime($letters_id); } /** * 发奖逻辑 */ public function awards($letters_id,$winTime){ $objDLotteryEventsUserLetters = new LotteryEventsUserLetters(); $info = array( 'uid' => $this->uid, 'events_id' => $this->eventsId, 'ABC_data' => $letters_id, ); $objDLotteryEventsUserLetters -> addLetters($info); $this->objDLotteryEventsLetters -> upLettersWinningNum($letters_id, 1); $this->objDLotteryEventsLetters -> cumulativeTodayHasWinTotal($letters_id); } /** * 开启抽奖锁 * @return boolean | null */ public function lock($letters_id) { return $this->objMemcached->add($this->getCacheKeyOfLock($letters_id), true, 10); } /** * 关闭抽奖锁 * @return boolean | null */ public function unlock($letters_id) { return $this->objMemcached->delete($this->getCacheKeyOfLock($letters_id)); } /** * 获取抽奖锁 cache key * @return string */ public function getCacheKeyOfLock($letters_id) { return "lottery_draw_lock_letters_{$letters_id}"; } /** * 设置当天下次中奖时间 * @return boolean | null */ public function setNextWinTime($letters_id) { $hasWinTotal = $this->objDLotteryEventsLetters->getTodayHasWinTotal($letters_id); $timeBucketWinTimes = $this->getTimeBucketWinTimes($letters_id); $next_win_time = $this->objRank->winnerCal($timeBucketWinTimes, $hasWinTotal); return $this->objMemcached->set($this->getCacheKeyOfNextWinTime($letters_id), $next_win_time, 60*60*24); } /** * 获取当天每个时间断中奖次数 * @return array */ public function getTimeBucketWinTimes($letters_id) { $memkey = $this->getCacheKeyOfTimeBucketWinTimes($letters_id); $timeBucketWinTimes = $this->objMemcached->get($memkey); if (!$timeBucketWinTimes) { $replyInfo = $this->objDLotteryEventsLetters->get($letters_id); $eventsData = $this->objDLotteryEvents->get($this->eventsId); $gift_amount = $replyInfo['num']; if ($gift_amount <= 0) { return false; } $start_time = $eventsData['start_time']; $end_time = $eventsData['end_time']; $today_time = mktime(0,0,0); $overTotal = ceil(($end_time - $today_time) / (60*60*24));//剩余天数 $hasWinTimes = $replyInfo['winning_num']; $todayHasWinTimes = $this->objDLotteryEventsLetters->getTodayHasWinTotal($letters_id); $needWinTimes = ceil(($gift_amount - $hasWinTimes) / $overTotal) - $todayHasWinTimes; $timeBucketWinTimes = $this->objRank->getTimeValue($needWinTimes, date('G'), 23); $this->objMemcached->set($memkey, $timeBucketWinTimes, 60 * 60 * 24); } return $timeBucketWinTimes; } /** * 获取当天中奖数 */ public function getCacheKeyOfTimeBucketWinTimes($letters_id){ return date('Y_m_d',$this->thisTime).'_letters_'.$letters_id.'_winning_num'; } /** * 获取下次中奖时间 * @return string */ public function getNextWinTime($letters_id) { $next_win_time = $this->objMemcached->get($this->getCacheKeyOfNextWinTime($letters_id)); return $next_win_time; } /** * cache中下次中奖时间的key */ public function getCacheKeyOfNextWinTime($letters_id){ return date('Y_m_d',$this->thisTime).'_letters_'.$letters_id.'_next_time'; } }