goodsnotices/app/Jobs/notice.php

74 lines
1.8 KiB
PHP
Raw Normal View History

2024-05-24 07:55:55 +00:00
<?php
namespace App\Jobs;
2024-05-25 14:06:58 +00:00
use App\helpers\apiRequest;
use App\helpers\FormType;
use App\Models\Good;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
2024-05-24 07:55:55 +00:00
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;
2024-05-25 14:06:58 +00:00
public Client $client;
2024-05-24 07:55:55 +00:00
/**
* Create a new job instance.
*/
2024-05-25 14:06:58 +00:00
public function __construct(public int $type,public mixed $data)
2024-05-24 07:55:55 +00:00
{
2024-05-25 14:06:58 +00:00
$this->goodsUrl=env('GOODS_URL');
2024-05-24 07:55:55 +00:00
}
2024-05-25 14:06:58 +00:00
public string $goodsUrl ;
2024-05-24 07:55:55 +00:00
/**
* Execute the job.
2024-05-25 14:06:58 +00:00
* @throws GuzzleException
2024-05-24 07:55:55 +00:00
*/
public function handle(): void
{
2024-05-25 14:06:58 +00:00
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);
2024-05-24 07:55:55 +00:00
}
}
2024-05-25 14:06:58 +00:00