goodsnotices/app/Jobs/notice.php

172 lines
5.4 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;
2024-05-26 14:06:21 +00:00
use App\Models\GoodsItem;
use Exception;
2024-05-25 14:06:58 +00:00
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;
2024-05-26 14:06:21 +00:00
use Illuminate\Support\Facades\DB;
use Throwable;
2024-05-24 07:55:55 +00:00
class notice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2024-05-25 14:06:58 +00:00
public Client $client;
2024-05-26 14:06:21 +00:00
2024-05-24 07:55:55 +00:00
/**
* Create a new job instance.
*/
2024-05-26 14:06:21 +00:00
public function __construct(public int $type, public mixed $data)
2024-05-24 07:55:55 +00:00
{
2024-05-26 14:06:21 +00:00
$this->goodsUrl = env('GOODS_URL');
2024-05-24 07:55:55 +00:00
}
2024-05-26 14:06:21 +00:00
public string $goodsUrl;
2024-05-25 14:06:58 +00:00
2024-05-24 07:55:55 +00:00
/**
* Execute the job.
2024-05-25 14:06:58 +00:00
* @throws GuzzleException
2024-05-26 14:06:21 +00:00
* @throws Throwable
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:
2024-05-26 14:06:21 +00:00
2024-05-25 14:06:58 +00:00
case 3:
2024-05-27 02:21:38 +00:00
$this->changeProductState();
break;
2024-05-25 14:06:58 +00:00
case 4:
case 5:
2024-05-27 02:21:38 +00:00
$this->changePrice();
break;
2024-05-25 14:06:58 +00:00
}
}
/**
* @throws GuzzleException
2024-05-26 14:06:21 +00:00
* @throws Exception
* @throws Throwable
2024-05-25 14:06:58 +00:00
*/
public function addGoods(): void
{
2024-05-26 14:06:21 +00:00
$goods = $goodsItems = $noticeIds = [];
2024-05-25 14:06:58 +00:00
$this->client = new Client();
2024-05-26 14:06:21 +00:00
$apiToken = env('API_TOKEN');
2024-05-26 14:49:03 +00:00
$now = date('Y-m-d H:i:s');
2024-05-27 02:21:38 +00:00
$good = new Good();
$goodsItem = new GoodsItem();
$goodsIds = array_column(array_values($this->data), 'itemId');
$existedIds = Good::query()->whereIn('itemid', $goodsIds)->select('itemid')->get()->toArray();
$newIds = array_diff($goodsIds, $existedIds);
if (!$newIds) {
return;
}
foreach ($newIds as $id) {
$item = $this->data[$id];
2024-05-26 14:06:21 +00:00
$product = apiRequest::requests($this->client, $this->goodsUrl, [
'token' => $apiToken,
'itemId' => $item['result']['itemId'],
], FormType::json);
if ($product['errCode'] != '0000') {
throw new Exception('request item ' . $item['item_id'] . ' err:' . $product['desc'] ?? '');
2024-05-25 14:06:58 +00:00
}
2024-05-26 14:06:21 +00:00
$noticeIds[] = $item['notice_id'];
2024-05-25 14:06:58 +00:00
$product = $product['data']['product'];
$product['itemid'] = $product['itemId'];
2024-05-27 02:21:38 +00:00
2024-05-25 14:06:58 +00:00
$good->fill($product);
2024-05-26 14:49:03 +00:00
$goodsData = $good->toArray();
$goodsData['created_at'] = $now;
$goods[] = $goodsData;
2024-05-27 02:21:38 +00:00
2024-05-26 14:06:21 +00:00
$product['item_id'] = $product['itemid'];
$product['specifications'] = json_encode($product['specifications']);
$goodsItem->fill($product);
2024-05-26 14:49:03 +00:00
$itemData = $goodsItem->toArray();
$itemData['created_at'] = $now;
$goodsItems[] = $itemData;
2024-05-26 14:06:21 +00:00
}
try {
DB::transaction(function () use ($goods, $goodsItems, $noticeIds) {
if (!Good::insert($goods) || !GoodsItem::insert($goodsItems) || !\App\Models\Notice::query()->whereIn('notice_id', $noticeIds)->update(['state' => 3])) {
throw new Exception('insert failed');
2024-05-26 14:49:03 +00:00
}
2024-05-26 14:06:21 +00:00
});
} catch (Throwable $throwable) {
\App\Models\Notice::query()->whereIn('notice_id', $noticeIds)
->update(['err_message' => $throwable->getMessage()]);
2024-05-26 14:28:16 +00:00
throw $throwable;
2024-05-26 14:06:21 +00:00
}
}
/**
* @throws Throwable
*/
public function changeProductState(): void
{
$ids = $idss = [];
foreach ($this->data as $item) {
$idss[] = $item['notice_id'];
$ids[$item['result']['state']][] = $item['result']['itemId'];
}
try {
DB::transaction(function () use ($ids, $idss) {
foreach ($ids as $state => $item) {
Good::query()->whereIn('itemid', $item)->update(['state' => $state]);
}
\App\Models\Notice::query()->whereIn('notice_id', $idss)
->update(['state' => 3]);
});
} catch (Throwable $throwable) {
\App\Models\Notice::query()->whereIn('notice_id', $idss)
->update(['err_message' => $throwable->getMessage()]);
throw $throwable;
2024-05-25 14:06:58 +00:00
}
2024-05-26 14:06:21 +00:00
}
/**
* @throws Throwable
*/
public function changePrice(): void
{
$updateData = array_column($this->data, 'result');
$priceType = [4 => 'sell_price', 5 => 'settle_price'];
$tablePriceType = [4 => 'market_price', 5 => 'settlement'];
$updateData = array_map(function ($item) use ($priceType, $tablePriceType) {
$item[$tablePriceType[$this->type]] = $item[$priceType[$this->type]];
unset($item[$priceType[$this->type]]);
return $item;
}, $updateData);
$ids = array_column($this->data, 'notice_id');
try {
DB::transaction(function () use ($updateData, $ids) {
Good::updateBatch($updateData);
\App\Models\Notice::query()->whereIn('notice_id', $ids)
->update(['state' => 3]);
});
} catch (Throwable $throwable) {
\App\Models\Notice::query()->whereIn('notice_id', $updateData)
->update(['err_message' => $throwable->getMessage()]);
throw $throwable;
}
2024-05-24 07:55:55 +00:00
}
}
2024-05-25 14:06:58 +00:00