<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/3/29
 * Time: 9:28
 */
namespace App\Models;
use Illuminate\Support\Facades\DB;
class SportsSoccer extends BaseModel {
    protected $table = "st_zq_competition";
    public $timestamps = false;

    function getinfo($list = 10, $page, $where = '',$orwhere = '')
    {
        $zqmatch = $this->where('status','<','2')->where('match_date','<=',date("Y-m-d",time()))->get();
        for($b=0;$b<count($zqmatch);$b++){
            if($zqmatch[$b]->match_date < date("Y-m-d",time()) && strtotime($zqmatch[$b]->match_time.' '.$zqmatch[$b]->match_time)+7200 < time()){
                \App\Models\SportsSoccer::where('id',$zqmatch[$b]->id)->update(['status'=>'2']);
                \App\Models\Stzqresultrecord::where('match_id',$zqmatch[$b]->match_id)->update(['status'=>'2']);
            }
            if($zqmatch[$b]->match_date == date("Y-m-d",time()) && strtotime($zqmatch[$b]->match_date.' '.$zqmatch[$b]->match_time)+7200 < time()){
                \App\Models\SportsSoccer::where('id',$zqmatch[$b]->id)->update(['status'=>'2']);
                \App\Models\Stzqresultrecord::where('match_id',$zqmatch[$b]->match_id)->update(['status'=>'2']);
            }
        }
        
        $where[] = array('st_zq_league.name_chinese','<>','');

//        $source = \App\Models\Setinfo::where('id','1000')->first();
//        if($source->infocontent != '混合数据'){
//            $where[] = array('st_zq_local_match.source', '=', $source->infocontent);
//            $where[] = array('st_zq_local_league.source', '=', $source->infocontent);
//        }
//        if (is_array ($where) && count ($where) > 0) {
//            $data = $this->join('st_zq_league','st_zq_competition.lg_id','=','st_zq_league.id')
//                ->join('st_zq_local_match','st_zq_competition.id','=','st_zq_local_match.match_id')
//                ->join('st_zq_local_league','st_zq_league.id','=','st_zq_local_league.lg_id')
//                ->select('st_zq_competition.id','st_zq_local_match.match_id','st_zq_local_league.lg_id','st_zq_league.name_chinese','st_zq_league.last_time','st_zq_competition.home_team','st_zq_competition.guest_team','st_zq_competition.match_date','st_zq_competition.match_time','st_zq_competition.status','st_zq_competition.recommend')
//                ->orderby('st_zq_competition.match_date','desc')
//                ->orderby('st_zq_competition.match_time','desc')
//                ->where($where)
//                ->orwhere($orwhere)
//                ->paginate ($list);
//        } else {
//            $data = $this->join('st_zq_league','st_zq_competition.lg_id','=','st_zq_league.id')
//                ->join('st_zq_local_match','st_zq_competition.id','=','st_zq_local_match.match_id')
//                ->join('st_zq_local_league','st_zq_league.id','=','st_zq_local_league.lg_id')
//                ->select('st_zq_competition.id','st_zq_local_match.match_id','st_zq_local_league.lg_id','st_zq_league.name_chinese','st_zq_league.last_time','st_zq_competition.home_team','st_zq_competition.guest_team','st_zq_competition.match_date','st_zq_competition.match_time','st_zq_competition.status','st_zq_competition.recommend')
//                ->orderby('st_zq_competition.match_date','desc')
//                ->orderby('st_zq_competition.match_time','desc')
//                ->paginate ($list);
//        }



        if (is_array ($where) && count ($where) > 0) {
            $data = $this->join('st_zq_league','st_zq_competition.lg_id','=','st_zq_league.lg_id')
                ->select('st_zq_competition.id','st_zq_competition.match_id','st_zq_competition.lg_id','st_zq_league.name_chinese','st_zq_league.last_time','st_zq_competition.home_team','st_zq_competition.guest_team','st_zq_competition.match_date','st_zq_competition.match_time','st_zq_competition.status','st_zq_competition.recommend')
                ->orderby('st_zq_competition.match_date','desc')
                ->orderby('st_zq_competition.match_time','desc')
                ->where($where)
                ->orwhere($orwhere)
                ->paginate ($list);
        } else {
            $data = $this->join('st_zq_league','st_zq_competition.lg_id','=','st_zq_league.lg_id')
                ->select('st_zq_competition.id','st_zq_competition.match_id','st_zq_competition.lg_id','st_zq_league.name_chinese','st_zq_league.last_time','st_zq_competition.home_team','st_zq_competition.guest_team','st_zq_competition.match_date','st_zq_competition.match_time','st_zq_competition.status','st_zq_competition.recommend')
                ->orderby('st_zq_competition.match_date','desc')
                ->orderby('st_zq_competition.match_time','desc')
                ->paginate ($list);
        }

        if (!$data < 0) {
            return -2021052003; //
        }

        for($i=0;$i<count($data);$i++){

            if($data[$i]->match_date == '' && $data[$i]->last_time != ''){
                $datetime = explode(' ',$data[$i]->last_time);
                $data[$i]->match_date = $datetime[0];
                $data[$i]->match_time = $datetime[1];
            }
            
            if($data[$i]->status==0){
                $data[$i]->status = '未开始';
            }else if($data[$i]->status==1){
                $data[$i]->status = '正在进行';
            }else if($data[$i]->status==2){
                $data[$i]->status = '已结束';
            }else if($data[$i]->status==3){
                $data[$i]->status = '已结算';
            }else if($data[$i]->status==4){
                $data[$i]->status = '已作废';
            }else if($data[$i]->status==5){
                $data[$i]->status = '待结算';
            }else if($data[$i]->status==6){
                $data[$i]->status = '已取消';
            }

            if($data[$i]->guest_team == ''){
                $data[$i]->home_guest = $data[$i]->home_team;
            }else{
                $data[$i]->home_guest = $data[$i]->home_team.' VS '.$data[$i]->guest_team;
            }

        }
        return $data->toArray();
    }

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

    function getmatchid($id){
        $res=$this->where('id',$id)->first();
        if(!$res){
            return -2021052003;
        }
        return $res->match_id;
    }


}