123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace Cas\Module;
- use KIF\Core\Request;
- use KIF\Page\Page;
- use KIF\Verify;
- use Cas\Dao\LotteryEvents;
- use KIF\Core\Config;
- use KIF\Image;
- use Cas\Dao\LotteryPrize;
- use KIF\Cache\Memcached;
- use KIF\Dao\SqlHelper;
- use Cas\Dao\LotteryPrizeVirtualData;
- use Cas\Dao\LotteryData;
- use Cas\Module\LotteryLog;
- use Cas\Dao\LotteryEventsLog as DLog;
- use Cas\Dao\LotteryEventsLetters;
- use Cas\Dao\LotteryEventsUserLetters;
- /**
- *
- *
- * 抽奖[字母] 模型
- *
- * @author lihuanchun
- */
- class LotteryDrawLetters {
-
- private $objMemcached;
-
- private $objRank;
-
- private $uid;
-
- private $eventsId;
-
- private $objDLotteryEvents;
-
- private $objMlottery;
-
- private $objDLotteryPrizeVirtualData;
-
- private $objMLotteryEventsLimitations;
-
- private $objDLotteryEventsLetters;
-
- private $objDLotteryData;
-
- private $objMLotteryLog;
-
- private $version;
-
- private $thisTime;
-
- public function __construct($uid=null , $eventsId=null ) {
- $this->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';
- }
-
-
- }
|