<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 场次信息
*/
class Levelinfo extends MY_Controller
{
	
	public $postdata = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);
	public $data = array('referer' =>'', 'refresh' => 'false', 'state' => 'error', 'message' => '');

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('cookie');
		$this->load->library('curl');

		//请求地址
		$this->env = get_cookie($this->_wenenv);
		$this->_url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
	}


	/**
	*	场次实时数据
	*/
	public function index()
	{
        $data = array();

        //时间查询
        $dateArr = array('stime' => strtotime('0:0:1'), 'etime' => strtotime('23:59:59'));

        //获取数据
        $postData = array_merge($dateArr, $this->postdata);
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'index';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);

        $today = $retData['today'];
        $lastDay = $retData['lastday'];
        //统计数据
        if( !empty($today) || !empty($lastDay)){
            $data['timeList'] = array_keys($lastDay);
            $list = $list2 =  array();
            $listData = empty($today) ? [] : $today;
            $lastListData = empty($lastDay) ? [] : $lastDay;

            foreach ($listData as $time => $info) {
                foreach ($info as $smallblind => $num) {
                    $list[$smallblind][] = $num;
                }
            }
            //处理昨天的数据
            foreach ($lastListData as $time2 => $info2) {
                foreach ($info2 as $smallblind2 => $num2) {
                    $list2[$smallblind2][] = $num2;
                }
            }

            $str = '';
            foreach ($list as $sblind => $numList) {
                if ($sblind != 500){
                    $str .= '{ visible: false,';
                }else{
                    $str .= '{';
                }
                $str .= " name:'".$sblind."盲住',data:[";
                foreach ($numList as $n) {
                    $str .= $n.',';
                }
                $str .= ']},';
            }
//            $str = rtrim($str, ',');

            $lastStr = '';
            foreach ($list2 as $sblind2 => $numList2) {
                if ($sblind2 != 500){
                    $lastStr .= '{ visible: false,';
                }else{
                    $lastStr .= '{';
                }
                $lastStr .= " name:'".'昨日'.$sblind2."盲住',data:[";
                foreach ($numList2 as $n) {
                    $lastStr .= $n.',';
                }
                $lastStr .= ']},';
            }
            $lastStr = rtrim($lastStr, ',');
            $data['info'] = $str.$lastStr;
//            var_dump($data['info']);exit;
        }

        //实时牌桌数
        $postData = $this->postdata;
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'getUsedTableNum';
        $this->env == 2 && $postData['demo'] = 1;
        $retData = $this->curl->req($this->_url, $postData);
        $ret = json_decode($retData, 1);
        $data['game'] = $ret['game'];
        $data['omaha'] = $ret['omaha'];
        $data['private'] = $ret['private'];
        $data['match'] = $ret['match'];
        $data['allin'] = $ret['allin'];
        $data['redblack'] = $ret['redblack'];
        //在线的人数统计
        $data['texasNum'] = $ret['texasNum'];
        $data['privateNum'] = $ret['privateNum'];
        $data['omahaNum'] = $ret['omahaNum'];
        $data['matchNum'] = $ret['matchNum'];
        $data['allinNum'] = $ret['allinNum'];
        $data['redblackNum'] = $ret['redblackNum'];

        $this->load->view('levelinfo/index', $data);
	}


    /**
     * 统计 所有玩家的玩牌局数,和平均牌局数
     */
	public function allplayer(){
        $data = array('stime' => '', 'etime' => '');

        //时间查询
        $dateArr = array();
        if( $stime = get_cookie('levelinfoplayer_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('-7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('levelinfoplayer_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 day 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }
        $postData = array_merge($dateArr, $this->postdata);

        //获取DAU
        $sidList = $this->_getSidList();
        $postData['sidList'] = json_encode($sidList);
        $postData['mod'] = 'Statistics';
        $postData['act'] = 'loginplayer';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retDataDau = json_decode($res, TRUE);

        //获取玩牌数据
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'player';
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        if ($retData){
            //需要记录 德州场, 奥马哈, 私人房, 比赛场所有的玩牌局数
            $daylist = $data['daylist'] = $retData['daylist'];
            $texasList = $retData['info'];//德州数据
            $omahaList = $retData['omahaInfo'];//奥马哈数据
            $matchList = $retData['matchInfo'];//比赛场数据
            $redblackList = $retData['redblackInfo'];//红黑数据
            $allPlayTimes = [];//总玩牌局数
            foreach ($daylist as $day){

                //统计德州场,包括私人房
                foreach ($texasList as $texasInfo){
//                    foreach ($texasInfo as $texasPlayNum){
//                    }
                    $allPlayTimes[$day] += $texasInfo[$day];
                }

                //统计奥马哈
                foreach ($omahaList as $amahaInfo){
//                    foreach ($amahaInfo as $amahaPlayNum){
//                    }
                    $allPlayTimes[$day] += $amahaInfo[$day];
                }

                //统计比赛场
                foreach ($matchList as $matchInfo){
//                    foreach ($matchInfo as $matchDay => $matchPlayNum){
//                    }
                    $allPlayTimes[$day] += $matchInfo[$day];
                }

                //统计红黑
//                foreach ($redblackList as $redblackInfo){
////                    foreach ($redblackInfo as $redblackDay => $redblackPlayNum){
////                    }
//                }
                $allPlayTimes[$day] += $redblackList[$day];
            }
            $data['allPlayTimesStr'] = array_values($allPlayTimes);

            //处理DAY
            $dailyday = $playrate = [];
            foreach ($retDataDau as $dauday => $val){
                $tempDay = date('Ymd', strtotime($dauday));
                $dailyday[$tempDay] = $val[0]['total'];
                $playrate[$tempDay] = $val[0]['total'] == 0 ? 0 : ceil($allPlayTimes[$tempDay] / $val[0]['total']);
             }
            $data['dau'] = array_values($dailyday);
            $data['playrate'] = array_values($playrate);
        }
        $this->load->view('levelinfo/allplayer', $data);
    }

    /**
    *   获取场次实时人数统计
    */
    public function player()
    {
        $data = array('stime' => '', 'etime' => '');

        //时间查询
        $dateArr = array();
        if( $stime = get_cookie('levelinfoplayer_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('-7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('levelinfoplayer_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 day 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }

        //获取数据
        $postData = array_merge($dateArr, $this->postdata);
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'player';
        $postData['flag'] = 'T';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        //统计数据
        if( !empty($retData) ){
            $info = $retData['info'];//德州场
            $texasNumList = $retData['texasNumList'];//德州场人数列表
            $privateNumList = $retData['privateNumList'];//私人房人数列表
            $texasExpendList = $retData['texasExpendList'];//德州场消耗
            $texasTotalNumList = $retData['texasTotalNumList'];//德州场总的玩牌局数
            $dayList = $retData['daylist'];
            $list = array();
            $str = '';
            $str2 = '';
            $total = 0;

            foreach ($info as $smallblind => $val) {
                if(!in_array($smallblind, $this->texasBlindList)){
                    continue;
                }
//                $smallblind > 5001 ? $visibleStr = 'visible:false,' : $visibleStr = '';
                $visibleStr = '';

                $str .= is_numeric($smallblind) ? "{ name:'".$smallblind."盲住',".$visibleStr."data:[" : "{ name:'".$smallblind."',data:[";
                foreach ($val as $num) {
                    $list[$smallblind][] = $num;
                    $str .= $num.',';
                    $total += $num;
                }
                $str .= ']},';
            }
            $avgExpendList = $avgNumList = [];
            foreach ($dayList as $day) {
                $avgExpendList[$day] = empty($texasNumList[$day]) ? 0 : round($texasExpendList[$day] / $texasNumList[$day], 2);
                $avgNumList[$day] = empty($texasNumList[$day]) ? 0 : round($texasTotalNumList[$day] / $texasNumList[$day], 2);
            }
            $str2 .= "{ name:'德州场人数',data:[" .implode(',', $texasNumList). "]},";
            $str2 .= "{ name:'私人房人数',data:[" .implode(',', $privateNumList). "]}";
            $str2 .= ",{ name:'德州场总玩牌局数', data:[" .implode(',', $texasTotalNumList). "]}";
            $str2 .= ",{ name:'德州场人均玩牌局数', data:[" .implode(',', $avgNumList). "]}";
            $str2 .= ",{ name:'消耗', visible:false, data:[" .implode(',', $texasExpendList). "]}";
            $str2 .= ",{ name:'人均消耗', visible:false,data:[" .implode(',', $avgExpendList). "]}";
            $str = rtrim($str, ',');

            $data['info'] = $str;
            $data['infoPlayer'] = $str2;
            $data['daylist'] = $dayList;
        }
        $this->load->view('levelinfo/player', $data);
    }

  /**
    *   获取奥马哈场次人数统计
    */
    public function omaha()
    {
        $data = array('stime' => '', 'etime' => '');

        //时间查询
        $dateArr = array();
        if( $stime = get_cookie('levelinfoplayer_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('-7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('levelinfoplayer_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 day 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }

        //获取数据
        $postData = array_merge($dateArr, $this->postdata);
        $postData['falg'] = 'O';
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'player';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        //统计数据
        if( !empty($retData) ){
            $omahaInfo = $retData['omahaInfo'];//奥马哈
            $omahaNumList = $retData['omahaNumList'];
            $omahaExpendList = $retData['omahaExpendList'];//奥马哈场消耗
            $omahaTotalNumList = $retData['omahaTotalNumList'];//奥马哈总的玩牌局数
            $dayList = $retData['daylist'];
            $list = array();
            $omahaStr = '';
            $omahaPlayerStr = '';

            //奥马哈
            foreach ($omahaInfo as $smallblind => $val) {
                if(!in_array($smallblind, $this->omahaBlindList)){
                    continue;
                }
                $omahaStr .= is_numeric($smallblind) ? "{ name:'".$smallblind."盲住',data:[" : "{ name:'".$smallblind."',data:[";
                foreach ($val as $num) {
                    $list[$smallblind][] = $num;
                    $omahaStr .= $num.',';
                }
                $omahaStr .= ']},';
            }

            $avgExpendList = $avgNumList = [];
            foreach ($dayList as $day) {
                $avgExpendList[$day] = empty($omahaNumList[$day]) ? 0 : round($omahaExpendList[$day] / $omahaNumList[$day], 2);
                $avgNumList[$day] = empty($omahaTotalNumList[$day]) ? 0 : round($omahaTotalNumList[$day] / $omahaNumList[$day], 2);
            }

            $omahaPlayerStr .= "{ name:'人数',data:[" .implode(',', $omahaNumList). "]}";
            $omahaPlayerStr .= ",{ name:'总玩牌局数', data:[" .implode(',', $omahaTotalNumList). "]}";
            $omahaPlayerStr .= ",{ name:'人均玩牌局数', data:[" .implode(',', $avgNumList). "]}";
            $omahaPlayerStr .= ",{ name:'奥马哈场消耗', visible:false, data:[" .implode(',', $omahaExpendList). "]}";
            $omahaPlayerStr .= ",{ name:'奥马哈人均消耗', visible:false,data:[" .implode(',', $avgExpendList). "]}";

            $omahaStr = rtrim($omahaStr, ',');
            $data['omahaInfo'] = $omahaStr;
            $data['omahaPlayerInfo'] = $omahaPlayerStr;
            $data['daylist'] = $retData['daylist'];
        }
        $this->load->view('levelinfo/omaha', $data);
    }


	/**
	*	场次商品配置
	*/
	public function goods()
	{
//        if(!$this->isAdmin()) {//如果不是管理员没有权限查看用户列表
//            redirect('permission/forbidpage');
//            exit;
//        }
		//获取数据
        $postData = $this->postdata;
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'goods';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        //统计数据
        if( !empty($retData) ){
        	$list = $retData['list'];
        	$smallblind = $retData['smallblind'];
        	$data['list'] = $list;
        	$data['smallblind'] = $smallblind;
        	$data['total'] = count($list);
        }
        $this->load->view('levelinfo/goods', $data);
	}

	/**
	*	根据不同支付渠道返回对应商品
	*/
	public function getGoodsByChannel()
	{
		$gchannelId = $this->input->post('gchannelId');
		//获取数据
        $postData = $this->postdata;
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'getGoodsByChannel';
        $postData['gchannelId'] = $gchannelId;
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        die($res);
	}


	/**
	*	添加场次对应商品数据
	*/
	public function actionAdd()
	{
		$data = $this->data;
		$smallblind = $this->input->post('smallblind');
		$gtype = $this->input->post('gtype');
		$official_gname = $this->input->post('official_gname');//官方商品
		$gname = $this->input->post('gname');//第三方商品
        $gchannel = $this->input->post('gchannel');
        $official_broke_goods = $this->input->post('official_broke_goods');
		$broke_gname = $this->input->post('broke_gname');

		//获取数据
        $postData = array_merge($this->postdata, array('smallblind' => $smallblind, 'gtype' => $gtype, 'official_gname' => serialize($official_gname), 'gname' => serialize($gname), 'gchannel' => $gchannel, 'official_broke_goods' => $official_broke_goods, 'broke_gname' => $broke_gname));
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'actionAdd';
        $this->env == 2 && $postData['demo'] = 1;
        $retData = $this->curl->req($this->_url, $postData);
        $ret = json_decode($retData, 1);
        if( $ret['code'] > 0 ){
            $data['state'] = 'success';
            $data['message'] = '添加成功';
            $data['refresh'] = true;
        }
        die(json_encode($data));
	}

	/**
	*	根据ID获取单条数据
	*/
	public function actionGetOne()
	{
		$id = $this->input->post('id');
		//获取数据
        $postData = array_merge($this->postdata, array('id' => $id));
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'actionGetOne';
        $this->env == 2 && $postData['demo'] = 1;
        $retData = $this->curl->req($this->_url, $postData);
        $ret = json_decode($retData, 1);
        if( $ret ){
        	$data['smallblind'] = intval($ret['smallblind']);
        	$data['official_goods'] = unserialize( $ret['official_goods'] );
        	$data['other_goods'] = unserialize( $ret['other_goods'] );
            $data['gchannel'] = intval($ret['gchannel']);
            $data['official_broke_goods'] = $ret['official_broke_goods'];
        	$data['other_broke_goods'] = $ret['other_broke_goods'];
        }
        die(json_encode($data));
	}

	public function getPlayingTids()
    {
        $postData = $this->postdata;
        $postData['mod'] = 'Levelinfo';
        $postData['act'] = 'getUsedTableNum';
        $this->env == 2 && $postData['demo'] = 1;
        $retData = $this->curl->req($this->_url, $postData);
        $ret = json_decode($retData, 1);
        $this->load->view('levelinfo/playingtids', $ret);
    }

}