LotteryDrawLetters.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. use Cas\Dao\LotteryEventsLetters;
  17. use Cas\Dao\LotteryEventsUserLetters;
  18. /**
  19. *
  20. *
  21. * 抽奖[字母] 模型
  22. *
  23. * @author lihuanchun
  24. */
  25. class LotteryDrawLetters {
  26. private $objMemcached;
  27. private $objRank;
  28. private $uid;
  29. private $eventsId;
  30. private $objDLotteryEvents;
  31. private $objMlottery;
  32. private $objDLotteryPrizeVirtualData;
  33. private $objMLotteryEventsLimitations;
  34. private $objDLotteryEventsLetters;
  35. private $objDLotteryData;
  36. private $objMLotteryLog;
  37. private $version;
  38. private $thisTime;
  39. public function __construct($uid=null , $eventsId=null ) {
  40. $this->objMemcached = new Memcached();
  41. $this->objRank = new Rank();
  42. $this->uid = $uid;
  43. $this->eventsId = $eventsId;
  44. $this->objDLotteryEvents = new LotteryEvents();
  45. $this->objDLotteryPrize = new LotteryPrize();
  46. $this->objDLotteryPrizeVirtualData = new LotteryPrizeVirtualData();
  47. $this->objDLotteryData = new LotteryData();
  48. $this->objMlottery = new Lottery();
  49. $this->objMLotteryEventsLimitations = new LotteryEventsLimitations();
  50. $this->objMLotteryLog = new LotteryLog($uid,$eventsId);
  51. $this->objDLotteryEventsLetters = new LotteryEventsLetters();
  52. $this->version = '0.1';
  53. $this->thisTime = time();
  54. }
  55. public function verifyPrize($letters_ids=array()){
  56. $eventsAndPrize = $this->objMlottery->getOneLotteryEventsAndPrize($this->eventsId);
  57. $eventsLettersData = $this->objDLotteryEventsLetters -> getEventData($this->eventsId);
  58. // 过滤用户抽中的 字母
  59. foreach($letters_ids as $key => $letters_id){
  60. unset($eventsLettersData[$letters_id]);
  61. }
  62. $keys = array_keys($eventsLettersData);
  63. shuffle($keys);
  64. $randEventsLettersData = array();
  65. foreach($keys as $key=>$data){
  66. $randEventsLettersData[$data] = $eventsLettersData[$data];
  67. }
  68. foreach($randEventsLettersData as $letters){
  69. $letters_id = $letters['id'];
  70. $begin_hour = 0;
  71. $end_hour = 24;
  72. $this_hour = date("H",$this->thisTime);
  73. # 验证当前将奖项 库存数量
  74. if($letters['num']-$letters['winning_num'] <= 0){
  75. continue;
  76. }
  77. # 获取下一次的中奖时间 【开始抽奖逻辑】
  78. $nextWinTime = $this->getNextWinTime($letters_id);
  79. if (!$this->lock($letters_id)) return false;
  80. if (!$nextWinTime) {
  81. $isWin = false;
  82. $this->setNextWinTime($letters_id);
  83. } elseif ($nextWinTime <= $this->thisTime) {
  84. $isWin = true;
  85. # 发奖
  86. $this->awards($letters_id,$nextWinTime);
  87. $this->setNextWinTime($letters_id);
  88. $this->unlock($letters_id);
  89. return $letters_id;
  90. break;
  91. }
  92. $this->unlock($letters_id);
  93. continue;
  94. }
  95. return false;
  96. }
  97. /**
  98. * 清除下次中奖时间 并且动态计算下次中奖时间
  99. */
  100. public function clear_draw_next_time($letters_id) {
  101. $memkey = $this->getCacheKeyOfTimeBucketWinTimes($letters_id);
  102. $this->objMemcached->delete($memkey);
  103. $memkey = $this->getCacheKeyOfNextWinTime($letters_id);
  104. $this->objMemcached->delete($memkey);
  105. $this->setNextWinTime($letters_id);
  106. }
  107. /**
  108. * 发奖逻辑
  109. */
  110. public function awards($letters_id,$winTime){
  111. $objDLotteryEventsUserLetters = new LotteryEventsUserLetters();
  112. $info = array(
  113. 'uid' => $this->uid,
  114. 'events_id' => $this->eventsId,
  115. 'ABC_data' => $letters_id,
  116. );
  117. $objDLotteryEventsUserLetters -> addLetters($info);
  118. $this->objDLotteryEventsLetters -> upLettersWinningNum($letters_id, 1);
  119. $this->objDLotteryEventsLetters -> cumulativeTodayHasWinTotal($letters_id);
  120. }
  121. /**
  122. * 开启抽奖锁
  123. * @return boolean | null
  124. */
  125. public function lock($letters_id) {
  126. return $this->objMemcached->add($this->getCacheKeyOfLock($letters_id), true, 10);
  127. }
  128. /**
  129. * 关闭抽奖锁
  130. * @return boolean | null
  131. */
  132. public function unlock($letters_id) {
  133. return $this->objMemcached->delete($this->getCacheKeyOfLock($letters_id));
  134. }
  135. /**
  136. * 获取抽奖锁 cache key
  137. * @return string
  138. */
  139. public function getCacheKeyOfLock($letters_id) {
  140. return "lottery_draw_lock_letters_{$letters_id}";
  141. }
  142. /**
  143. * 设置当天下次中奖时间
  144. * @return boolean | null
  145. */
  146. public function setNextWinTime($letters_id) {
  147. $hasWinTotal = $this->objDLotteryEventsLetters->getTodayHasWinTotal($letters_id);
  148. $timeBucketWinTimes = $this->getTimeBucketWinTimes($letters_id);
  149. $next_win_time = $this->objRank->winnerCal($timeBucketWinTimes, $hasWinTotal);
  150. return $this->objMemcached->set($this->getCacheKeyOfNextWinTime($letters_id), $next_win_time, 60*60*24);
  151. }
  152. /**
  153. * 获取当天每个时间断中奖次数
  154. * @return array
  155. */
  156. public function getTimeBucketWinTimes($letters_id) {
  157. $memkey = $this->getCacheKeyOfTimeBucketWinTimes($letters_id);
  158. $timeBucketWinTimes = $this->objMemcached->get($memkey);
  159. if (!$timeBucketWinTimes) {
  160. $replyInfo = $this->objDLotteryEventsLetters->get($letters_id);
  161. $eventsData = $this->objDLotteryEvents->get($this->eventsId);
  162. $gift_amount = $replyInfo['num'];
  163. if ($gift_amount <= 0) {
  164. return false;
  165. }
  166. $start_time = $eventsData['start_time'];
  167. $end_time = $eventsData['end_time'];
  168. $today_time = mktime(0,0,0);
  169. $overTotal = ceil(($end_time - $today_time) / (60*60*24));//剩余天数
  170. $hasWinTimes = $replyInfo['winning_num'];
  171. $todayHasWinTimes = $this->objDLotteryEventsLetters->getTodayHasWinTotal($letters_id);
  172. $needWinTimes = ceil(($gift_amount - $hasWinTimes) / $overTotal) - $todayHasWinTimes;
  173. $timeBucketWinTimes = $this->objRank->getTimeValue($needWinTimes, date('G'), 23);
  174. $this->objMemcached->set($memkey, $timeBucketWinTimes, 60 * 60 * 24);
  175. }
  176. return $timeBucketWinTimes;
  177. }
  178. /**
  179. * 获取当天中奖数
  180. */
  181. public function getCacheKeyOfTimeBucketWinTimes($letters_id){
  182. return date('Y_m_d',$this->thisTime).'_letters_'.$letters_id.'_winning_num';
  183. }
  184. /**
  185. * 获取下次中奖时间
  186. * @return string
  187. */
  188. public function getNextWinTime($letters_id) {
  189. $next_win_time = $this->objMemcached->get($this->getCacheKeyOfNextWinTime($letters_id));
  190. return $next_win_time;
  191. }
  192. /**
  193. * cache中下次中奖时间的key
  194. */
  195. public function getCacheKeyOfNextWinTime($letters_id){
  196. return date('Y_m_d',$this->thisTime).'_letters_'.$letters_id.'_next_time';
  197. }
  198. }