<?php
namespace Site\Model;

use Think\Model;

/**
 * 分类模型 开服
 * lwx
 */
class SiteServerModel extends Model{
	protected $_validate = array(
	);

	protected $_auto = array(
	);

	/**
	 * 构造函数
	 * @param string $name 模型名称
	 * @param string $tablePrefix 表前缀
	 * @param mixed $connection 数据库连接信息
	 */
	public function __construct($name = '', $tablePrefix = '', $connection = '') {
			/* 设置默认的表前缀 */
			$this->tablePrefix ='tab_';
			/* 执行构造方法 */
			parent::__construct($name, $tablePrefix, $connection);
	}
	
	/**
	 * 游戏开服列表
	 * @param integer	$p			当前页码
	 * @param string	$field	游戏开服表字段
	 * @param integer	$row		限制条数
	 * @param string	$order	排序条件
	 * @return array  				结果数据列表
	 */
	public function lists($p=1,$field=true,$row=10,$order='tab_site_server.start_time desc,tab_site_game.id desc') {

		$page = intval($p);
		
		$page = $page ? $page : 1; //默认显示第一页数据
		
		$map['tab_site_server.promote_id'] = PID;
		
		$map['tab_site_server.status'] = 1;

		$map['tab_site_server.start_time'] = $_GET['st']>0?array('egt',mktime(0,0,0,date('m'),date('d')+1,date('Y'))):($_GET['st']<0?array('lt',mktime(0,0,0,date('m'),date('d'),date('Y'))):array('between',array(mktime(0,0,0,date('m'),date('d'),date('Y')),mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1)));
		if($_GET['st']>0){
            $order='tab_site_server.start_time asc,tab_site_game.id asc';
        }
        if($_GET['st'] == 0){
            $order='tab_site_server.start_time asc,tab_site_game.id asc';
        }
		if($field === true) {$field = 'tab_site_server.*';}

		if (DEVICES) {$map['tab_site_server.server_version']=DEVICES;}
		
		$count = $this->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')->where($map)->count();
		
		$total = intval(($count-1)/$row)+1;

		if ($page>$total) {if (DEVICES) {return array('data'=>'','count'=>$count,'row'=>$row,'total'=>$total);}else{$page=$total;}}

		$data = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source')
		
					->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')
		
					->where($map)->order($order)->page($page,$row)->select();

		if (DEVICES)
			foreach ($data as $k => $v) {
				
				$data[$k]['downloadurl'] = $v['game_source']==2?($v['game_dow_url']?$v['game_dow_url']:''):('http://'.$_SERVER['HTTP_HOST'].U('Down/down_file',array('game_id'=>$v['game_id'],'promote_id'=>PID,'type'=>$v['sdk_version'])));
				
				$data[$k]['game_icon_url'] = get_cover($v['game_icon'],'path');
				
				$data[$k]['jump_url'] = U('Game/detail',array('name'=>$v['game_name'],'source'=>$v['game_source'],'account'=>I('account')));
				
				$data[$k]['game_name']=str_replace(array('(安卓版)','(苹果版)'),'',$v['game_name']);
				
				$data[$k]['start_time_format'] = date('m/d H:i',$v['start_time']);
				
				
			}
		return array('data'=>$data,'count'=>$count,'row'=>$row,'total'=>$total);

	}
	
	
	/**
	 * 游戏开服列表
	 * @param integer	$p			当前页码
	 * @param array		$map		搜索条件
	 * @param string	$field	游戏开服表字段
	 * @param integer	$row		限制条数
	 * @param string	$order	排序条件
	 * @return array  				结果数据列表
	 */
	public function otherlists($p=1,$map=array(),$field=true,$row=10,$order='tab_site_game.create_time desc,tab_site_game.id desc') {
		
		$page = intval($p);
		
		$page = $page ? $page : 1; //默认显示第一页数据
		
		$map['tab_site_server.promote_id'] = PID;
		
		$map['tab_site_server.status'] = 1;		
		
		if($field === true) {$field = 'tab_site_server.*';}
		
		$data = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source,count(distinct tab_site_server.server_name,tab_site_game.game_name,tab_site_game.game_source)')
		
				->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')
				
				->where($map)->group('tab_site_server.server_name,tab_site_game.game_name,tab_site_game.game_source')->order($order)->select();
		
		$count = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source,count(distinct tab_site_server.server_name,tab_site_game.game_name,tab_site_game.game_source)')
		
				->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')
				
				->where($map)->group('tab_site_server.server_name,tab_site_game.game_name,tab_site_game.game_source')->count();
		
		return array('data'=>$data,'count'=>$count,'row'=>$row);
		
	}
	
	/**
	 * 所有游戏开服列表
	 * @param array		$map		搜索条件
	 * @param string	$field	游戏开服表字段
	 * @param string	$order	排序条件
	 * @return array  				结果数据列表
	 */
	public function all($p,$map=array(),$field=true,$row=10,$order='tab_site_server.start_time asc,tab_site_server.id asc',$order1='tab_site_server.start_time desc,tab_site_server.id desc') {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
		$map['tab_site_server.promote_id'] = PID;
		
		$map['tab_site_server.status'] = 1;
		
		if($field === true) {$field = 'tab_site_server.*';}

		if (DEVICES) {
		    $map['tab_site_server.server_version']=DEVICES;
		}
		$count = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source')

            ->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')

            ->where($map)->count();
        $total = intval(($count-1)/$row)+1;
        if(DEVICES){$row = $count;};
        if ($page>$total) {if (DEVICES) {return array('data'=>'','count'=>$count,'row'=>$row,'total'=>$total);}else{$page=$total;}}
        $map['tab_site_server.start_time'] = array('egt',mktime(0,0,0,date('m'),date('d')+1,date('Y')));//即将开服;
        $data1 = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source')

            ->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')

            ->where($map)->order($order)->select();

        $map['tab_site_server.start_time'] = array('between',array(mktime(0,0,0,date('m'),date('d'),date('Y')),mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1));//今日开服

        $data2 = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source')

            ->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')

            ->where($map)->order($order)->select();

        $map['tab_site_server.start_time'] =array('lt',mktime(0,0,0,date('m'),date('d'),date('Y')));//已开服

        $data3 = $this->field($field.',tab_site_game.game_name,tab_site_game.game_icon,tab_site_game.game_type,tab_site_game.game_type_id,tab_site_game.game_source')

            ->join('tab_site_game on (tab_site_game.id=tab_site_server.game_id)','inner')

            ->where($map)->order($order1)->select();

		$data = array_merge_recursive($data1,$data2,$data3);
        $data = array_slice($data,($page-1)*$row,$row);
        return array('data'=>$data,'count'=>$count,'row'=>$row);
	}
	
	
	/**
	 * 取几条游戏数据
	 * @param array		$map		搜索条件
	 * @param integer	$limit	限制条数
	 * @param string	$field	游戏表字段
	 * @return array  				结果数据列表
	 */
	public function limits($map=array(),$limit=10,$field=true) {
		
		if (PID<=0) {return null;}
        if($field === true) {$field = 'tab_site_server.*';}
		$map['tab_site_server.promote_id'] = PID;
		$map['tab_site_server.status']=1;
		$order = "tab_site_server.start_time asc";
        $map['tab_site_server.start_time'] = array('between',array(mktime(0,0,0,date('m'),date('d'),date('Y')),mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1));//今日开服
        $data = $this->field($field.',tab_site_game.game_name')
            ->join('tab_site_game  on (tab_site_server.game_id=tab_site_game.id) ','inner')
            ->where($map)->order($order)->limit($limit)->select();
        return $data;
		
	}

}