<?php
/**
 * Created by PhpStorm.
 * 获取不同球类默认赔率玩法
 * User: Jun.peng
 * Date: 2019/4/16
 * Time: 9:29
 */
namespace Biz\Match;
class GetOddsData {
    public function __construct() {
        $this->commonFunction =  C()->get('commonFunction');
    }
    /**
     * 根据不同球类获取相关赔率数据
     */
    public function getOddsData($data,$whereDate,$source,$oddsTypeWhere=''){

        if($data['search']){
            $search = $data['search'];
        }

        //根据 球类代码 获取相关model
        $models = $this->commonFunction->getModels($data['game_code']);

        $model_league = $models['model_league'];
        $model_match = $models['model_match'];
        $model_odds = $models['model_odds'];

        $league = lm($model_league, 'Sports')
            ->select('id as lg_id','name_chinese')
            ->where(['id'=>$data['leagueID']])
            ->first();
        if(empty($league->lg_id))  throw new \Exception(Render([], '10003', lang('Tips','Sports')->get('PARAM_ERROR')));

        $matchData = lm($model_match, 'Sports')
            ->select('lg_id','id as match_id','tag','match_date','match_time','home_team','guest_team')
            ->where(['lg_id'=>$data['leagueID']])
            ->where($whereDate)
            ->where($model_match.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4))
            ->where('status','<',2)
            ->where($model_match.'.home_team','<>',null)
            ->where($model_match.'.guest_team','<>',null)
            ->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();

        //===获取当前赛事是否有赔率，如果没有则去除===
        $matchData = $this->commonFunction->Handle_Odds_Null($matchData,$models);
        if(empty($matchData))  throw new \Exception(Render([], '10004', lang('Tips','Sports')->get('PARAM_ERROR')));

        if(empty($matchData)){
            Render([], '10006', lang('Tips','Sports')->get('empty'));
        }

        switch ($data['game_code']){
            case 'zq'://获取足球默认赔率数据
                $list = $this->getOddsZQ($matchData,$model_odds,$source,$oddsTypeWhere);
                break;
            case 'lq'://获取篮球默认赔率数据
                $list = $this->getOddsLQ($matchData,$model_odds,$source,$oddsTypeWhere);
                break;
            case 'wq'://获取网球默认赔率数据
                $list = $this->getOddsWQ($matchData,$model_odds,$source,$oddsTypeWhere);
                break;
            case 'bq'://获取棒球默认赔率数据
                $list = $this->getOddsBQ($matchData,$model_odds,$source,$oddsTypeWhere);
                break;
            default:
                throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
        }

        $data = [
            'lg_id'=>$league->lg_id,
            'leagueName'=>$league->name_chinese,
            'matchNum'=>count($list) ,
            'matchData'=>$list,
        ];

        return $data;
    }

    /**
     * @param $matchData 赛事数据
     * @param $model_odds 赔率model
     * @param $source 数据源
     * @return array
     * @throws \Exception
     * 获取足球默认赔率数据
     */
    public function getOddsZQ($matchData,$model_odds,$source='',$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];

        foreach ($matchData as $k=>$v){
            $match_ids[] = (int)$v['match_id'];
        }
        //获取非滚球/即将状态足球默认玩法
        $get_DefaultOdds = require "Config/DefaultOdds.php";
        $DefaultOdds = $get_DefaultOdds['Other']['zq'];
        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only','source')
            ->whereIn('match_id',$match_ids)
            ->where(['status'=>0])
            ->where($oddsTypeWhere)
            ->where(
                function($query)use ($model_odds,$DefaultOdds){
                    $query->where($model_odds.'.odds_code',$DefaultOdds['zq_concede_home'])
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['zq_concede_guest']);
                        })
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['zq_goal_size_big']);
                        })
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['zq_goal_size_small']);
                        });
                }
            )
            ->get()->toArray();

        foreach ($matchData as $kk=>$vv){
            //获取赛事下赔率并且分组
            $odds = [];
            foreach ($oddsData as $kkk=>$vvv) {
                if ($vv['match_id'] == $vvv['match_id'] and $vvv['sort']==0) {
                    //获取数据源ID
                    $vvv['sourceID'] = $this->commonFunction->getSourceID($vvv['source']);
                    unset($vvv['source']);
                    if($vvv['odds_code'] == $DefaultOdds['zq_concede_guest']){
                        $concede_g = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['zq_concede_home']){
                        $concede_h = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['zq_goal_size_small']){
                        $size_s = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['zq_goal_size_big']){
                        $size_b = $vvv;
                    }
                }
            }
            $concede_size = [$concede_g,$concede_h,$size_s,$size_b];
            foreach ($concede_size as $key => $vaule){
                if($vaule == null) unset($concede_size[$key]);
            }

          
            sort($concede_size);
            $odds = $concede_size;
            $matchData[$kk]['oddsData'] = $odds;
        }

        return $matchData;
    }

    /**
     * @param $matchData 赛事数据
     * @param $model_odds 赔率model
     * @param $source 数据源
     * @return array
     * @throws \Exception
     * 获取篮球默认赔率数据
     */
    public function getOddsLQ($matchData,$model_odds,$source='',$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }
        //获取非滚球/即将状态篮球默认玩法
        $get_DefaultOdds = require "Config/DefaultOdds.php";
        $DefaultOdds = $get_DefaultOdds['Other']['lq'];

        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only','source')
            ->whereIn('match_id',$match_ids)
            ->where(['status'=>0])
            ->where($oddsTypeWhere)
            ->where(
                function($query)use ($model_odds,$DefaultOdds){
                    $query->where($model_odds.'.odds_code',$DefaultOdds['lq_concede_home'])
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['lq_concede_guest']);
                        })
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['lq_total_size_big']);
                        })
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['lq_total_size_small']);
                        });
                }
            )
            ->get()->toArray();

        foreach ($matchData as $kk=>$vv){
            //获取赛事下赔率并且分组
            $odds = [];
            foreach ($oddsData as $kkk=>$vvv) {
                
                if ($vv['match_id'] == $vvv['match_id'] and $vvv['sort']==0) {
                    //获取数据源ID
                    $vvv['sourceID'] = $this->commonFunction->getSourceID($vvv['source']);
                    unset($vvv['source']);
                    if($vvv['odds_code'] == $DefaultOdds['lq_concede_guest']){
                        $concede_g = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['lq_concede_home']){
                        $concede_h = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['lq_total_size_small']){
                        $size_s = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['lq_total_size_big']){
                        $size_b = $vvv;
                    }
                }
            }
            $concede_size = [$concede_g,$concede_h,$size_s,$size_b];
            foreach ($concede_size as $key => $vaule){
                if($vaule == null) unset($concede_size[$key]);
            }

          
            sort($concede_size);
            $odds = $concede_size;
            $matchData[$kk]['oddsData'] = $odds;
        }

        return $matchData;
    }

    /**
     * @param $matchData 赛事数据
     * @param $model_odds 赔率model
     * @param $source 数据源
     * @return array
     * @throws \Exception
     * 获取网球默认赔率数据
     */
    public function getOddsWQ($matchData,$model_odds,$source='',$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }
        //获取非滚球/即将状态网球默认玩法
        $get_DefaultOdds = require "Config/DefaultOdds.php";
        $DefaultOdds = $get_DefaultOdds['Other']['wq'];

        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only','source')
            ->whereIn('match_id',$match_ids)
            ->where(['status'=>0])
            ->where($oddsTypeWhere)
            ->where(
                function($query)use ($model_odds,$DefaultOdds){
                    $query->where($model_odds.'.odds_code',$DefaultOdds['wq_dishes_home'])
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['wq_dishes_guest']);
                        })
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['wq_kemp_home']);
                        })
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['wq_kemp_guest']);
                        });
                }
            )
            ->get()->toArray();

        foreach ($matchData as $kk=>$vv){
            //获取赛事下赔率并且分组
            $odds = [];
            foreach ($oddsData as $kkk=>$vvv) {
                if ($vv['match_id'] == $vvv['match_id'] and $vvv['sort']==0) {
                    //获取数据源ID
                    $vvv['sourceID'] = $this->commonFunction->getSourceID($vvv['source']);
                    unset($vvv['source']);
                    if($vvv['odds_code'] == $DefaultOdds['wq_dishes_guest']){
                        $concede_g = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['wq_dishes_home']){
                        $concede_h = $vvv;
                    }
                    if($vvv['odds_code'] ==$DefaultOdds['wq_kemp_home']){
                        $size_s = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['wq_kemp_guest']){
                        $size_b = $vvv;
                    }
                }
            }
            $concede_size = [$concede_g,$concede_h,$size_s,$size_b];
            foreach ($concede_size as $key => $vaule){
                if($vaule == null) unset($concede_size[$key]);
            }

          
            sort($concede_size);
            $odds = $concede_size;
            $matchData[$kk]['oddsData'] = $odds;
        }
        return $matchData;
    }

    /**
     * @param $matchData 赛事数据
     * @param $model_odds 赔率model
     * @param $source 数据源
     * @return array
     * @throws \Exception
     * 获取棒球默认赔率数据
     */
    public function getOddsBQ($matchData,$model_odds,$source='',$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }

        //获取非滚球/即将状态网球默认玩法
        $get_DefaultOdds = require "Config/DefaultOdds.php";
        $DefaultOdds = $get_DefaultOdds['Other']['bq'];

        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only','source')
            ->whereIn('match_id',$match_ids)
            ->where(['status'=>0])
            ->where($oddsTypeWhere)
            ->where(
                function($query)use ($model_odds,$DefaultOdds){
                    $query->where($model_odds.'.odds_code',$DefaultOdds['bq_capot_home'])
                        ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                            $query->where($model_odds.'.odds_code',$DefaultOdds['bq_capot_guest']);
                        });
                }
            )
            ->get()->toArray();

        foreach ($matchData as $kk=>$vv){
            //获取赛事下赔率并且分组
            $odds = [];
            foreach ($oddsData as $kkk=>$vvv) {
                if ($vv['match_id'] == $vvv['match_id'] and $vvv['sort']==0) {
                    //获取数据源ID
                    $vvv['sourceID'] = $this->commonFunction->getSourceID($vvv['source']);
                    unset($vvv['source']);
                    if($vvv['odds_code'] == $DefaultOdds['bq_capot_home']){
                        $concede_g = $vvv;
                    }
                    if($vvv['odds_code'] == $DefaultOdds['bq_capot_guest']){
                        $concede_h = $vvv;
                    }
                }
            }
            $concede_size = [$concede_g,$concede_h];
            foreach ($concede_size as $key => $vaule){
                if($vaule == null) unset($concede_size[$key]);
            }

            sort($concede_size);
            $odds = $concede_size;
            $matchData[$kk]['oddsData'] = $odds;
        }
        return $matchData;
    }
}