<?php
class CarModel extends DBModel {
	protected $_name;
	protected $_name2;
	public function init(){
		$this->_name = VODCMS_CAR;
		$this->_name2 = VODCMS_USERGROUP;
	}
	public function add($POST){
		$array = array();
		$max = (int)$POST['max'] ? $POST['max'] : 8;
		$pow = pow(10,$max);
		$array['usergroup'] = (int)$POST['usergroup'];
		$array['usertype'] = (int)$POST['usertype'];
		$array['addtime'] = time();
		$array['lifetime'] = time()+(86400* (int)$POST['lifetime']);
		$array['admin'] = $this->_Response->getCookie('admin');
		$array['usercount'] = (int)$POST['usercount'];
		$array['money'] = (int)$POST['money'];
		$i=0;
		while($i< (int)$POST['num']){
			$i++;
			$array['carid'] = $POST['prefix'].substr(mt_rand(time(),$pow),0, $max );
			$array['carpwd'] = substr(mt_rand(time(),$pow),0, $max );
			$select = $this->_DB->select();
			$sql = $select->from($this->_name)->where(array('carid'=> $array['carid']))->limit(1)->toString();
			if (!$this->_DB->fetRow($sql)){
				$this->_DB->insert($this->_name, $array);
			}
			unset($sql);
		}
		systemlog::set(_('管理员发行了'.$i.'张电影卡!'));
		return true;
	}
	public function getAll($where = 'a.locked=0'){
		$select = $this->_DB->select();
		$sql = $select->from($this->_name. ' as a', 'a.*')->join($this->_name2.' as b', 'a.usergroup=b.id', 'b.title')->where($where)->order('a.id DESC')->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printPage = $this->_Page->printPage();
		//echo $sql;
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*
	搜索
	*/
	public function Search($POST){
		$select = $this->_DB->select();
		$where = ' 1=1';
		if ($POST['usergroup'] != 'all'){
			$where .= ' AND a.usergroup='.intval($POST['usergroup']);
		}
		if ($POST['usertype'] != 'all'){
			$where .= ' AND a.usertype='.intval($POST['usertype']);
		}
		if ($POST['stime'] && $POST['etime']){
			if ( (int)$POST['locked'] == 1){
				$where .= ' AND a.endtime<='.strtotime($POST['stime']).' AND a.endtime>='.strtotime($POST['etime']);
			}else{
				$where .= ' AND a.addtime<='.strtotime($POST['stime']).' AND a.addtime>='.strtotime($POST['etime']);
			}
		}
		if ($POST['keyword']){
			$where .= " AND a.".$POST['type']." like '%".$POST['keyword']."%'";
		}
		$where .= ' AND a.locked='.intval($POST['locked']);
		$sql = $select->from($this->_name. ' as a', 'a.*')->join($this->_name2.' as b', 'a.usergroup=b.id', 'b.title')->where($where)->order('a.id DESC')->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printPage = $this->_Page->printPage();
		//echo $sql;
		return $this->_DB->fetAll($sql.$this->_Page->limit());	
	}
	public function delete($id){
		if (is_array($id)){
			$this->_DB->delete($this->_name, 'id in ('.implode(',', $id).')');
		}else{
			$this->_DB->delete($this->_name, 'id='.(int)$id);
		}
		//echo $this->_DB->sql;
		$rows = $this->_DB->affected_rows();
		systemlog::set(_('删除了'.$rows.'条记录!'));
	}
	public function getRow($where=null){
		$select = $this->_DB->select();
		$select->from(VODCMS_CAR)->where($where)->limit(1);
		$sql = $select->toString();
		return $this->_DB->fetRow($sql);
	}
	public function stat(){
		$sql = 'select * from (SELECT count(id) as f,DATE_FORMAT(from_unixtime(addtime), "%Y-%m") as d FROM '.VODCMS_CAR.' WHERE locked=0 group by DATE_FORMAT(from_unixtime(addtime), "%Y-%m")) as a left join (SELECT count(id) as u,DATE_FORMAT(from_unixtime(addtime), "%Y-%m") as dd FROM '.VODCMS_CAR.' WHERE locked=1 group by DATE_FORMAT(from_unixtime(addtime), "%Y-%m")) as b on a.d=b.dd order by a.d desc';
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total);
		$this->printPage = $this->_Page->printPage();
		return $this->_DB->fetAll($sql.$this->_Page->limit());	
	}
	/**/
	public function changeState($POST){
		if($POST['username']){
			$user = new UsersModel();
			$array = array();
			$users = $user->getRow(array('username' => $POST['username']));
			if (!$users['uid']){
				$this->error = _('充值用户不存在!');
				return false;
			}
			$row = $this->getRow(array('carid' => $POST['carid'], 'carpwd' => $POST['carpwd']));
			if ($row['id']){
				if ($row['locked'] == 1){
					$this->error = _('影卡已经被使用过!');
					return false;
				}
				if ($row['usertype'] == 1){
					if ($users['lifetime']<=time()){
						$array['lifetime'] = time()+($row['usercount']*86400);
					}else{
						$array['lifetime'] = $users['lifetime']+($row['usercount']*86400);
					}
				}else{
					$array['point'] = $row['usercount'] + $users['point'];
				}
				$array['payment'] = $users['payment'] + 1;
				$array['usergroup'] = $row['usergroup'];
				$this->_DB->update(VODCMS_MEMBERS, $array, array('uid'=>(int)$users['uid']));
				$this->_DB->update(VODCMS_CAR, 'locked=1', array('id'=> $row['id']));
				systemlog::set(_('用户'.$POST['username'].'激活卡号为'.$POST['carid'].'的电影充值卡'));
				return true;
			}else{
				$this->error = _('影卡号码或者密码错误!');
				return  false;
			}
		}else{
			$this->_error = _('激活影卡的用户不能为空!');
			return false;
		}
	}
}