<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/3/21
 * Time: 14:07
 */
namespace App\Sports\Controller;

use BaseController\Controller;
use App\Sports\Model\St_zq_competition as matchModel;
use \System\Model;

class RollingBall extends Controller{
    public function init() {
        $this->commonFunction =  C()->get('commonFunction');
    }

    /**
     * @throws \Exception
     * 首页足球滚球
     */
    public function zqrollingball(){
        $models = $this->commonFunction->getModels('zq',1);

        $model_match = $models['model_match'];

        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'zq');

        //统计联赛下的赛事及查询赛事
        $ret['game_code'] = 'zq';
        $matchData = matchModel::getRollMatchDataAll('',$models,$where,[],$ret);

        //如果赛事为空，直接返回
        if(empty($matchData)) return $matchData;

        //===获取当前赛事是否有赔率，如果没有则去除===
        $matchData_new = $this->commonFunction->Handle_Odds_Null($matchData,$models);

        $data = [
            'game_code'=>'zq',
            'gameName'=>'足球',
            'matchData'=>$matchData_new
        ];

        return $data;

    }
    public function __zqrollingball(){
        $models = $this->commonFunction->getModels('zq',1);

        $model_result = $models['model_result'];
        $model_match = $models['model_match'];
        $model_league = $models['model_league'];
        $result_record = $models['model_result_record'];

        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'zq');

        $result =lm($model_match,"Sports")
            ->join($model_league,$model_league.'.id',$model_match.'.lg_id')
            ->select($model_match.'.id as match_id',$model_match.'.tag','match_date as start_date',$model_match.'.match_time as start_time',$model_match.'.home_team',$model_match.'.guest_team')
            ->where([[$model_league.'.name_chinese','!=','']])
            ->where($where)
            ->get()
            ->toarray();
        //===获取当前赛事是否有赔率，如果没有则去除===
        $result = $this->commonFunction->Handle_Odds_Null($result,$models);
        foreach ($result as $k=>$v){
            $v['home_score'] = 0;
            $v['guest_score'] = 0;
            $v['match_time'] = 0;
            $v['match_process'] = '';

            $result[$k] = $v;
        }

        $data = [
            'game_code'=>'zq',
            'gameName'=>'足球',
            'matchData'=>$result
        ];

        return $data;

    }

    /**
     * @throws \Exception
     * 首页篮球滚球
     */
    public function lqrollingball(){
        $models = $this->commonFunction->getModels('lq',1);

        $model_match = $models['model_match'];

        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'lq');

        //统计联赛下的赛事及查询赛事
        $ret['game_code'] = 'lq';
        $matchData = matchModel::getRollMatchDataAll('',$models,$where,[],$ret);

        //如果赛事为空，直接返回
        if(empty($matchData)) return $matchData;

        //===获取当前赛事是否有赔率，如果没有则去除===
        $matchData_new = $this->commonFunction->Handle_Odds_Null($matchData,$models);

        $data = [
            'game_code'=>'lq',
            'gameName'=>'篮球',
            'matchData'=>$matchData_new
        ];

        return $data;
    }
    public function __lqrollingball(){
        $models = $this->commonFunction->getModels('lq',1);

        $model_result = $models['model_result'];
        $model_match = $models['model_match'];
        $model_league = $models['model_league'];
        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'lq');
        $result =lm($model_match,"Sports")
            ->join($model_league,$model_league.'.id',$model_match.'.lg_id')
            ->select($model_match.'.id as match_id',$model_match.'.tag','match_date as start_date',$model_match.'.match_time as start_time',$model_match.'.home_team',$model_match.'.guest_team')
            ->where($where)
            ->where([[$model_league.'.name_chinese','!=','']])
            ->get()
            ->toarray();
        
         //===获取当前赛事是否有赔率，如果没有则去除===
         $result = $this->commonFunction->Handle_Odds_Null($result,$models);

        foreach ($result as $k=>$v){
            $v['home_score'] = 0;
            $v['guest_score'] = 0;
            $v['match_time'] = 0;
            $v['match_process'] = '';

            $result[$k] = $v;
        }

        $data = [
            'game_code'=>'lq',
            'gameName'=>'篮球',
            'matchData'=>$result
        ];
        return $data;
    }

    public function wqrollingball(){
        $models = $this->commonFunction->getModels('wq',1);

        $model_match = $models['model_match'];

        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'wq');

        //统计联赛下的赛事及查询赛事
        $ret['game_code'] = 'wq';
        $matchData = matchModel::getRollMatchDataAll('',$models,$where,[],$ret);

        //如果赛事为空，直接返回
        if(empty($matchData)) return $matchData;

        //===获取当前赛事是否有赔率，如果没有则去除===
        $matchData_new = $this->commonFunction->Handle_Odds_Null($matchData,$models);

        $data = [
            'game_code'=>'wq',
            'gameName'=>'网球',
            'matchData'=>$matchData_new
        ];

        return $data;
    }
    public function __wqrollingball(){
        $models = $this->commonFunction->getModels('wq',1);

        $model_result = $models['model_result'];
        $model_match = $models['model_match'];
        $model_league = $models['model_league'];
        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'wq');
        $result =lm($model_match,"Sports")
            ->join($model_league,$model_league.'.id',$model_match.'.lg_id')
            ->select($model_match.'.id as match_id',$model_match.'.tag','match_date as start_date',$model_match.'.match_time as start_time',$model_match.'.home_team as home_player_name',$model_match.'.guest_team as guest_player_name')
            ->where($where)
            ->where([[$model_league.'.name_chinese','!=','']])
            ->get()
            ->toarray();

         //===获取当前赛事是否有赔率，如果没有则去除===
         $result = $this->commonFunction->Handle_Odds_Null($result,$models);
        foreach ($result as $k=>$v){
            $v['first_inning_score'] = 0;
            $v['second_inning_score'] = 0;
            $v['third_inning_score'] = 0;
            $v['match_time'] = 0;
            $v['match_process'] = '';

            $result[$k] = $v;
        }

        $data = [
            'game_code'=>'wq',
            'gameName'=>'网球',
            'matchData'=>$result
        ];
        return $data;
    }

    public function bqrollingball(){
        $models = $this->commonFunction->getModels('bq',1);

        $model_match = $models['model_match'];

        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'bq');

        //统计联赛下的赛事及查询赛事
        $ret['game_code'] = 'bq';
        $matchData = matchModel::getRollMatchDataAll('',$models,$where,[],$ret);

        //如果赛事为空，直接返回
        if(empty($matchData)) return $matchData;

        //===获取当前赛事是否有赔率，如果没有则去除===
        $matchData_new = $this->commonFunction->Handle_Odds_Null($matchData,$models);

        $data = [
            'game_code'=>'bq',
            'gameName'=>'棒球',
            'matchData'=>$matchData_new
        ];

        return $data;
    }
    public function __bqrollingball(){
        $models = $this->commonFunction->getModels('bq',1);

        $model_result = $models['model_result'];
        $model_match = $models['model_match'];
        $model_league = $models['model_league'];
        //获取 滚球查询条件
        $where = $this->commonFunction->getState('StRollBall',$model_match,'bq');

        $result =lm($model_match,"Sports")
            ->join($model_league,$model_league.'.id',$model_match.'.lg_id')
            ->select($model_match.'.id as match_id',$model_match.'.tag','match_date as start_date',$model_match.'.match_time as start_time',$model_match.'.home_team',$model_match.'.guest_team')
            ->where($where)
            ->where([[$model_league.'.name_chinese','!=','']])
            ->get()
            ->toarray();

         //===获取当前赛事是否有赔率，如果没有则去除===
         $result = $this->commonFunction->Handle_Odds_Null($result,$models);
        foreach ($result as $k=>$v){
            $v['home_score'] = 0;
            $v['guest_score'] = 0;
            $v['match_time'] = 0;
            $v['match_process'] = '';

            $result[$k] = $v;
        }


        $data = [
            'game_code'=>'bq',
            'gameName'=>'棒球',
            'matchData'=>$result
        ];
        return $data;

    }

    /**
     * 获取所有球类滚球
     */
    public function getRollingBall(){
        $zqRollingBall = $this->zqrollingball();
        $lqRollingBall = $this->lqrollingball();
        $wqRollingBall = $this->wqrollingball();
        $bqRollingBall = $this->bqrollingball();

        $data = [
            'zqData' =>$zqRollingBall,
            'lqData' =>$lqRollingBall,
            'wqData' =>$wqRollingBall,
            'bqData' =>$bqRollingBall,
        ];

        Render($data, '1', lang('Tips','Sports')->get('success'));
    }
}