<?php
defined('IN_WEB') or die('Include Error!');
/**
* 用户 等级 --- 筹码 分布
*/
class ModelMoneylevel
{

	//统计玩家等级,筹码区间
	private $moneyRange = array(
		array(0, 1000),
		array(1000, 2000),
		array(2000, 4000),
		array(4000, 10000),
		array(10000, 20000),
		array(20000, 40000),
		array(40000, 60000),
		array(60000, 100000),
		array(100000, 200000),
		array(200000, 400000),
		array(400000, 600000),
		array(600000, 1000000),
		array(1000000, 2000000),
		array(2000000, 3000000),
		array(3000000, 6000000),
		array(6000000, 10000000),
		array(10000000, 20000000),
		array(20000000, 1000000000),
	);

	private $diamondRange = [
	    [0, 5],
        [6, 10],
        [11, 15],
        [16, 20],
        [21, 25],
        [26, 30],
        [31, 50],
        [51, 100],
        [101, 1000],
        [1001, 2000],
        [2001, 100000]
    ];

    /**
     * 处理数据
     * @param $moneyRangeList
     * @param $levelRangeList
     * @param $versionRangeList
     * @param int $type
     * @param $date
     * @param int $sid
     * @return bool
     */
    private function dealDiamondLevel(&$diamondRangeList, $type = 1, $date, $sid = 0)
    {

        if ($type == 1) {//活跃用户
            $diamondRangeKey = 'MONEYLEVEL:DIAMOND:PLAYER:'.$sid.':'.$date.':';
        }else{
            $diamondRangeKey = 'MONEYLEVEL:DIAMOND:ALLPLAYER'.$date.':';
        }


        $diamondRangeCount = count($this->diamondRange);
        for ($i=0; $i < $diamondRangeCount; $i++) {
            $diamondRangeList[$i] += intval(oo::commonOprRedis('common')->get($diamondRangeKey.$i));
        }

        return true;
    }

    /**
     * 获取玩家钻石分布
     * @return string
     */
	public function diamondlevel($sidList = [])
    {
        $date = date('Ymd', strtotime('-1 day'));

        //获取钻石区间
        $diamondRangeList = [];
        //如果sidList为空或者为999,则默认全部的sid
        if(empty($sidList) || is_array($sidList) && in_array(999, $sidList)) {
            //包列表
            $sidList = oo::commonOprModel('statistics')->getSidList();
        }else {
            if(!is_array($sidList)) {
                $sidList = array($sidList);
            }
        }

        foreach ($sidList as $sid) {
            $this->dealDiamondLevel($diamondRangeList, 1, $date, $sid);
        }


        $data['diamondRangeList'] = $diamondRangeList;
        $data['diamondRange'] = $this->diamondRange;
        return $data;
    }

  
  	public function crazyGodMoneyLevel($type = '',$sidList = [999]){
        $levelRange = oo::commonOprModel('statistics')->getLevelRange();
        $data = array('moneyRange' => $this->moneyRange, 'levelRange' => $levelRange, 'moneyRangeList' => [], 'levelRangeList' => [], 'versionRangeList' => []);
	    $tb = otable::gh_gameserver(1);
        $moneyRangeList = $levelRangeList = $spinsRangeList = [];
        if($type == 'money'){
            foreach ($this->moneyRange as $key=>$row){
                $sql = "SELECT count(*) as count FROM {$tb} WHERE money BETWEEN {$row[0]} AND {$row[1]}";
                $ret = oo::commonOprDb('common')->getOne($sql,1);
                $moneyRangeList[] = intval($ret['count']);
            }
        }else if($type == 'level'){
            if(in_array(999, $sidList)) {
                $levelRange = oo::commonOprModel('statistics')->getLevelRange();
                foreach ($levelRange as $key=>$row){
                    $sql = "SELECT count(*) as count FROM {$tb} WHERE levelId={$row}";
                    $ret = oo::commonOprDb('common')->getOne($sql,1);
                    $levelRangeList[] = intval($ret['count']);
                }
            }else{
                $login_plat = 1;
                if(!empty($sidList) && is_array($sidList)){
                    $login_plat = implode(',',$sidList);
                }
                $tbUserinfo = otable::userinfo(1);
                $levelRange = oo::commonOprModel('statistics')->getLevelRange();
                foreach ($levelRange as $key=>$row){
                    $sql = "SELECT count(*) as count FROM {$tb} AS a INNER JOIN {$tbUserinfo} AS b WHERE b.login_plat in ({$login_plat}) AND b.uid = a.mid AND a.levelId={$row}";
                    $ret = oo::commonOprDb('common')->getOne($sql,1);
                    $levelRangeList[] = intval($ret['count']);
                }
            }
        }else if($type == 'spins'){
            for ($i=0;$i<=50;$i++){
                $sql = "SELECT count(*) as count FROM {$tb} WHERE spins={$i}";
                $ret = oo::commonOprDb('common')->getOne($sql,1);
                $spinsRangeList[] = intval($ret['count']);
            }

            //新加的体力区间
            $spinsRange = array(
                '0'=>[51,100],
                '1'=>[101,500],
                '2'=>[501,1000],
                '3'=>[1001,5000],
                '4'=>[5001,9999999],
            );

            foreach($spinsRange as $k => $v){
                $start = $v[0];
                $end = $v[1];
                $sql = "SELECT count(*) as count FROM {$tb} WHERE spins<={$end} AND spins >= {$start}";
                $ret = oo::commonOprDb('common')->getOne($sql,1);
                $spinsRangeList[] = intval($ret['count']);
            }
        }

        $data['moneyRangeList'] = $moneyRangeList;
        $data['levelRangeList'] = $levelRangeList;
        $data['spinsRangeList'] = $spinsRangeList;
        return json_encode($data);
    }
}