<?php

namespace App\Jobs;

use App\Game\GlobalUserInfo;
use App\Models\AccountsInfo;
use App\Util;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class GameTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $data;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }
    public $tries = 5;
    public function backoff(): array
    {
        return [30, 60, 120];  // wait 30s before 1st retry, 60s before 2nd, etc.
    }
    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 300;  // allow up to 5 minutes
    public function timeoutOn()
    {
        return now()->addMinutes(10);
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if (empty($this->data)) return;

//        return ;
        Util::WriteLog('24680task',$this->data);
        // 接收数据
        [$cmd,$data] = $this->data;
        switch ($cmd){
            case 'UpdateLogin':
                [$UserID,$LastLogonDate,$LastLogonIP,$FPID,$FF]=$data;
                Redis::del('UpdateLoginCheck_'.$UserID);
                $accountInfo = AccountsInfo::where('UserID', $UserID)->first();

                if(!$accountInfo)return;


                $LastFPID=$FPID;
                GlobalUserInfo::where('UserID',$UserID)->update(compact('LastLogonDate','LastLogonIP','FF','LastFPID'));
                AccountsInfo::where('UserID', $UserID)->update(compact('LastLogonDate','LastLogonIP'));
                DB::table('QPRecordDB.dbo.RecordUserLogonStatistics')->insert([
                    'UserID' => $UserID,
                    'LogonIP'=>$LastLogonIP,
                    'LastDate'=>$LastLogonDate,
                    'LogonCount'=>1,
                    'mac'=>$FPID??""

                ]);

                $channel = intval($accountInfo->Channel);
                Util::WriteLog('24680dispatch-xxxx', ['UpdateLogin', [$UserID, $LastLogonDate, $accountInfo->LastLogonDate,$accountInfo->Channel,strtotime($accountInfo->LastLogonDate)]]);
                if(date('Ymd',strtotime($LastLogonDate)) != date('Ymd',strtotime($accountInfo->LastLogonDate))){

                    //DB::connection('write')->select("SET NOCOUNT ON use QPRecordDB exec GSP_GP_AddPlatformData  2,$channel,1");

                    $result = DB::connection('write')->select("DECLARE  @return_value int
        SET NOCOUNT ON use QPRecordDB exec GSP_GP_AddPlatformData 2,$channel,1,0
        SELECT 'ReturnValue' = @return_value
        ");
                }

                //DB::connection('write')->select("SET NOCOUNT ON use QPRecordDB exec GSP_GP_AddUserLogin  $UserID");

                //EXEC QPRecordDB.dbo.GSP_GP_AddPlatformData 2,@Channel,1

                $result = DB::connection('write')->select("DECLARE  @return_value int
        SET NOCOUNT ON use QPRecordDB exec GSP_GP_AddUserLogin $UserID
        SELECT 'ReturnValue' = @return_value
        ");

                break;
        }

    }
}
