<?php

namespace App\Jobs;

use App\Services\LogisticYDService;
use App\Services\LogService;
use App\Services\OrderPackageReceivedSyncService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class LogisticYDSync implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * @var $logisticYDService LogisticYDService
     * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
     * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
     * @var $logistic_number string
     */

    protected $logistic_number;

    protected $logisticYDService;
    protected $orderPackageReceivedSyncService;

    /**
     * LogisticYDSync constructor.
     * @param $logistic_number
     */
    public function __construct($logistic_number)
    {
        $this->logistic_number = $logistic_number;
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        ini_set('max_execution_time', 60);
        LogService::log(LogisticYDSync::class, "{$this->logistic_number}-JOB-YD", '');
        $this->logisticYDService = app('LogisticYDService');
        //先订阅订单
        $this->logisticYDService->registerApi([$this->logistic_number]);
        //查询订单路由信息
        $nativeResponse = $this->logisticYDService->query($this->logistic_number);
        //格式化信息
        $formattedData = $this->logisticYDService->format($nativeResponse);
        $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
        //更新数据
        $this->orderPackageReceivedSyncService->update([$formattedData]);
    }
}
