<?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.id')->join('st_zq_competition','st_zq_competition.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.'.u_home_score',$this->table.'.start_time','st_zq_league.name_chinese as saisname')->orderby('st_zq_result.match_id','desc')->where($where)->orwhere($orwhere)->paginate ($list);
        } else { 
            $data = $this->join('st_zq_league','st_zq_result.lg_id','=','st_zq_league.id')->join('st_zq_competition','st_zq_competition.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.'.u_home_score',$this->table.'.start_time','st_zq_league.name_chinese as saisname')->orderby('st_zq_result.match_id','desc')->paginate ($list);
        }
        for ($i=0; $i < count($data); $i++) { 
            $data[$i]->newstime = date("Y-m-d H:i:s");
        }

        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, $match_id)
    {
        $res = $this->where ('match_id', $match_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;//
    }

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

    /**
     * POST请求https接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $post [请求的参数]
     * @return  string
     */
    function post_curls($url, $post)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $res = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $res; // 返回数据，json格式
 
    }

    //单式结算处理
    function simplexs($token,$match_id,$jsurl,$type){
        $simplex = array(
            'token'=>$token,  
            'order_ids'=>'', //订单id字符串，用半角都好分隔
            'bettype'=>1, //结算类型  1单式  2串式 
            'settype'=>2, //结算次数  1首次  2非首冷饮
            'game_code'=>$type, //赛事类型  zq lq wq bq
            'match_id'=>$match_id, // 赛事ID
            'change_status'=>1   //是否改状态
        );
        $url=$jsurl.'/Settelement';
        $simplex_res = $this->post_curls($url,$simplex);//返回json
        $huawei_res = json_decode($simplex_res,true);

        return $huawei_res;
    }

    //串式结算处理
    function tandems($token,$match_id,$jsurl,$type){
        $tandem = array(
            'token'=>$token,  
            'order_ids'=>'', //订单id字符串，用半角都好分隔
            'bettype'=>2, //结算类型  1单式  2串式 
            'settype'=>2, //结算次数  1首次  2非首冷饮
            'game_code'=>$type, //赛事类型  zq lq wq bq
            'match_id'=>$match_id,  // 赛事ID
            'change_status'=>1  //是否改状态
        );
        $url=$jsurl.'/Settelement';
        $tandem_res = $this->post_curls($url,$tandem);//返回json
        $tandem_res = json_decode($tandem_res,true);

        return $tandem_res;
    }


}