<?php
class CaseModel extends DBModel {
	public $_name1;
	public $_name2;
	public function init(){
		$this->_name1 = VODCMS_SHOP;
		$this->_name2 = VODCMS_USERGROUP;
	}
	/*
	添加一个价格
	*/
	public function AddShop($POST){
		$POST['admin'] = $this->_Response->getCookie('admin');
		systemlog::set('添加一条价格设定记录!');
		return $this->_DB->insert($this->_name1, $POST);
	}
	/*
	获取全部价格记录
	*/
	public function getAll($where = null){
		$select = $this->_DB->select();
		$sql = $select->from($this->_name1.' AS a', 'a.*')->join($this->_name2.' AS b', 'a.usergroup=b.id', 'b.title as groupname, b.title')->where($where)->order('a.id DESC')->toString();
		return $this->_DB->fetAll($sql);
	}
	public function getCase($where = null, $orderby='a.id'){
		$select = $this->_DB->select();
		$select->from(VODCMS_PAYMENT.' as a', 'a.*');
		$select->join(VODCMS_USERGROUP.' as b', 'a.groupid=b.id', 'b.title as groupname');
		$select->where($where);
		$select->order($orderby.' 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 getList($where = null, $orderby='a.id'){
		$select = $this->_DB->select();
		$select->from(VODCMS_PAYMENT.' as a', 'a.*');
		$select->join(VODCMS_USERGROUP.' as b', 'a.groupid=b.id', 'b.title as groupname');
		$select->where($where);
		$select->order($orderby.' DESC');
		$sql = $select->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total, 10);
		$this->printPage = $this->_Page->PrintPage();
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*获取订单订单*/
	public function getRow($id){
		$select = $this->_DB->select();
		$sql = $select->from($this->_name1)->where(array('id'=>$id))->limit(1)->toString();
		return $this->_DB->fetRow($sql);
	}
	public function modify($POST){
		$POST['admin'] = $this->_Response->getCookie('admin');
		
		systemlog::set('修改了编号为'.$POST['id'].'的价格设定记录!');
		return $this->_DB->update($this->_name1, $POST, array('id'=>(int)$POST['id']));
	}
	/*
	生成系统订单号码
	*/
	public function makeOrder($data){
		$array = array();
		$array['orderno'] = $data['orderno'];
		$array['username'] = $data['username'];
		$array['usertype'] = $data['usertype'];
		$array['price'] = $data['money'];
		if ($data['usertype'] ==1){
			$array['point'] = $data['days'];
		}else{
			$array['point'] = $data['point'];
		}
		$array['groupid'] = $data['usergroup'];
		$array['payvia'] = $data['payvia'];
		$array['userip'] = $data['userip'];
		$array['recommend'] = $data['recommend'];
		$array['addtime'] = time();
		$array['state'] = 0;
		$this->_DB->insert(VODCMS_PAYMENT, $array);
		return $this->_DB->lastInsertId();
	}
	/*
	用户服务开通
	@param 参数为订单编号
	*/
	public function changeState($id){
		$select = $this->_DB->select();
		$select->from(VODCMS_PAYMENT)->where(array('state'=>0, 'id'=>(int)$id))->limit(1);
		$sql = $select->toString();
		$row = $this->_DB->fetRow($sql);
		$array = array();
		if ($row['id']){
			$username = $row['username'];
			$user = new UserModel();
			$users = $user->getRow(array('username'=>$username));
			$array['usergroup'] = $row['groupid'];
			$array['plays'] = 0;
			$array['locked'] = 0;
			$array['usertype'] = $row['usertype'];
			if ($row['usertype'] == 1){
				if($users['lifetime']<=time()){
					$array['lifetime'] = time()+($row['point']*86400);
				}else{
					$array['lifetime'] = $users['lifetime']+($row['point']*86400);
				}
				
			}else {
				$array['point'] = $users['point'] + $row['point'];
			}
			$array['payment'] = $users['payment']+1;
			$this->_DB->update(VODCMS_MEMBERS, $array, array('uid'=>(int)$users['uid']));
			//echo $this->_DB->sql;
			$mail = new Mail();
			if (file_exists(ROOT.'Config/revice.htm')){	//模板不存在则不发送
				$contents = file_get_contents(ROOT.'Config/revice.htm');
				$contents = str_replace('[username]', $users['username'], $contents);
				$contents = str_replace('[orderno]', $row['orderno'], $contents);
				$contents = str_replace('[date]', date('Y-m-d H:i:s'), $contents);
				$mail->sendMail($users['email'], '您的定单'.$row['orderno'].'已经开通成功!', $contents);
			}
			$v_payvia = $this->v_payvia;
			$this->_DB->update(VODCMS_PAYMENT, array('state'=>1, 'payvia'=>$v_payvia), array('id'=>$row['id']));
			return true;
		}else{
			return false;
		}
	}
	/*删除一个价格设定*/
	public function delShop($id){
		if (is_array($id)){
			$id = implode(',', array_map('intval', $id));
		}else{
			$id = intval($id);
		}
		systemlog::set('删除了一个编号为'.$id.'的订单记录');
		return $this->_DB->delete(VODCMS_SHOP, 'id in ('.$id.')');
	}
	/*删除一个订单*/
	public function delete($id){
		if (is_array($id)){
			$id = implode(',', array_map('intval', $id));
		}else{
			$id = intval($id);
		}
		systemlog::set('删除了一个编号为'.$id.'的订单记录');
		return $this->_DB->delete(VODCMS_PAYMENT, 'id in ('.$id.')');
	}
	/*订单统计报表*/
	public function report(){
		$sql = 'select * from (SELECT count(id) as f,DATE_FORMAT(from_unixtime(addtime), "%Y-%m") as d,count(price) as price1 FROM '.VODCMS_PAYMENT.' WHERE state=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,count(price) as price2 FROM '.VODCMS_PAYMENT.' WHERE state=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());	
	}
}
?>