<?php
namespace App\Sports\Model;

use \System\Model;

/**
 * Class St_zq_competition
 * @package App\Sports\Model
 * 足球赛事
 */
class St_zq_competition extends Model
{

    protected $table = 'st_zq_competition';
    public function init() {
        // $this->commonFunction =  C()->get('commonFunction');
    }

    /**
     * @param array $where 条件
     * @param string $select 字段
     * @param string $sort 排序字段
     * @param string $orderby 排序方式
     * @return mixed
     * 获取赛事数据
     */
    public static function getMatchData($where=[],$select='id',$sort='id',$orderby='asc'){
        $matchData = self::select($select)->where($where)->orderBy($sort,$orderby)->get();
        return $matchData;
    }

    /*
     * 获取各球类 滚球赛事关联赛事结果数据
     */
    public static function getRollMatchDataAll($source,$models,$where,$lg_ids=[],$ret){
        $model_match = $models['model_match'];
        $model_result = $models['model_result'];
        $game_code = $ret['game_code'];

        $select = [$model_match.'.lg_id',$model_match.'.id as match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team'];

        //获取赛事数据
        if(!empty($lg_ids)){
            $matchData = lm($model_match,"Sports")
            ->select($select)
            ->whereIn($model_match.'.lg_id',$lg_ids)
            ->where($where)
            ->get()
            ->toarray();
        }else{
            $matchData = lm($model_match,"Sports")
            ->select($select)
            ->where($where)
            ->get()
            ->toarray();
        }
        
        if(empty($matchData)) return $matchData;

        $match_ids = [];
        foreach ($matchData as $k=>$v){
            $match_ids[] = $v['match_id'];
        }
        //查询 赛事结果记录最新的一条
        $match_ids_str = implode(",", $match_ids);
        $result_record = $models['model_result_record'];

        //如果是网球
//         if($result_record == 'st_wq_result_record'){
//             $sql = "select a.match_id,a.home_player_score,a.guest_player_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 ";
//         }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 ";
//         }
        //如果是网球
        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);

        //处理赛事数据，追加比分及进程
        /*
        if(!empty($matchData)){
            foreach ($matchData as $k=>$v){
                if( !empty($match_result_record)){
                    foreach ($match_result_record as $kk => $vv){
                        if($v['match_id'] == $vv->match_id){
                            //如果是网球
                            if($model_result == 'st_wq_result'){
                                $matchData[$k]['home_player_score'] = $vv->home_player_score?:0;
                                $matchData[$k]['guest_player_score'] = $vv->guest_player_score?:0;
                            }else{
                                $matchData[$k]['home_score'] = $vv->home_score?:0;
                                $matchData[$k]['guest_score'] = $vv->guest_score?:0;
                            }
                            $matchData[$k]['a_time'] = $vv->a_time?:C()->get('commonFunction')->getMatchTime($v['match_date'],$v['match_time']);
                            $matchData[$k]['match_process'] = $vv->match_process?:C()->get('commonFunction')->getMatchProcess( $matchData[$k]['a_time'],$ret['game_code']);
                        }
                    }
                }
                else{
                     //如果是网球
                     if($model_result == 'st_wq_result'){
                        $matchData[$k]['home_player_score'] = 0;
                        $matchData[$k]['guest_player_score'] = 0;
                    }else{
                        $matchData[$k]['home_score'] = 0;
                        $matchData[$k]['guest_score'] = 0;
                    }
                    $matchData[$k]['a_time'] = C()->get('commonFunction')->getMatchTime($v['match_date'],$v['match_time']);
                    $matchData[$k]['match_process'] = C()->get('commonFunction')->getMatchProcess( $matchData[$k]['a_time'],$ret['game_code']);
                }
            }
        }
        */

        if(!empty($matchData)){
            foreach ($matchData as $k=>$v){
                if( !empty($match_result_record)){
                    foreach ($match_result_record as $kk => $vv){
                        if($v['match_id'] == $vv->match_id){
                            //如果是网球
                            if($model_result == 'st_wq_result'){
                                $matchData[$k]['home_player_score'] = $vv->home_player_score?:0;
                                $matchData[$k]['guest_player_score'] = $vv->guest_player_score?:0;
                            }else{
                                $matchData[$k]['home_score'] = $vv->home_score?:0;
                                $matchData[$k]['guest_score'] = $vv->guest_score?:0;
                            }
                            $matchData[$k]['a_time'] = $vv->a_time;//?:C()->get('commonFunction')->getMatchTime($v['match_date'],$v['match_time']);
                            $matchData[$k]['match_process'] = $vv->match_process;//?:C()->get('commonFunction')->getMatchProcess( $matchData[$k]['a_time'],$ret['game_code']);
                        }
                    }
                }
                else{
                     //如果是网球
                     if($model_result == 'st_wq_result'){
                        $matchData[$k]['home_player_score'] = 0;
                        $matchData[$k]['guest_player_score'] = 0;
                    }else{
                        $matchData[$k]['home_score'] = 0;
                        $matchData[$k]['guest_score'] = 0;
                    }
                    $matchData[$k]['a_time'] = 0;//C()->get('commonFunction')->getMatchTime($v['match_date'],$v['match_time']);
                    $matchData[$k]['match_process'] = '';//C()->get('commonFunction')->getMatchProcess( $matchData[$k]['a_time'],$ret['game_code']);
                }
            }

            //追加当赛事数量为N ，赛事结果记录< N时的处理
            foreach($matchData as $k=>$v){
                if(count($v) < 10){
                    //如果是网球
                    if($model_result == 'st_wq_result'){
                        $matchData[$k]['home_player_score'] = 0;
                        $matchData[$k]['guest_player_score'] = 0;
                    }else{
                        $matchData[$k]['home_score'] = 0;
                        $matchData[$k]['guest_score'] = 0;
                    }
                    $matchData[$k]['a_time'] = 0;// C()->get('commonFunction')->getMatchTime($v['match_date'],$v['match_time']);
                    $matchData[$k]['match_process'] = '';//C()->get('commonFunction')->getMatchProcess( $matchData[$k]['a_time'],$ret['game_code']);
                }
            }
        }

        return $matchData;
    }

    /*
     * 获取各球类 非滚球赛事关联赛事结果数据
     */
    public static function getSoonMatchDataAll($source,$models,$where,$lg_ids=[]){
        $model_match = $models['model_match'];

        $data = lm($model_match,"Sports")
            ->select($model_match.'.lg_id',$model_match.'.id as match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team',$model_match.'.us_time')
            ->where($where)
            ->whereIn($model_match.'.lg_id',$lg_ids)
            ->orderBy('match_time','asc')
            ->get()
            ->toarray();

        return $data;
    }


}