| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 | 
							- <?php
 
- namespace Cas\Module;
 
- use KIF\Core\Request;
 
- use KIF\Data\Convert;
 
- use KIF\Data\ResultWrapper;
 
- use KIF\Db\Transaction;
 
- use KIF\Cache\Memcached;
 
- use Cas\Dao\LotteryEvents;
 
- use Cas\Dao\LotteryPrize;
 
- use Cas\Dao\LotteryPrizeVirtualData;
 
- use Cas\Dao\LotteryData;
 
- use Cas\Dao\Platform;
 
- /**
 
-  * 抽奖模型
 
-  * 
 
-  * @author lihuanchun
 
-  *        
 
-  */
 
- class Lottery {
 
- 	private $objDLotteryData;
 
- 	private $objDLotteryEvents;
 
- 	private $objDLotteryPrize;
 
- 	private $objDLotteryPrizeVirtualData;
 
- 	private $objMLotteryEventsLimitations;
 
- 	private $objMemcached;
 
- 	private $runTime ;
 
- 	private $objRank ;
 
- 	
 
- 	/**
 
- 	 * 初始化
 
- 	 */
 
- 	public function __construct() {
 
- 		$this->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}";
 
- 	}
 
- 	
 
- 	
 
- 	
 
- 	
 
- }
 
 
  |