<?php
namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Response\Response;

/**
 * Class Account
 * @package App\Sports\Model
 * 用户账号
 */
class StWqResult extends Model
{
    protected $table = 'st_wq_result';
    public $timestamps = false;

    //写赛事结果
    public static function WQresult($model){
        //获取赛事表15天内所有非 未开始赛事
        $matchData = $model['model_match']::select('id','match_id','home_player_name','guest_player_name','lg_id','status','tag','match_date','match_time')
            ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
            ->get()
            ->toarray();

        //没有数据，无需操作
        if(empty($matchData)) return Response::success();
        //获取赛事结果表 所有当月
        $matchData_r = $model['model_result']::select('match_id')
            ->where([['ctime','>',date('Y-m-d H:i:s', strtotime("-7 day"))]])
            ->get()
            ->toarray();

        //结果表无数据，直接插入
        if(empty($matchData_r)){
            foreach ($matchData as $k=>$v){
                $start_time = ($v['match_date'].' '.$v['match_time']);
                $time = time()-strtotime($v['match_time']);
                $match_time = self::secTime($time);
                $set_match_r[] = [
                    "match_id"=> $v['match_id'],
                    "home_player_name"=>$v['home_player_name'],
                    "guest_player_name"=>$v['guest_player_name'],
                    "lg_id"=>$v['lg_id'],
                    "status"=>$v['status'],
                    "tag"=> $v['tag'],
                    'match_time'=>$match_time,
                    "ctime"=>date('Y-m-d H:i:s'),
                    "update_time"=>date('Y-m-d H:i:s'),
                    "start_time"=>date('Y-m-d H:i:s',strtotime($start_time))
                ];
            }
            $ret = $model['model_result']::insert($set_match_r);
            if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
        }else {

            //如果结果表有数据，则获取结果表没有的赛事
            foreach ($matchData as $k => $v) {
                foreach ($matchData_r as $kk => $vv) {
                    if ($v['match_id'] == $vv['match_id']) {
                        unset($matchData[$k]);
                    }
                }
            }
            //如果还有未写入赛事
            if (!empty($matchData)) {
                //写入结果表不存在赛事
                foreach ($matchData as $k => $v) {
                    $start_time = ($v['match_date'] . ' ' . $v['match_time']);
                    $time = time() - strtotime($v['match_time']);
                    $match_time = self::secTime($time);
                    $set_match_r[] = [
                        "match_id" => $v['match_id'],
                        "home_player_name" => $v['home_player_name'],
                        "guest_player_name" => $v['guest_player_name'],
                        "lg_id" => $v['lg_id'],
                        "status" => $v['status'],
                        "tag" => $v['tag'],
                        'match_time' => $match_time,
                        "ctime" => date('Y-m-d H:i:s'),
                        "update_time" => date('Y-m-d H:i:s'),
                        "start_time" => date('Y-m-d H:i:s', strtotime($start_time))
                    ];
                }
                $ret = $model['model_result']::insert($set_match_r);
                if ($ret != true) throw new \Exception(Response::generate('', Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
            }
        }
        return Response::success();
    }

    //计算滚球 赛事进行时间
    public static function secTime($sec=0){
        $min = floor($sec/60);
        $res = $min.':'.($sec-$min*60);
        return $res;
    }
}