<?php

namespace App\Jobs\QappTikTok;

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 QappTikTokUserCharge implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $reportParams;

    protected $url;

    /**
     * 一般情况都上传
     */
    const COMMON = 'common';
    /**
     * 只有当天注册当天充值用户才上传
     */
    const CURRENT_DAY_REGISTER = 'current_day_register';
    /**
     * 注册24小时之内充值
     */
    const REGISTER_24_CHARGE = 'register_24_charge';

    /**
     * QappTikTokUserCharge constructor.
     * @param QappTikTokUserChargeRequest $reportParams
     */
    public function __construct(QappTikTokUserChargeRequest $reportParams)
    {
        $this->reportParams = $reportParams;
        $this->url = env('TIKTOK_API_URL'). 'api/qappuser/charge';
    }

    /**
     * Execute the job.
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle()
    {
        $client         = new Client();
        $params         = [
            'uid'           => (int)$this->reportParams->uid,
            'amount'        => (float)$this->reportParams->amount,
            'pay_time'      => (string)$this->reportParams->pay_time,
            'type'          => (string)$this->reportParams->type,
            'book_id'       => (string)$this->reportParams->book_id,
            'book_name'     => (string)$this->reportParams->book_name,
            'molecule'      => (int)$this->reportParams->molecule,
            'denominator'   => (int)$this->reportParams->denominator,
            'source'        => 'zsy',
            'send_order_id' => $this->reportParams->send_order_id,
        ];
        $params['sign'] = _sign($params, SysConsts::TIKTOK_KEY);
        $response       = $client->request('post', $this->url, ['form_params' => $params])->getBody()->getContents();
        myLog('qapp_user_charge')->info($response);
        $result = json_decode($response);
        if ($result) {
            if ($result->code != 0) {
                myLog('qapp_user_charge')->info($response);
            }
        }
    }
}
