<?php
/**
 * 用户信息相关
 */
defined( 'IN_WEB') or die( 'Include Error!');

include_once dirname(__FILE__) . "/Base.php";


class Rank extends  Base{
	
	/**
     * 总排行榜 合并体力、金币、等级接口
     * @return string
     * Created by: hss
     * Created on: 2020/6/1 11:40
     */
    public function index($param){
    	$sidList = oo::functions()->uint($param['sidList']);
    	$type = oo::functions()->escape($param['type']);
    	if(empty($sidList)){
    		$sidList = 999;
    	}
    	if(empty($type)){
    		$type = 'money';
    	}
    	$tb  = otable::gh_gameserver(1);
    	if($sidList == 999){
        	$sql = "SELECT mid as uid,{$type} FROM {$tb} ORDER BY {$type} DESC LIMIT 100";
    	}else{
    		$tbUserinfo = otable::userinfo(1);
    		$sql = "SELECT a.mid as uid,a.{$type} FROM {$tb} AS a INNER JOIN {$tbUserinfo} AS b WHERE b.login_plat = {$sidList} AND b.uid = a.mid  ORDER BY a.{$type} DESC LIMIT 100";
    	}
    	
    	$ret = oo::commonOprDb('common')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = [];
        if(!empty($uidList)){
        	$slTable = otable::summarylist();
	        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
	        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
	        $list = oo::commonOprDB('payment')->getAll($sql, 1);
	        
	        foreach($list as $k => $v){
	        	$pay_list[] = $v['sl_uid'];
	        }
        }
        
        return json_encode(["data"=>$ret,"pay_list"=>$pay_list]);
    }
	
    public function money($param){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,money FROM {$tb} ORDER BY money DESC LIMIT 100";
        $ret = oo::commonOprDb('common')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = [];
        if(!empty($uidList)){
        	$slTable = otable::summarylist();
	        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
	        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
	        $list = oo::commonOprDB('payment')->getAll($sql, 1);
	        
	        foreach($list as $k => $v){
	        	$pay_list[] = $v['sl_uid'];
	        }
        }
        
        return json_encode(["data"=>$ret,"pay_list"=>$pay_list]);
    }

    public function star($param){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,star FROM {$tb} ORDER BY star DESC LIMIT 100";
        $ret = oo::commonOprDb('common')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = [];
        if(!empty($uidList)){
        	$slTable = otable::summarylist();
	        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
	        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
	        $list = oo::commonOprDB('payment')->getAll($sql, 1);
	        
	        foreach($list as $k => $v){
	        	$pay_list[] = $v['sl_uid'];
	        }
        }
        
        return json_encode(["data"=>$ret,"pay_list"=>$pay_list]);
    }
	
	//等级排行榜
    public function level($param){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,levelId as level FROM {$tb} ORDER BY levelId DESC LIMIT 100";
        $ret = oo::commonOprDb('common')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = [];
        if(!empty($uidList)){
        	$slTable = otable::summarylist();
	        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
	        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
	        $list = oo::commonOprDB('payment')->getAll($sql, 1);
	        
	        foreach($list as $k => $v){
	        	$pay_list[] = $v['sl_uid'];
	        }
        }
        
        return json_encode(["data"=>$ret,"pay_list"=>$pay_list]);
    }
	
	//体力排行榜
    public function spins($param){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,spins FROM {$tb} ORDER BY spins DESC LIMIT 100";
        $ret = oo::commonOprDb('common')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = [];
        if(!empty($uidList)){
        	$slTable = otable::summarylist();
	        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
	        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
	        $list = oo::commonOprDB('payment')->getAll($sql, 1);
	        
	        foreach($list as $k => $v){
	        	$pay_list[] = $v['sl_uid'];
	        }
        }
        
        return json_encode(["data"=>$ret,"pay_list"=>$pay_list]);
    }
  
  	public function day($param){
        $date = $param['date'];
        //var_dump(date('Ymd'));
        if($date == date('Ymd')){
            $key = ['level','money','spins'];
            $inset = [];
            
            foreach ($key as $row){
                $data = oo::commonOprRedis('common')->hGetAll(okeys::todayDataRank($row));
                arsort($data);
                $data = array_slice($data,0,50,true);
                $inset[$row] = $data;
                
                $uidList = [];
                foreach($data as $k => $v){
                	$uidList[] = $k;
                }
                
                //查出付费用户
                $pay_list = [];
                if(!empty($uidList)){
                	$slTable = otable::summarylist();
			        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
			        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
			        $list = oo::commonOprDB('payment')->getAll($sql, 1);
			        
			        foreach($list as $k => $v){
			        	$pay_list[] = $v['sl_uid'];
			        }
                }
		        $inset[$row."Vip"] = $pay_list;
            }
            return json_encode($inset);
        }

        $tb = otable::dayRank();
        $sql = "SELECT * FROM {$tb} WHERE date={$date} LIMIT 1;";
        $ret = oo::commonOprDb('statistics')->getOne($sql,MYSQLI_ASSOC);
        foreach ($ret as $key=>$row){
            $ret[$key] = json_decode($row,true);
            
            $uidList = [];
            foreach($ret[$key] as $k => $v){
            	$uidList[] = $k;
            }
            
            //查出付费用户
            $pay_list = [];
            if(!empty($uidList)){
            	$slTable = otable::summarylist();
		        $UidStr = empty($uidList) ? '' : " AND sl_uid IN (".implode(',', $uidList).") ";
		        $sql = " SELECT DISTINCT sl_uid FROM  {$slTable} AS sl WHERE sl.sl_status = 2 AND sl.sl_pusd > 0 {$UidStr}";
		        $list = oo::commonOprDB('payment')->getAll($sql, 1);
		        
		        foreach($list as $k => $v){
		        	$pay_list[] = $v['sl_uid'];
		        }
            }
	        $ret[$key."Vip"] = $pay_list;
        }
        return json_encode($ret);
    }
}