<?php
namespace App\Sports\Controller;
use BaseController\Controller;
/**
 * Class Register
 * @package App\Sports\Controller
 * User: junghwi
 * Date: 2019/3/21
 */
class Sports extends Controller{
    public function init() {
        $this->commonFunction =  C()->get('commonFunction');

    }

    /**
     * 首页体育-球类比赛数据方法
     * @param  [type] $game_code  球类代码
     * @param  [array] $where 球类额外条件
     * @param  [int]  $limit_mt  赛事显示条数 默认3条
     * @param  [int]  $limit_lg  联赛显示条数 默认3条
     * @return [array] $data
     */
    private function Sports_info($game_code,$where,$limit_mt = 3,$limit_lg = 3){
        $data=[];
        $data['name'] = lm('GameType','Sports')->where('game_code',$game_code)->value('game_name');
        $data['game_code'] = $game_code;
        //===查询该球类Models===
        $getModels = $this->commonFunction->getModels($game_code);
        $st_league = $getModels['model_league'];
        $st_competition = $getModels['model_match'];
        //查询足球早盘其他球的所有赛事
        if ($game_code == 'zq'){
            $data['all']['name'] ='今日赛事';
            $data['all']['game_code'] = $game_code;
            $data['all']['code'] = 'today';
            $matchData = lm($st_competition,'Sports')
                ->join($st_league,$st_league.'.id',$st_competition.'.lg_id')
                ->select($st_competition.'.id as match_id')
                ->where(getState('StToday',$st_competition))
                ->where([
                    [$st_competition.'.home_team','<>',null],
                    [$st_competition.'.guest_team','<>',null],
                    [$st_league.'.name_chinese','<>',null],
                    [$st_competition.'.status','<',2],
                    [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
                ])
                ->get()
                ->toArray();
                // ->count('*');
            } else {
                $data['all']['name'] = '所有赛事';
                $data['all']['game_code'] = $game_code;
                $data['all']['code'] = 'all';
                $matchData = lm($st_competition,'Sports')
                    ->join($st_league,$st_league.'.id',$st_competition.'.lg_id')
                    ->select($st_competition.'.id as match_id')
                    // ->where([[$st_competition.'.status', '<', '2']])
                    ->where([
                        [$st_competition.'.home_team','<>',null],
                        [$st_competition.'.guest_team','<>',null],
                        [$st_league.'.name_chinese','<>',null],
                        [$st_competition.'.status','<',2],
                        [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
                    ])
                    // ->where($st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4))
                    ->get()
                    ->toArray();
                    // ->count('*');
            }
            //===获取当前赛事是否有赔率，如果没有则去除===
            $matchData = $this->commonFunction->Handle_Odds_Null($matchData,$getModels);
            $data['all']['count'] = count($matchData);
        //查询正在进行的和未开始的球类比赛
        $data['match'] = lm($st_competition,"Sports")
            ->join($st_league,$st_league.'.id',$st_competition.'.lg_id')
            ->select($st_competition.'.id as match_id',$st_competition.'.home_team',$st_competition.'.guest_team',$st_competition.'.match_date',$st_competition.'.match_time')
            // ->where([[$st_competition.'.status','<',2]])
            // ->where($st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4))
            ->where($where)
            ->where([
                [$st_competition.'.home_team','<>',null],
                [$st_competition.'.guest_team','<>',null],
                [$st_league.'.name_chinese','<>',null],
                [$st_competition.'.status','<',2],
                [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
            ])
            ->limit($limit_mt)
            ->get()
            ->toArray();

        //===获取当前赛事是否有赔率，如果没有则去除===
        $data['match'] = $this->commonFunction->Handle_Odds_Null($data['match'],$getModels);

        if(!empty($data['match'])){
            $data['match'] = $data['match'];
        }
//        获取正在进行的和未开始联赛
/*
        $data['league'] = lm($st_competition,"Sports")
            ->join($st_league,$st_league.'.id',$st_competition.'.lg_id')
            ->select($st_league.'.id as lg_id',$st_league.'.name_chinese',$st_competition.'.id as match_id')
            // ->where([[$st_competition.'.status','<',2]])
            // ->where($st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4))
            ->where($where)
            ->where([
                [$st_competition.'.home_team','<>',null],
                [$st_competition.'.guest_team','<>',null],
                [$st_league.'.name_chinese','<>',null],
                [$st_competition.'.status','<',2],
                [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
            ])
            ->limit($limit_lg)
            ->distinct($st_league.'id')
            ->get()
            ->toArray();
            */

        $data['league'] = lm($st_league,"Sports")
            ->join($st_competition,$st_competition.'.lg_id',$st_league.'.id')
            ->select($st_league.'.id as lg_id',$st_league.'.name_chinese',$st_competition.'.id as match_id')
            ->where($where)
            ->where([
                [$st_competition.'.home_team','<>',null],
                [$st_competition.'.guest_team','<>',null],
                [$st_league.'.name_chinese','<>',null],
                [$st_competition.'.status','<',2],
                [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
            ])
            ->limit($limit_lg)
            ->distinct($st_league.'id')
            ->get()
            ->toArray();
        //去重
        $data['league'] = $this->commonFunction->array_unset_tt($data['league'],'lg_id');

        //===获取当前赛事是否有赔率，如果没有则去除===
        $data['league'] = $this->commonFunction->Handle_Odds_Null($data['league'],$getModels);

        if (!empty($data['league'])) {
            foreach($data['league'] as $k => $v){
                //统计当前联赛下的联赛
                $data['league'][$k]['game_code'] = $game_code;
                $data['league'][$k]['code'] = "qt";
                $matchData = lm($st_competition,'Sports')
                    ->select($st_competition.'.id as match_id')
                    // ->where([[$st_competition.'.status','<',2]])
                    ->where('lg_id',$v['lg_id'])
                    ->where($where)
                    ->where([
                        [$st_competition.'.home_team','<>',null],
                        [$st_competition.'.guest_team','<>',null],
                        [$st_competition.'.status','<',2],
                        [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
                    ])
                    ->get()
                    ->toArray();
                    //===获取当前赛事是否有赔率，如果没有则去除===
                $matchNum = $this->commonFunction->Handle_Odds_Null($matchData,$getModels);
                $data['league'][$k]['count'] = count($matchNum);
            }

        }
        return $data;
    }

    /*
     * 获取首页体育信息数据接口
     */
    public  function  data(){
        $where = [];//球类额外条件
        $zq[] = $this->Sports_info('zq',$where);
        $lq[] = $this->Sports_info('lq',$where);
        $wq[] = $this->Sports_info('wq',$where);
        $bq[] = $this->Sports_info('bq',$where);
        $data = array_merge_recursive($zq,$lq,$bq,$wq);
        Render($data,'1', lang('Tips','Sports')->get('success'));
    }
}

