<?php
namespace App\Sports\Controller;
use BaseController\Controller;
/**
 * Class Register
 * @package App\Sports\Controller
 * User: junghwi
 * Date: 2019/3/21
 */
class Sports extends Controller{

    /**
     * 首页体育-球类比赛数据方法
     * @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 = 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';
            $data['all']['count'] = lm($st_competition,'Sports')
                ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
                ->where(getState('StToday',$st_competition))
                ->where([
                    [$st_competition.'.source',$this->source['source']],
                    [$st_competition.'.home_team','<>',null],
                    [$st_competition.'.guest_team','<>',null],
                    [$st_league.'.name_chinese','<>',null],
                    [$st_competition.'.status','<',2],
                    [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
                ])
                ->count('*');
            } else {
                $data['all']['name'] = '所有赛事';
                $data['all']['game_code'] = $game_code;
                $data['all']['code'] = 'all';
                $data['all']['count'] = lm($st_competition,'Sports')
                    ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
                    ->where([[$st_competition.'.status', '<', '2'],
                        [$st_competition.'.source',$this->source['source']]])
                    ->where([
                        [$st_competition.'.source',$this->source['source']],
                        [$st_competition.'.home_team','<>',null],
                        [$st_competition.'.guest_team','<>',null],
                        [$st_league.'.name_chinese','<>',null],
                        [$st_competition.'.status','<',2],
                        [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
                    ])
                    ->where($st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4))
                    ->count('*');
            }
        //查询正在进行的和未开始的球类比赛
        $data['match'] = lm($st_competition,"Sports")
            ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
            ->select($st_competition.'.match_id',$st_competition.'.home_team',$st_competition.'.guest_team',$st_competition.'.match_date',$st_competition.'.match_time')
            ->where([[$st_competition.'.status','<',2],[$st_competition.'.source',$this->source]])
            ->where($st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4))
            ->where($where)
            ->where([
                [$st_competition.'.source',$this->source['source']],
                [$st_competition.'.home_team','<>',null],
                [$st_competition.'.guest_team','<>',null],
                [$st_league.'.name_chinese','<>',null],
                [$st_competition.'.status','<',2],
                [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
            ])
            ->limit($limit_mt)
            ->get();
        if(!empty($data['match'])){
            $data['match'] = $data['match']->toarray();
        }
//        获取正在进行的和未开始联赛
        $data['league'] = lm($st_competition,"Sports")
            ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
            ->select($st_league.'.lg_id',$st_league.'.name_chinese')
            ->where([[$st_competition.'.status','<',2],[$st_competition.'.source',$this->source]])
            ->where($st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4))
            ->where($where)
            ->where([
                [$st_competition.'.source',$this->source['source']],
                [$st_competition.'.home_team','<>',null],
                [$st_competition.'.guest_team','<>',null],
                [$st_league.'.name_chinese','<>',null],
                [$st_competition.'.status','<',2],
                [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
            ])
            ->limit($limit_lg)
            ->distinct($st_league.'lg_id')
            ->get();
        if (!empty($data['league'])) {
            $data['league'] = $data['league']->toarray();
//            $data['league'] = array_column($data['league'],'lg_id');
            foreach($data['league'] as $k => $v){
                //统计当前联赛下的联赛
                $data['league'][$k]['game_code'] = $game_code;
                $data['league'][$k]['code'] = "qt";
                $data['league'][$k]['count'] = lm($st_competition,'Sports')
                    ->where([[$st_competition.'.status','<',2],[$st_competition.'.source',$this->source]])
                    ->where('lg_id',$v['lg_id'])
                    ->where($where)
                    ->where([
                        [$st_competition.'.source',$this->source['source']],
                        [$st_competition.'.home_team','<>',null],
                        [$st_competition.'.guest_team','<>',null],
                        [$st_competition.'.status','<',2],
                        [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
                    ])
                    ->count('*');
            }
        }
        return $data;
    }

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

