<?php

namespace App\Jobs;

use App\Exceptions\ErrorException;
use App\Services\BatchService;
use App\Services\LogService;
use App\Traits\TestableInstant;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class BatchTaskJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    use TestableInstant;

    /** @var $batchService BatchService */
    protected $batchService;
    protected $batches;
    /**
     * Create a new job instance.
     *
     * @param $batches
     */
    public function __construct($batches)
    {
        $this->batches=$batches;
        LogService::log(__METHOD__,'BatchTaskJob','波次任务注册：'.json_encode($this->batches));
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        LogService::log(__METHOD__,'BatchTaskJob','波次任务执行0：');
        if(!$this->batches)
            throw new ErrorException('波次任务中波次不存在！');
        LogService::log(__METHOD__,'BatchTaskJob','波次任务执行1：'.json_encode($this->batches));
        $this->instant($this->batchService, 'BatchService');
        $this->batchService->assignTasks($this->batches);    //在这里为波次注册任务执行！
    }
}
