<?php
namespace App\Sports\Controller;

use BaseController\Controller;
use App\Sports\Model\St_zq_competition as ZQmatchModel;
use App\Sports\Model\St_lq_competition as LQmatchModel;
use App\Sports\Model\St_wq_competition as WQmatchModel;
use App\Sports\Model\St_bq_competition as BQmatchModel;
use App\Sports\Model\St_zq_odds as ZQoddsModel;
use App\Sports\Model\St_lq_odds as LQoddsModel;
use App\Sports\Model\St_wq_odds as WQoddsModel;
use App\Sports\Model\St_bq_odds as BQoddsModel;
use App\Sports\Model\St_zq_league as leagueModel;

use Biz\Match\GetmatchData;
use Biz\Match\GetOddsData;

/**
 * Class Head
 * @package App\Sports\Controller
 * User: tank
 * Date: 2019/3/19
 */
class Head extends Controller{
    public function init() {
        $this->commonFunction =  C()->get('commonFunction');
        $this->getTypeData = new GetmatchData();
        $this->getOddsData = new GetOddsData();

    }

    /**
     * @throws \Exception
     * 返回首页球类列表
     */
    public function ballList (){
        $ret = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where('status',1)->get();

        if(!empty($ret)) Render($ret, '1', lang('Tips','Sports')->get('success'));

    }


    //获取各状态下所有赛事数量
    public function typeList(){
        $this->typeList_v2();
    }

    public function typeList_v1(){
        //获取所有未结束赛事
        $where = [
            ['status','<','2']
        ];
        $zqModel = $this->commonFunction->getModels('zq');
        $lqModel = $this->commonFunction->getModels('lq');
        $wqModel = $this->commonFunction->getModels('wq');
        $bqModel = $this->commonFunction->getModels('bq');

        $ZQmatchData = leagueModel::getLeagueMatchData($zqModel,$where,'zq');
        $LQmatchData = leagueModel::getLeagueMatchData($lqModel,$where,'lq');
        $WQmatchData = leagueModel::getLeagueMatchData($wqModel,$where,'wq');
        $BQmatchData = leagueModel::getLeagueMatchData($bqModel,$where,'bq');

        //处理空赔率赛事
        $ZQmatchData = $this->commonFunction->Handle_Odds_Null($ZQmatchData,$zqModel);
        $LQmatchData = $this->commonFunction->Handle_Odds_Null($LQmatchData,$lqModel);
        $WQmatchData = $this->commonFunction->Handle_Odds_Null($WQmatchData,$wqModel);
        $BQmatchData = $this->commonFunction->Handle_Odds_Null($BQmatchData,$bqModel);

        //合并所有球类赛事
        $matchAll = array_merge_recursive($ZQmatchData,$LQmatchData,$WQmatchData,$BQmatchData);

        //获取所有冠军盘口
        $where = [
            ['type','=',1],
            // ['source','=',$this->source]
        ];
        $select = ['match_id','type'];
        $ZQoddsData = ZQoddsModel::getOddsData($where,$select)->toArray();
        $LQoddsData = LQoddsModel::getOddsData($where,$select)->toArray(); 
        $WQoddsData = WQoddsModel::getOddsData($where,$select)->toArray();
        $BQoddsData = BQoddsModel::getOddsData($where,$select)->toArray();
        //合并所有球类冠军盘口
        $oddsAll = array_merge_recursive($ZQoddsData,$LQoddsData,$WQoddsData,$BQoddsData);

        $StRollBall = [];
        $StSoon = [];
        $StToday = [];
        $StMorningPlate = [];
        $StStringScene = [];
         //获取当天结束时间戳
         $endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //按状态条件分装数组
        foreach ($matchAll as $kk=>$vv){
            //滚球
            // if($vv['status'] < 2 and $vv['match_date'] == date("Y-m-d") and $vv['match_time'] > date("H:i:s", time()-(90*60)) and $vv['match_time'] < date("H:i:s", time())){
            //     $StRollBall[] = $vv;
            // }
            if($vv['status'] == 1 ){
                $StRollBall[] = $vv;
            }
            //即将
            if($vv['status'] == '0' and $vv['match_date'] == date("Y-m-d") and $vv['match_time'] < date("H:i:s", strtotime("+2 hour")) and $vv['match_time'] > date("H:i:s", time())){
                $StSoon[] = $vv;
            }
            //今日
            if($vv['match_date'] == date("Y-m-d") and $vv['match_time'] > date("H:i:s", time()) and $vv['status'] < 2){
                $StToday[] = $vv;
            }
            //早盘
            if($vv['us_time'] > $this->commonFunction->qgmdate('Y-m-d H:i:s', $endTime, -4) and $vv['status'] < 2 ){
                $StMorningPlate[] = $vv;
            }
            //串场
            if($vv['is_stringscene'] == 1 and $vv['us_time'] > $this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4) and $vv['status'] < 2){
                $StStringScene[] = $vv;
            }
        }
        //获取所有状态
        $type = lang('GameTypes','Sports')->getAll();
        foreach ($type as $k=>$v){
            switch ($v['type_code']) {
                case 'StRollBall'://滚球
                    $type[$k]['matchNum'] = count($StRollBall);
                    break;
                case 'StSoon'://即将
                    $type[$k]['matchNum'] = count($StSoon);
                    break;
                case 'StToday'://今日
                    $type[$k]['matchNum'] = count($StToday);
                    break;
                case 'StMorningPlate'://早盘
                    $type[$k]['matchNum'] = count($StMorningPlate);
                    break;
                case 'StStringScene'://串场
                    $type[$k]['matchNum'] = count($StStringScene);
                    break;
                case 'StChampion'://冠军
                    $type[$k]['matchNum'] = count($oddsAll);
                    break;
                default:
                    throw new \Exception(Render([], '10002', lang('Tips', 'Sports')->get('PARAM_ERROR')));
            }
        }
        Render($type, '1', lang('Tips','Sports')->get('success'));

    }


    public function typeList_v2(){
        //获取所有未结束赛事
        $where = [
            ['status','<','2']
        ];
        $zqModel = $this->commonFunction->getModels('zq');
        $lqModel = $this->commonFunction->getModels('lq');
        $wqModel = $this->commonFunction->getModels('wq');
        $bqModel = $this->commonFunction->getModels('bq');

        $ZQmatchData = leagueModel::getLeagueMatchData($zqModel,$where,'zq'); 
        $LQmatchData = leagueModel::getLeagueMatchData($lqModel,$where,'lq');
        $WQmatchData = leagueModel::getLeagueMatchData($wqModel,$where,'wq');
        $BQmatchData = leagueModel::getLeagueMatchData($bqModel,$where,'bq');

        //处理空赔率赛事
        $ZQmatchData = $this->commonFunction->Handle_Odds_Null($ZQmatchData,$zqModel);
        $LQmatchData = $this->commonFunction->Handle_Odds_Null($LQmatchData,$lqModel);
        $WQmatchData = $this->commonFunction->Handle_Odds_Null($WQmatchData,$wqModel);
        $BQmatchData = $this->commonFunction->Handle_Odds_Null($BQmatchData,$bqModel);

        //合并所有球类赛事
        $matchAll = array_merge_recursive($ZQmatchData,$LQmatchData,$WQmatchData,$BQmatchData);

        //获取所有冠军盘口
        $where = [
            ['type','=',1],
            // ['source','=',$this->source]
        ];
        $select = ['match_id','type'];
        $ZQoddsData = ZQoddsModel::getOddsData($where,$select)->toArray();
        $LQoddsData = LQoddsModel::getOddsData($where,$select)->toArray(); 
        $WQoddsData = WQoddsModel::getOddsData($where,$select)->toArray();
        $BQoddsData = BQoddsModel::getOddsData($where,$select)->toArray();
        //合并所有球类冠军盘口
        $oddsAll = array_merge_recursive($ZQoddsData,$LQoddsData,$WQoddsData,$BQoddsData);
     
        $StRollBall = [];
        $StSoon = [];
        $StToday = [];
        $StMorningPlate = [];
        $StStringScene = [];
        //获取当天结束时间戳
        $endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //按状态条件分装数组
        foreach ($matchAll as $kk=>$vv){
            //滚球
            if($vv['status'] == 1 ){
                $StRollBall[] = $vv;
            }
            //即将
            if($vv['status'] == '0' and $vv['match_date'] == date("Y-m-d") and $vv['match_time'] < date("H:i:s", strtotime("+2 hour")) and $vv['match_time'] > date("H:i:s", time())){
                $StSoon[] = $vv;
            }
            //今日
            if($vv['match_date'] == date("Y-m-d") and $vv['match_time'] > date("H:i:s", time()) and $vv['status'] < 2){
                $StToday[] = $vv;
            }
            //早盘
            if($vv['us_time'] > $this->commonFunction->qgmdate('Y-m-d H:i:s', $endTime, -4) and $vv['status'] < 2 ){
                $StMorningPlate[] = $vv;
            }
            //串场
            if($vv['is_stringscene'] == 1 and $vv['us_time'] > $this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4) and $vv['status'] < 2){
                $StStringScene[] = $vv;
            }
        }
        //获取所有状态
        $type = lang('GameTypes','Sports')->getAll();
        foreach ($type as $k=>$v){
            switch ($v['type_code']) {
                case 'StRollBall'://滚球
                    $type[$k]['matchNum'] = count($StRollBall);
                    // $type[$k]['matchNum'] = $this->getRollBallNum();
                    break;
                case 'StSoon'://即将
                    $type[$k]['matchNum'] = count($StSoon);
                    break;
                case 'StToday'://今日
                    $type[$k]['matchNum'] = count($StToday);
                    break;
                case 'StMorningPlate'://早盘
                    $type[$k]['matchNum'] = count($StMorningPlate);
                    break;
                case 'StStringScene'://串场
                    $type[$k]['matchNum'] = count($StStringScene);
                    break;
                case 'StChampion'://冠军
                    $type[$k]['matchNum'] = count($oddsAll);
                    break;
                default:
                    throw new \Exception(Render([], '10002', lang('Tips', 'Sports')->get('PARAM_ERROR')));
            }
        }
        Render($type, '1', lang('Tips','Sports')->get('success'));

    }

    /**
     * 获取各球类滚球数量
     */
    public function getRollBallNum(){
        $ret = lm('GameType', 'Sports')->select('id','game_code')->where('status',1)->get()->toArray();

        //滚球赛事数量
        $matchNum = [];
        foreach($ret as $k=>$v){
            if($v['game_code'] != 'gj'){
                //根据球类代码 获取相关model
                $models = $this->commonFunction->getModels($v['game_code']);
                $model_match = $models['model_match'];
                //获取 不同状态的查询条件
                $where = $this->commonFunction->getState('StRollBall',$model_match,$v['game_code']);
                //获取滚球数据
                $match_data= $this->getTypeData->getRollBall('',$models,$where,$v);
                if(!empty($match_data)){
                    foreach($match_data as $kk=>$vv){
                        $matchNum[] = $vv['matchNum'];
                    }
                }
            }
        }
        $matchNum = array_sum($matchNum);

        return $matchNum;
    }
}