<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;
class Stzqresult extends BaseModel
{
	protected $table = 'st_zq_result';
	public $timestamps = false;
    protected $fillable = ['id','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','penalty_card','corner_ball','source','p_code'];

    function resultlist($list = 10, $page, $where = '',$orwhere = '')
    {
        if (is_array ($where) && count ($where) > 0) {
            $data = $this->join('st_zq_league','st_zq_result.lg_id','=','st_zq_league.lg_id')->join('st_zq_competition','st_zq_competition.match_id','=','st_zq_result.match_id')->select($this->table.'.id',$this->table.'.home_team',$this->table.'.guest_team',$this->table.'.match_id','st_zq_result.guest_team','st_zq_result.home_rate','st_zq_result.guest_rate','st_zq_result.first_score','st_zq_result.status','st_zq_result.last_score','st_zq_result.match_score','st_zq_result.match_winer','st_zq_result.update_time','st_zq_competition.match_time','st_zq_result.match_process',$this->table.'.home_score',$this->table.'.guest_score',$this->table.'.u_guest_score',$this->table.'.start_time',$this->table.'.u_home_score','st_zq_league.name_chinese as saisname')->orderby('st_zq_result.id','desc')->where($where)->orwhere($orwhere)->paginate ($list);
        } else { 
            $data = $this->join('st_zq_league','st_zq_result.lg_id','=','st_zq_league.lg_id')->join('st_zq_competition','st_zq_competition.match_id','=','st_zq_result.match_id')->select($this->table.'.id',$this->table.'.home_team',$this->table.'.guest_team',$this->table.'.match_id','st_zq_result.guest_team','st_zq_result.home_rate','st_zq_result.guest_rate','st_zq_result.first_score','st_zq_result.status','st_zq_result.last_score','st_zq_result.match_score','st_zq_result.match_winer','st_zq_result.update_time','st_zq_competition.match_time','st_zq_result.match_process',$this->table.'.home_score',$this->table.'.guest_score',$this->table.'.u_guest_score',$this->table.'.start_time',$this->table.'.u_home_score','st_zq_league.name_chinese as saisname')->orderby('st_zq_result.id','desc')->paginate ($list);
        }

        return $data->toArray ();

    }

    //地区联赛赛事查询
    function alldiqu(){
        $data = $this->join('st_zq_league','st_zq_result.lg_id','=','st_zq_league.id')->select('st_zq_result.id','st_zq_result.lg_id','st_zq_result.home_team','st_zq_result.guest_team','st_zq_result.home_rate','st_zq_result.guest_rate','st_zq_result.first_score','st_zq_result.status','st_zq_result.last_score','st_zq_result.match_score','st_zq_result.match_winer','st_zq_result.update_time','st_zq_result.match_time','st_zq_result.match_process','st_zq_league.name_chinese as saisname')->orderby('st_zq_result.id','desc')->get();
        return $data->toArray ();   
    }

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

    
    //计算总进球数
    function totalgoal($match_id)
    {
        $data = $this->where('match_id',$match_id)->first();
        $all_goal = $data['home_score']+$data['guest_score'];
        $res = $this->where ('match_id', $match_id)->update (['all_goal'=>$all_goal]);
        if (!$res) {
            return -7010101202; //更新失败
        }
        return 1;//
    }

}
