<?php
namespace Mobile\Controller;
use Think\Controller;
use User\Api\SuserApi;

/**
* 游戏
*/
class GameController extends BaseController {

	protected function _initialize(){
        $config = api('Config/lists');
        C($config);
    }

  public function index() {
		$this->display();
	}

    public function lists($name='',$type='',$p=1) {
        $map = $this->condition($name,$type);
        $list = A("Game","Event")->game_lists('Game',$p,$map,true);
        $this->assign('lists',$list);
        $this->assign('category',array('title'=>$map['title']));
        $this->assign('name',$name);
        $this->assign('type',$type);
        $this->assign('page',$p);
        $this->display();
    }

    public function ajaxlists($name='',$type='',$condition='',$p=2) {
        $status = 0;
        $map = $this->condition($name,$type,$condition);
        $list = A("Game","Event")->game_lists('Game',$p,$map);
        if (!empty($list) && is_array($list)) {$status=1;}
        echo json_encode(array('status'=>$status,'page'=>$p,'lists'=>$list));
    }

    /**
     * 简介
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function detail($id='') {
        $this->game_share($id);
        if (!empty($id) && is_numeric($id)) {
            $game = M('Game', 'tab_')->field(['id', 'relation_game_id'])->where(['id' => $id])->find();

            if (empty($game)) {
                $this->error('此游戏不存在!');
            }

            $gameIds = M('Game', 'tab_')->where(['relation_game_id' => $game['relation_game_id']])->getField('id', true);

			$devices = get_devices_type();
            $promote_id = empty(PROMOTE_ID) ? 0 : PROMOTE_ID;
            $rebate_join = "and (r.promote_id = {$promote_id} or r.promote_id = -1)";
            $time = NOW_TIME;
            $data = D('Game')
                ->alias('g')
                ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio,IF(g.down_port=1,s.file_size,g.game_address_size) as game_size')
                //游戏原包
                ->join("left join tab_game_source as s on s.game_id = g.id")
                //折扣
                ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
                ->where(array('g.id'=>['in', $gameIds],'g.sdk_version'=>array('like','%'.$devices.'%'),'g.display_site'=>['like','%'.DISPLAY_SITE.'%']))
                ->find();

            if (empty($data) || !is_array($data)) {$this->error('此游戏不存在!!');}
//            if($data['game_size']==0){
//                $data['game_size'] = $data['game_address_size'];
//            }
            $map['game_id'] = $id;

						$member = new SuserApi;

            $map['account'] = $member->login_info('account');
            $collect = M("collect_game",'tab_')->where($map)->getField('status');
            if(empty($collect) || $collect == 2){
                $isCollect = 0;
            }else{
                $isCollect = 1;
            }
            $condition['g.relation_game_id'] = $data['relation_game_id'];
            $condition['g.id'] = array('neq',$id);
            $condition['g.game_status'] = 1;
            $condition['g.display_site'] = ['like','%'.DISPLAY_SITE.'%'];
            $data1 = D('Game')
                ->alias('g')
                ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio,IF(g.down_port=1,s.file_size,g.game_address_size) as game_size')
                //游戏原包
                ->join("left join tab_game_source as s on s.game_id = g.id")
                //折扣
                ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
                ->where($condition)
                ->find();
            if($data1){
                $this->assign("data1",$data1);
            }


						$this->type_number($id);
						if(!is_mobile_request()){
						    $data['relation_game_name'] = $data['game_name'];
                        }

            $this->assign('url','http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
            $this->assign('game_icon','http://'.$_SERVER['HTTP_HOST'].get_cover($data['icon'],'path'));
            $this->assign('data',$data);
            $this->assign('isCollect',$isCollect);
            $this->assign('devices',$devices);
            $this->assign('mate_title',$data['game_name']);
            $this->display();

        } else {
            echo "<script>javascript:history.go(-1);</script>";exit;
        }

    }

    /**
     * 礼包
     * @param  string  $id [description]
     * @param  integer $p  [description]
     * @return [type]      [description]
     */
    public function gift($id='',$p=1) {
        $this->game_share($id);
				$devices = get_devices_type();
        if (!empty($id) && is_numeric($id) && $id>0) {
            $promote_id = empty(PROMOTE_ID) ? 0 : PROMOTE_ID;
            $rebate_join = "and (r.promote_id = {$promote_id} or r.promote_id = -1)";
            $time = NOW_TIME;
            $data = D('Game')
                ->alias('g')
                ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
                //游戏原包
                ->join("left join tab_game_source as s on s.game_id = g.id")
                //折扣
                ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
                ->where(array('g.id'=>$id,'g.sdk_version'=>array('like','%'.$devices.'%'),'g.display_site'=>['like','%'.DISPLAY_SITE.'%']))
                ->find();
						if(!is_mobile_request()){
                            $data['relation_game_name'] = $data['game_name'];
                        }
                        if($data['game_size']==0){
                $data['game_size'] = $data['game_address_size'];
            }
            $this->assign('data',$data);
						$map['game_id'] = $id;$member = new SuserApi;
						$map['account'] = $member->login_info('account');
            $collect = M("collect_game",'tab_')->where($map)->getField('status');
            if(empty($collect) || $collect == 2){
                $isCollect = 0;
            }else{
                $isCollect = 1;
            }
            $condition['g.relation_game_id'] = $data['relation_game_id'];
            $condition['g.id'] = array('neq',$id);
            $condition['g.game_status'] = 1;
            $condition['g.display_site'] = ['like','%'.DISPLAY_SITE.'%'];
            $data1 = D('Game')
                ->alias('g')
                ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
                //游戏原包
                ->join("left join tab_game_source as s on s.game_id = g.id")
                //折扣
                ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
                ->where($condition)
                ->find();
            if($data1){
                $this->assign("data1",$data1);
            }

						$this->type_number($id);

            $giftbag = D("Giftbag");
            $row = C('LIST_ROWS');
            $time = time();
            $lists = $giftbag->where("game_id=$id and status=1 and (end_time > $time or end_time = 0)")->page($p,$row)->order('create_time desc')->select();
            $count = $giftbag->where("game_id=$id")->count();
            $total = ceil($count/$row);
            $this->assign('mate_title',$data['game_name']);
            $this->assign('total',$total);

        }

        foreach ($lists as $key => $val){
            $novice_arr = str2arr($val['novice'],',');
            $novice_num = count(array_filter($novice_arr));
            $lists[$key]['novice_num'] = $novice_num;$member = new SuserApi;
						$exist = D("Giftbag")->checkAccountGiftExist($member->login_info('user_id'),$val['id']);
            if(empty($exist)){
                $exist = 1;//未领取
            }else{
                $exist = 0;//已领取
            }

            if($novice_num == 0){
                $exist = 2;//激活码已领取完
            }
            $lists[$key]['exist'] = $exist;
            $lists[$key]['icon'] = get_cover($data['icon'],'path');
            $lists[$key]['novice_num'] = $novice_num;
        }
        $this->assign('lists',$lists);
        $this->assign('page',1);
        $this->assign('mate_title',$data['relation_game_name']);
				$this->assign('devices',$devices);
				$this->assign('isCollect',$isCollect);
        $this->display();
    }

    public function ajaxGift(){
        $id = $_POST['id'];
				$devices = get_devices_type();
        if (!empty($id) && is_numeric($id) && $id>0) {
            $p = I('post.p');
            $status=0;
            $row = C('LIST_ROWS');
            $promote_id = empty(PROMOTE_ID) ? 0 : PROMOTE_ID;
            $rebate_join = "and (r.promote_id = {$promote_id} or r.promote_id = -1)";
            $time = NOW_TIME;
            $data = D('Game')
                ->alias('g')
                ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
                //游戏原包
                ->join("left join tab_game_source as s on s.game_id = g.id")
                //折扣
                ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and endtime = 0 or endtime > {$time}")
                ->where(array('g.id'=>$id,'g.sdk_version'=>array('like','%'.$devices.'%'),'g.display_site'=>['like','%'.DISPLAY_SITE.'%']))
                ->find();
                if(!is_mobile_request()){
                    $data['relation_game_name'] = $data['game_name'];
                }
            $this->assign('data',$data);
            $giftbag = D("Giftbag");
            $row = C('LIST_ROWS');
            $time = time();
            $lists = $giftbag->where("game_id=$id and status=1 and (end_time > $time or end_time = 0)")->page($p,$row)->select();

            foreach ($lists as $key => $val){
                $novice_arr = str2arr($val['novice'],',');
                $novice_num = count(array_filter($novice_arr));
                $lists[$key]['novice_num'] = $novice_num;$member = new SuserApi;
                $exist = D("Giftbag")->checkAccountGiftExist($member->login_info('user_id'),$val['id']);
                if(empty($exist)){
                    $exist = 1;//未领取
                }else{
                    $exist = 0;//已领取
                }

                if($novice_num == 0){
                    $exist = 2;//激活码已领取完
                }
                $lists[$key]['exist'] = $exist;
                $lists[$key]['icon'] = get_cover($data['icon'],'path');
                $lists[$key]['novice_num'] = $novice_num;
            }

        }
        if (!empty($lists) && is_array($lists)) {
            $status = 1;
        }
        echo json_encode(array('status'=>$status,'page'=>$p,'lists'=>$lists));
    }

    public function category() {
        $this->assign('category',array('title'=>'分类'));
        $this->display();
    }


    /**
     * 开服
     * @param  [type]  $id [description]
     * @param  integer $p  [description]
     * @return [type]      [description]
     */
    public function open($id , $p=1) {
        $this->game_share($id);
				$devices = get_devices_type();
        $page = $page ? $page : 1; //默认显示第一页数据
        $row = C('LIST_ROWS');
        $promote_id = empty(PROMOTE_ID) ? 0 : PROMOTE_ID;
        $rebate_join = "and (r.promote_id = {$promote_id} or r.promote_id = -1)";
        $time = NOW_TIME;
        $data = D('Game')
            ->alias('g')
            ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
            //游戏原包
            ->join("left join tab_game_source as s on s.game_id = g.id")
            //折扣
            ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
            ->where(array('g.id'=>$id,'g.sdk_version'=>array('like','%'.$devices.'%'),'g.display_site'=>['like','%'.DISPLAY_SITE.'%']))
            ->find();
			if(!is_mobile_request()){
                $data['relation_game_name'] = $data['game_name'];
            }
            if($data['game_size']==0){
                $data['game_size'] = $data['game_address_size'];
            }
        $this->assign('data',$data);
				$map['game_id'] = $id;$member = new SuserApi;
            $map['account'] = $member->login_info('account');
            $collect = M("collect_game",'tab_')->where($map)->getField('status');
            if(empty($collect) || $collect == 2){
                $isCollect = 0;
            }else{
                $isCollect = 1;
            }
        $condition['g.relation_game_id'] = $data['relation_game_id'];
        $condition['g.id'] = array('neq',$id);
        $condition['g.game_status'] = 1;
        $condition['g.display_site'] = ['like','%'.DISPLAY_SITE.'%'];
        $data1 = D('Game')
            ->alias('g')
            ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
            //游戏原包
            ->join("left join tab_game_source as s on s.game_id = g.id")
            //折扣
            ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
            ->where($condition)
            ->find();
        if($data1){
            $this->assign("data1",$data1);
        }

				$this->type_number($id);

        $where['game_id'] = $id;
        $where['show_status'] = 1;
        $lists = D("Server")
            ->where($where)
            ->page($page, $row)
            ->select();

        //if (empty($lists) || !is_array($lists)) {$this->error('暂无数据');}
        $time = time();
        foreach ($lists as $key => $value) {
            $status = $value['start_time']>= $time ? '<span class="blue">即将开服</span>' : '<span>已开服</span>';
            $lists[$key]['status'] = $status;
        }

        $this->assign('lists',$lists);
        $this->assign('page',$page);
        $this->assign('mate_title',$data['relation_game_name']);
				$this->assign('devices',$devices);
				$this->assign('isCollect',$isCollect);
        $this->display();
    }

    public function ajaxopen() {
        $p = I('post.p');
				$devices = get_devices_type();

        $id = I('post.id');
        $status = 0;
        $row = C('LIST_ROWS');  // C('LIST_ROWS')
        $where['game_id'] = $id;
        $where['show_status'] = 1;
        $list = D("Server")
            ->where($where)
            ->page($p, $row)
            ->select();
        foreach ($list as $key => $value) {
            $status = $value['start_time']>= $time ? '<span class="blue">即将开服</span>' : '<span>已开服</span>';
            $list[$key]['status'] = $status;
            $list[$key]['time'] = date('H:i',$value['start_time']);
            $list[$key]['date'] = date('Y-m-d',$value['start_time']);
        }
        if (!empty($list) && is_array($list)) {$status=1;}

        echo json_encode(array('status'=>$status,'page'=>$p,'lists'=>$list));
    }

    /**
     * 评论
     * @param  [type]  $id [description]
     * @param  integer $p  [description]
     * @return [type]      [description]
     */
    public function comment($id , $p=1) {
        $this->game_share($id);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row = C('LIST_ROWS');
				$devices = get_devices_type();

        $promote_id = empty(PROMOTE_ID) ? 0 : PROMOTE_ID;
        $rebate_join = "and (r.promote_id = {$promote_id} or r.promote_id = -1)";
        $time = NOW_TIME;
        $data = D('Game')
            ->alias('g')
            ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
            //游戏原包
            ->join("left join tab_game_source as s on s.game_id = g.id")
            //折扣
            ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
            ->where(array('g.id'=>$id,'g.sdk_version'=>array('like','%'.$devices.'%'),'g.display_site'=>['like','%'.DISPLAY_SITE.'%']))
            ->find();

			if (empty($data) || !is_array($data)) {$this->error('此游戏不存在!!');}

			if(!is_mobile_request()){
                $data['relation_game_name'] = $data['game_name'];
            }
            if($data['game_size']==0){
                $data['game_size'] = $data['game_address_size'];
            }
        $this->assign('data',$data);
				$map['game_id'] = $id;$member = new SuserApi;
            $map['account'] = $member->login_info('account');
            $collect = M("collect_game",'tab_')->where($map)->getField('status');
            if(empty($collect) || $collect == 2){
                $isCollect = 0;
            }else{
                $isCollect = 1;
            }
        $condition['g.relation_game_id'] = $data['relation_game_id'];
        $condition['g.id'] = array('neq',$id);
        $condition['g.game_status'] = 1;
        $condition['g.display_site'] = ['like','%'.DISPLAY_SITE.'%'];
        $data1 = D('Game')
            ->alias('g')
            ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
            //游戏原包
            ->join("left join tab_game_source as s on s.game_id = g.id")
            //折扣
            ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
            ->where($condition)
            ->find();
        if($data1){
            $this->assign("data1",$data1);
        }

				$this->type_number($id);

        $where['game_id'] = $id;
        $where['status'] = 1;

        $lists = D('comment')
            ->field("account,create_time,comment,game_id")
            ->where($where)
            ->order("create_time desc")
            ->page($page,$row)
            ->select();

        foreach ($lists as $key => $val){
            $user = D("User")->where(array('account'=>$val['account']))->find();
            $lists[$key]['nickname'] = $user['nickname'];
            $lists[$key]['head_img'] = is_numeric($user['head_img'])?get_cover($user['head_img'],"path"):$user['head_img'];
            $lists[$key]['create_time'] = date('Y-m-d H:i',$val['create_time']);
        }
        $this->assign('mate_title',$data['relation_game_name']);
        $this->assign('lists',$lists);
        $this->assign('page',$page);$this->assign('mate_title',$data['relation_game_name']);
				$this->assign('devices',$devices);
				$this->assign('isCollect',$isCollect);
        $this->display();
    }

    public function ajaxcomment() {
        $p = I('post.p');
				$devices = get_devices_type();

        $id = I('post.id');
        $status = 0;
        $row = C('LIST_ROWS');  // C('LIST_ROWS')
        $where['game_id'] = $id;
        $where['show_status'] = 1;
        $where['status'] = 1;

        $lists = D('comment')
            ->field("account,create_time,comment,game_id")
            ->where($where)
            ->order("create_time desc")
            ->page($page,$row)
            ->select();

        foreach ($lists as $key => $val){
            $user = D("User")->where(array('account'=>$val['account']))->find();
            $lists[$key]['nickname'] = $user['nickname'];
            $lists[$key]['head_img'] = $user['head_img'];
            $lists[$key]['create_time'] = date('Y-m-d H:i',$val['create_time']);
        }
        if (!empty($list) && is_array($list)) {$status=1;}

        echo json_encode(array('status'=>$status,'page'=>$p,'lists'=>$list));
    }

    public function ajax_add_comment() {
        if(IS_POST){
            $post = I('post.');
						$member = new SuserApi;
						$logininfo = $member->login_info();
            if($logininfo){

                $data = array();
                $data['comment'] = $post['comment'];
                $data['game_id'] = $post['game_id'];
                $data['status'] = 0;
                $data['account'] = $logininfo['account'];
                $data['create_time'] = time();
                $Model =  D('comment');
                if(trim($post['comment'])){
                    if($Model->create()){
                        $res = $Model ->add($data);
                        if($res){
                            $result = array('code'=>1,'msg'=>'发布评论成功,等待审核');
                        }else{
                            $result = array('code'=>0,'msg'=>'发布评论失败');
                        }
                    } else {
                        $result = array('code'=>0,'msg'=>$Model->getError());
                    }
                }else{
                    $result = array('code'=>0,'msg'=>'请输入评论');
                }
            }else{
                $result = array('code'=>0,'msg'=>'请登录');
            }
        }

        echo json_encode($result);
    }

    public function rank($name='',$p=1) {
        $map = $this->condition($name,'','rank');

        $list = A("Game","Event")->game_lists('Game',$p,$map,true);

        if (empty($list) || !is_array($list)) {$this->error('暂无数据');}

        $this->assign('lists',$list);

        $this->assign('category',array('title'=>'游戏排行榜'));
        $this->assign('name',$name);
        $this->assign('page',$p);
        $this->display();
    }


    public function condition($name='',$type='',$condition='') {
        $where = " game_status=1 and apply_status = 1 and display_site like '%".DISPLAY_SITE."%' ";
        if ($condition == 'rank') {
            switch ($name) {
                case 'hp':
                    $map['order'] = "game_score desc";
                    break;
                default:
                    $name = '';
                    $map['order'] = "dow_num desc";
                    $map['map']['recommend_status'] = 2;
            }
            $map['map']['_string'] = $where;
        } else {
            if ($type && is_numeric($type))
                switch($name) {
                    case 'category':$where .= " and category = $type ";$title = get_open_type($type);break;
                    default:$where .= " and game_type_id = $type ";$title = get_game_type_name($type);
                }
            $map['map']['_string'] = $where;
            $map['title'] = empty($title)?'全部':$title;
        }
        return $map;
    }

    /**
     * 游戏收藏
     * @param $game_id
     * @param $token
     * @param $status   1 收藏 2 取消收藏
     * author: xmy 280564871@qq.com
     */
    public function collect_game($game_id,$status=1){
        if(!$this->islogin()){
            $this->error('请先登录');
        }$member = new SuserApi;
        $logininfo =$member->login_info();
        $result = D("Game")->collectGame($game_id,$logininfo['account'],$status);
        if($result !== false){
            $msg = $status == 1?"收藏成功":"取消成功";
            $this->success($msg);
        }else{
            $this->error("收藏失败");
        }
    }


		/**
     * 游戏交易
     * @param  [type]  $id [description]
     * @param  integer $p  [description]
     * @return [type]      [description]
     * author: 鹿文学
     */
		public function trade($id , $p=1) {
            $this->game_share($id);
			$this->game_info($id);
			$map['game_id'] = $id;
			$map['status'] = 3;

			$lists = D('Merchandise')->lists($p,$map,'id,title,game_name,online_time,price,screenshot');

			foreach($lists['lists'] as $k => $v) {
				$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'online_time'=>date('Y-m-d H:i:s',$v['online_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
				$data[$k]['icon'] = '';
				if(!empty($v['screenshot'])) {
				$icon = explode(',',$v['screenshot']);
				$data[$k]['icon'] = get_cover($icon[0],'path');
				}
				$data[$k]['url'] = U('Trade/detail',array('id'=>$v['id']));
			}

			$this->assign('lists',$data);
			$this->assign('page',$lists['current']);
			$this->assign('totalpage',$lists['total']);

			$this->display();
		}

		public function ajaxtrade($id , $p=1) {
			$map['game_id'] = $id;
			$map['status'] = 3;

			$lists = D('Merchandise')->lists($p,$map,'id,title,game_name,online_time,price,screenshot');

			foreach($lists['lists'] as $k => $v) {
				$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'online_time'=>date('Y-m-d H:i:s',$v['online_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
				$data[$k]['icon'] = '';
				if(!empty($v['screenshot'])) {
				$icon = explode(',',$v['screenshot']);
				$data[$k]['icon'] = get_cover($icon[0],'path');
				}
				$data[$k]['url'] = U('Trade/detail',array('id'=>$v['id']));
			}

			$lists['lists'] = $data;

			$this->ajaxReturn($lists,'json');

		}

		/**
		 * 游戏信息
		 * @param   integer   $id   游戏编号
		 * @author   鹿文学
		 */
		private function game_info($id) {
			$devices = get_devices_type();

			$promote_id = empty(PROMOTE_ID) ? 0 : PROMOTE_ID;
			$rebate_join = "and (r.promote_id = {$promote_id} or r.promote_id = -1)";
			$time = NOW_TIME;
			$data = D('Game')
					->alias('g')
					->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
					//游戏原包
					->join("left join tab_game_source as s on s.game_id = g.id")
					//折扣
					->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
					->where(array('g.id'=>$id,'g.sdk_version'=>array('like','%'.$devices.'%'),'g.display_site'=>['like','%'.DISPLAY_SITE.'%']))
					->find();
			if (empty($data) || !is_array($data)) {$this->error('此游戏不存在!!');}
			$data['icon'] = get_cover($data['icon'],'path');
			if(!is_mobile_request()){
                $data['relation_game_name'] = $data['game_name'];
            }
            if($data['game_size']==0){
                $data['game_size'] = $data['game_address_size'];
            }
			$this->assign('data',$data);
			$map['game_id'] = $id;$member = new SuserApi;
			$map['account'] = $member->login_info('account');
			$collect = M("collect_game",'tab_')->where($map)->getField('status');
			if(empty($collect) || $collect == 2){
					$isCollect = 0;
			}else{
					$isCollect = 1;
			}
			$condition['g.relation_game_id'] = $data['relation_game_id'];
        $condition['g.id'] = array('neq',$id);
        $condition['g.game_status'] = 1;
        $condition['g.display_site'] = ['like','%'.DISPLAY_SITE.'%'];
        $data1 = D('Game')
            ->alias('g')
            ->field('g.*,g.ratio as gratio,g.bind_recharge_discount as discount,s.pack_name,IFNULL(r.ratio,0) as ratio')
            //游戏原包
            ->join("left join tab_game_source as s on s.game_id = g.id")
            //折扣
            ->join("left join tab_rebate r on r.game_id = g.id {$rebate_join} and r.starttime < {$time} and (endtime = 0 or endtime > {$time})")
            ->where($condition)
            ->find();
        if($data1){
            $this->assign("data1",$data1);
        }

				$this->assign('devices',$devices);
				$this->assign('mate_title',$data['relation_game_name']);
				 $this->assign('isCollect',$isCollect);


				$this->type_number($id);


		}

		/**
		 * 统计评论及交易
		 * @author  鹿文学
		 */
		private function type_number($id) {
			// 评论
			$map['game_id'] = $id;
			$map['status'] = 1;
			$comment_count = D('comment')->total($map);
			$this->assign('comment_count',number_format_on_mobile($comment_count));

			// 交易
			$map['status'] = 3;
			$trade_count = D('Merchandise')->total($map);
			$this->assign('trade_count',number_format_on_mobile($trade_count));
		}

    /**
     *分享参数
     * lcj
     */
	private function game_share($id){
        $game_data = M('Game','tab_')->field('icon,introduction')->where(['id'=>$id,'display_site'=>['like','%'.DISPLAY_SITE.'%']])->find();
        if(empty($game_data)){
            return '';
        }else{
            $user = D('User')->getLoginInfo();
            //$data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/mobile.php?s=/Game/detail/id/'.$id;
            $data['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('Game/detail',array('id'=>$id,'account'=>$user['user_id']));
            $data['icon'] = icon_url($game_data['icon']);
            $data['introduction'] = $game_data['introduction'];
            $this->assign('sahre_data',$data);
        }
    }

}