goodsnotices/app/Jobs/notice.php

234 lines
7.8 KiB
PHP
Raw Permalink 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\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;
2024-05-27 03:45:15 +00:00
use Illuminate\Support\Facades\Log;
2024-05-26 14:06:21 +00:00
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-27 06:27:46 +00:00
/**
* 计算重试任务之前要等待的秒数
*
* @return array<int, int>
*/
public function backoff(): array
{
return [1, 5, 10];
}
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-27 03:45:15 +00:00
$this->goodsUrl = '/Product/getGoodInfo';
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-27 06:27:46 +00:00
/**
* 处理失败作业
*/
public function failed(Throwable $exception): void
{
// 向用户发送失败通知等...
}
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-27 06:27:46 +00:00
Log::info('===========================开始执行type' . $this->type . ' 的队列============================');
try {
2024-06-03 13:38:34 +00:00
match ($this->type) {
1 => $this->addGoods(),
2 => $this->updateGoods(),
3 => $this->changeProductState(),
4, 5 => $this->changePrice(),
};
2024-05-27 06:27:46 +00:00
Log::info('===========================执行完成================================');
} catch (Throwable $throwable) {
$this->fail($throwable);
Log::info('===========================执行出错================================');
throw $throwable;
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: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();
2024-05-27 03:45:15 +00:00
$goodsIds = array_map(fn($item) => $item['result']['itemId'], $this->data);
2024-05-27 02:21:38 +00:00
$existedIds = Good::query()->whereIn('itemid', $goodsIds)->select('itemid')->get()->toArray();
$newIds = array_diff($goodsIds, $existedIds);
2024-05-27 03:45:15 +00:00
$newIds = array_unique($newIds);
2024-05-27 02:21:38 +00:00
if (!$newIds) {
2024-05-27 03:45:15 +00:00
Log::info('没有新的商品添加');
2024-05-27 02:21:38 +00:00
return;
}
2024-05-27 03:45:15 +00:00
$newIdss = array_flip($newIds);
foreach ($this->data as $item) {
if (!isset($newIdss[$item['result']['itemId']])) {
continue;
}
$product = apiRequest::apis($this->goodsUrl, $this->client, [
2024-05-26 14:06:21 +00:00
'itemId' => $item['result']['itemId'],
2024-05-27 03:45:15 +00:00
]);
2024-05-26 14:06:21 +00:00
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) {
2024-05-27 03:45:15 +00:00
Log::error('添加商品失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
2024-05-26 14:28:16 +00:00
throw $throwable;
2024-05-26 14:06:21 +00:00
}
}
2024-05-27 08:03:31 +00:00
/**
* @throws GuzzleException
* @throws Exception
*/
2024-05-27 06:27:46 +00:00
public function updateGoods(): void
{
$this->client = new Client();
$itemIds = $goods = $noticeIdMapItemId = [];
foreach ($this->data as $item) {
$noticeIdMapItemId[$item['result']['itemId']] = $item['notice_id'];
$product = apiRequest::apis($this->goodsUrl, $this->client, [
'itemId' => $item['result']['itemId'],
]);
if ($product['errCode'] != '0000') {
throw new Exception('request item ' . $item['item_id'] . ' err:' . $product['desc'] ?? '');
}
$product = $product['data']['product'];
$goods[$product['itemId']] = $product;
$itemIds[] = $product['itemId'];
}
2024-05-27 09:52:02 +00:00
$items = Good::query()->whereIn('itemid', $itemIds)->with('item')->get();
DB::transaction(function () use ($goods, $itemIds, $noticeIdMapItemId, $items) {
2024-05-27 06:27:46 +00:00
$notices = [];
foreach ($items as $item) {
2024-05-27 07:43:40 +00:00
$item->fill($goods[$item->itemid]);
2024-05-27 06:27:46 +00:00
if ($item->isDirty()) {
$item->save();
}
2024-05-27 07:43:40 +00:00
$item->item->fill($goods[$item->itemid]);
if ($item->item->isDirty()) {
$item->item->save();
2024-05-27 06:27:46 +00:00
}
2024-05-27 07:43:40 +00:00
$notices[] = $noticeIdMapItemId[$item->itemid];
2024-05-27 06:27:46 +00:00
}
\App\Models\Notice::query()->whereIn('notice_id', $notices)
2024-05-27 07:43:40 +00:00
->update(['state' => 3]);
2024-05-27 06:27:46 +00:00
});
}
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 {
2024-05-27 12:27:49 +00:00
DB::transaction(function () use ($ids, $idss) {
2024-05-26 14:06:21 +00:00
foreach ($ids as $state => $item) {
2024-05-27 12:27:49 +00:00
Good::query()->whereIn('itemid', $item)->update(['state' => $state]);
2024-05-26 14:06:21 +00:00
}
\App\Models\Notice::query()->whereIn('notice_id', $idss)
->update(['state' => 3]);
});
} catch (Throwable $throwable) {
2024-05-27 06:27:46 +00:00
Log::error('修改状态失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
2024-05-26 14:06:21 +00:00
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'];
2024-05-27 06:27:46 +00:00
$now = date('Y-m-d H:i:s');
$updateData = array_map(function ($item) use ($priceType, $tablePriceType, $now) {
2024-05-26 14:06:21 +00:00
$item[$tablePriceType[$this->type]] = $item[$priceType[$this->type]];
unset($item[$priceType[$this->type]]);
2024-05-27 06:27:46 +00:00
$item['updated_at'] = $now;
2024-05-26 14:06:21 +00:00
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) {
2024-05-27 06:27:46 +00:00
Log::error('修改价格失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
2024-05-26 14:06:21 +00:00
throw $throwable;
}
2024-05-24 07:55:55 +00:00
}
}
2024-05-25 14:06:58 +00:00