<?php
namespace app\index\controller;

use \think\Controller;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Admin extends Commone
{
     public function config(){
         $this->AI();
         $this->ADMIN_AI();
         $config = Db::name('config')->where('id',1)->find();
	     $this->assign('config',$config);
         return $this->fetch();
     } 
     
     public function config_set(){
         $this->AI();
         $this->ADMIN_AI();
         $config = Db::name('set')->select();
        
	     $this->assign('config',$config);
         return $this->fetch();
     } 
     
     public function msg(){
         $this->AI();
         $this->ADMIN_AI();
         $config = Db::name('config')->where('id',1)->find();
	     $this->assign('config',$config);
         return $this->fetch();
     } 
     
     //配置保存
    
    public function config_save(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $_POST;
        $save = Db::name('config')->where('id',1)->update($post);
        if($save){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    
    public function config_set_save(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $_POST;
        foreach ( $_POST as $key => $value )
        {
           $save = Db::name('set')->where('key',$key)->update(['value'=>$value]);
        }
        
        
        $this->success("修改成功");
        
    }
    
    public function home(){
	    $this->AI();
        $this->ADMIN_AI();
        $where['state'] = 2;
        $tj_data['jr_pay'] = Db::name('orders')->where($where)->whereTime('pay_time', 'today')->sum('money');
        $tj_data['zr_pay'] = Db::name('orders')->where($where)->whereTime('pay_time', 'yesterday')->sum('money');
        $tj_data['jr_count'] = Db::name('orders')->where($where)->whereTime('pay_time', 'today')->count();
        $tj_data['zr_count'] = Db::name('orders')->where($where)->whereTime('pay_time', 'yesterday')->count();
        $tj_data['jr_sxf'] = Db::name('log')->whereTime('time', 'today')->sum('money');
        $tj_data['zr_sxf'] = Db::name('log')->whereTime('time', 'yesterday')->sum('money');
        $tj_data['user_num'] = Db::name('user')->count();
        $tj_data['user_hy'] = Db::name('user')->whereTime('create_time', 'today')->count();
        $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
        
        for ($i = 0; $i < 8; $i++) {
             $time[] = date("m-d",time()-86400*$i);
             $time_1 = date("Y-m-d 00:00:00",$nowTime-86400*$i);
             $time_2 = date("Y-m-d 00:00:00",$nowTime-86400*($i - 1));
             $biao_money[] = Db::name('orders')->where($where)->whereTime('pay_time', 'between', [$time_1, $time_2])->sum('money');
             $biao_count[] = Db::name('orders')->where($where)->whereTime('pay_time', 'between', [$time_1, $time_2])->count();
             $sxf_money[] = Db::name('log')->whereTime('time', 'between', [$time_1, $time_2])->sum('money');
        }
        
        if(config('cate') == '1'){
            $web_type = '多商户系统';
        }else{
            $web_type = '单商户系统';
        }
        
        $this->assign('time',json_encode(array_reverse($time)));
        $this->assign('biao_money',json_encode(array_reverse($biao_money)));
        $this->assign('biao_count',json_encode(array_reverse($biao_count)));
        $this->assign('sxf_money',json_encode(array_reverse($sxf_money)));
        $this->assign('version',config('version'));
        $this->assign('web_type',$web_type);
        $this->assign('name',config('name'));
        $this->assign('tj_data',$tj_data);
       
        return $this->fetch();
	}
     
     public function money(){
          $this->AI();
          $this->ADMIN_AI();
          return $this->fetch();
     }
     
     public function user(){
          $this->AI();
          $this->ADMIN_AI();
          $game_list = Db::name('game_list')->where('id','in',config('game_id'))->where('game_status',1)->select();
	      $this->assign('game_list',$game_list);
          return $this->fetch();
     }
     
     public function news(){
          $this->AI();
          $this->ADMIN_AI();
          return $this->fetch();
     }
     
     public function land(){
          $this->AI();
          $this->ADMIN_AI();
          $game_list = Db::name('game_list')->where('id','in',config('game_id'))->where('game_status',1)->select();
	      $this->assign('game_list',$game_list);
          return $this->fetch();
     }
     
     public function orders(){
          $this->AI();
          $this->ADMIN_AI();
          $game_list = Db::name('game_list')->where('id','in',config('game_id'))->where('game_status',1)->select();
	      $this->assign('game_list',$game_list);
          return $this->fetch();
     }
     
     public function log(){
          $this->AI();
          $this->ADMIN_AI();
          return $this->fetch();
     }
     
    public function game(){
          $this->AI();
          $this->ADMIN_AI();
          $game_type = Db::name('game_type')->select();
	      $this->assign('game_type',$game_type);
          return $this->fetch();
    }
    
    public function game_type(){
          $this->AI();
          $this->ADMIN_AI();
          return $this->fetch();
    }
    
    public function game_type_list(){
        $this->AI();
        $this->ADMIN_AI();
        $where = $this->getparam(['title']);
        $list = Db::name("game_type")->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        $this->json_success("",$list);
    }
    
    public function game_type_add(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->post();
        if($post['id']==""){
            $save = Db::name("game_type")->insert($post);
        }else{
            $save = Db::name("game_type")->update($post);
        }
        if($save){
            $this->success("操作成功");
        }else{
            $this->error("添加或修改失败");
        }
    }
    
    public function game_type_del(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $del = Db::name("game_type")->where($where)->delete();
        if($del){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
     
     //ajax通道数据
    public function game_list()
    { 
        $this->AI();
        $this->ADMIN_AI();
	    $where = $this->getparam(['game_name']);
      
        if(isset($where['game_name'])){
            $where['game_name'] = ['like','%'.$where['game_name'].'%'];
        }
        
        $list = Db::name("game_list")->where('id','in',config('game_id'))->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
       
        for ($i = 0; $i < count($list); $i++) {
            
             $where_1['payc'] = $list[$i]['id'];
             $where_1['state'] = 2;
             $temp = $list[$i];
             $orders = Db::name('orders')->where($where_1)->whereTime('pay_time','last month')->sum('money');
             $orders_1 = Db::name('orders')->where($where_1)->whereTime('pay_time','month')->sum('money');
             $game_type = Db::name('game_type')->where('id',$list[$i]['game_type_id'])->find();
             $temp['yue_pay'] = $orders;  
             $temp['yue_pay_1'] = $orders_1;  
             $temp['game_type_name'] = $game_type['title'];  
             $list[$i] = $temp;
             
        }
        
       $this->json_success("",$list);
	     
    }
    
    public function game_upload(){
        $this->AI();
        $this->ADMIN_AI();
        $file = request()->file('file');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
            
                $this->json_success("", $info->getSaveName());
          
             }else{
            
                $this->json_error("",$file->getError());
            }
        }
        
    }
    
    public function game_save(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->post();
        unset($post['file']);
        
        $r = Db::name("game_list")->update($post);
        
        if($r){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    
    
    public function user_list()
    { 
        $this->AI();
        $this->ADMIN_AI();
	    $where = $this->getparam(['name']);
	    if(isset($where['name'])){
            $where['name'] = ['like','%'.$where['name'].'%'];
        }
	    
        $list = Db::name("user")->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        for ($i = 0; $i < count($list); $i++) {
            
             $temp = $list[$i];
             $jr_pay = Db::name('log')->where('userid',$list[$i]['id'])->whereTime('time', 'today')->sum('money');
             $zr_pay = Db::name('log')->where('userid',$list[$i]['id'])->whereTime('time', 'yesterday')->sum('money');
             $temp['jr_pay'] = $jr_pay;
             $temp['zr_pay'] = $zr_pay;
             $list[$i] = $temp;
        }
      
        $this->json_success("",$list);
	     
    }
    
    
    public function user_save(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->post();
 
        if($post['balance'] == ""){
            $this->error("商户余额不可为空");
        }
        
        if($post['pay_sxf'] == ""){
            $this->error("商户手续费不可为空");
        }
        
        if($post['api_id'] == ""){
            $this->error("商户PID不可为空");
        }
        
        if($post['api_key'] == ""){
            $this->error("商户KEY不可为空");
        }
        
        if($post['game_id']){
            if($post['game_id']!=0){
            foreach ($post['game_id'] as $value) {
                if($game_id){
                    $s = ',';
                }else{
                    $s = '';
                }
                $game_id = $game_id.$s.$value;
            }
            }else{
                $game_id = 0;
            }
            $post['game_id'] = $game_id;
        }
        
        if($post['pwd2']==''){
          unset($post['pwd2']);  
        }else{
          $user_data = Db::name("user")->where('id',$post['id'])->find();
          $post['password']=$this->password($post['pwd2']); 
          unset($post['pwd2']);
        }
        
        $save = Db::name("user")->update($post);
        
        if($save){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    
    public function del_user_list(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $del = Db::name("user")->where($where)->delete();
        Db::name("orders")->where('userid',$post['id'])->delete();
        Db::name("log")->where('userid',$post['id'])->delete();
        Db::name("land")->where('userid',$post['id'])->delete();
        Db::name("game_my")->where('userid',$post['id'])->delete();
        if($del){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    
    }
    
    //ajax用户数据
    public function land_list()
    { 
        $this->AI();
        $this->ADMIN_AI();
	    $where = $this->getparam(['name','username','typec']);
	   
        if(isset($where['name'])){
            $user_cx = Db::name("user")->where('name',$where['name'])->find();
            $where['userid'] = $user_cx['id'];
        }
        
        if(isset($where['username'])){
            $where['username'] = ['like','%'.$where['username'].'%'];
        }
        
        
        unset($where['name']);
        $list = Db::name("land")->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
         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];
             $user = Db::name("user")->where($user_where)->find();
             $game_list = Db::name("game_list")->where('id','in',config('game_id'))->where($game_where)->find();
             $jr_money = Db::name("orders")->where($orders_where)->whereTime('order_time', 'today')->sum('money');
             $zr_money = Db::name("orders")->where($orders_where)->whereTime('order_time', 'yesterday')->sum('money');
             $z_money = Db::name("orders")->where($orders_where)->sum('money');
             
             $typec = $game_list['game_name'] ;
             $typec_id = $list[$i]['typec'];
             $user_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.'---';
			 }
			 
			 $json = json_decode(encode($list[$i]['reback'],2));
							    
             $temp['name'] = $user_name;
             $temp['typec'] = $typec;
             $temp['typec_id'] = $typec_id;
             $temp['jr_pay'] = $jr_money;
             $temp['zr_pay'] = $zr_money;
             $temp['z_pay'] = $z_money;
             $temp['method'] = $json->method;
             $temp['ck'] = $ck;
             $list[$i] = $temp;
             
        }
       
        
        $this->json_success("",$list);
	     
    }
    

    public function land_save(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->post();
        $post1 = $this->request->post();
        if($post['username'] == ""){
            $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['name']);  
        unset($post['method']);
        
        unset($post1['name']);  
        unset($post1['method']);
        unset($post1['username']);
        unset($post1['id']);
        
        foreach($post as $k=>$v)
        {
          if($post[$k] == $post1[$k]){
              unset($post[$k]);
          }
        }
        
        if($post1['1ck']){
            $post1['ck'] = $post1['1ck'];
            unset($post1['1ck']); 
        }
        
        $post['json'] = json_encode($post1);
        
        $save = Db::name("land")->update($post);
        if($save){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    
 
    public function land_status_save(){
        $this->AI();
        $this->ADMIN_AI(); 
        $post = $this->request->get();
        $save = Db::name("land")->update($post);
        if($save){
            $this->success("操作成功");
        }else{
            $this->error("修改失败");
        }
    }
    

    public function land_del(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $land = Db::name('land')->where('id',$post['id'])->find();
        $del = Db::name("land")->where($where)->delete();
        $where_orders['land_id'] = $post['id'];
        $where_log['land'] = $post['id'];
        $update['land_name'] = $land['username'];
        DB::name("orders")->where($where_orders)->delete();
        DB::name("log")->where($where_log)->delete();
        if($del){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
    
     
    public function orders_list()
    { 
        $this->AI();
        $this->ADMIN_AI();
	    $where = $this->getparam(['name','username','num','payc','remark','state']);
	    
        if(isset($where['name'])){
            $user_cx = Db::name("user")->where('name',$where['name'])->find();
            $where['userid'] = $user_cx['id'];
        }
        
        if(isset($where['username'])){
            $land_cx = Db::name("land")->where('username',$where['username'])->find();
            if($land_cx){
                $where['land_id'] = $land_cx['id'];
            }else{
                $where['land_name'] = $where['username'];
            }
            
        }
        
        if(isset($where['num'])){
            $where['num'] = ['like','%'.$where['num'].'%'];
        }
        
        if(isset($where['remark'])){
            $where['remark'] = ['like','%'.$where['remark'].'%'];
        }
        
        unset($where['name']);
        unset($where['username']);
        $list = Db::name("orders")->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        for ($i = 0; $i < count($list); $i++) {
             
             $land_where['id'] = $list[$i]['land_id'];
             $game_where['id'] = $list[$i]['payc'];
             $user_where['id'] = $list[$i]['userid'];
             $temp = $list[$i];
             
             $land = Db::name("land")->where($land_where)->find();
             $game = Db::name("game_list")->where('id','in',config('game_id'))->where($game_where)->find();
             $user = Db::name("user")->where($user_where)->find();
             if($list[$i]['land_name']!=''){
             $temp['land_id'] = $list[$i]['land_name'].'('.$game['game_name'].')';    
             }else{
             $temp['land_id'] = $land['username'].'('.$game['game_name'].')';    
             }
             $temp['name'] = $user['name'].'('.$list[$i]['userid'].')';    
             $list[$i] = $temp;
        }
        
        $this->json_success("",$list);
	     
    }
    
 
    public function log_list()
    { 
        $this->AI();
        $this->ADMIN_AI();
	    $where = $this->getparam(['name','num']);
	    
        if(isset($where['name'])){
            $user_cx = Db::name("user")->where('name',$where['name'])->find();
            $where['userid'] = $user_cx['id'];
        }
        
        if(isset($where['num'])){
            $where['num'] = ['like','%'.$where['num'].'%'];
        }
        unset($where['name']);
       
        $list = Db::name("log")->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        for ($i = 0; $i < count($list); $i++) {
             
             $land_where['id'] = $list[$i]['land'];
             $game_where['id'] = $list[$i]['payc'];
             $user_where['id'] = $list[$i]['userid'];
             $temp = $list[$i];
             
             $land = Db::name("land")->where($land_where)->find();
             $game = Db::name("game_list")->where('id','in',config('game_id'))->where($game_where)->find();
             $user = Db::name("user")->where($user_where)->find();
             if($list[$i]['land_name']!=''){
             $temp['land_id'] = $list[$i]['land_name'];    
             $temp['payc'] = $game['game_name'];
             }else{
             $temp['land_id'] = $land['username'];    
             $temp['payc'] = $game['game_name'];
             }
             $temp['name'] = $user['name'].'('.$list[$i]['userid'].')';    
             $list[$i] = $temp;
        }
        
        $this->json_success("",$list);
	     
    }
    
    
    public function orders_del(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $r = Db::name("orders")->where($where)->delete();
        if($r){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    
    }
    
    public function log_del(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $r = Db::name("log")->where($where)->delete();
        if($r){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    
    }
    
    public function orders_del_type(){
        
        $this->AI();
        $this->ADMIN_AI();
        $type = $this->request->param('type');
        
        if($type==1){
           $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
           $xqTime = $nowTime-(86400*7); //7天的时间
           $where['order_time'] = ['<',$xqTime];
          
           
        }else if($type==2){
           $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
           $xqTime = $nowTime-(86400*14); //14天的时间
           $where['order_time'] = ['<',$xqTime];
          
        }else if($type==3){
           $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
           $xqTime = $nowTime-(86400*30); //30天的时间
           $where['order_time'] = ['<',$xqTime];
          
        }else if($type==4){
           $nowTime = strtotime(date("Y-m-d",time()) . ' 00:00:00');
           $xqTime = $nowTime-(86400*90); //90天的时间
           $where['order_time'] = ['<',$xqTime];
           
        }else if($type==5){
           
           $where['state'] = 3;
           
        }else if($type==6){
 
           $where['id'] = ['>',0];
        }
        
        $del_status = Db::name('orders')->where($where)->delete();
        
        if($del_status){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
        
        
    }
    
    //ajax通道数据
    public function news_list()
    { 
        $this->AI();
        $this->ADMIN_AI();
	    $where = $this->getparam(['title']);
        
        $list = Db::name("news")->where($where)->order('id desc')->paginate([
            'list_rows'=> $this->request->param("limit")??10,
            'var_page' => 'page',
        ]);
        
        $this->json_success("",$list);
	     
    }
    
    
    public function news_save(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->post();

        if($post['id']==""){
            $post['time'] = time();
            $r = Db::name("news")->insert($post);
        }else{
            $r = Db::name("news")->update($post);
        }
        if($r){
            $this->success("操作成功");
        }else{
            $this->error("添加或修改失败");
        }
    }
   
    public function news_del(){
        $this->AI();
        $this->ADMIN_AI();
        $post = $this->request->get();
        $where['id'] = $post['id'];
        $r = Db::name("news")->where($where)->delete();
        if($r){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    
    }
    
    
    
    
}