<?php
namespace App\Sports\Model;

use \System\Model;

/**
 * Class Account
 * @package App\Sports\Model
 * 用户账号
 */
class St_zq_odds extends Model
{
    protected $table = 'st_zq_odds';

    /**
     * @param array $where 条件
     * @param string $select 字段
     * @param string $sort 排序字段
     * @param string $orderby 排序方式
     * @return mixed
     * 获取赔率数据
     */
    public static function getOddsData($where=[],$select='id',$sort='id',$orderby='asc'){
        $oddsData = self::select($select)->where($where)->groupby("lg_id","match_id","type")->get();
        return $oddsData;
    }

    //获取赔率数据 用于pc端
    public static function getOddsDataPC($model_odds='',$select=[],$match_ids=[],$whereOr=[],$p_code=[],$game_code='',$whereIn=[]){
        //父级玩法为空，查询所有
        if(empty($p_code)){
            $oddsData = lm($model_odds,"Sports")
                ->select($select)
                ->whereIn('match_id',$match_ids)
                ->where($whereOr)
                ->get()
                ->toArray();

            return $oddsData;
        }
        //冠军
        if($p_code == 'kemp'){
            $oddsData = lm($model_odds,"Sports")
                ->select($select)
                ->whereIn('lg_id',$match_ids)
                ->where($whereOr)
                ->get()
                ->toArray();
            return $oddsData;
        }
        //波胆，只查询全场
        if($p_code[0] == 'B' and $game_code == 'zq'){
            $whereIn = ['b10','b20','b21','b30','b31','b32','b40','b41','b42','b43','b01','b02','b12','b03','b13','b23','b04','b14','b24','b34','b00','b11','b22','b33','b44','bo'];
            $oddsData = lm($model_odds,"Sports")
            ->select($select)
            ->whereIn('match_id',$match_ids)
            ->whereIn('p_code',$p_code)
            ->whereIn('odds_code',$whereIn)
            ->where($whereOr)
            ->get()
            ->toArray();

            // if(!empty($oddsData)){
            //     foreach($oddsData as $k=>$v){
            //         if(strstr($v['odds_code'],'h')){
            //             unset($oddsData[$k]);
            //         }
            //     }
            //     sort($oddsData);
            // }

            return $oddsData;
        }

        //非足球，查询各父玩法的指定子玩法
        if(!empty($whereIn)){
            $oddsData = lm($model_odds,"Sports")
            ->select($select)
            ->whereIn('match_id',$match_ids)
            ->whereIn('p_code',$p_code)
            ->whereIn('odds_code',$whereIn)
            ->where($whereOr)
            ->get()
            ->toArray();

            return $oddsData;
        }

        //其他
        $oddsData = lm($model_odds,"Sports")
        ->select($select)
        ->whereIn('match_id',$match_ids)
        ->whereIn('p_code',$p_code)
        ->where($whereOr)
        ->get()
        ->toArray();

        
        return $oddsData;
    }

    //获取足球赛事下 滚球盘默认赔率
    public static function getMatchOdds($source,$models,$match_ids=[],$DefaultOdds=[]){
        
        //不是串场赔率
        $oddsTypeWhere = [
            [$models['model_odds'].'.is_stringscene','=',0],
        ];
        $model_odds = $models['model_odds'];
        $oddsData= lm($model_odds,"Sports")
            ->select($model_odds.'.match_id',$model_odds.'.id','p_code','odds_code',$model_odds.'.status','odds','condition','odds_only','sort','source')
            ->whereIn($model_odds.'.match_id',$match_ids)
            ->where([$model_odds.'.type'=>0,$model_odds.'.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']);
                    })
                    ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                        $query->where($model_odds.'.odds_code',$DefaultOdds['zq_capot_home']);
                    })
                    ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                        $query->where($model_odds.'.odds_code',$DefaultOdds['zq_capot_dogfall']);
                    })
                    ->orWhere(function($query)use ($model_odds,$DefaultOdds){
                        $query->where($model_odds.'.odds_code',$DefaultOdds['zq_capot_guest']);
                    });
            })
            ->get()
            ->toarray();

        return $oddsData;
    }
}