Lottery.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Core\Request;
  4. use KIF\Data\Convert;
  5. use KIF\Data\ResultWrapper;
  6. use KIF\Db\Transaction;
  7. use KIF\Cache\Memcached;
  8. use Cas\Dao\LotteryEvents;
  9. use Cas\Dao\LotteryPrize;
  10. use Cas\Dao\LotteryPrizeVirtualData;
  11. use Cas\Dao\LotteryData;
  12. use Cas\Dao\Platform;
  13. /**
  14. * 抽奖模型
  15. *
  16. * @author lihuanchun
  17. *
  18. */
  19. class Lottery {
  20. private $objDLotteryData;
  21. private $objDLotteryEvents;
  22. private $objDLotteryPrize;
  23. private $objDLotteryPrizeVirtualData;
  24. private $objMLotteryEventsLimitations;
  25. private $objMemcached;
  26. private $runTime ;
  27. private $objRank ;
  28. /**
  29. * 初始化
  30. */
  31. public function __construct() {
  32. $this->objDLotteryEvents = new LotteryEvents (); // 活动
  33. $this->objDLotteryPrize = new LotteryPrize (); // 奖项
  34. $this->objDLotteryPrizeVirtualData = new LotteryPrizeVirtualData (); // 奖池数据
  35. $this->objDLotteryData = new LotteryData (); // 获奖数据
  36. $this->objMemcached = new Memcached ();
  37. $this->objMLotteryEventsLimitations = new LotteryEventsLimitations();
  38. $this->runTime = time();
  39. $this->objRank = new Rank();
  40. }
  41. public function setRunTime($time){
  42. return $this->runTime=$time;
  43. }
  44. /**
  45. * 临时:抽奖
  46. */
  47. public function tmpWantToDraw($eventsId,$uid){
  48. $objLotteryDraw = new LotteryDraw($uid, $eventsId);
  49. return $objLotteryDraw-> verifyPrize();
  50. }
  51. /**
  52. * 获取抽奖锁 cache key
  53. * @return string
  54. */
  55. public function getCacheKeyOfLock($prize_id) {
  56. return "lottery_draw_lock_{$prize_id}";
  57. }
  58. /**
  59. * 开启抽奖锁
  60. * @return boolean | null
  61. */
  62. public function lock($prize_id) {
  63. return $this->objMemcached->add($this->getCacheKeyOfLock($prize_id), true, 10);
  64. }
  65. /**
  66. * 关闭抽奖锁
  67. * @return boolean | null
  68. */
  69. public function unlock($prize_id) {
  70. return $this->objMemcached->delete($this->getCacheKeyOfLock($prize_id));
  71. }
  72. /**
  73. * 检查是否是百分百中奖名单中的用户
  74. */
  75. public function ckHundredPercentList($prize_id,$uid){
  76. // 查看百分百中奖名单中的用户
  77. $hundred_percent_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_hundred_percent_list');
  78. if(!empty($hundred_percent_list)){
  79. if(isset($hundred_percent_list[$uid])){
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * 检查白名单 & 黑名单
  87. */
  88. public function ckBlackAndWhiteList($prize_id,$uid){
  89. // 查看黑名单
  90. $black_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_black_list');
  91. if(!empty($black_list)){
  92. if(isset($black_list[$uid])){
  93. return false;
  94. }
  95. }
  96. // 查看白名单
  97. $white_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_white_list');
  98. if(!empty($white_list)){
  99. if(!isset($white_list[$uid])){
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * 添加活动
  107. */
  108. public function addLotteryEvents($info) {
  109. if($info['type']==LotteryEvents::TYPE_EVENTS_NULL){
  110. return false;
  111. }
  112. if($info['type']==LotteryEvents::TYPE_EVENTS_TRY){
  113. $info['participation_user_all_num'] = 1;
  114. }
  115. if($info['type']==LotteryEvents::TYPE_EVENTS_SURVEY){
  116. $info['participation_user_all_num'] = 1;
  117. }
  118. if($info['type']==LotteryEvents::TYPE_EVENTS_INVITATION){
  119. $info['participation_user_all_num'] = 1;
  120. $info['participation_user_max_num'] = 0;//最大参与人数 0 不限制
  121. }
  122. if($info['type']==LotteryEvents::TYPE_EVENTS_CODE){
  123. $info['two_dimensional_code'] = 2;//默认不以二维码输出
  124. }
  125. $info['display'] = LotteryEvents::EVENT_DISPLAY_UNFINISHED; // 默认添加操作 显示未设置完成
  126. if($info['type']==LotteryEvents::TYPE_EVENTS_OTHER){
  127. $info['display'] = LotteryEvents::EVENT_DISPLAY_HIDE; // 当如果是其他类型 默认添加时改成 未发布(停用)
  128. }
  129. return $this->objDLotteryEvents->add ( $info );
  130. }
  131. /**
  132. * 获取活动信息
  133. */
  134. public function getOneLotteryEvents($events_id) {
  135. $events_Data = $this->objDLotteryEvents->get ( $events_id );
  136. if(isset($events_Data ['forms'])){
  137. foreach ( $events_Data ['forms'] as $tmpkey => $tmpval ) {
  138. if ($tmpval ['type'] == 'select') {
  139. // $events_Data ['forms'] [$tmpkey] ['options'] = implode ( "\n", $tmpval ['options'] );
  140. }
  141. }
  142. }
  143. return $events_Data;
  144. }
  145. /**
  146. * 获取奖项信息
  147. */
  148. public function getOneLotteryPrize($prize_id) {
  149. return $this->objDLotteryPrize->get ( $prize_id );
  150. }
  151. /**
  152. * 获取活动当前参与人数
  153. */
  154. public function getParticipationUserNum($events_id){
  155. return $this->objDLotteryData->totals(array('events_id' =>$events_id ));
  156. }
  157. /**
  158. * 获取活动列表
  159. */
  160. public function getLotteryEventsList($condition, $limit, $order){
  161. $ids = $this->objDLotteryEvents->findIdsBy($condition, $limit, $order);
  162. $events = $this->objDLotteryEvents-> gets($ids);
  163. foreach($events as $key => $data){
  164. $events[$key]['url'] = $this->getEventsUrl($data['type'],$data['create_time']);
  165. }
  166. return $events;
  167. }
  168. /**
  169. * 获取获奖数据
  170. */
  171. public function getLotteryDataList($condition, $limit = null, $order){
  172. $ids = $this->objDLotteryData->findIdsBy($condition, $limit, $order);
  173. return $this->objDLotteryData-> gets($ids);
  174. }
  175. /**
  176. * 获取一条获奖数据
  177. */
  178. public function getOneLotteryData($lottery_data_id){
  179. return $this->objDLotteryData->get ( $lottery_data_id );
  180. }
  181. /**
  182. * 通过IDs获取多条获奖数据
  183. */
  184. public function getLotteryDatas($lottery_data_ids){
  185. return $this->objDLotteryData->gets ( $lottery_data_ids );
  186. }
  187. /**
  188. * 修改获奖数据
  189. */
  190. public function upOneLetteryData($info,$condition){
  191. return $this->objDLotteryData->modify($info, $condition);
  192. }
  193. /**
  194. * 获取中奖数据【条数】
  195. */
  196. public function getLotteryDataListNum($condition){
  197. return $this->objDLotteryData->totals($condition);
  198. }
  199. /**
  200. * 获取活动总条数
  201. */
  202. public function getLotteryEventsListNum($condition){
  203. return $this->objDLotteryEvents->totals($condition);
  204. }
  205. /**
  206. * 获取虚拟数据
  207. */
  208. public function getLotteryPrizeVirtualDataList($condition, $limit, $order){
  209. $ids = $this->objDLotteryPrizeVirtualData->findIdsBy($condition, $limit, $order);
  210. return $this->objDLotteryPrizeVirtualData-> gets($ids);
  211. }
  212. /**
  213. * 删除虚拟数据
  214. */
  215. public function delLotteryPrizeVirtual($prize_virtua_id){
  216. $prizeVirtualData = $this->objDLotteryPrizeVirtualData->get($prize_virtua_id);
  217. $prize_id = $prizeVirtualData['prize_id'];
  218. $this->objDLotteryPrizeVirtualData->delete(array('id' => $prize_virtua_id ));
  219. $this->addLotteryPrizeNum($prize_id,-1);
  220. return $prize_virtua_id;
  221. }
  222. /**
  223. * 获取虚拟数据【条数】
  224. */
  225. public function getLotteryPrizeVirtualDataListNum($condition){
  226. return $this->objDLotteryPrizeVirtualData->totals($condition);
  227. }
  228. /**
  229. * 获取活动信息&奖项信息
  230. */
  231. public function getOneLotteryEventsAndPrize($events_id) {
  232. $returnData = array ();
  233. $returnData ['events'] = $this->getOneLotteryEvents ( $events_id );
  234. $ids = $this->objDLotteryPrize->findIdsBy ( array (
  235. 'events_id' => $events_id
  236. ) );
  237. $returnData ['prize'] = $this->objDLotteryPrize->gets ( $ids );
  238. return $returnData;
  239. }
  240. /**
  241. * 修改活动信息 & 更新中奖时间
  242. */
  243. public function upLotteryEvents($info,$condition){
  244. $events_id = $condition['id'];
  245. $this->objDLotteryEvents->modify($info,$condition);
  246. $eventsAndPrize_Data = $this ->getOneLotteryEventsAndPrize($events_id);
  247. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  248. foreach($eventsAndPrize_Data['prize'] as $prize_id=> $prize){
  249. $objMLotteryDraw->clear_draw_next_time($prize_id);
  250. }
  251. return $events_id;
  252. }
  253. /**
  254. * 修改活动奖项
  255. */
  256. public function UpLotteryPrize($info,$condition){
  257. $this->objDLotteryPrize-> modify($info,$condition );
  258. $prize_id = $condition['id'];
  259. // 白黑名单&百分百中奖名单 cache
  260. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_white_list', $info ['white_list'] );
  261. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_hundred_percent_list', $info ['hundred_percent_list'] );
  262. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_black_list', $info ['black_list'] );
  263. // 清除下次中奖时间
  264. $eventsData = $this->objDLotteryPrize->get($prize_id);
  265. $events_id = $eventsData['events_id'];
  266. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  267. $objMLotteryDraw->clear_draw_next_time($prize_id);
  268. return $prize_id;
  269. }
  270. /**
  271. * 修改奖项数量
  272. */
  273. public function addLotteryPrizeNum($prize_id,$num=null,$virtual_data=array()){
  274. if(empty($num) && empty($virtual_data)){
  275. return false;
  276. }
  277. if(!empty($virtual_data)){
  278. foreach ( $virtual_data as $virtual ) {
  279. $virtualInfo = array (
  280. 'prize_id' => $prize_id,
  281. 'data' => $virtual
  282. );
  283. $this->objDLotteryPrizeVirtualData->add ( $virtualInfo );
  284. }
  285. $num = count($virtual_data);
  286. }
  287. $objDLotteryPrize = new LotteryPrize();
  288. $objDLotteryPrize->upPrizeNum($prize_id, $num);
  289. // 清除下次中奖时间
  290. $eventsData = $this->objDLotteryPrize->get($prize_id);
  291. $events_id = $eventsData['events_id'];
  292. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  293. $objMLotteryDraw->clear_draw_next_time($prize_id);
  294. return $prize_id;
  295. }
  296. /**
  297. * 添加奖项
  298. */
  299. public function addLotteryPrize($info) {
  300. $virtual_data = $info ['virtual_data'];
  301. unset ( $info ['virtual_data'] );
  302. $prize_id = $this->objDLotteryPrize->add ( $info );
  303. // 白黑名单 cache
  304. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_white_list', $info ['white_list'] );
  305. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_black_list', $info ['black_list'] );
  306. if (! empty ( $virtual_data )) {
  307. foreach ( $virtual_data as $virtual ) {
  308. $virtualInfo = array (
  309. 'prize_id' => $prize_id,
  310. 'data' => $virtual
  311. );
  312. $this->objDLotteryPrizeVirtualData->add ( $virtualInfo );
  313. }
  314. }
  315. // 清除下次中奖时间
  316. $eventsData = $this->objDLotteryPrize->get($prize_id);
  317. $events_id = $eventsData['events_id'];
  318. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  319. $objMLotteryDraw->clear_draw_next_time($prize_id);
  320. return $prize_id;
  321. }
  322. /**
  323. * 解析条件
  324. */
  325. public function parseCondition($condition){
  326. return $this->objDLotteryEvents -> parseCondition($condition);
  327. }
  328. /**
  329. * 页面返回不同活动的地址 “简单粗暴“
  330. * @param int $eventsId
  331. */
  332. public function getEventsUrl($type,$create_time,$z_url=null){
  333. $create_time= date('YmdHis',$create_time);
  334. if(!$z_url){
  335. $z_url = Request::g('url');
  336. }
  337. $alltype = LotteryEvents::getType();
  338. $cstr = $alltype[$type]['c'];
  339. $eventRoute = LotteryEvents::getEventRoute();
  340. $str = '';
  341. foreach($eventRoute as $key =>$data){
  342. if($data == $cstr){
  343. $str = $key;
  344. break;
  345. }
  346. }
  347. return Request::schemeDomain ().'/'."{$z_url}/{$create_time}{$str}";
  348. }
  349. }