<?php
/*
点播日志记录模型
*/
class PlaylogModel extends DBModel{
	public function init(){
	}
	/*
	设置点播日志
	成功返回ID
	*/
	public function setlook(&$data){
		$this->_DB->insert(VODCMS_PLAYLOG, $data);
		//echo $this->_DB->sql;
		unset($data);
		return $this->_DB->lastInsertId();
	}
	/*
	设置当前活动记录表
	成功返回ID
	*/
	public function setActive(&$data){
		$this->_DB->insert(VODCMS_ACTIVES, $data);
		unset($data);
		return $this->_DB->lastInsertId();
	}
	/*写入用户在线记录表*/
	public function setSession($data){
		$select = $this->_DB->select();
		$select->from(VODCMS_SESSION);
		$select->where(array('sid'=>$data['sid']));
		$select->limit(1);
		$sql = $select->toString();
		if ($this->_DB->getCount($sql)>0){
			$this->_DB->update(VODCMS_SESSION, $data, array('sid'=>$data['sid']));
		}else{
			$this->_DB->insert(VODCMS_SESSION, $data);
		}
		$sid = $data['sid'];
		unset($data);
		return $sid;
	}
	/*
	获取一行点播日志记录
	*/
	public function getRow($where=null){
		$select = $this->_DB->select();
		$select->from(VODCMS_PLAYLOG.' AS a', 'a.*');
		$select->join(VODCMS_URL.' AS b', 'a.urlid=b.urlid', 'a.urlid');
		$select->join(VODCMS_MOVIE.' AS c', 'b.movid=c.movid', 'CONCAT(c.title,b.title) as movie,c.movid as movid');
		$select->where($where);
		$select->order('a.id DESC');
		$select->limit(1);
		$sql = $select->toString();
		
		return $this->_DB->fetRow($sql);
	}
	public function getAll($where=null){
		$select = $this->_DB->select();
		$select->from(VODCMS_PLAYLOG.' AS a', 'a.*');
		$select->join(VODCMS_URL.' AS b', 'a.urlid=b.urlid', 'a.urlid');
		$select->join(VODCMS_MOVIE.' AS c', 'b.movid=c.movid', 'CONCAT(c.title,b.title) as movie,c.movid as movid');
		$select->where($where);
		$select->order('a.id DESC');
		$sql = $select->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total, 10);
		$this->printPage = $this->_Page->PrintBody();
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*清理超时颠簸用户*/
	public function clearPlayer($cleartime=3600){
		$config = $GLOBALS['config'];
		$lifetime = time() - ($config['refreshplayer']+10);
		$this->_DB->delete(VODCMS_ACTIVES, 'lastime<'.$lifetime);
	}
	/*清理超时用户*/
	public function clearOnline(){
		$time = time();
		$config = $GLOBALS['config'];
		$this->_DB->delete(VODCMS_SESSION, $time - ($config['refreshonline']*60).'>lastime' );
	}
	/*清理点播日志*/
	public function clearlog($cleartime=3600){
		$config = $GLOBALS['config'];
		$lifetime = time() - ($config['clearlog'] * 86400);
		if ($cleartime){
			return $this->_DB->delete(VODCMS_PLAYLOG, 'addtime < '.$lifetime);
		}else{
			return false;
		}
	}
	/*
	检测用户是否在线
	@param $lifetime 为清理多长时间不在线的用户
	*/
	public function state($GET, $lifetime=10){
		$username = urldecode($GET['username']);
		$state = intval($GET['state']);
		$online = intval($GET['id']);
		$userip = sprintf("%u", ip2long($this->_Response->struserip()));
		/*
		删除指定超时用户。超时时间在配置文件config.inc.php中的
		*/
		$row = $this->_DB->update(VODCMS_ACTIVES, array('lastime'=>(time()+10)), array('id'=>(int)$online, 'userip'=>$userip));
		if ($state == 0){
			
			if ($row == 0){
				exit('404');
			}
		}
	}
	/*清理超时的用户*/
	public function delSession($sid){
		$this->_DB->delete(VODCMS_SESSION, array('sid'=>$sid));
		$this->clearOnline();
	}
	/*判断用户是否在线。在线返回用户登陆信息*/
	public function isOnline($username){
		$this->clearOnline();
		$select = $this->_DB->select();
		$select->from(VODCMS_SESSION);
		$select->where(array('username'=>$username));
		$select->limit(1);
		$sql = $select->toString();
		$row = $this->_DB->fetRow($sql);
		return $row;
	}
	/*更新用户在线状态*/
	public function upSession($sid){
		return $this->_DB->update(VODCMS_SESSION, array('lastime'=>time()), array('sid'=>$sid));
	}
	/*
	判断网吧用户是否超过设定值限制
	*/
	public function checkNetbarMax($netbar, $max=0){
		$config = $GLOBALS['config'];
		$lifetime = time() - $config['refreshplayer']+30;
		$sql = 'SELECT COUNT(id) FROM '.VODCMS_ACTIVES.' WHERE username=\''.$netbar.'\' AND lastime < '.$lifetime;
		//./echo $sql;
		$total = $this->_DB->getCount($sql);
		//echo $total;
		if ($total >= $max && $max>0){
			return false;
		}else{
			return true;
		}
	}
}
?>