<?php
class Lottery_model {
	var $_flag_m=null;
	var $_zjlist_m=null;
	var $_view_zjlist_m=null;
	var $_award_m=null;
	function __construct(){
		$this->_flag_m=new M('flag');
		$this->_zjlist_m=new M('zjlist');
		$this->_view_zjlist_m=new M('view_zjlist');
		$this->_award_m=new M('award');
	}

	//获取所有奖品的列表
	function getAwards($fromplug=''){
		$where=' isdel=1 ';
		if(!empty($fromplug)){
			$where.=' and fromplug="'.$fromplug.'"';
		}
		$awardlist=$this->_award_m->select($where);
		return $awardlist;
	}

	//删除中奖纪录
	function deleteZjrecord($from='',$awardid=0,$openid=''){
		$condition='1 ';
		if($from!=''){
			$condition.=' and fromplug="'.$from.'"';
		}
		if(intval($awardid)>0){
			$condition.=' and awardid='.$awardid;
		}
		if($openid!=''){
			$condition.=' and openid="'.$openid.'"';
		}
		$deletecondition=$condition.' and (status=2 or status=3) and (designated is NULL or designated=1)';
		$updatecondition=$condition.' and (status=2 or status=3) and designated=2 or designated=3';
		//删除未内定的中奖纪录
		$this->_zjlist_m->delete($deletecondition);
		//重置内定人的中奖状态
		$this->_zjlist_m->update($updatecondition,array('status'=>1));
	}

	//删除一个用户的所有中奖记录
	public function deleteUserRecord($openid){
		$this->_zjlist_m->delete('openid="'.$openid.'"');
	}
	
	//获取参与制定抽奖类型，指定奖品的抽奖人数
	public function getUserCount($from,$awardid){
		//所有完成签到并通过审核的总人数
		$total=$this->_flag_m->find('flag=2 and status=1','*','count');

		//已经中奖和中过其他奖的
		$where='flagflag=2 and flagstatus=1 and `fromplug` = "'.$from.'" and status>1';
		$zhongcount=$this->_view_zjlist_m->find($where,'*','count');

		//没有中奖，但是内定中其他奖的
		$where='flagflag=2 and flagstatus=1 and `fromplug` = "'.$from.'" and status=1 and designated=2 and awardid!='.$awardid;
		$neidingcount=$this->_view_zjlist_m->find($where,'*','count');

		//内定不中的
		$where='flagflag=2 and flagstatus=1 and `fromplug` = "'.$from.'" and (`designated`= 3 and awardid='.$awardid.') ';
		$buzhongcount=$this->_view_zjlist_m->find($where,'*','count');

		//内定不中，但是已经在其他奖项中过奖，在不中的人数里面重复计算了，需要去掉
		$where='flagflag=2 and flagstatus=1 and openid in(select openid from weixin_zjlist where `fromplug` = "'.$from.'" and (`designated`= 3 and awardid='.$awardid.') ) and awardid!='.$awardid.' and (`status`>1 or designated=2)';
		$buzhong1count=$this->_view_zjlist_m->find($where,'*','count');
		$buzhongcount=$buzhongcount-$buzhong1count;

		return  $total-$zhongcount-$neidingcount-$buzhongcount;
	}
	//获取一个参与抽奖的用户列表
	public function getCjUserlist($limit=100){
		// $sql='select `weixin_flag`.`flag` AS `flag`,
		// 	`weixin_flag`.`openid` AS `openid`,
		// 	`weixin_flag`.`nickname` AS `nickname`,
		// 	`weixin_flag`.`avatar` AS `avatar`,
		// 	`weixin_flag`.`status` AS `status`,
		// 	`weixin_flag`.`datetime` AS `datetime`,
		// 	`weixin_flag`.`phone` AS `phone`,
		// 	`weixin_flag`.`signname` AS `signname` from weixin_flag order by rand() limit '.$limit;
		// $query=$this->_zjlist_m->query($sql);
		// $this->_zjlist_m->fetch_array($query);
		$data=$this->_flag_m->select(' flag=2 and status=1 order by rand() limit 0,'.$limit,'flag,openid,nickname,avatar,phone,signname');
		return $data;
	}

	//获取已经中奖的用户的信息
	public function getZjUserlist($from,$awardid=0){
		//已经中奖和中过其他奖的
		$where='flagflag=2 and flagstatus=1 and `fromplug` = "'.$from.'" and status>1';
		if($awardid>0){
			$where.=' and awardid='.$awardid;
		}
		return $this->_view_zjlist_m->select($where,'*');
	}

	//设置一个openid对应的用户为中奖用户
	public function setZjUser($openid,$from,$awardid){
		$where='openid="'.$openid.'" and awardid='.$awardid.' and fromplug="'.$from.'"';
		$zjperson=$this->_zjlist_m->find($where);
		$data=array(
				'status'=>2,
				'zjdatetime'=>time(),
				'verifycode'=>uniqid('wxq')
		);

		if(!$zjperson){
			$data['fromplug']=$from;
			$data['openid']=$openid;
			$data['awardid']=$awardid;
			$return=$this->_zjlist_m->add($data);
		}else{
			$return=$this->_zjlist_m->update($where, $data);
		}
		return $return;
	}

	//获取中奖用户的信息
	public function getZjUser($openid,$from,$awardid){
		$userinfo=$this->_view_zjlist_m->find('openid="'.$openid.'" and fromplug="'.$from.'"and awardid='.$awardid,'*');
		return $userinfo;
	}
	//获取指定用户的中奖纪录
	public function getZjUserRecord($openid){
		$data=$this->_view_zjlist_m->select('openid="'.$openid.'" and status>1');
		return $data;
	}
	//随机抽取一个用户的Openid作为中奖用户的openid
	public function getRandZjUserOpenid($from,$awardid){
		//优先处理内定
		$where='flagflag=2 and flagstatus=1 and designated=2 and status=1 and fromplug="'.$from.'" and awardid='.$awardid;
		$userinfo=$this->_view_zjlist_m->find($where,'openid');
		//没有内定的那就不是内定
		if(!$userinfo){
			$where='flag=2 and weixin_flag.status=1 and weixin_flag.openid not in(select weixin_zjlist.openid from weixin_zjlist where fromplug="'.$from.'"  and ((weixin_zjlist.status=2 or weixin_zjlist.status=3) or (awardid='.$awardid.' and designated=3) or designated=2)) order by rand() limit 1';
			$userinfo=$this->_flag_m->find($where,'weixin_flag.openid');
		}
		return $userinfo['openid'];

	}
	//清空中奖数据
	public function clearLotteryData(){
		$this->_zjlist_m->query('truncate table weixin_zjlist');
	}
}