<?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('lg_id','name_chinese')
            ->where($source)
            ->where(['lg_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','match_id','tag','match_date','match_time','home_team','guest_team')
            ->where($source)
            ->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();
        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 = [
            'leagueID'=>$league->lg_id,
            'leagueName'=>$league->name_chinese,
            'matchNum'=>count($list) ,
            'matchData'=>$list,
//                'lang' => lang('OddsTemp','Sports')->getAll(),
        ];

        return $data;
    }

    /**
     * @param $matchData 赛事数据
     * @param $model_odds 赔率model
     * @param $source 数据源
     * @return array
     * @throws \Exception
     * 获取足球默认赔率数据
     */
    public function __getOddsZQ($matchData,$model_odds,$source,$oddsTypeWhere=''){
        $list = array();
        foreach ($matchData as $key=>$item){
            $list[$key] = $item;
            $oddsData = lm($model_odds, 'Sports')
                ->select('id','p_code','odds_code','status','odds','condition','sort','odds_only')
                ->where($source)
//                ->where($oddsTypeWhere)
                ->where(['match_id'=>$item['match_id'],'type'=>0])
//                    ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))//追加查询有效时间
                ->where(
                    function($query)use ($model_odds){
                        $query->where($model_odds.'.odds_code','concede_home')
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','concede_guest');
                            })
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','size_home');
                            })
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','size_guest');
                            });
                    }
                )
                ->get()->toArray();
            if(!empty($oddsData)){
                //根据 排序 获取 最新让球/大小玩法赔率
                $sortData = array_column($oddsData,'sort');
                array_multisort($sortData,SORT_ASC ,$oddsData);
                $list[$key]['oddsData'] = array_slice($oddsData,0,4);//前四条 放入
            }else{
                $list[$key]['oddsData'] = [];
            }
        }
        return $list;
    }
    public function getOddsZQ($matchData,$model_odds,$source,$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = (int)$v['match_id'];
        }
        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only')
            ->whereIn('match_id',$match_ids)
            ->where($source)
            ->where(
                function($query)use ($model_odds){
                    $query->where($model_odds.'.odds_code','concede_home')
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','concede_guest');
                        })
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','goal_size_big');
                        })
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','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) {
                    $odds[] = $vvv;
                }
            }
            $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=''){
        $list = array();
        foreach ($matchData as $key=>$item){
            $list[$key] = $item;
            $oddsData = lm($model_odds, 'Sports')
                ->select('id','p_code','odds_code','status','odds','condition','sort','odds_only')
                ->where($source)
//                ->where($oddsTypeWhere)
                ->where(['match_id'=>$item['match_id'],'type'=>0])
//                    ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))//追加查询有效时间
                ->where(
                    function($query)use ($model_odds){
                        $query->where($model_odds.'.odds_code','concede_home')
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','concede_guest');
                            })
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','total_sizes_big');
                            })
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','total_sizes_small');
                            });
                    }
                )
                ->get()->toArray();
            if(!empty($oddsData)){
                //根据 排序 获取 最新让球/大小玩法赔率
                $sortData = array_column($oddsData,'sort');
                array_multisort($sortData,SORT_ASC ,$oddsData);
                $list[$key]['oddsData'] = array_slice($oddsData,0,4);//前四条 放入
            }else{
                $list[$key]['oddsData'] = [];
            }
        }
        return $list;
    }
    public function getOddsLQ($matchData,$model_odds,$source,$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }
        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only')
            ->where($source)
            ->whereIn('match_id',$match_ids)
            ->where(
                function($query)use ($model_odds){
                    $query->where($model_odds.'.odds_code','concede_home')
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','concede_guest');
                        })
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','total_size_big');
                        })
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','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) {
                    $odds[] = $vvv;
                }
            }
            $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=''){
        $list = array();
        foreach ($matchData as $key=>$item){
            $list[$key] = $item;
            $oddsData = lm($model_odds, 'Sports')
                ->select('id','p_code','odds_code','status','odds','condition','sort','odds_only')
                ->where($source)
//                ->where($oddsTypeWhere)
                ->where(['match_id'=>$item['match_id'],'type'=>0])
//                    ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))//追加查询有效时间
                ->where(
                    function($query)use ($model_odds){
                        $query->where($model_odds.'.odds_code','dishes_home')
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','dishes_guest');
                            })
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','kemp_home');
                            })
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','kemp_guest');
                            });
                    }
                )
                ->get()->toArray();
            if(!empty($oddsData)){
                //根据 排序 获取 最新让球/大小玩法赔率
                $sortData = array_column($oddsData,'sort');
                array_multisort($sortData,SORT_ASC ,$oddsData);
                $list[$key]['oddsData'] = array_slice($oddsData,0,4);//前四条 放入
            }else{
                $list[$key]['oddsData'] = [];
            }
        }
        return $list;
    }
    public function getOddsWQ($matchData,$model_odds,$source,$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }
        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only')
            ->where($source)
            ->whereIn('match_id',$match_ids)
            ->where(
                function($query)use ($model_odds){
                    $query->where($model_odds.'.odds_code','dishes_home')
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','dishes_guest');
                        })
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','kemp_home');
                        })
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','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) {
                    $odds[] = $vvv;
                }
            }
            $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=''){
        $list = array();
        foreach ($matchData as $key=>$item){
            $list[$key] = $item;
            $oddsData = lm($model_odds, 'Sports')
                ->select('id','p_code','odds_code','status','odds','condition','sort','odds_only')
                ->where($source)
//                ->where($oddsTypeWhere)
                ->where(['match_id'=>$item['match_id'],'type'=>0])
//                    ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))//追加查询有效时间
                ->where(
                    function($query)use ($model_odds){
                        $query->where($model_odds.'.odds_code','capot_home')
                            ->orWhere(function($query)use ($model_odds){
                                $query->where($model_odds.'.odds_code','capot_guest');
                            });
                    }
                )
                ->get()->toArray();
            if(!empty($oddsData)){
                //根据 排序 获取 最新让球/大小玩法赔率
                $sortData = array_column($oddsData,'sort');
                array_multisort($sortData,SORT_ASC ,$oddsData);
                $list[$key]['oddsData'] = array_slice($oddsData,0,2);//前四条 放入
            }else{
                $list[$key]['oddsData'] = [];
            }
        }
        return $list;
    }
    public function getOddsBQ($matchData,$model_odds,$source,$oddsTypeWhere=''){
        //获取赛事id 集合
        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }
        $oddsData = lm($model_odds, 'Sports')
            ->select('match_id','id','p_code','odds_code','status','odds','condition','sort','odds_only')
            ->where($source)
            ->whereIn('match_id',$match_ids)
            ->where(
                function($query)use ($model_odds){
                    $query->where($model_odds.'.odds_code','capot_home')
                        ->orWhere(function($query)use ($model_odds){
                            $query->where($model_odds.'.odds_code','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) {
                    $odds[] = $vvv;
                }
            }
            $matchData[$kk]['oddsData'] = $odds;
        }
        return $matchData;
    }
}