<?php

namespace App\Jobs;

use App\Consts\SysConsts;
use App\Modules\User\Models\QappUserAddDestop;
use App\Modules\User\Models\QappUserRententionLog;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

/**
 * 用户加桌
 */
class UserAddDeskJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $uid;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(int $uid)
    {
        $this->uid = $uid;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $exists = QappUserAddDestop::where('uid', $this->uid)->exists();
        if ($exists) {
            return;
        }
        $client = new Client();
        $params = [
            'uid' => $this->uid,
            'source' => 'zsy'
        ];
        $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
        $url = env('TIKTOK_API_URL').'api/qappuser/addDesk';
        myLog('user_add_desk')->info(compact('url'));
        myLog('user_add_desk')->info($params);
        $response =  $client->post($url, ['form_params' => $params])->getBody()->getContents();
        myLog('user_add_desk')->info($response);
        $result =  json_decode($response);
        if ($result) {
            if ($result->code == 0) {
                QappUserAddDestop::create([
                    'uid' => $this->uid,
                    'status' => 1,
                ]);
            }
            myLog('user_add_desk')->info(json_encode(['code' => $result->code,'msg' => $result->msg,'uid' => $this->uid]));
        }
    }
}
