objDLotteryEvents = new LotteryEvents (); // 活动 $this->objDLotteryPrize = new LotteryPrize (); // 奖项 $this->objDLotteryPrizeVirtualData = new LotteryPrizeVirtualData (); // 奖池数据 $this->objDLotteryData = new LotteryData (); // 获奖数据 $this->objMemcached = new Memcached (); $this->objMLotteryEventsLimitations = new LotteryEventsLimitations(); $this->runTime = time(); $this->objRank = new Rank(); } public function setRunTime($time){ return $this->runTime=$time; } /** * 临时:抽奖 */ public function tmpWantToDraw($eventsId,$uid){ $objLotteryDraw = new LotteryDraw($uid, $eventsId); return $objLotteryDraw-> verifyPrize(); } /** * 获取抽奖锁 cache key * @return string */ public function getCacheKeyOfLock($prize_id) { return "lottery_draw_lock_{$prize_id}"; } /** * 开启抽奖锁 * @return boolean | null */ public function lock($prize_id) { return $this->objMemcached->add($this->getCacheKeyOfLock($prize_id), true, 10); } /** * 关闭抽奖锁 * @return boolean | null */ public function unlock($prize_id) { return $this->objMemcached->delete($this->getCacheKeyOfLock($prize_id)); } /** * 检查是否是百分百中奖名单中的用户 */ public function ckHundredPercentList($prize_id,$uid){ // 查看百分百中奖名单中的用户 $hundred_percent_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_hundred_percent_list'); if(!empty($hundred_percent_list)){ if(isset($hundred_percent_list[$uid])){ return true; } } return false; } /** * 检查白名单 & 黑名单 */ public function ckBlackAndWhiteList($prize_id,$uid){ // 查看黑名单 $black_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_black_list'); if(!empty($black_list)){ if(isset($black_list[$uid])){ return false; } } // 查看白名单 $white_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_white_list'); if(!empty($white_list)){ if(!isset($white_list[$uid])){ return false; } } return true; } /** * 添加活动 */ public function addLotteryEvents($info) { if($info['type']==LotteryEvents::TYPE_EVENTS_NULL){ return false; } if($info['type']==LotteryEvents::TYPE_EVENTS_TRY){ $info['participation_user_all_num'] = 1; } if($info['type']==LotteryEvents::TYPE_EVENTS_SURVEY){ $info['participation_user_all_num'] = 1; } if($info['type']==LotteryEvents::TYPE_EVENTS_INVITATION){ $info['participation_user_all_num'] = 1; $info['participation_user_max_num'] = 0;//最大参与人数 0 不限制 } if($info['type']==LotteryEvents::TYPE_EVENTS_CODE){ $info['two_dimensional_code'] = 2;//默认不以二维码输出 } $info['display'] = LotteryEvents::EVENT_DISPLAY_UNFINISHED; // 默认添加操作 显示未设置完成 if($info['type']==LotteryEvents::TYPE_EVENTS_OTHER){ $info['display'] = LotteryEvents::EVENT_DISPLAY_HIDE; // 当如果是其他类型 默认添加时改成 未发布(停用) } return $this->objDLotteryEvents->add ( $info ); } /** * 获取活动信息 */ public function getOneLotteryEvents($events_id) { $events_Data = $this->objDLotteryEvents->get ( $events_id ); if(isset($events_Data ['forms'])){ foreach ( $events_Data ['forms'] as $tmpkey => $tmpval ) { if ($tmpval ['type'] == 'select') { // $events_Data ['forms'] [$tmpkey] ['options'] = implode ( "\n", $tmpval ['options'] ); } } } return $events_Data; } /** * 获取奖项信息 */ public function getOneLotteryPrize($prize_id) { return $this->objDLotteryPrize->get ( $prize_id ); } /** * 获取活动当前参与人数 */ public function getParticipationUserNum($events_id){ return $this->objDLotteryData->totals(array('events_id' =>$events_id )); } /** * 获取活动列表 */ public function getLotteryEventsList($condition, $limit, $order){ $ids = $this->objDLotteryEvents->findIdsBy($condition, $limit, $order); $events = $this->objDLotteryEvents-> gets($ids); foreach($events as $key => $data){ $events[$key]['url'] = $this->getEventsUrl($data['type'],$data['create_time']); } return $events; } /** * 获取获奖数据 */ public function getLotteryDataList($condition, $limit = null, $order){ $ids = $this->objDLotteryData->findIdsBy($condition, $limit, $order); return $this->objDLotteryData-> gets($ids); } /** * 获取获奖数据 */ public function getOnesLotteryData($condition){ $id = $this->objDLotteryData->findIdsBy($condition); if ($id && isset($id[0])) { return $this->objDLotteryData-> get($id[0]); } return array(); } /** * 获取一条获奖数据 */ public function getOneLotteryData($lottery_data_id){ return $this->objDLotteryData->get ( $lottery_data_id ); } /** * 通过IDs获取多条获奖数据 */ public function getLotteryDatas($lottery_data_ids){ return $this->objDLotteryData->gets ( $lottery_data_ids ); } /** * 修改获奖数据 */ public function upOneLetteryData($info,$condition){ return $this->objDLotteryData->modify($info, $condition); } /** * 获取中奖数据【条数】 */ public function getLotteryDataListNum($condition){ return $this->objDLotteryData->totals($condition); } /** * 获取活动总条数 */ public function getLotteryEventsListNum($condition){ return $this->objDLotteryEvents->totals($condition); } /** * 获取虚拟数据 */ public function getLotteryPrizeVirtualDataList($condition, $limit, $order){ $ids = $this->objDLotteryPrizeVirtualData->findIdsBy($condition, $limit, $order); return $this->objDLotteryPrizeVirtualData-> gets($ids); } /** * 删除虚拟数据 */ public function delLotteryPrizeVirtual($prize_virtua_id){ $prizeVirtualData = $this->objDLotteryPrizeVirtualData->get($prize_virtua_id); $prize_id = $prizeVirtualData['prize_id']; $this->objDLotteryPrizeVirtualData->delete(array('id' => $prize_virtua_id )); $this->addLotteryPrizeNum($prize_id,-1); return $prize_virtua_id; } /** * 获取虚拟数据【条数】 */ public function getLotteryPrizeVirtualDataListNum($condition){ return $this->objDLotteryPrizeVirtualData->totals($condition); } /** * 获取活动信息&奖项信息 */ public function getOneLotteryEventsAndPrize($events_id) { $returnData = array (); $returnData ['events'] = $this->getOneLotteryEvents ( $events_id ); $ids = $this->objDLotteryPrize->findIdsBy ( array ( 'events_id' => $events_id ) ); $returnData ['prize'] = $this->objDLotteryPrize->gets ( $ids ); return $returnData; } /** * 修改活动信息 & 更新中奖时间 */ public function upLotteryEvents($info,$condition){ $events_id = $condition['id']; $this->objDLotteryEvents->modify($info,$condition); $eventsAndPrize_Data = $this ->getOneLotteryEventsAndPrize($events_id); $objMLotteryDraw = new LotteryDraw(null,$events_id); foreach($eventsAndPrize_Data['prize'] as $prize_id=> $prize){ $objMLotteryDraw->clear_draw_next_time($prize_id); } return $events_id; } /** * 修改活动奖项 */ public function UpLotteryPrize($info,$condition){ $this->objDLotteryPrize-> modify($info,$condition ); $prize_id = $condition['id']; // 白黑名单&百分百中奖名单 cache $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_white_list', $info ['white_list'] ); $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_hundred_percent_list', $info ['hundred_percent_list'] ); $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_black_list', $info ['black_list'] ); // 清除下次中奖时间 $eventsData = $this->objDLotteryPrize->get($prize_id); $events_id = $eventsData['events_id']; $objMLotteryDraw = new LotteryDraw(null,$events_id); $objMLotteryDraw->clear_draw_next_time($prize_id); return $prize_id; } /** * 修改奖项数量 */ public function addLotteryPrizeNum($prize_id,$num=null,$virtual_data=array()){ if(empty($num) && empty($virtual_data)){ return false; } if(!empty($virtual_data)){ foreach ( $virtual_data as $virtual ) { $virtualInfo = array ( 'prize_id' => $prize_id, 'data' => $virtual ); $this->objDLotteryPrizeVirtualData->add ( $virtualInfo ); } $num = count($virtual_data); } $objDLotteryPrize = new LotteryPrize(); $objDLotteryPrize->upPrizeNum($prize_id, $num); // 清除下次中奖时间 $eventsData = $this->objDLotteryPrize->get($prize_id); $events_id = $eventsData['events_id']; $objMLotteryDraw = new LotteryDraw(null,$events_id); $objMLotteryDraw->clear_draw_next_time($prize_id); return $prize_id; } /** * 添加奖项 */ public function addLotteryPrize($info) { $virtual_data = $info ['virtual_data']; unset ( $info ['virtual_data'] ); $prize_id = $this->objDLotteryPrize->add ( $info ); // 白黑名单 cache $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_white_list', $info ['white_list'] ); $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_black_list', $info ['black_list'] ); if (! empty ( $virtual_data )) { foreach ( $virtual_data as $virtual ) { $virtualInfo = array ( 'prize_id' => $prize_id, 'data' => $virtual ); $this->objDLotteryPrizeVirtualData->add ( $virtualInfo ); } } // 清除下次中奖时间 $eventsData = $this->objDLotteryPrize->get($prize_id); $events_id = $eventsData['events_id']; $objMLotteryDraw = new LotteryDraw(null,$events_id); $objMLotteryDraw->clear_draw_next_time($prize_id); return $prize_id; } /** * 解析条件 */ public function parseCondition($condition){ return $this->objDLotteryEvents -> parseCondition($condition); } /** * 页面返回不同活动的地址 “简单粗暴“ * @param int $eventsId */ public function getEventsUrl($type,$create_time,$z_url=null){ $create_time= date('YmdHis',$create_time); if(!$z_url){ $z_url = Request::g('url'); } $alltype = LotteryEvents::getType(); $cstr = $alltype[$type]['c']; $eventRoute = LotteryEvents::getEventRoute(); $str = ''; foreach($eventRoute as $key =>$data){ if($data == $cstr){ $str = $key; break; } } return Request::schemeDomain ().'/'."{$z_url}/{$create_time}{$str}"; } }