<?php
/**
 * Created by PhpStorm.
 * User: Jun.peng
 * Date: 2019/5/14
 * Time: 16:25
 */
namespace App\Sports\Controller;

use BaseController\Controller;
use Biz\Match\GetmatchData;
use Biz\Match\GetOddsData;
use App\Sports\Model\St_area_country as area_countryModel;
use App\Sports\Model\St_zq_odds as ZQoddsModel;

/**
 * Class MatchListWeb
 * @package App\Sports\Controller
 * pc端 赛事接口
 */
class MatchListWeb extends Controller{
    private $getTypeData;
    public function init() {
        $this->getTypeData = new GetmatchData();
        $this->getOddsData = new GetOddsData();
        $this->commonFunction =  C()->get('commonFunction');
    }

    //各球类玩法列表及数量
    public function gameList(){
        $type = $_REQUEST['type'];//必填 状态代码
        $game_code = $_REQUEST['game_code'];//球类代码
        $p_code = $_REQUEST['p_code'];//父级玩法代码
        $time = $_REQUEST['match_date'];//时间条件
        $lg_ids = $_REQUEST['lg_id'];//联赛筛选
        $search = $_REQUEST['search'];//球队搜索

        try {
            if(empty($type) ){
                throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
            } 
            //如果有lg_id,则将其转为数组
            if(!empty($lg_ids)){
                if(strstr($lg_ids,',') != false){
                    $pizza  = $lg_ids;
                    $lg_ids = explode(",", $pizza);
                }else{
                    $lg_ids = [$lg_ids];
                }
            }

            switch ($type){
                case 'StRollBall'://滚球
                    $this->getRollBall($type,$game_code,$p_code,$lg_ids,$search);
                    break;
                case 'StSoon'://即将
                    $this->Other($type,$game_code,$p_code,$time,$lg_ids,$search);
                    break;
                case 'StToday'://今日
                    $this->Other($type,$game_code,$p_code,$time,$lg_ids,$search);
                    break;
                case 'StMorningPlate'://早盘
                    $this->Other($type,$game_code,$p_code,$time,$lg_ids,$search);
                    break;
                case 'StStringScene'://串场
                    $this->Other($type,$game_code,$p_code,$time,$lg_ids,$search);
                    break;
                default:
                    throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
            }
            Render('', '1', lang('Tips','Sports')->get('success'));
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    /*
     * 获取滚球数据
     */
    public function getRollBall($type,$game_code,$p_code='',$lg_ids=[],$search=''){

        //根据球类代码及玩法代码 获取相关赛事赔率数据
        if(!empty($game_code)){
            $matchData = $this->getMatchData($type,$game_code,$p_code,'',$lg_ids,$search);
            //将为空的数组去除
            $matchData = array_filter($matchData) ;
            if(empty($matchData)) Render('', '1', lang('Tips','Sports')->get('success'));

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

        //没有球类代码，则获取每个球类 各状态玩法下赛事数量
        $game = lm('GameType', 'Sports')->select('game_code')->where('status',1)->get()->toArray();
        $gameData = [];
        foreach ($game as $k=>$v){
            if($v['game_code'] !='gj'){
                $models = $this->commonFunction->getModels($v['game_code'],1);
                $where = $this->commonFunction->getState($type,$models['model_match'],$v['game_code']);
                $matchData = lm($models['model_league'],"Sports")
                    ->join($models['model_match'],$models['model_match'].'.lg_id',$models['model_league'].'.id')
                    ->select($models['model_match'].'.id as match_id')
                    ->where($where)
                    ->get()
                    ->toArray();
                //===获取当前赛事是否有赔率，如果没有则去除===
                $matchData_new = $this->commonFunction->Handle_Odds_Null($matchData,$models);
                $matchNum = count($matchData_new);
                //获取满足状态的球类
                if($matchNum != 0){
                    $gameData[$k]['game_code'] = $v['game_code'];
                    $gameData[$k]['game_num'] = $matchNum;
                }
            }
        }
        sort($gameData);
        $num = [];
        foreach ($gameData as $kk=>$vv){
            $num[] =  $vv['game_num'];
        }
        $gameData[]['all_num'] = array_sum($num);
        if(empty($gameData)) Render('', '1', lang('Tips','Sports')->get('success'));

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

    /*
     * 获取非滚球数据
     */
    public function Other($type,$game_code,$p_code,$time='',$lg_ids=[],$search=''){
        //根据球类代码及玩法代码 获取相关赛事赔率数据
        if(!empty($game_code) and !empty($p_code)){

            $matchData = $this->getMatchData($type,$game_code,$p_code,$time,$lg_ids,$search);
            //将为空的数组去除
            $matchData = array_filter($matchData) ;
            if(empty($matchData)) Render('', '1', lang('Tips','Sports')->get('success'));
            Render($matchData, '1', lang('Tips','Sports')->get('success'));
        }

        //根据状态获取所有球类/玩法下赛事数量
        $game = lm('GameType', 'Sports')->select('game_code')->where('status',1)->get()->toArray();
        $oddsCodeNum = [];
        foreach ($game as $k=>$v) {
            if ($v['game_code'] != 'gj') {
                //获取各球类下父级玩法赛事数量
                $oddsCodeNum[$k] = $this->getMatchData($type,$v['game_code'],'','','',$search);
            }
        }

        $matchDataNum = [];
        foreach ($oddsCodeNum as $k => $v){
            foreach ($v as $kk=>$vv){
                $matchDataNum[] = $vv;
            }
        }
         //将为空的数组去除
         $matchDataNum = array_filter($matchDataNum) ;
        if(empty($matchDataNum)) Render('', '1', lang('Tips','Sports')->get('success'));
                //==补充缺少玩法==
        //默认玩法
        $codeData=[
            [
                'game_code'=>'zq',
                'game_num'=>0,
                'p_code'=>'B',
                'code_num'=>0
            ],
            [
                'game_code'=>'zq',
                'game_num'=>0,
                'p_code'=>'CO',
                'code_num'=>0
            ],
            [
                'game_code'=>'zq',
                'game_num'=>0,
                'p_code'=>'C',
                'code_num'=>0
            ],
            [
                'game_code'=>'zq',
                'game_num'=>0,
                'p_code'=>'TS',
                'code_num'=>0
            ],
            [
                'game_code'=>'zq',
                'game_num'=>0,
                'p_code'=>'TG',
                'code_num'=>0
            ],
            // [
            //     'game_code'=>'zq',
            //     'game_num'=>0,
            //     'p_code'=>'kemp',
            //     'code_num'=>0
            // ],
            [
                'game_code'=>'bq',
                'game_num'=>0,
                'p_code'=>'CO',
                'code_num'=>0
            ],
            [
                'game_code'=>'bq',
                'game_num'=>0,
                'p_code'=>'B',
                'code_num'=>0
            ],
            // [
            //     'game_code'=>'bq',
            //     'game_num'=>0,
            //     'p_code'=>'kemp',
            //     'code_num'=>0
            // ],
            [
                'game_code'=>'wq',
                'game_num'=>0,
                'p_code'=>'CO',
                'code_num'=>0
            ],
            // [
            //     'game_code'=>'wq',
            //     'game_num'=>0,
            //     'p_code'=>'kemp',
            //     'code_num'=>0
            // ],
            // [
            //     'game_code'=>'lq',
            //     'game_num'=>0,
            //     'p_code'=>'kemp',
            //     'code_num'=>0
            // ],
            [
                'game_code'=>'lq',
                'game_num'=>0,
                'p_code'=>'CO',
                'code_num'=>0
            ]
        ];

        foreach($codeData as $k=>$v){
            foreach($matchDataNum as $kk=>$vv){
                if($v['game_code'] == $vv['game_code'] and $v['p_code'] == $vv['p_code']){
                    $codeData[$k] = $vv;
                }
                if($vv['game_code'] == 'wq' and $v['game_code'] == 'wq' and $vv['p_code'] == 'C'){
                    $v['game_num'] = $vv['game_num'];
                    $v['code_num'] = $vv['code_num'];

                    $codeData[$k] = $v;
                }
            }
        }

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

    /*
     * 获取赛事数据1
     */
    public function getMatchData($type,$game_code,$p_code='',$time='',$lg_ids=[],$search=''){

        //$p_code 获取具体玩法下的赛事数据
        $models = $this->commonFunction->getModels($game_code,1);
        $where = $this->commonFunction->getState($type,$models['model_match'],$game_code, $p_code);

        $select = [$models['model_match'].'.id as match_id',$models['model_match'].'.lg_id'];
        $timeWhere = [];
        //如果有传玩法代码，则获取查询字段
        if($p_code){
            //时间条件
            if(!empty($time)){
                if($time == 'all'){//全部七天
                    $timeWhere[] = ['match_date','<',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time()))))];
                } 
                else if($time == 'other'){//七天后 所有
                    $timeWhere[] = ['match_date','>',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time()))))];
                }
                else{//根据 传入 日期
                    $timeWhere[] = ['match_date',$time];
                }
            }
            if($p_code == 'kemp'){
                $select = [$models['model_league'].'.id as lg_id',$models['model_league'].'.name_chinese as leagueName','last_time','country_id','area_id'];
            }else{
                $select = [$models['model_league'].'.id as lg_id',$models['model_league'].'.name_chinese as leagueName',$models['model_match'].'.id as match_id','home_team','guest_team','match_date','match_time','tag','country_id','area_id'];
            }
        }
       
        //如果是滚球
        if($type == 'StRollBall'){
            $select = [$models['model_league'].'.id as lg_id',$models['model_league'].'.name_chinese as leagueName',$models['model_match'].'.id as match_id','home_team','guest_team','match_date','match_time','tag','country_id','area_id'];
        }
        $model_match = $models['model_match'];
        //赛事数据
        if(!empty($lg_ids)){
            $matchData = lm($models['model_league'],"Sports")
                ->join($model_match,$model_match.'.lg_id',$models['model_league'].'.id')
                ->select($select)
                ->where($where)//状态条件
                ->whereIn($models['model_league'].'.id',$lg_ids)//联赛id筛选
                ->where($timeWhere)//时间条件
                ->where(function($query)use ($model_match,$search){//追加 球队搜索
                    $query->where($model_match.'.home_team','like','%'.$search.'%')
                        ->orWhere(function($query)use ($model_match,$search) {
                            $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
                        });
                })
                ->get()->toArray();
        }else{
            $matchData = lm($models['model_league'],"Sports")
            ->join($model_match,$model_match.'.lg_id',$models['model_league'].'.id')
            ->select($select)
            ->where($where)//状态条件
            ->where($timeWhere)//时间条件
            ->where(function($query)use ($model_match,$search){//追加 球队搜索
                $query->where($model_match.'.home_team','like','%'.$search.'%')
                    ->orWhere(function($query)use ($model_match,$search) {
                        $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
                    });
            })
            ->get()->toArray();
        }

        //不是冠军，则验证赛事赔率
        if($p_code != 'kemp'){
            //===获取当前赛事是否有赔率，如果没有则去除===
            $matchNum = $this->commonFunction->Handle_Odds_Null($matchData,$models);
        }else{
            //===获取当前联赛是否有冠军赔率，没有则去除===
            $matchNum = $this->commonFunction->Handle_Odds_Null_lg($matchData,$models);
        }
        
        if(empty($matchNum)) return $matchNum;
        //获取赛事id/联赛id
        $match_ids = [];
        $lg_ids = [];
        foreach ($matchNum as $k => $v){
            $match_ids[] = $v['match_id'];
            $lg_ids[] = $v['lg_id'];
        }

        //如果是滚球，为赛事追加比分/进行时间/进程 数据
        if($type == 'StRollBall'){
            $match_ids_str = implode(",", $match_ids);
            $result_record = $models['model_result_record'];

            //如果是网球
            if($game_code == 'wq'){
                $sql = "select a.match_id,a.home_player_score,a.guest_player_score,a.match_time as a_time,a.match_process,a.result_mark from $result_record a,
(select match_id,max(id) id from $result_record where match_id IN ($match_ids_str) group by match_id)b
where a.match_id = b.match_id and a.id = b.id ";
            }else
            //如果是棒球
            if($game_code == 'bq' || $game_code == 'lq'){
                $sql = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.match_process,a.result_mark from $result_record a,
(select match_id,max(id) id from $result_record where match_id IN ($match_ids_str) group by match_id)b
where a.match_id = b.match_id and a.id = b.id ";
            }else{
                //其他球类
                $sql = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.match_process from $result_record a,
(select match_id,max(id) id from $result_record where match_id IN ($match_ids_str) group by match_id)b
where a.match_id = b.match_id and a.id = b.id ";
            }
            //查询 赛事结果记录最新的一条
            $match_result_record = S ('DB')->select ($sql);

            //处理赛事数据，追加比分及进程
            foreach ($matchNum as $k=>$v){
                foreach ($match_result_record as $kk => $vv){
                    if($v['match_id'] == $vv->match_id){
                        //如果是网球结果
                        if($game_code == 'wq'){
                            $matchNum[$k]['home_player_score'] = $vv->home_player_score?:0;
                            $matchNum[$k]['guest_player_score'] = $vv->guest_player_score?:0;
                            $matchNum[$k]['result_mark'] = $vv->result_mark;
                        }else if($game_code == 'bq'){
                            $matchNum[$k]['home_score'] = $vv->home_score?:0;
                            $matchNum[$k]['guest_score'] = $vv->guest_score?:0;
                            $matchNum[$k]['result_mark'] = $vv->result_mark;
                        }
                        else{
                            $matchNum[$k]['home_score'] = $vv->home_score?:0;
                            $matchNum[$k]['guest_score'] = $vv->guest_score?:0;
                        }
                        $matchNum[$k]['a_time'] = $vv->a_time?:0;
                        $matchNum[$k]['match_process'] = $vv->match_process?:'';
                    }
                }
            }
        }

        //赔率数据
        $oddsCodeNum = $this->getOddsData($game_code,$models['model_odds'],$match_ids,$lg_ids,$p_code,$type);

        if(empty($p_code) and $type != 'StRollBall') {
            return $oddsCodeNum;
        }
        
        //处理联赛/赛事/赔率 数据
        $data = $this->get_l_m_o($p_code,$matchNum,$oddsCodeNum,$models['model_result_record'],$game_code,$type);

        return $data;
    }
    
    /*
     * 联赛/赛事/赔率 组装数据
     */
    public function get_l_m_o($p_code='',$matchData=[],$oddsCodeNum=[],$model_result_record='',$game_code='',$type=''){
        //如果是滚球赔率 追加状态字段
        if($type == 'StRollBall' and !empty($oddsCodeNum)){
            $rollOdds = [];
            foreach($oddsCodeNum as $k=>$v){
                $v['oddsType'] = 'StRollBall';
                $rollOdds[] = $v;
            }

            $oddsCodeNum = $rollOdds;
        }
        $match_odds = [];
        //如果是冠军盘口
        if($p_code == 'kemp'){
            //二维数组去重
            $matchData =  $this->commonFunction->array_unset_tt($matchData,'lg_id');

            foreach ($oddsCodeNum as $k=>$v){
                $v['mark'] = 'gj-'.$game_code.'-'.$v['lg_id'].'-'.$v['p_code'].'-'.$v['odds_code'].'-'.$v['sort'];

                foreach ($matchData as $kk => $vv){
                    if($v['lg_id'] == $vv['lg_id']){
                        $match_odds[$kk]['area_id'] = $vv['area_id'];
                        $match_odds[$kk]['country_id'] = $vv['country_id'];
                        $match_odds[$kk]['lg_id'] = $vv['lg_id'];
                        $match_odds[$kk]['leagueName'] = $vv['leagueName'];
                        $match_odds[$kk]['last_time'] = $vv['last_time'];
                        $match_odds[$kk]['oddsData'][] = $v;
                    }
                }
            }
            sort($match_odds);

            $gjData = [];
            foreach ($match_odds as $k=>$v){
                //按p_code分组
                $p_code=array();
                foreach($v['oddsData'] as $kk=>$vv){
                    $p_code[$vv['p_code']][]=$vv;
                }

                $sd = [];
                foreach ($p_code as $k1=>$v1){
                    foreach ($v1 as $k2=>$v2){
                        $sd[$v2['p_code']]['last_time'] =$v['last_time'];
                        $sd[$v2['p_code']][$v2['odds_code']][] = $v2;
                    }
                }
                $gjData[] = $sd;
            }
            //国家数据
            $country=area_countryModel::getName('',2);
            //获取国家联赛列表
            $countryList = [];
            foreach ($country as $k=>$v){
                foreach ($match_odds as $kk=>$vv){
                    if($v['country_id'] == $vv['country_id']){
                        unset($vv['last_time']);
                        //联赛下父级玩法 盘口数量
                        $oddsCode = array_column($vv['oddsData'], 'odds_code');
                        //获取冠军盘口下 父级玩法 数量
                        $oddsCodeN = array_count_values($oddsCode);
                        $vv['matchNum'] = count($oddsCodeN);
                        unset($vv['oddsData']);
                        $v['lg_list'][] = $vv;
                        $countryList[$k] = $v;
                    }
                }
            }
            sort($countryList);

            //地区数据
            $area=area_countryModel::getName('',1);
            //获取地区联赛列表
            $areaList = [];
            foreach ($area as $k=>$v){
                foreach ($match_odds as $kk=>$vv){
                    if($v['id'] == $vv['area_id'] and $vv['country_id'] == 0){
                        unset($vv['last_time']);
                        //联赛下父级玩法 盘口数量
                        $oddsCode = array_column($vv['oddsData'], 'odds_code');
                        //获取冠军盘口下 父级玩法 数量
                        $oddsCodeN = array_count_values($oddsCode);
                        $vv['matchNum'] = count($oddsCodeN);
                        unset($vv['oddsData']);
                        $v['lg_list'][] = $vv;
                        $areaList[$k] = $v;
                    }
                }
            }
            sort($areaList);
            //国家/地区 联赛列表
            $leagueList = array_merge($countryList,$areaList);
            $data = [$gjData,$leagueList];
            return $data;
        }
        //处理非冠军盘口
        foreach ($oddsCodeNum as $k=>$v){
            $v['mark'] = $game_code.'-'.$v['match_id'].'-'.$v['p_code'].'-'.$v['odds_code'].'-'.$v['sort'];

            //没有玩法代码 则为滚球，追加结果数据
            if(empty($p_code)){
                foreach ($matchData as $kk=>$vv){
                    if($v['match_id'] == $vv['match_id']){
                        $match_odds[$kk]['lg_id'] = $vv['lg_id'];
                        $match_odds[$kk]['match_id'] = $vv['match_id'];
                        $match_odds[$kk]['home_team'] = $vv['home_team'];
                        $match_odds[$kk]['guest_team'] = $vv['guest_team'];
                        $match_odds[$kk]['match_date'] = $vv['match_date'];
                        $match_odds[$kk]['match_time'] = $vv['match_time'];
                        $match_odds[$kk]['tag'] = $vv['tag'];
                        $match_odds[$kk]['a_time'] = $vv['a_time'];//?:$this->commonFunction->getMatchTime($vv['match_date'],$vv['match_time']);
                        $match_odds[$kk]['match_process'] = $vv['match_process'];//?:$this->commonFunction->getMatchProcess( $match_odds[$kk]['a_time'],$game_code);
                        //如果是网球结果
                        if($game_code == 'wq'){
                            $match_odds[$kk]['home_player_score'] = $vv['home_player_score']?:0;
                            $match_odds[$kk]['guest_player_score'] = $vv['guest_player_score']?:0;
                            //追加赛事实时结果json
                            $match_odds[$kk]['result_mark'] = $vv['result_mark'];
                            //按第一、二、三盘/全场 分组
                            if(strpos($v['odds_code'],'one') !== false)
                            {
                                $match_odds[$kk]['one_oddsData'][] = $v;
                            }
                            else if(strpos($v['odds_code'],'two') !== false)
                            {
                                $match_odds[$kk]['two_oddsData'][] = $v;
                            }
                            else if(strpos($v['odds_code'],'three') !== false)
                            {
                                $match_odds[$kk]['three_oddsData'][] = $v;
                            }
                            else
                            {
                                $match_odds[$kk]['full_oddsData'][] = $v;
                            }
                        }else if($game_code == 'bq'){
                            $match_odds[$kk]['home_score'] = $vv['home_score']?:0;
                            $match_odds[$kk]['guest_score'] = $vv['guest_score']?:0;
                            //赛事实时结果 json
                            $match_odds[$kk]['result_mark'] = $vv['result_mark'];
                            //按 前五局/全场 分组
                            if(strpos($v['odds_code'],'five') !== false)
                            {
                                $match_odds[$kk]['five_oddsData'][] = $v;
                            }
                            else
                            {
                                $match_odds[$kk]['full_oddsData'][] = $v;
                            }
                        }
                        else
                        {
                            $match_odds[$kk]['home_score'] = $vv['home_score']?:0;
                            $match_odds[$kk]['guest_score'] = $vv['guest_score']?:0;
                            $match_odds[$kk]['oddsData'][] = $v;
                        }
                    }
                }
            }else{
                foreach ($matchData as $kk=>$vv){
                    if($v['match_id'] == $vv['match_id']){
                        $match_odds[$kk]['lg_id'] = $vv['lg_id'];
                        $match_odds[$kk]['match_id'] = $vv['match_id'];
                        $match_odds[$kk]['home_team'] = $vv['home_team'];
                        $match_odds[$kk]['guest_team'] = $vv['guest_team'];
                        $match_odds[$kk]['match_date'] = $vv['match_date'];
                        $match_odds[$kk]['match_time'] = $vv['match_time'];
                        $match_odds[$kk]['tag'] = $vv['tag'];
                        $match_odds[$kk]['oddsData'][] = $v;
                    }
                }
            }
        }

        //排序
        sort($match_odds);
        $data = [];
        foreach ($matchData as $k=>$v){
            foreach ($match_odds as $kk=>$vv){
                if($v['lg_id'] == $vv['lg_id']){
                    $data[$k]['area_id'] = $v['area_id'];
                    $data[$k]['country_id'] = $v['country_id'];
                    $data[$k]['lg_id'] = $v['lg_id'];
                    $data[$k]['leagueName'] = $v['leagueName'];
                    $data[$k]['matchData'][] = $vv;
                }
            }
        }
        //赛事详细数据 去重
        $matchData =  $this->commonFunction->array_unset_tt($data,'lg_id');

        //国家数据
        $country = area_countryModel::getName('',2);
        //获取国家联赛列表
        $countryList = [];
        foreach ($country as $k=>$v){
            foreach ($matchData as $kk=>$vv){
                if($v['id'] == $vv['country_id']){
                    //联赛下赛事数量
                    $vv['matchNum'] = count($vv['matchData']);
                    unset($vv['matchData']);
                    $v['lg_list'][] = $vv;
                    $countryList[$k] = $v;
                }
            }
        }
        sort($countryList);

        //地区数据
        $area = area_countryModel::getName('',1);
        //获取地区联赛列表
        $areaList = [];
        foreach ($area as $k=>$v){
            foreach ($matchData as $kk=>$vv){
                if($v['id'] == $vv['area_id'] and $vv['country_id'] == 0){
                    //联赛下赛事数量
                    $vv['matchNum'] = count($vv['matchData']);
                    unset($vv['matchData']);
                    $v['lg_list'][] = $vv;
                    $areaList[$k] = $v;
                }
            }
        }
        sort($areaList);
        sort($matchData);

        //国家/地区 联赛列表
        $leagueList = array_merge($countryList,$areaList);
        $data = [$matchData,$leagueList];
        return $data;
    }

    /*
     * 根据match_id获取赔率数据
     */
    public function getOddsData($game_code,$model_odds,$match_ids=[],$lg_ids=[],$p_code='',$type='', $matchData = []){

        //获取pc版各球类滚球/让球玩法代码
        $get_DefaultOdds = require "Config/DefaultOdds.php";
        $DefaultOddsRoll = $get_DefaultOdds['PC_StRollBall'];//滚球
        $DefaultOddsConcede = $get_DefaultOdds['PC_concede'];//让球

        $select = ['match_id','p_code'];
        $whereOr = [];
        //如果是串场赛事，则获取串场赔率
        if($type== 'StStringScene'){
            $whereOr = [['sort','=',0],['status','=',0],['is_stringscene','=',1]];
        }else{
            $whereOr = [['sort','=',0],['status','=',0],['is_stringscene','=',0]];
        }
        //有父级玩法，则获取该玩法下详细数据
        if($p_code){ 
            $select = ['lg_id','match_id','others_match_id','id','p_code','odds_code','condition','odds','odds_only','sort','status','team'];
            //获取冠军盘口
            if($p_code == 'kemp'){
                $whereOr = [['sort','=',0],['type','=',1],['status','=',0]];
                $oddsData = ZQoddsModel::getOddsDataPC($model_odds,$select,$lg_ids,$whereOr,$p_code);
                return $oddsData;
            }
            //足球 让球
            else if($p_code == 'CO' and $game_code == 'zq'){
                $p_code = $DefaultOddsConcede['zq'];
                $whereIn = [];
                //$p_code = ['CO','GS'];//让球 进球大小
            }
            //篮球 让球
            else if($p_code == 'CO'  and $game_code == 'lq'){
                $p_code = $DefaultOddsConcede['lq'];
                $whereIn = ['coh','cog','tns','tnb','tbhb','tbhs','tbgb','tbgs'];
                //$p_code = ['CO','TN','TB'];//让球 总分大小 球队得分大小
            }
            //网球 让球
            else if($p_code == 'CO'  and $game_code == 'wq'){
                $p_code = $DefaultOddsConcede['wq'];
                $whereIn = ['ldh','ldg','lbh','lbg','tnb','tns'];
                // $p_code = ['LD','LB','TN'];//让盘 让局 总局数大小
            }
            //棒球 让球
            else if($p_code == 'CO'  and $game_code == 'bq'){
                $p_code = $DefaultOddsConcede['bq'];
                $whereIn = ['coh','cog','tnb','tns','tss','tsd'];
                // $p_code = ['CO','TN','TS'];//让球 总得分大小 总得分单双
            }else{
                $p_code = [$p_code];
            }

            $oddsData = ZQoddsModel::getOddsDataPC($model_odds,$select,$match_ids,$whereOr,$p_code,$game_code,$whereIn);
            return $oddsData;
        }
        //如果是滚球，则获取滚球下详细数据
        if($type == 'StRollBall'){
            $select = ['lg_id','match_id','others_match_id','id','p_code','odds_code','condition','odds','odds_only','sort','status','team'];
            $whereOr = [['sort','=',0],['status','=',0],['is_stringscene','=',0]];
            //获取滚球下，各球类默认玩法代码
            if($game_code == 'zq'){
                $p_code = $DefaultOddsRoll['zq'];
                //$p_code = ['CO','GS'];//让球 进球大小
            }
            if($game_code == 'lq'){
                $p_code = $DefaultOddsRoll['lq'];
                $whereIn = ['coh','cog','tns','tnb','tbhb','tbhs','tbgb','tbgs','ch','cg'];
                //$p_code = ['CO','TN','TB','C'];//让球 得分大小 得分单双 独赢盘
            }
            if($game_code == 'wq'){
                $p_code = $DefaultOddsRoll['wq'];
                //$p_code = ['LD','LB','TN','C'];//让盘 让局 总局数大小 冠军
            }
            if($game_code == 'bq'){
                $p_code = $DefaultOddsRoll['bq'];
                //$p_code = ['CO','TN','TS','C'];//让球 总得分大小 总得分单双 独赢盘
            }
            $oddsData = ZQoddsModel::getOddsDataPC($model_odds,$select,$match_ids,$whereOr,$p_code,$game_code,$whereIn);
            return $oddsData;
        }
        $oddsData = ZQoddsModel::getOddsDataPC($model_odds,$select,$match_ids,$whereOr,'',$game_code);

        //按父级玩法分组
        $oddsCode = [];
        foreach ($oddsData as $kk=>$vv){
           $oddsCode[$vv['p_code']][]= $vv;
        }

         //追加当前球类冠军盘口数量
         $where = [
            ['type','=',1],
        ];
        $select = ['match_id','type'];

        $oddsData_gj_num = lm($model_odds,"Sports")
            // ->select($select)
            ->where($where)
            ->groupby("lg_id","match_id","type")
            ->pluck('lg_id')
            ->toArray();


        // 去重 获取每个玩法下的赛事数量
        $oddsCodeNum = [];
        $game_num = count($match_ids);
        foreach ($oddsCode as $kkk=>$vvv){
            $oddsCodeNum[$kkk]['game_code'] = $game_code;
            $oddsCodeNum[$kkk]['game_num'] = $game_num;
            $oddsCodeNum[$kkk]['p_code'] = $kkk;
            $oddsCodeNum[$kkk]['code_num'] = count($this->commonFunction->array_unset_tt($vvv,'match_id'));
        }
        $oddsCodeNum['kemp']['game_code'] = $game_code;
        $oddsCodeNum['kemp']['game_num'] = $game_num;
        $oddsCodeNum['kemp']['p_code'] = 'kemp';
        $oddsCodeNum['kemp']['code_num'] = count($oddsData_gj_num);

        sort($oddsCodeNum);

        return $oddsCodeNum;
        //==end==
    }

    //获取直播数据
    public function getBroadcast(){
         //球类代码
         $game_code = $_REQUEST['game_code'];
         if(empty($game_code)) throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));

         $where = [];
         if($game_code != 'all'){//拼接查询条件
             $where[] = ['game_type','=',$game_code];
         }
         $data = lm('st_broadcast',"Sports")
             ->select('id','showid','shower','league_name','game_type','host_team','guest_team','start_time','doing')
             ->where($where)
             ->orderBy('start_time','asc')
             ->get()
             ->toArray();

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