<?php namespace App\Jobs\Company\BatchDeliver;

use App\Facades\Servers\Logs\Log;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class BatchDeliverJobs implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /** 奖项列表 **/
    protected $list;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 3;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 获取数据
        $list                               = $this->list;
        $orderIds                           = $list['order_ids'];
        $companyId                          = $list['company_id'];
        $data = [
            'express_id'      => $list['express_id'],
            'express_no'      => $list['express_no'],
            'logistics_type'  => $list['logistics_type'],
            'company_id'      => $companyId,
        ];
        try {
            // 循环分派任务
            foreach ($orderIds as $item)           {
                $data['order_id'] = $item;
                // 上锁
                DeliverJobs::dispatch($data);
            }
        } catch (Throwable $th) {
            Log::info('job_error/deliver_jobs', $th->getMessage(), $orderIds);
        }
    }


}
