<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-13 18:45:24------
 */

namespace App\Models;

use DB;
use App\Models\MoneyBuyMatch as MoneyBuyMatchModel;
use App\Lib\Biz\Sport\Common;


class Comendnotice extends BaseModel
{

    protected $table = 'comendnotice';
    public $timestamps = false;
    protected $fillable = ['status', 'pcount', 'logs', 'game_code', 'match_id', 'result', 'done_time', 'start_time', 'ctime'];

    //添加赛事结束纪录
    function addcomendnotice($match_id, $type, $pcount = 1, $game_start_time = 0)
    {
        $data = $this->where(['match_id' => $match_id, 'game_code' => $type])->first();
        if (!$data) {
            $insertArr = ['game_code' => $type, 'status' => 0, 'pcount' => $pcount, 'match_id' => $match_id, 'ctime' => date('Y-m-d H:i:s')];
            if ($game_start_time) {
                $insertArr['game_start_time'] = $game_start_time;
            }
            $res = $this->insert($insertArr);
            if (!$res) {
                return -6030001222;
            }
        }
        return 1;
    }

    //
    public function getNeedSettelent()
    {
        $lastDay = date("Y-m-d H:i:s", time() - 86400 * 30);
        $now = date("Y-m-d H:i:s", time() - 5 * 3600);

        $now_2 = date('Y-m-d H:i:s', time() - 60);
        // $model = $this->where([['game_start_time', '<=', $now], ['game_start_time', '>=', $lastDay], ['pcount', '=', 0]])->orderBy('game_start_time', 'desc')->first();
        $model = $this->where([['ctime', '<=', $now_2], ['ctime', '>=', $lastDay], ['pcount', '=', 0]])->orderBy('ctime', 'desc')->first();
        return $model;
    }

    //找到比赛已经结束，还没结算的记录，插入到消息通知表里[多个球类]
    public function InsertEndGame($day = 30)
    {
        $types = ['zq', 'lq', 'wq', 'bq'];
        foreach ($types as $gtype) {
            $rows = $this->findByType($gtype, $day);
            if (!empty($rows) && count($rows) > 0) {
                Common::SET_NOMAM_RECORD('自动结算之添加通知消息!', ['count' => count($rows)]);
                foreach ($rows as $matchidObj) {
                    $matchid = $matchidObj->match_id;
                    $start_time = $matchidObj->start_time;
                    $this->addcomendnotice($matchid, $gtype, 0, $start_time);
                }
            }
        }
        return true;
    }


    public function findByType($type, $days = 30)
    {
        $days = intval($days);
        $time = date("Y-m-d 00:00:00", (time() - 86400 * $days));
        $time2 = date("Y-m-d 00:00:00", (time() - 86400 * ($days + 5)));
        $table = "st_" . $type . "_result";
        $sql = <<<EOS
    select tabb.match_id,tabb.start_time  from 
( select  match_id,start_time  from  $table where start_time>='$time'  and status=2 ) as tabb
where match_id not in (select match_id from  comendnotice  where game_code='$type' and game_start_time>='$time2')
order by match_id asc 
EOS;
        $result = DB::select($sql);
        return $result;
    }


}