<?php
namespace app\index\controller;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Game extends Commone
{
    
    public function list(){
        $this->AI();
        $this->INDEX_AI();
        $type = $this->request->param('t');
        $this->assign('t',$type);
        return view();
    }
    
    public function game_my(){
        $this->AI();
        $this->INDEX_AI();
        return view();
    }
    
    public function game_list(){
        $this->AI();
        $keyword = $this->request->param('game_name');
        $where['game_name'] = ['LIKE',"%".$keyword."%"];
        $where['game_status'] = 1;
        if($this->request->param('t')!=0){
            $where['game_type_id'] = $this->request->param('t');
        }
        
        $orders_where['userid'] = $this->get_user('id');
        $orders_where['state'] = 2;
        
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
            $user_game_id = config('game_id');
        }
        
        $jr_sql = Db::name('orders')->where($orders_where)->field('payc,sum(money) jr_pay')->whereTime('pay_time', 'today')->group('payc')->buildSql();
        $zr_sql = Db::name("orders")->where($orders_where)->field('payc,sum(money) zr_pay')->whereTime('pay_time', 'yesterday')->group('payc')->buildSql();
       
        
        $list = Db::name("game_list")->alias("a")
        ->join([$jr_sql=>'b'],'b.payc=a.id','LEFT')
        ->join([$zr_sql=>'c'],'c.payc=a.id','LEFT')
        ->field('a.*,b.jr_pay,c.zr_pay')
        ->where($where)
        ->where('id','in',$user_game_id)
        ->order('id asc')
        ->paginate(['list_rows'=> $this->request->param("limit")??100,'var_page' => 'page',]);
        // echo Db::name("game_list")->getLastSql();

        for ($i = 0; $i < count($list); $i++) {
            $temp = $list[$i];
            if($list[$i]['jr_pay']==''){
                $temp['jr_pay'] = 0;
             
            }
            if($list[$i]['zr_pay']==''){
                $temp['zr_pay'] = 0;
                
            }
            $game_where['game_id'] = $list[$i]['id'];
            $game_where['userid'] = $this->get_user('id');
            $game_my = Db::name('game_my')->where($game_where)->find();
            $land_count = Db::name('land')->where(['typec'=>$list[$i]['id'],'userid'=>$this->get_user('id')])->count();
            $temp['land_count'] = $land_count;
            if($game_my){
                $temp['game_my'] = 0;
            }else{
                $temp['game_my'] = 1;
            }
            $list[$i] = $temp;
        }
      
        

        $this->json_success("",$list);
    }


    public function game_my_list(){
        
        $this->AI();
        $this->INDEX_AI();
        $keyword = $this->request->param('game_name');
        $game_my = Db::name('game_my')->where('userid',$this->get_user('id'))->select();
        $game_id = '';
        $ve = '';
        for ($i = 0; $i < count($game_my); $i++) {
         if($i>0){
             $ve=',';
         }
        
         $game_id=$game_id.$ve.$game_my[$i]['game_id'];
        }
        $where['game_name'] = ['LIKE',"%".$keyword."%"];
        $where['game_status'] = 1;
        $where['id'] = ['in',$game_id];
        $orders_where['userid'] = $this->get_user('id');
        $orders_where['state'] = 2;
        
        $jr_sql = Db::name('orders')->where($orders_where)->field('payc,sum(money) jr_pay')->whereTime('pay_time', 'today')->group('payc')->buildSql();
        $zr_sql = Db::name("orders")->where($orders_where)->field('payc,sum(money) zr_pay')->whereTime('pay_time', 'yesterday')->group('payc')->buildSql();
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
            $user_game_id = config('game_id');
        }
        
        $list = Db::name("game_list")->alias("a")
        ->join([$jr_sql=>'b'],'b.payc=a.id','LEFT')
        ->join([$zr_sql=>'c'],'c.payc=a.id','LEFT')
        ->field('a.*,b.jr_pay,c.zr_pay')
        ->where($where)
        ->where('id','in',$user_game_id)
        ->order('id asc')
        ->paginate(['list_rows'=> $this->request->param("limit")??100,'var_page' => 'page',]);
        // echo Db::name("game_list")->getLastSql();

        for ($i = 0; $i < count($list); $i++) {
            $temp = $list[$i];
            if($list[$i]['jr_pay']==''){
                $temp['jr_pay'] = 0;
             
            }
            if($list[$i]['zr_pay']==''){
                $temp['zr_pay'] = 0;
                
            }
            $game_where['game_id'] = $list[$i]['id'];
            $game_where['userid'] = $this->get_user('id');
            $game_my = Db::name('game_my')->where($game_where)->find();
            $land_count = Db::name('land')->where(['typec'=>$list[$i]['id'],'userid'=>$this->get_user('id')])->count();
            $temp['land_count'] = $land_count;
            if($game_my){
                $temp['game_my'] = 0;
            }else{
                $temp['game_my'] = 1;
            }
            $list[$i] = $temp;
        }
        

        $this->json_success("",$list);
    }
    
    //新增账号
    public function land_add(){
        $this->AI();
        $this->INDEX_AI();
        $post = $this->request->post();
        $post1 = $this->request->post();
        if(!isset($post['r_money'])){
            $this->error("日限额不可为空");
            
        }
        if(!isset($post['z_money'])){
            $this->error("总限额不可为空");
            
        }
        if(!isset($post['username'])){
            $this->error("账号备注不可为空");
            
        }
        if(!islang_str($post['username'],4,56)){
            $this->error("账号备注长度限制4-56字符");
            
        }
        $method_array = array('get','post');
        if (!in_array($post['method'], $method_array)){
            $this->error("回调类型有误");
            
        }
        
       
        $json = encode(json_encode(array(
            'method'=>$post['method'],
            'data'=>'key=[key]&money=[money]&amount=[amount]&order=[order]&record=[record]&remark=[remark]&sign=[sign]',
            'header'=>'',
            'cookie'=>''
        )));
        
        $post['reback']=$json;
        unset($post['method']);
        
        //查询账号备注是否存在
        $user = Db::name('land')->where('username',$post['username'])->find();
        if($user){
            $this->error("账号备注已存在");
        }
        
        
          unset($post1['username']);
          unset($post1['game_id']);
          unset($post1['r_money']);
          unset($post1['z_money']);
          unset($post1['land_id']);
          unset($post1['method']);
          $json_1 = json_encode($post1);  
        
        
        if($post['game_id']==4){
          $status['alipay_status'] = '0';
          $json_1 = json_encode($status);  
        }
        
        $data = array(
            'mid'=>substr(md5(time() . mt_rand(1000,9999) . $post['game_id']), 0,6),
            'userid'=>$this->get_user('id'),
            'username'=>$post['username'],
            'typec'=>$post['game_id'],
            'reback'=>$json,
            'ban'=>1,
            'timec'=>time(),
			'json'=>$json_1,
			'r_money'=>$post['r_money'],
			'z_money'=>$post['z_money'],
			'bug'=>0,
			'yc_bei'=>0
            );
        
        $land_add = Db::name('land')->insert($data);
        
        if($land_add){
            $this->success("添加成功"); 
        }else{
            $this->error("添加失败");
        }
        
    }
    
    
    public function cy_set(){
        $this->AI();
        $this->INDEX_AI();
        $post = $this->request->post();
        $game_my_where['game_id'] = $post['id'];
        $game_my_where['userid'] = $this->get_user('id');
        $game_my = Db::name('game_my')->where($game_my_where)->find();
        if($game_my){
           $game_save = Db::name('game_my')->where($game_my_where)->delete(); 
           $type = 1;
        }else{
           $game_save = Db::name('game_my')->insert(array('userid'=>$this->get_user('id'),'game_id'=>$post['id']));
           $type = 2;
        }
        
        if($game_save){
            if($type==1){
                $this->success("取消常用成功"); 
            }else if($type==2){
                $this->success("设置常用成功");
            }
        }else{
            $this->error("操作失败");
        }
        
        
    }
    
    public function land_list(){
         $this->AI();
         $this->INDEX_AI();
         $id = $this->request->param('id');
         $this->assign('id',$id);
         return view(); 
    }
    
    public function land_ajax_list(){
        $this->AI();
        $this->INDEX_AI();
        $where = $this->getparam(['id','username','typec']);
        $where['userid'] = $this->get_user('id');
        $list = Db::name("land")->where($where)->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        $user_game_id = $this->get_user('game_id');
        if($user_game_id == 0){
            $user_game_id = config('game_id');
        }
         for ($i = 0; $i < count($list); $i++) {
             $user_where['id'] = $list[$i]['userid'];
             $game_where['id'] = $list[$i]['typec'];
             $orders_where['land_id'] = $list[$i]['id'];
             $orders_where['state'] = 2;
             $temp = $list[$i];
             
             $daili_user = Db::name("user")->where($user_where)->find();
             $game_list = Db::name("game_list")->where('id','in',$user_game_id)->where($game_where)->find();
             
             $jr_money = Db::name("orders")->where($orders_where)->whereTime('pay_time', 'today')->sum('money');
             $zr_money = Db::name("orders")->where($orders_where)->whereTime('pay_time', 'yesterday')->sum('money');
             $z_money = Db::name("orders")->where($orders_where)->sum('money');
             
             $typec = $game_list['game_name'] ;
             $name = $user['name'] ;
             
             $json = json_decode($list[$i]['json'],true);
             $ck = "";
             
			 if($game_list['game_type']=='1'){
					$s=$list[$i]['json'];
					preg_match_all('/ck(.+?)"/',$s,$r);
					
					$m=count($r[1]);
				
					for($k = 0;$k<$m;$k++){
					    if($r[1][$k]!=''){
					    $ck =  $ck.$r[1][$k].'元';
					       if($k<$m-1){$ck =  $ck.' / ';}
					    }
					    
					}
					
			 }else{
			     
					$ck = $ck.'---';
			 }
			 
             $temp['name'] = $name;
             $temp['typec_name'] = $typec;
             $temp['jr_pay'] = $jr_money;
             $temp['zr_pay'] = $zr_money;
             $temp['z_pay'] = $z_money;
             $temp['alipay_status'] = $json['alipay_status'];
             $temp['ck'] = $ck;
             $list[$i] = $temp;
             
        }
       
        
        $this->json_success("",$list);
	     
    }
}