<?php

namespace App\Jobs;

use App\Consts\SysConsts;
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 NewTikTokUserChargeV2 implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $uid;
    private $amount;
    private $pay_time;
    private $order_no;

    protected $url = 'https://newtrackapi.zhuishuyun.com/api/user/new_charge';

    /**
     * 一般情况都上传
     */
    const COMMON = 'common';
    /**
     * 只有当天注册当天充值用户才上传
     */
    const CURRENT_DAY_REGISTER = 'current_day_register';
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(int $uid, float $amount, string $pay_time, string $order_no)
    {
        $this->uid = $uid;
        $this->amount = $amount;
        $this->pay_time = $pay_time;
        $this->order_no = $order_no;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $client = new Client();
        $params = [
            'uid' => $this->uid,
            'amount' => $this->amount,
            'pay_time' => $this->pay_time,
            'order_no' => $this->order_no,
            'source' => 'wdy'
        ];
        $params['sign'] = _sign($params,  SysConsts::TIKTOK_KEY);
        myLog('new_user_charge')->info("uid:{$this->uid}".json_encode($params));
        $response =  $client->request('post', $this->url, ['form_params' => $params])->getBody()->getContents();
        myLog('new_user_charge')->info("uid:{$this->uid}{$response}");
        $result =  json_decode($response);
        if ($result) {
            if ($result->code != 0) {
                myLog('new_user_charge')->info($response);
            }
        }
    }
}
