<?php
namespace App\Sports\Controller;

use BaseController\Controller;

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

    }

    /**
     * 首页头部类别列表 弃用
     */
    public function __typeList (){

        //获取类型 代码
        $type = lang('GameTypes','Sports')->getAll();
        $ret = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where('status',1)->get()->toArray();

        $data = [];
        foreach ($type as $key =>$item){
            $matchNum = [];
            $where = $this->commonFunction->getState($item['type_code']);
            foreach ($ret as $key1 => $item1){
                if($item1['game_code'] != 'gj'){
                    //根据球类代码 获取相关model
                    $models = $this->commonFunction->getModels($item1['game_code'],0);
                    $model_match = $models['model_match'];

                    $matchNum[$key1][] = lm($model_match, 'Sports')
//                    ->select('match_id')
                        ->where($this->source)
                        ->where($where)
                        ->count("*");
                }
            }

            $num_s = array_sum(array_column($matchNum, 0));

            //追加统计 各状态赛事数量
            $item['matchNum'] = $num_s;

            $data[$key][]=$item;
        }

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

    /**
     * 重写获取头部状态及其赛事数量
     */
    public function typeList(){
        //获取所有状态
        $type = lang('GameTypes','Sports')->getAll();
        $d = [];

        $ret = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where('status',1)->get()->toArray();

        foreach ($type as $key =>$item){
            $matchNum = [];
            switch ($item['type_code']){
                case 'StRollBall'://滚球
                    foreach ($ret as $key1 => $item1){
                        //根据球类代码 获取相关model
                        $models = $this->commonFunction->getModels($item1['game_code'],1);
                        $model_match = $models['model_match'];
                        $model_result = $models['model_result'];
                        $model_odds = $models['model_odds'];
                        $where = $this->commonFunction->getState($item['type_code'],$model_match);
                        if($item1['game_code'] != 'gj'){
                            $matchNum[$key1][] = lm($model_match, 'Sports')
                                ->join($model_result,$model_result.'.match_id',$model_match . '.match_id')
                                ->where($model_match.'.source',$this->source['source'])
                                ->where($where)
                                ->count("*");
                        }
                    }
                    break;
                case 'StSoon'://即将
                    foreach ($ret as $key1 => $item1){
                        //根据球类代码 获取相关model
                        $models = $this->commonFunction->getModels($item1['game_code'],1);
                        $model_match = $models['model_match'];
                        $model_league = $models['model_league'];

                        $where = $this->commonFunction->getState($item['type_code'],$model_match);
                        if($item1['game_code'] != 'gj'){
                            $matchNum[$key1][] = lm($model_league,"Sports")
                                ->join($model_match,$model_match.'.lg_id',$model_league.'.lg_id')
                                ->where($model_match.'.source',$this->source['source'])
                                ->where($where)
                                ->count("*");
                        }
                    }
                    break;
                case 'StToday'://今日
                    foreach ($ret as $key1 => $item1){
                        //根据球类代码 获取相关model
                        $models = $this->commonFunction->getModels($item1['game_code'],1);
                        $model_match = $models['model_match'];
                        $model_league = $models['model_league'];
                        $where = $this->commonFunction->getState($item['type_code'],$model_match);
                        if($item1['game_code'] != 'gj'){
//                            $matchNum[$key1][] = lm($model_match, 'Sports')
//                                ->where($model_match.'.source',$this->source['source'])
//                                ->where($where)
//                                ->count("*");
                            $matchNum[$key1][] = lm($model_league,"Sports")
                                ->join($model_match,$model_match.'.lg_id',$model_league.'.lg_id')
                                ->where($model_match.'.source',$this->source['source'])
                                ->where($where)
                                ->count("*");
                        }
                    }
                    break;
                case 'StMorningPlate'://早盘
                    foreach ($ret as $key1 => $item1){
                        //根据球类代码 获取相关model
                        $models = $this->commonFunction->getModels($item1['game_code'],1);
                        $model_match = $models['model_match'];
                        $model_league = $models['model_league'];
                        $where = $this->commonFunction->getState($item['type_code'],$model_match);
                        if($item1['game_code'] != 'gj'){
                            $matchNum[$key1][] = lm($model_league,"Sports")
                                ->join($model_match,$model_match.'.lg_id',$model_league.'.lg_id')
                                ->where($model_match.'.source',$this->source['source'])
                                ->where($where)
                                ->count("*");
                        }
                    }
                    break;
                case 'StStringScene'://串场
                    foreach ($ret as $key1 => $item1) {
                        //根据球类代码 获取相关model
                        $models = $this->commonFunction->getModels($item1['game_code'], 1);
                        $model_match = $models['model_match'];
                        $model_league = $models['model_league'];
                        $where = $this->commonFunction->getState($item['type_code'], $model_match);
                        if ($item1['game_code'] != 'gj') {
                            $matchNum[$key1][] = lm($model_league,"Sports")
                                ->join($model_match,$model_match.'.lg_id',$model_league.'.lg_id')
                                ->where($model_match.'.source',$this->source['source'])
                                ->where($where)
                                ->count("*");
                        }
                    }
                    break;
                case 'StChampion'://冠军
                    foreach ($ret as $key1 => $item1) {
                        //根据球类代码 获取相关model
                        $models = $this->commonFunction->getModels($item1['game_code'], 1);
                        $model_odds = $models['model_odds'];
                        $where = $this->commonFunction->getState($item['type_code']);
                        if ($item1['game_code'] != 'gj') {
                            $matchNum[$key1][] =count(lm($model_odds, 'Sports')
                                ->select("match_id","type")
                                ->where($this->source)
                                ->where($where)
                                ->groupby("match_id","type")
                                ->get()
                            );
                        }
                    }
                    break;
                default:
                    throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
            }

            $num_s = array_sum(array_column($matchNum, 0));

            //追加统计 各状态赛事数量
            $item['matchNum'] = $num_s;

            $data[$key][]=$item;

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



    }

    /**
     * @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'));

    }
}