goodsnotices/app/Jobs/notice.php
2024-05-25 22:06:58 +08:00

74 lines
1.8 KiB
PHP

<?php
namespace App\Jobs;
use App\helpers\apiRequest;
use App\helpers\FormType;
use App\Models\Good;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class notice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public Client $client;
/**
* Create a new job instance.
*/
public function __construct(public int $type,public mixed $data)
{
$this->goodsUrl=env('GOODS_URL');
}
public string $goodsUrl ;
/**
* Execute the job.
* @throws GuzzleException
*/
public function handle(): void
{
switch ($this->type) {
case 1:
$this->addGoods();
break;
case 2:
case 3:
case 4:
case 5:
}
}
/**
* @throws GuzzleException
* @throws \Exception
*/
public function addGoods(): void
{
$goods = [];
$this->client = new Client();
foreach ($this->data as $item) {
$product = apiRequest::requests($this->client,$this->goodsUrl,[
'itemId' =>$item['result']['itemId'],
],FormType::json);
if($product['errCode']!='0000'){
throw new \Exception('request item '. $item['item_id'].' err:' .$product['desc'] ?? '');
}
$product = $product['data']['product'];
$product['itemid'] = $product['itemId'];
$good = new Good();
$good->fill($product);
$goods[]=$good->toArray();
}
Good::insert($goods);
}
}