<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;
class Stlqresult extends BaseModel
{
	protected $table = 'st_lq_result';
	public $timestamps = false;
    protected $fillable = ['lg_id','home_team','guest_team','home_rate','guest_rate','home_score','status','first_score','last_score','match_score','guest_score','match_winer','update_time','match_time','match_process','tag','u_home_score','u_guest_score','match_id','all_goal','source'];

    function resultbklist($list = 10, $page, $where = '', $orwhere = '',$if_order)
    {
        $match_ids = \App\Models\MoneyBuyMatch::select('match_id')->where('game_code','lq')->get()->toArray();
        $match_ids = array_column($match_ids,'match_id');
        //去重
        $match_ids = array_unique($match_ids);

        if($if_order == 1){
            if (is_array ($where) && count ($where) > 0) {
                $data = $this->join('st_lq_league','st_lq_result.lg_id','=','st_lq_league.id')->join('st_lq_competition','st_lq_competition.id','=','st_lq_result.match_id')->select($this->table.'.id',$this->table.'.lg_id',$this->table.'.home_team',$this->table.'.guest_team',$this->table.'.home_rate',$this->table.'.guest_rate',$this->table.'.home_score',$this->table.'.guest_score',$this->table.'.all_goal','st_lq_competition.status',$this->table.'.first_score',$this->table.'.last_score',$this->table.'.match_score',$this->table.'.match_winer',$this->table.'.update_time',$this->table.'.match_time',$this->table.'.match_process',$this->table.'.tag',$this->table.'.match_id',$this->table.'.u_home_score',$this->table.'.start_time',$this->table.'.u_guest_score',$this->table.'.manual_result','st_lq_league.name_chinese')->orderby($this->table.'.id','desc')->where($where)->wherein('st_lq_competition.id',$match_ids)->paginate ($list);
            } else {
                $data = $this->join('st_lq_league','st_lq_result.lg_id','=','st_lq_league.id')->join('st_lq_competition','st_lq_competition.id','=','st_lq_result.match_id')->select($this->table.'.id',$this->table.'.lg_id',$this->table.'.home_team',$this->table.'.guest_team',$this->table.'.home_rate',$this->table.'.guest_rate',$this->table.'.home_score',$this->table.'.guest_score',$this->table.'.all_goal','st_lq_competition.status',$this->table.'.first_score',$this->table.'.last_score',$this->table.'.match_score',$this->table.'.match_winer',$this->table.'.update_time',$this->table.'.match_time',$this->table.'.match_process',$this->table.'.tag',$this->table.'.match_id',$this->table.'.u_home_score',$this->table.'.start_time',$this->table.'.u_guest_score',$this->table.'.manual_result','st_lq_league.name_chinese')->orderby($this->table.'.id','desc')->wherein('st_lq_competition.id',$match_ids)->paginate ($list);
            }
        }else{
            if (is_array ($where) && count ($where) > 0) {
                $data = $this->join('st_lq_league','st_lq_result.lg_id','=','st_lq_league.id')->join('st_lq_competition','st_lq_competition.id','=','st_lq_result.match_id')->select($this->table.'.id',$this->table.'.lg_id',$this->table.'.home_team',$this->table.'.guest_team',$this->table.'.home_rate',$this->table.'.guest_rate',$this->table.'.home_score',$this->table.'.guest_score',$this->table.'.all_goal','st_lq_competition.status',$this->table.'.first_score',$this->table.'.last_score',$this->table.'.match_score',$this->table.'.match_winer',$this->table.'.update_time',$this->table.'.match_time',$this->table.'.match_process',$this->table.'.tag',$this->table.'.match_id',$this->table.'.u_home_score',$this->table.'.start_time',$this->table.'.u_guest_score',$this->table.'.manual_result','st_lq_league.name_chinese')->orderby($this->table.'.id','desc')->where($where)->paginate ($list);
            } else {
                $data = $this->join('st_lq_league','st_lq_result.lg_id','=','st_lq_league.id')->join('st_lq_competition','st_lq_competition.id','=','st_lq_result.match_id')->select($this->table.'.id',$this->table.'.lg_id',$this->table.'.home_team',$this->table.'.guest_team',$this->table.'.home_rate',$this->table.'.guest_rate',$this->table.'.home_score',$this->table.'.guest_score',$this->table.'.all_goal','st_lq_competition.status',$this->table.'.first_score',$this->table.'.last_score',$this->table.'.match_score',$this->table.'.match_winer',$this->table.'.update_time',$this->table.'.match_time',$this->table.'.match_process',$this->table.'.tag',$this->table.'.match_id',$this->table.'.u_home_score',$this->table.'.start_time',$this->table.'.u_guest_score',$this->table.'.manual_result','st_lq_league.name_chinese')->orderby($this->table.'.id','desc')->paginate ($list);
            }
        }


        $newsim = new \App\Models\SportsNoteList();
        $simplex = $newsim->getsim('lq');
        $newstr = new \App\Models\MoneyBuyStr();
        $str = $newstr->strmatchs('lq');
        //print_r($str);exit;
        for ($i=0; $i < count($data); $i++) {
            $data[$i]->numsim = 0;
            for ($j=0; $j < count($simplex); $j++) {
                if($simplex[$j]['match_id'] == $data[$i]->match_id){
                    $data[$i]->numsim = $data[$i]->numsim+1;
                }
            }
            $data[$i]->numstr = 0;
            for ($a=0; $a < count($str); $a++) {
                if(in_array($data[$i]->match_id,$str[$a]['match_ids'])){
                    $data[$i]->numstr = $data[$i]->numstr+1;
                }
            }

            $data[$i]->manual_result = json_decode($data[$i]->manual_result,true);

        }

        return $data->toArray ();
	}

    //根据match_id查询比赛1-4节比分
    function allscore($match_id,$count=4){
        for ($i=1; $i < $count; $i++) { 
            $where = array(
                'match_id'=>$match_id,
                'match_process'=>$i,
            );
            $data[] = $this->where($where)->select($this->table.'.home_score',$this->table.'.guest_score')->first();
        }
        return $data;
    }

    //根据match_id查询
    function onlyselect($match_id){
        $data = $this->where('match_id',$match_id)->select('lg_id','home_team','guest_team','home_rate','guest_rate','all_goal','first_score','last_score','match_score','match_winer','match_time','tag','match_id','u_home_score','u_guest_score','source')->first();
        if (!$data) {
            return -5040000102; //无相关信息
        }
        return $data->toArray();
    }

    //更新状态
    function updatastatus($match_id,$data){
        $res = $this->where ('match_id', $match_id)->update ($data);
        if (!$res) {
            return -7010101202; //更新失败
        }
        return 1;
    }

    //查询赛事
    function matchjg($match_id){
        $data = $this->where('match_id',$match_id)->select('id','home_team','guest_team')->first();
        if (!$data) {
            return -5040000102; //无相关信息
        }
        return $data->toArray();
    }

    //更新状态
    function updatestatus($field,$where,$data){
        $res=$this->where($field,$where)->update($data);
        if(!$res) {
            return -7020050022;//更新失败
        }
        return 1;
    }


}

?>