<?php
/**
 * 排行榜
 */
class RankModel extends CI_Model
{

	public function __construct(){
        parent::__construct();
        $this->load->model('okeys');
		$this->load->model('otable');
		$this->load->model('oo');
		
		if (file_exists(APPPATH.'config/reason.php')) {
			$this->reason = include APPPATH.'config/reason.php';
		}
    }

    /**
     * 查出付费用户
     * @return array
     */
    public function findPayList(array $uidList = array()) {
        //查出付费用户
        $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('slavedb')->getAll($sql, 1);
	        
	        foreach($list as $k => $v){
	        	$pay_list[] = $v['sl_uid'];
	        }
        }
        return $pay_list;
    }

	/**
     * 总排行榜 合并体力、金币、等级接口
     * @return string
     * Created by: hss
     * Created on: 2020/6/1 11:40
     */
    public function index($type = 'money', $sid = 999){
    	$sid = oo::functions()->uint($sid);
    	$type = oo::functions()->escape($type);
    	if(empty($sid)){
    		$sid = 999;
    	}
    	if(empty($type)){
    		$type = 'money';
    	}
    	$tb  = otable::gh_gameserver(1);
    	if($sid == 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 = {$sid} AND b.uid = a.mid  ORDER BY a.{$type} DESC LIMIT 100";
    	}
    	
    	$ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = $this->findPayList($uidList);
        return ["data"=>$ret,"pay_list"=>$pay_list];
    }

    public function diamond(){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,diamond FROM {$tb} ORDER BY diamond DESC LIMIT 100";
        $ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = $this->findPayList($uidList);
        return ["data"=>$ret,"pay_list"=>$pay_list];
    }
	
    public function money(){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,money FROM {$tb} ORDER BY money DESC LIMIT 100";
        $ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = $this->findPayList($uidList);
        return ["data"=>$ret,"pay_list"=>$pay_list];
    }

    public function star(){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,star FROM {$tb} ORDER BY star DESC LIMIT 100";
        $ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = $this->findPayList($uidList);        
        return ["data"=>$ret,"pay_list"=>$pay_list];
    }
	
	//等级排行榜
    public function level(){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,levelId as level FROM {$tb} ORDER BY levelId DESC LIMIT 100";
        $ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = $this->findPayList($uidList);        

        return ["data"=>$ret,"pay_list"=>$pay_list];
    }
	
	//体力排行榜
    public function spins(){
        $tb  = otable::gh_gameserver(1);
        $sql = "SELECT mid as uid,spins FROM {$tb} ORDER BY spins DESC LIMIT 100";
        $ret = oo::commonOprDb('slavedb')->getAll($sql,1);
        $uidList = [];
        foreach($ret as $k => $v){
        	$uidList[] = $v['uid'];
        }
        //查出付费用户
        $pay_list = $this->findPayList($uidList);
        return ["data"=>$ret,"pay_list"=>$pay_list];
    }
  
  	public function day($date){
        if($date == date('Ymd')){
            $key = oo::commonOprModel('statistics')->getResources();
            $inset = [];
            
            foreach ($key as $row){
                $data = oo::commonOprRedis('games')->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 = $this->findPayList($uidList);
		        $inset[$row."Vip"] = $pay_list;
            }
            return $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 = $this->findPayList($uidList);
	        $ret[$key."Vip"] = $pay_list;
        }
        return $ret;
    }
}
