<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 统计数据相关
 */
class CoinsValue extends MY_Controller
{
    public $postdata = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);
    private $reason;
    protected $page = 10;
    protected $pageConfig = array(
        'first_tag_open' => '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' => '<li>',
        'last_tag_close' => '</li>',
        'next_tag_open' => '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' => '<li>',
        'prev_tag_close' => '</li>',
        'cur_tag_open' => "<li class='active'><a hef='#'>",
        'cur_tag_close' => "</a></li>",
        'num_tag_open' => "<li>",
        'num_tag_close' => "</li>",
    );

    private $scale  = [0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2050,2100,2150,2200,2250,2300,2350,2400,2450,2500,2550,2600,2650,2700,2750,2800,2850,2900,2950,3000,3050,3100,3150,3200,3250,3300,3350,3400,3450,3500,3550,3600,3650,3700,3750,3800,3850,3900,3950,4000,4050,4100,4150,4200,4250,4300,4350,4400,4450,4500,4550,4600,4650,4700,4750,4800,4850,4900,4950,5000,5500,6000,6500,7000,7500,8000,8500,9000,9500,10000,10500,11000,11500,12000,12500,13000,13500,14000,14500,15000,15500,16000,16500,17000,17500,18000,18500,19000,19500,20000,20500,21000,21500,22000,22500,23000,23500,24000,24500,25000,25500,26000,26500,27000,27500,28000,28500,29000,29500,30000,30500,31000,31500,32000,32500,33000,33500,34000,34500,35000,35500,36000,36500,37000,37500,38000,38500,39000,39500,40000,40500,41000,41500,42000,42500,43000,43500,44000,44500,45000,45500,46000,46500,47000,47500,48000,48500,49000,49500,50000];
    private $scaleCenter = 5000;

    public function __construct()
    {

        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->load->library('pagination');
        $config = array(
            'savePath' => 'Excel/uploads',             //存储文件夹
            'maxSize'  => 1024*5,                   //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.xls','.xlsx')  //允许的文件格式
        );
        $this->load->library('uploader', $config);
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/PHPExcel/IOFactory');
        $this->load->library('curl');
        //请求地址
        $this->env = get_cookie($this->_wenenv);
        $this->_url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');

        $this->load->model('oo');

        if (file_exists(APPPATH.'config/reason.php')) {
            $this->reason = include APPPATH.'config/reason.php';
        }
    }

    public function base($objPHPExcel,$outPutFileName){
        // 设置第一个sheet为工作的sheet
        $objPHPExcel->setActiveSheetIndex(0);

        header('Content-Type: applicationnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$outPutFileName.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;
    }
    private function CurSpinValue($uidList = []){
        if(empty($uidList)){
            $cache = oo::commonOprRedis('statistics')->get(okeys::CurSpinValue());
            if(!empty($cache)){
                return json_decode($cache,true);
            }
        }
        $statisticsRedis = oo::commonOprRedis('statistics');
        $tmpCoins = $statisticsRedis->hGetAll(okeys::CoinsValueSta("coins"));
        $totalCoins = [];
        foreach ($tmpCoins as $uid=>$row){
            $tmp = explode('-',$uid);
            if(empty($uidList) || in_array($tmp[0],$uidList)){
                $totalCoins[$tmp[1]] += $row;
            }
        }
        $tmpSpins= $statisticsRedis->hGetAll(okeys::CoinsValueSta("spins"));
        $totalSpins = [];
        foreach ($tmpSpins as $uid=>$row){
            $tmp = explode('-',$uid);
            if(empty($uidList) || in_array($tmp[0],$uidList)){
                $totalSpins[$tmp[1]] += $row;
            }
        }
        foreach ($totalSpins as $levelId=>$num){
            $spinValue[$levelId] = $num == 0 ? 0 : intval($totalCoins[$levelId]/$num);
        }
        empty($uidList) && oo::commonOprRedis('statistics')->setex(okeys::CurSpinValue(),json_encode($spinValue),5*60);
        return $spinValue;
    }
    private function actionScale($data){
        $userNum = [];
        $min = min($this->scale);
        $max = max($this->scale);
        foreach ($data as $uid=>$num){
            if($num == $min){
                $userNum[0] += 1;
                continue;
            }else if($num > $max){
                $userNum[50500] += 1;
                continue;
            }else{
                foreach ($this->scale as $row){
                    if($num <= $row){
                        $userNum[$row] += 1;
                        break;
                    }
                }
            }
        }
        $base   = $this->scale;
        $base[] = 50500;
        foreach ($base as $row){
            if(empty($userNum[$row])){
                $userNum[$row] = 0;
            }
        }
        ksort($userNum);
        $num1 = [];
        $num2 = [];
        foreach ($userNum as $key=>$num){
            if($key <= $this->scaleCenter){
                $num1[$key] = $num;
            }else{
                $num2[$key] = $num;
            }
        }
        return [
            $num1,
            $num2,
        ];
    }
    // 变化负数
    private function actionScale2($data){
        $scale = [];
        foreach ($this->scale as $row){
            $scale[] = -$row;
        }
        $scale   = array_reverse($scale);
        $userNum = [];
        $min = min($scale);
        $max = max($scale);
        foreach ($data as $uid=>$num){
            if($num == $max){
                $userNum[0] += 1;
                continue;
            }else if($num < $min){
                $userNum[-50500] += 1;
                continue;
            }else{
                $cur = -50000;
                foreach ($scale as $row){
                    if($num > $row){
                        $cur = $row;
                        continue;
                    }else{
                        $userNum[$cur] += 1;
                    }
                }
            }
        }
        $base   = $scale;
        $base[] = -50500;
        foreach ($base as $row){
            if(empty($userNum[$row])){
                $userNum[$row] = 0;
            }
        }
        ksort($userNum);
        $num1 = [];
        $num2 = [];
        foreach ($userNum as $key=>$num){
            if($key <= -$this->scaleCenter){
                $num1[$key] = $num;
            }else{
                $num2[$key] = $num;
            }
        }
        return [
            $num1,
            $num2,
        ];
    }

    private function actionScaleBase($base){
        $num1 = [];
        $num2 = [];
        foreach ($base as $num){
            if($num <= $this->scaleCenter){
                $num1[] = $num;
            }else{
                $num2[] = $num;
            }
        }
        return [
            $num1,
            $num2,
        ];
    }
    private function actionScaleBase2($base){
        $base = array_reverse($base);
        foreach ($base as $row){
            $scale[] = -$row;
        }
        $num1 = [];
        $num2 = [];
        foreach ($scale as $num){
            if($num <= -$this->scaleCenter){
                $num1[] = $num;
            }else{
                $num2[] = $num;
            }
        }
        return [
            $num1,
            $num2,
        ];
    }

    private function getUidArrByUserGroupOnDate($date,$groupId){
        if($date == date("Ymd")){
            $dauUidArr = oo::commonOprRedis('statistics')->hKeys(okeys::CoinsValueSta("dau"));
        }else{
            $tb = otable::ResourceMonitoringSta();
            $ret= oo::commonOprDb('statistics')->getAll("SELECT uid FROM {$tb} WHERE `date`='{$date}'",MYSQLI_ASSOC);
            $dauUidArr = array_column($ret,"uid");
        }
        $uidArr = [];
        foreach ($dauUidArr as $uid){
            $userGroupId = explode(",",oo::commonOprModel('member')->getUserDynamicsData($uid)['groupId2']);
            if(!empty(array_intersect($groupId,$userGroupId))){
                $uidArr[] = $uid;
            }
        }
        return $uidArr;
    }
    private function exportCommon($fileName,$title,$data){
        $headerTable = array_values($title);
        $objPHPExcel = new PHPExcel();
        //处理表头
        $char = 'A';
        foreach ($headerTable as $k => $v) {
            $objPHPExcel->getActiveSheet()->setCellValue($char.'1', $v);
            $char = chr(ord($char) + 1);
        }
        //处理数据
        $i = 2;
        $key = array_keys($title);
        foreach ($data as $kb => $vb) {
            $objPHPExcel->getActiveSheet()->setCellValue("A".$i, $vb[$key[0]]);
            $objPHPExcel->getActiveSheet()->setCellValue("B".$i, $vb[$key[1]]);
            $objPHPExcel->getActiveSheet()->setCellValue("C".$i, $vb[$key[2]]);
            $objPHPExcel->getActiveSheet()->setCellValue("D".$i, $vb[$key[3]]);
            $i++;
        }

        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $this->base($objPHPExcel,$fileName);
    }
    private function payNumCommon($date,$ret){
        $payTimeS= strtotime("$date 00:00:00");
        $payTimeE= strtotime("$date 23:59:59");
        foreach ($ret as $index=>$row){
            $uid = $row['uid'];
            $payNum = oo::commonOprRedis('statistics')->get(okeys::PayNumByDate($date,$uid));
            if(empty($payNum)){
                $tb  = otable::payment($uid);
                $payInfo = oo::commonOprDb('slavedb')->getOne("SELECT sum(pusd) as num FROM {$tb} WHERE status=2 AND uid={$uid} AND `pay_time`>{$payTimeS} AND `pay_time`<{$payTimeE}",MYSQLI_ASSOC);
                $payNum  = round($payInfo['num'],2);
                oo::commonOprRedis('statistics')->setex(okeys::PayNumByDate($date,$uid),$payNum,24*60*60);
            }
            $ret[$index]["payNum"] = $payNum;
        }
        return $ret;
    }

    /******** 列表 ********/
    public function index($type = 1){
        if(!empty(get_cookie('stime')) && !empty(get_cookie('etime'))){
            $dateList = oo::getDateRange2(get_cookie('stime'),min(get_cookie('etime'),date('Y-m-d')));
        }else{
            $dateList = [
                date('Ymd'),
                date('Ymd', strtotime('-1 days')),
                date('Ymd', strtotime("-2 days")),
            ];
        }
        $base = $this->scale;
        $out = ["setTime"=>[get_cookie('stime'),min(get_cookie('etime'),date('Y-m-d'))],"id"=>$type];
        $out['userGroup'] = array_column(oo::commonOprModel('config')->getUserGroupConfig2(),'name','id');
        if($type == 1){
            $out['data'] = $this->spinValue($dateList);
        }else if($type == 2){
            foreach ($this->reason as $id=>$name){
                if(in_array($id,[213,10,41,1,204,4,9,2,3])){
                    $reason[$id] = $name;
                }
            }
            $out['reason'] = $reason;
            $out = array_merge($out,$this->coinsAdd($base,$dateList));
        }else if($type == 3){
            $out = array_merge($out,$this->coinsSpend($base,$dateList));
        }else if($type == 4){
            $out = array_merge($out,$this->coinsStock($base,$dateList));
        }else if($type == 5){
            $out = array_merge($out,$this->coinsStockMean($base,$dateList));
        }else if($type == 6){
            $out = array_merge($out,$this->coinsStockChange($base,$dateList));
        }else if($type == 7){
            foreach ($this->reason as $id=>$name){
                if(in_array($id,[204,1,111,5,208,15,80,213,217,30,451,210,33])){
                    $reason[$id] = $name;
                }
            }
            $out['reason'] = $reason;
            $out = array_merge($out,$this->spinsAdd($base,$dateList));
        }else if($type == 8){
            $out = array_merge($out,$this->spinsSpend($base,$dateList));
        }else if($type == 9){
            $out = array_merge($out,$this->spinsStock($base,$dateList));
        }else if($type == 10){
            $out = array_merge($out,$this->spinsStockMean($base,$dateList));
        }else if($type == 11){
            $out = array_merge($out,$this->spinsStockChange($base,$dateList));
        }else if($type == 12){
            $out = array_merge($out,$this->slotCount($base,$dateList));
        }
        $this->load->view('statistics/coinsValue/'.$type,$out);
    }
    private function spinValue($dateList){
        $curMaxLevelId = oo::commonOprModel('config')->getCurMaxLevelId();
        $data = [];
        $tb = otable::CoinsValueSta();
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $uidArr= [];
                if(!empty($param)){
                    $tmpSpins = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("spins"));
                    $totalSpins = [];
                    foreach ($tmpSpins as $uid=>$row){
                        $tmp = explode('-',$uid);
                        $totalSpins[$tmp[1]] += $row;
                    }
                    foreach ($totalSpins as $uid=>$spins){
                        if($param['min'] >= $spins && $spins < $param['max']){
                            $uidArr[] = $uid;
                        }
                    }
                }
                $spinValue  = $this->CurSpinValue($uidArr);
                for($i=1;$i<$curMaxLevelId;$i++){
                    $data[$date][$i] = intval($spinValue[$i]);
                }
            }else{
                $ret = oo::commonOprDb('statistics')->getAll("SELECT * FROM {$tb} WHERE `date`={$date} ORDER BY levelId",MYSQLI_ASSOC);
                $ret = array_column($ret,"value","levelId");
                for($i=1;$i<$curMaxLevelId;$i++){
                    $data[$date][$i] = intval($ret[$i]);
                }
            }
        }
        return $data;
    }
    private function coinsAdd($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $spinValue  = $this->CurSpinValue();
                $coinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("money+"));
                $coinsAddArr= [];
                foreach ($coinsAdd as $uid=>$num){
                    $tmp = explode('-',$uid);
                    $coinsAddArr[$tmp[0]] += intval($num/$spinValue[$tmp[1]]);
                }
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsAdd FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsAddArr = array_column($ret,"coinsAdd","uid");
            }
            $data[$date] = $this->actionScale($coinsAddArr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsSpend($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $spinValue  = $this->CurSpinValue();
                $coinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("money-"));
                $coinsArr= [];
                foreach ($coinsAdd as $uid=>$num){
                    $tmp = explode('-',$uid);
                    $coinsArr[$tmp[0]] += intval($num/$spinValue[$tmp[1]]);
                }
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsSpend FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsArr = array_column($ret,"coinsSpend","uid");
            }
            $data[$date] = $this->actionScale($coinsArr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsStock($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $spinValue  = $this->CurSpinValue();
                $coinsArr= oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsArr = array_column($ret,"coinsStock","uid");
            }
            $data[$date] = $this->actionScale($coinsArr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsStockMean($base,$dateList){
        $data = [];
        $temp = [];
        $levelTmp = [];
        $curMaxLevelId = oo::commonOprModel('config')->getCurMaxLevelId();
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $spinValue  = $this->CurSpinValue();
                $coinsArr= oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
                foreach ($coinsArr as $uid=>$coins){
                    $temp[$dauArr[$uid]] += $coins;
                    $levelTmp[$dauArr[$uid]] += 1;
                }
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,levelId,coinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                foreach ($ret as $row){
                    $temp[$row['levelId']] += $row['coinsStock'];
                    $levelTmp[$row['levelId']] += 1;
                }
            }
            $base = [];
            for($i=1;$i<$curMaxLevelId;$i++){
                $data[$date][$i] = $levelTmp[$i] == 0 ? 0 : intval($temp[$i] / $levelTmp[$i]);
                $base[] = $i;
            }
        }
        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsStockChange($base,$dateList){
        $newList = $dateList;
        foreach ($dateList as $row){
            $time = date("Ymd",strtotime("$row -1 days"));
            if(!in_array($time,$newList)){
                $newList[] = $time;
            }
        }
        rsort($newList);
        foreach ($newList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $spinValue = $this->CurSpinValue();
                $coinsArr  = oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
            }else{
                $tb        = otable::ResourceMonitoringSta();
                $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsArr  = array_column($ret,"coinsStock","uid");
            }
            $tmp[$date] = $coinsArr;
        }
        $data = [];
        foreach ($dateList as $date){
            $time = date("Ymd",strtotime("$date -1 days"));
            $tmpUserAddNum = [];
            $tmpUserLowNum = [];
            foreach ($tmp[$date] as $uid=>$num){
                if(empty($tmp[$time][$uid])){
                    continue;
                }
                $actionNum = $num - $tmp[$time][$uid];
                if($actionNum >=0){
                    $tmpUserAddNum[$uid] = $actionNum;
                }else{
                    $tmpUserLowNum[$uid] = $actionNum;
                }
            }
            $add = $this->actionScale($tmpUserAddNum);
            $low = $this->actionScale2($tmpUserLowNum);
            $data[$date] = [
                $add[0],
                $add[1],
                $low[0],
                $low[1],
            ];
        }
        $base[] = 50500;
        $add = $this->actionScaleBase($base);
        $low = $this->actionScaleBase2($base);
        return [
            'data' => $data,
            'base' => [
                $add[0],
                $add[1],
                $low[0],
                $low[1],
            ],
        ];
    }
    private function spinsAdd($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $coinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("spins+"));
                $coinsAddArr= [];
                foreach ($coinsAdd as $uid=>$num){
                    $coinsAddArr[$uid] = $num;
                }
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsAdd FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsAddArr = array_column($ret,"spinsAdd","uid");
            }
            $data[$date] = $this->actionScale($coinsAddArr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsSpend($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $coinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("spins-"));
                $coinsArr= [];
                foreach ($coinsAdd as $uid=>$num){
                    $coinsArr[$uid] = $num;
                }
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsSpend FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsArr = array_column($ret,"spinsSpend","uid");
            }
            $data[$date] = $this->actionScale($coinsArr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsStock($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $coinsArr= oo::commonOprModel('Crontab')->UserSpin($dauArr); // 4,5,6
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsArr = array_column($ret,"coinsStock","uid");
            }
            $data[$date] = $this->actionScale($coinsArr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsStockMean($base,$dateList){
        $data = [];
        $temp = [];
        $levelTmp = [];
        $curMaxLevelId = oo::commonOprModel('config')->getCurMaxLevelId();
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $coinsArr= oo::commonOprModel('Crontab')->UserSpin($dauArr); // 4,5,6
                foreach ($coinsArr as $uid=>$coins){
                    $temp[$dauArr[$uid]] += $coins;
                    $levelTmp[$dauArr[$uid]] += 1;
                }
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,levelId,spinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                foreach ($ret as $row){
                    $temp[$row['levelId']] += $row['spinsStock'];
                    $levelTmp[$row['levelId']] += 1;
                }
            }
            $base = [];
            for($i=1;$i<$curMaxLevelId;$i++){
                $data[$date][$i] = $levelTmp[$i] == 0 ? 0 : intval($temp[$i] / $levelTmp[$i]);
                $base[] = $i;
            }
        }
        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsStockChange($base,$dateList){
        $newList = $dateList;
        foreach ($dateList as $row){
            $time = date("Ymd",strtotime("$row -1 days"));
            if(!in_array($time,$newList)){
                $newList[] = $time;
            }
        }
        rsort($newList);
        foreach ($newList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $coinsArr  = oo::commonOprModel('Crontab')->UserSpin($dauArr); // 4,5,6
            }else{
                $tb        = otable::ResourceMonitoringSta();
                $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $coinsArr  = array_column($ret,"spinsStock","uid");
            }
            $tmp[$date] = $coinsArr;
        }
        $data = [];
        foreach ($dateList as $date){
            $time = date("Ymd",strtotime("$date -1 days"));
            $tmpUserAddNum = [];
            $tmpUserLowNum = [];
            foreach ($tmp[$date] as $uid=>$num){
                if(empty($tmp[$time][$uid])){
                    continue;
                }
                $actionNum = $num - $tmp[$time][$uid];
                if($actionNum >=0){
                    $tmpUserAddNum[$uid] = $actionNum;
                }else{
                    $tmpUserLowNum[$uid] = $actionNum;
                }
            }
            $add = $this->actionScale($tmpUserAddNum);
            $low = $this->actionScale2($tmpUserLowNum);
            $data[$date] = [
                $add[0],
                $add[1],
                $low[0],
                $low[1],
            ];
        }
        $base[] = 50500;
        $add = $this->actionScaleBase($base);
        $low = $this->actionScaleBase2($base);
        return [
            'data' => $data,
            'base' => [
                $add[0],
                $add[1],
                $low[0],
                $low[1],
            ],
        ];
    }
    private function slotCount($base,$dateList){
        $base = array_reverse($base);
        $data = [];
        foreach ($dateList as $date){
            if($date == date('Ymd')){
                $arr = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("slot"));
            }else{
                $tb  = otable::ResourceMonitoringSta();
                $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,slotNum FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
                $arr = array_column($ret,"slotNum","uid");
            }
            $data[$date] = $this->actionScale($arr);
        }
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }

    /******** 筛选 ********/
    public function indexByParam($type,$param){
        $cache = oo::commonOprRedis('statistics')->get(okeys::CMSCache("CoinsValue","indexByParam",$type.$param));
        if(!empty($cache)){
            $out = json_decode($cache,true);
        }else{
            $out = ["setTime"=>[get_cookie('stime'),min(get_cookie('etime'),date('Y-m-d'))],"id"=>$type];
            $out['userGroup'] = array_column(oo::commonOprModel('config')->getUserGroupConfig2(),'name','id');
            $param = json_decode(base64_decode($param),true);
            $date  = str_replace("-","",$param['date']);
            if($type == 1){
                $out['data'] = $this->spinValueByParam($date,$param);
            }else if($type == 2){
                foreach ($this->reason as $id=>$name){
                    if(in_array($id,[213,10,41,1,204,4,9,2,3])){
                        $reason[$id] = $name;
                    }
                }
                $out['reason'] = $reason;
                $out = array_merge($out,$this->coinsAddByParam($date,$param));
            }else if($type == 3){
                $out = array_merge($out,$this->coinsSpendByParam($date,$param));
            }else if($type == 4){
                $out = array_merge($out,$this->coinsStockByParam($date,$param));
            }else if($type == 5){
                $out = array_merge($out,$this->coinsStockMeanByParam($date,$param));
            }else if($type == 6){
                $out = array_merge($out,$this->coinsStockChangeByParam($date,$param));
            }else if($type == 7){
                foreach ($this->reason as $id=>$name){
                    if(in_array($id,[204,1,111,5,208,15,80,213,217,30,451,210,33])){
                        $reason[$id] = $name;
                    }
                }
                $out['reason'] = $reason;
                $out = array_merge($out,$this->spinsAddByParam($date,$param));
            }else if($type == 8){
                $out = array_merge($out,$this->spinsSpendByParam($date,$param));
            }else if($type == 9){
                $out = array_merge($out,$this->spinsStockByParam($date,$param));
            }else if($type == 10){
                $out = array_merge($out,$this->spinsStockMeanByParam($date,$param));
            }else if($type == 11){
                $out = array_merge($out,$this->spinsStockChangeByParam($date,$param));
            }else if($type == 12){
                $out = array_merge($out,$this->slotCountByParam($date,$param));
            }
            $expire = $date == date("Ymd") ?10*60:24*60*60;
            oo::commonOprRedis('statistics')->setex(okeys::CMSCache("CoinsValue","indexByParam",$type.$param),json_encode($out),$expire);
        }
        $this->load->view('statistics/coinsValue/'.$type,$out);
    }
    private function spinValueByParam($date,$param){
        $curMaxLevelId = oo::commonOprModel('config')->getCurMaxLevelId();
        $data = [];
        if($date == date('Ymd')){
            $uidArr= [];
            $tmpSpins = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("spins"));
            $totalSpins = [];
            foreach ($tmpSpins as $uid=>$row){
                $tmp = explode('-',$uid);
                $totalSpins[$tmp[1]] += $row;
            }
            foreach ($totalSpins as $uid=>$spins){
                if($param['min'] >= $spins && $spins < $param['max']){
                    $uidArr[] = $uid;
                }
            }
            $spinValue  = $this->CurSpinValue($uidArr);
            for($i=1;$i<$curMaxLevelId;$i++){
                $data[$date][$i] = intval($spinValue[$i]);
            }
        }else{
            $spinValue  = [];
            $database   = oo::commonOprModel('common')->getDbInstance(otable::Assetslog(),strtotime($date));
            for($i=0;$i<10;$i++){
                $tb  = otable::Assetslog($date,$i);
                $notStr = "";
                if(!empty($payWhiteList)){
                    $actionWhiteList = [];
                    foreach ($payWhiteList as $uid){
                        if($uid % 10 == $i){
                            $actionWhiteList[] = $uid;
                        }
                    }
                    if(!empty($actionWhiteList)){
                        $actionWhiteList = implode(",",$actionWhiteList);
                        $notStr = "AND uid NOT IN ({$actionWhiteList})";
                    }
                }
                $ret = oo::commonOprDb($database)->getAll("SELECT DISTINCT uid,SUM(num) as num FROM {$tb} where `type`='spins' AND `crease`='-' AND `levelId` > 1 {$notStr} ORDER BY levelId DESC",MYSQLI_ASSOC);
                $uidArr = [];
                foreach ($ret as $row){
                    if($param['min'] >= $row['num'] && $row['num'] < $param['max']){
                        $uidArr[] = $row['uid'];
                    }
                }
                $uidArr    = array_column($ret,'uid');
                $inStr     = "";
                if(!empty($uidArr)){
                    $uidStr = implode(',',$uidArr);
                    $inStr  = "AND uid IN ({$uidStr})";
                }
                $coinArr = oo::commonOprDb($database)->getAll("SELECT SUM(num) as num,levelId FROM {$tb} where rid=1 AND `type`='money' AND `crease`='+' {$inStr} GROUP BY levelId",MYSQLI_ASSOC);
                $coinArr = array_column($coinArr,'num','levelId');
                $spinArr = oo::commonOprDb($database)->getAll("SELECT SUM(num) as num,levelId FROM {$tb} where rid=1 AND `type`='spins' AND `crease`='-' {$inStr} GROUP BY levelId",MYSQLI_ASSOC);
                $spinArr = array_column($spinArr,'num','levelId');
                foreach ($coinArr as $levelId=>$num){
                    if($spinArr[$levelId] > 0){
                        $spinValue[$levelId] += intval($num/$spinArr[$levelId]);
                    }else {
                        $spinValue[$levelId] += 0;
                    }
                }
            }
            for($i=1;$i<$curMaxLevelId;$i++){
                $data[$date][$i] = intval($spinValue[$i]);
            }
        }
        return $data;
    }
    private function coinsAddByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $tem = [];
        list($dauArr,$spinValue)  = oo::commonOprModel('crontab')->spinValue($date,$tem,$uidList);
        $dauUidArr = array_keys($dauArr);
        list($coinsAddArr, $coinsSpendArr) = oo::commonOprModel('crontab')->coinOutInPut($date,$dauUidArr,$spinValue,1,$param['way']);
        $data[$date] = $this->actionScale($coinsAddArr);
        $base   = $this->scale;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsSpendByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $tem = [];
        list($dauArr,$spinValue)  = oo::commonOprModel('crontab')->spinValue($date,$tem,$uidList);
        $dauUidArr = array_keys($dauArr);
        list($coinsAddArr, $coinsSpendArr) = oo::commonOprModel('crontab')->coinOutInPut($date,$dauUidArr,$spinValue,2,$param['way']);
        $data[$date] = $this->actionScale($coinsSpendArr);
        $base   = $this->scale;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsStockByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        if($date == date('Ymd')){
            $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $spinValue  = $this->CurSpinValue($uidList);
            $coinsArr= oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
        }else{
            $uidList = implode(',',$uidList);
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock FROM {$tb} WHERE `date`={$date} AND uid IN ({$uidList})",MYSQLI_ASSOC);
            $coinsArr = array_column($ret,"coinsStock","uid");
        }
        $data[$date] = $this->actionScale($coinsArr);
        $base   = $this->scale;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsStockMeanByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $data = [];
        $temp = [];
        $levelTmp = [];
        if($date == date('Ymd')){
            $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $spinValue  = $this->CurSpinValue($uidList);
            $coinsArr= oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
            foreach ($coinsArr as $uid=>$coins){
                $temp[$dauArr[$uid]] += $coins;
                $levelTmp[$dauArr[$uid]] += 1;
            }
        }else{
            $uidList = implode(',',$uidList);
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,levelId,coinsStock FROM {$tb} WHERE `date`={$date} AND uid IN ({$uidList})",MYSQLI_ASSOC);
            foreach ($ret as $row){
                $temp[$row['levelId']] += $row['coinsStock'];
                $levelTmp[$row['levelId']] += 1;
            }
        }
        $base = [];
        $curMaxLevelId = oo::commonOprModel('config')->getCurMaxLevelId();
        for($i=1;$i<$curMaxLevelId;$i++){
            $data[$date][$i] = $levelTmp[$i] == 0 ? 0 : intval($temp[$i] / $levelTmp[$i]);
            $base[] = $i;
        }
        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function coinsStockChangeByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $base = $this->scale;
        $dateList[] = $date;
        $newList = $dateList;
        foreach ($dateList as $row){
            $time = date("Ymd",strtotime("$row -1 days"));
            if(!in_array($time,$newList)){
                $newList[] = $time;
            }
        }
        rsort($newList);
        foreach ($newList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $spinValue = $this->CurSpinValue($uidList);
                $coinsArr  = oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
            }else{
                $uidList   = implode(',',$uidList);
                $tb        = otable::ResourceMonitoringSta();
                $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock FROM {$tb} WHERE `date`={$date} AND uid IN ({$uidList})",MYSQLI_ASSOC);
                $coinsArr  = array_column($ret,"coinsStock","uid");
            }
            $tmp[$date] = $this->actionScale($coinsArr);
        }
        $data = [];
        foreach ($dateList as $date){
            foreach ($tmp[$date] as $index=>$row){
                $time = date("Ymd",strtotime("$date -1 days"));
                $data[$date][$index] = $row - $tmp[$time][$index];
            }
        }

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsAddByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        list($addArr, $spendArr) = oo::commonOprModel('crontab')->spinOutInPut($date,$uidList,1,$param['way']);
        $data[$date] = $this->actionScale($addArr);
        $base   = $this->scale;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsSpendByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        list($coinsAddArr, $coinsSpendArr) = oo::commonOprModel('crontab')->spinOutInPut($date,$uidList,2,$param['way']);
        $data[$date] = $this->actionScale($coinsSpendArr);
        $base   = $this->scale;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsStockByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $base = array_reverse($this->scale);
        $data = [];
        if($date == date('Ymd')){
            $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $coinsArr= oo::commonOprModel('Crontab')->UserSpin(array_intersect($uidList,$dauArr)); // 4,5,6
        }else{
            $uidList = implode(",",$uidList);
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock FROM {$tb} WHERE `date`={$date} AND uid IN ({$uidList})",MYSQLI_ASSOC);
            $coinsArr = array_column($ret,"coinsStock","uid");
        }
        $data[$date] = $this->actionScale($coinsArr);
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsStockMeanByParam($date,$param){
        $data = [];
        $temp = [];
        $levelTmp = [];
        $curMaxLevelId = oo::commonOprModel('config')->getCurMaxLevelId();
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        if($date == date('Ymd')){
            $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $coinsArr= oo::commonOprModel('Crontab')->UserSpin(array_intersect($uidList,$dauArr)); // 4,5,6
            foreach ($coinsArr as $uid=>$coins){
                $temp[$dauArr[$uid]] += $coins;
                $levelTmp[$dauArr[$uid]] += 1;
            }
        }else{
            $uidList = implode(",",$uidList);
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,levelId,spinsStock FROM {$tb} WHERE `date`={$date} AND  uid IN ({$uidList})",MYSQLI_ASSOC);
            foreach ($ret as $row){
                $temp[$row['levelId']] += $row['spinsStock'];
                $levelTmp[$row['levelId']] += 1;
            }
        }
        $base = [];
        for($i=1;$i<$curMaxLevelId;$i++){
            $data[$date][$i] = $levelTmp[$i] == 0 ? 0 : intval($temp[$i] / $levelTmp[$i]);
            $base[] = $i;
        }

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function spinsStockChangeByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $base = $this->scale;
        $dateList[] = $date;
        $newList = $dateList;
        foreach ($dateList as $row){
            $time = date("Ymd",strtotime("$row -1 days"));
            if(!in_array($time,$newList)){
                $newList[] = $time;
            }
        }
        rsort($newList);
        foreach ($newList as $date){
            if($date == date('Ymd')){
                $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
                $coinsArr  = oo::commonOprModel('Crontab')->UserSpin(array_intersect($dauArr,$uidList)); // 4,5,6
            }else{
                $uidList   = implode(',',$uidList);
                $tb        = otable::ResourceMonitoringSta();
                $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock FROM {$tb} WHERE `date`={$date} AND uid IN ({$uidList})",MYSQLI_ASSOC);
                $coinsArr  = array_column($ret,"spinsStock","uid");
            }
            $tmp[$date] = $this->actionScale($coinsArr);
        }
        $data = [];
        foreach ($dateList as $date){
            foreach ($tmp[$date] as $index=>$row){
                $time = date("Ymd",strtotime("$date -1 days"));
                $data[$date][$index] = $row - $tmp[$time][$index];
            }
        }

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }
    private function slotCountByParam($date,$param){
        $uidList = $this->getUidArrByUserGroupOnDate($date,$param['group']);
        $base = array_reverse($this->scale);
        $data = [];
        if($date == date('Ymd')){
            $arr = [];
            foreach ($uidList as $uid){
                $arr[$uid] = oo::commonOprRedis('statistics')->hGet(okeys::CoinsValueSta("slot"),$uid);
            }
        }else{
            $uidList = implode(',',$uidList);
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,slotNum FROM {$tb} WHERE `date`={$date} AND uid IN ({$uidList})",MYSQLI_ASSOC);
            $arr = array_column($ret,"slotNum","uid");
        }
        $data[$date] = $this->actionScale($arr);
        $base   = array_reverse($base);
        $base[] = 50500;

        return [
            'data' => $data,
            'base' => $this->actionScaleBase($base),
        ];
    }

    /******** 导出 ********/
    public function export($type,$param){
        $param = json_decode(base64_decode($param),true);
        $date  = str_replace("-","",$param['date']);
        if($type == 2){
            $this->coinsAddExport($date,$param);
        }else if($type == 3){
            $this->coinsSpendExport($date,$param);
        }else if($type == 4){
            $this->coinsStockExport($date,$param);
        }else if($type == 5){
            $this->coinsStockMeanExport($date,$param);
        }else if($type == 6){
            $this->coinsStockChangeExport($date,$param);
        }else if($type == 7){
            $this->spinsAddExport($date,$param);
        }else if($type == 8){
            $this->spinsSpendExport($date,$param);
        }else if($type == 10){
            $this->spinsStockMeanExport($date,$param);
        }else if($type == 11){
            $this->spinsStockChangeExport($date,$param);
        }else if($type == 12){
            $this->slotCountExport($date,$param);
        }
    }
    private function coinsAddExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $spinValue  = $this->CurSpinValue();
            $coinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("money+"));
            $coinsAddArr= [];
            foreach ($coinsAdd as $uid=>$num){
                $tmp = explode('-',$uid);
                $coinsAddArr[$tmp[0]] += intval($num/$spinValue[$tmp[1]]);
            }
            $ret = [];
            foreach ($coinsAddArr as $uid=>$num) {
                if ($min <= $num && $num <= $max) {
                    $ret[] = [
                        'uid' => $uid,
                        'coinsAdd' => $num,
                        'levelId' => oo::commonOprRedis('statistics')->hGet(okeys::CoinsValueSta("dau"), $uid),
                    ];
                }
            }
        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsAdd,levelId FROM {$tb} WHERE `date`={$date} AND coinsAdd>{$min} AND coinsAdd<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('金币产出_'.$date,['uid'=>'用户ID','coinsAdd'=>'金币产出','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function coinsSpendExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $spinValue  = $this->CurSpinValue();
            $coinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("money-"));
            $coinsAddArr= [];
            foreach ($coinsAdd as $uid=>$num){
                $tmp = explode('-',$uid);
                $coinsAddArr[$tmp[0]] += intval($num/$spinValue[$tmp[1]]);
            }
            $ret = [];
            foreach ($coinsAddArr as $uid=>$num) {
                if ($min <= $num && $num <= $max) {
                    $ret[] = [
                        'uid' => $uid,
                        'coinsAdd' => $num,
                        'levelId' => oo::commonOprRedis('statistics')->hGet(okeys::CoinsValueSta("dau"), $uid),
                    ];
                }
            }
        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsSpend,levelId FROM {$tb} WHERE `date`={$date} AND coinsSpend>{$min} AND coinsSpend<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('金币消耗_'.$date,['uid'=>'用户ID','coinsSpend'=>'金币消耗','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);

    }
    private function coinsStockExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $spinValue  = $this->CurSpinValue();
            $dauArr     = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $userCoinArr= oo::commonOprModel('crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
            foreach ($userCoinArr as $uid=>$num) {
                if ($min <= $num && $num <= $max) {
                    $ret[] = [
                        'uid'     => $uid,
                        'coinsStock'=> $num,
                        'levelId' => $dauArr[$uid],
                    ];
                }
            }
        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock,levelId FROM {$tb} WHERE `date`={$date} AND coinsStock>{$min} AND coinsStock<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('金币存量_'.$date,['uid'=>'用户ID','coinsStock'=>'金币存量','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function coinsStockMeanExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $spinValue  = $this->CurSpinValue();
            $dauArr     = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $userCoinArr= oo::commonOprModel('crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
            foreach ($userCoinArr as $uid=>$num) {
                if ($min <= $dauArr[$uid] && $max <= $dauArr[$uid]) {
                    $ret[] = [
                        'uid'       => $uid,
                        'coinsStock'=> $num,
                        'levelId'   => $dauArr[$uid],
                    ];
                }
            }
        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock,levelId FROM {$tb} WHERE `date`={$date} AND coinsStock>{$min} AND coinsStock<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('金币存量_'.$date,['uid'=>'用户ID','coinsStock'=>'金币存量','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function coinsStockChangeExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        $preDate = date("Ymd",strtotime("$date -1 days"));
        $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
        if($date == date('Ymd')){
            $spinValue = $this->CurSpinValue();
            $coinsArr  = oo::commonOprModel('Crontab')->UserCoin($dauArr,$spinValue); // 4,5,6
        }else{
            $tb        = otable::ResourceMonitoringSta();
            $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
            $coinsArr  = array_column($ret,"coinsStock","uid");
        }
        $tb        = otable::ResourceMonitoringSta();
        $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,coinsStock FROM {$tb} WHERE `date`={$preDate}",MYSQLI_ASSOC);
        $coinsArr2  = array_column($ret,"coinsStock","uid");
        $ret = [];
        foreach ($coinsArr as $uid=>$num){
            if($min <= $num-intval($coinsArr2[$uid]) && $num-intval($coinsArr2[$uid]) <= $max){
                $ret[] = [
                    'uid'       => $uid,
                    'coinsStock'=> $num-intval($coinsArr2[$uid]),
                    'levelId'   => $dauArr[$uid],
                ];
            }
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('金币存量变化_'.$date,['uid'=>'用户ID','coinsStock'=>'金币存量变化','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function spinsAddExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $spinsAdd   = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("spins-"));
            foreach ($spinsAdd as $uid=>$num){
                if ($min <= $num && $num <= $max) {
                    $ret[] = [
                        'uid' => $uid,
                        'spinsAdd' => $num,
                        'levelId' => oo::commonOprRedis('statistics')->hGet(okeys::CoinsValueSta("dau"), $uid),
                    ];
                }
            }

        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsAdd,levelId FROM {$tb} WHERE `date`={$date} AND spinsAdd>{$min} AND spinsAdd<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('体力产出_'.$date,['uid'=>'用户ID','spinsAdd'=>'体力产出','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function spinsSpendExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $spinsSpend = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("spins-"));
            $ret = [];
            foreach ($spinsSpend as $uid=>$num) {
                if ($min <= $num && $num <= $max) {
                    $ret[] = [
                        'uid' => $uid,
                        'spinsSpend' => $num,
                        'levelId' => oo::commonOprRedis('statistics')->hGet(okeys::CoinsValueSta("dau"), $uid),
                    ];
                }
            }
        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsSpend,levelId FROM {$tb} WHERE `date`={$date} AND spinsSpend>{$min} AND spinsSpend<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('体力消耗_'.$date,['uid'=>'用户ID','spinsSpend'=>'体力消耗','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function spinsStockMeanExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
            $userCoinArr= oo::commonOprModel('Crontab')->UserSpin($dauArr); // 4,5,6
            foreach ($userCoinArr as $uid=>$num) {
                if ($min <= $dauArr[$uid] && $dauArr[$uid] <= $max) {
                    $ret[] = [
                        'uid'     => $uid,
                        'spinsStock'=> $num,
                        'levelId' => $dauArr[$uid],
                    ];
                }
            }
        }else{
            $tb  = otable::ResourceMonitoringSta();
            $ret = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock,levelId FROM {$tb} WHERE `date`={$date} AND spinsStock>{$min} AND spinsStock<{$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('体力存量_'.$date,['uid'=>'用户ID','spinsStock'=>'体力存量','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function spinsStockChangeExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        $preDate = date("Ymd",strtotime("$date -1 days"));
        $dauArr    = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("dau"));
        if($date == date('Ymd')){
            $coinsArr  = oo::commonOprModel('Crontab')->UserSpin($dauArr); // 4,5,6
        }else{
            $tb        = otable::ResourceMonitoringSta();
            $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock FROM {$tb} WHERE `date`={$date}",MYSQLI_ASSOC);
            $coinsArr  = array_column($ret,"spinsStock","uid");
        }
        $tb        = otable::ResourceMonitoringSta();
        $ret       = oo::commonOprDb('statistics')->getAll("SELECT uid,spinsStock FROM {$tb} WHERE `date`={$preDate}",MYSQLI_ASSOC);
        $coinsArr2  = array_column($ret,"spinsStock","uid");
        $ret = [];
        foreach ($coinsArr as $uid=>$num){
            if($min <= $num-intval($coinsArr2[$uid]) && $num-intval($coinsArr2[$uid]) <= $max){
                $ret[] = [
                    'uid'       => $uid,
                    'spinsStock'=> $num-intval($coinsArr2[$uid]),
                    'levelId'   => $dauArr[$uid],
                ];
            }
        }
        $ret = $this->payNumCommon($date,$ret);
        $this->exportCommon('体力存量_'.$date,['uid'=>'用户ID','spinsStock'=>'体力存量','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
    private function slotCountExport($date,$param){
        $min = $param['min'];
        $max = $param['max'];
        if($date == date('Ymd')){
            $coinsArr = [];
            $tmp  = oo::commonOprRedis('statistics')->hGetAll(okeys::CoinsValueSta("slot")); // 4,5,6
            foreach ($tmp as $uid=>$num){
                if($min <= $num && $num <= $max){
                    $coinsArr[] = [
                        'uid'       => $uid,
                        'slotNum'   => $num,
                        'levelId'   => oo::commonOprRedis('statistics')->hGet(okeys::CoinsValueSta("dau"),$uid),
                    ];
                }
            }
        }else{
            $tb        = otable::ResourceMonitoringSta();
            $coinsArr   = oo::commonOprDb('statistics')->getAll("SELECT uid,slotNum,levelId FROM {$tb} WHERE `date`={$date} AND slotNum>={$min} AND slotNum<={$max}",MYSQLI_ASSOC);
        }
        $ret = $this->payNumCommon($date,$coinsArr);
        $this->exportCommon('老虎机次数_'.$date,['uid'=>'用户ID','slotNum'=>'老虎机次数','levelId'=>'等级','payNum'=>'当日充值金额'],$ret);
    }
}