240 lines
8.0 KiB
PHP
240 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\helpers\apiRequest;
|
|
use App\Models\Good;
|
|
use App\Models\GoodsItem;
|
|
use Exception;
|
|
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;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class notice implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public Client $client;
|
|
|
|
/**
|
|
* 计算重试任务之前要等待的秒数
|
|
*
|
|
* @return array<int, int>
|
|
*/
|
|
public function backoff(): array
|
|
{
|
|
return [1, 5, 10];
|
|
}
|
|
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(public int $type, public mixed $data)
|
|
{
|
|
$this->goodsUrl = '/Product/getGoodInfo';
|
|
}
|
|
|
|
public string $goodsUrl;
|
|
|
|
/**
|
|
* 处理失败作业
|
|
*/
|
|
public function failed(Throwable $exception): void
|
|
{
|
|
// 向用户发送失败通知等...
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
* @throws GuzzleException
|
|
* @throws Throwable
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
Log::info('===========================开始执行type' . $this->type . ' 的队列============================');
|
|
try {
|
|
switch ($this->type) {
|
|
case 1:
|
|
$this->addGoods();
|
|
break;
|
|
case 2:
|
|
$this->updateGoods();
|
|
break;
|
|
case 3:
|
|
$this->changeProductState();
|
|
break;
|
|
case 4:
|
|
case 5:
|
|
$this->changePrice();
|
|
break;
|
|
}
|
|
Log::info('===========================执行完成================================');
|
|
} catch (Throwable $throwable) {
|
|
$this->fail($throwable);
|
|
Log::info('===========================执行出错================================');
|
|
throw $throwable;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws GuzzleException
|
|
* @throws Exception
|
|
* @throws Throwable
|
|
*/
|
|
public function addGoods(): void
|
|
{
|
|
$goods = $goodsItems = $noticeIds = [];
|
|
$this->client = new Client();
|
|
$now = date('Y-m-d H:i:s');
|
|
$good = new Good();
|
|
$goodsItem = new GoodsItem();
|
|
|
|
$goodsIds = array_map(fn($item) => $item['result']['itemId'], $this->data);
|
|
$existedIds = Good::query()->whereIn('itemid', $goodsIds)->select('itemid')->get()->toArray();
|
|
$newIds = array_diff($goodsIds, $existedIds);
|
|
$newIds = array_unique($newIds);
|
|
if (!$newIds) {
|
|
Log::info('没有新的商品添加');
|
|
return;
|
|
}
|
|
$newIdss = array_flip($newIds);
|
|
foreach ($this->data as $item) {
|
|
if (!isset($newIdss[$item['result']['itemId']])) {
|
|
continue;
|
|
}
|
|
$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'] ?? '');
|
|
}
|
|
$noticeIds[] = $item['notice_id'];
|
|
$product = $product['data']['product'];
|
|
$product['itemid'] = $product['itemId'];
|
|
|
|
$good->fill($product);
|
|
$goodsData = $good->toArray();
|
|
$goodsData['created_at'] = $now;
|
|
$goods[] = $goodsData;
|
|
|
|
$product['item_id'] = $product['itemid'];
|
|
$product['specifications'] = json_encode($product['specifications']);
|
|
$goodsItem->fill($product);
|
|
$itemData = $goodsItem->toArray();
|
|
$itemData['created_at'] = $now;
|
|
$goodsItems[] = $itemData;
|
|
}
|
|
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');
|
|
}
|
|
});
|
|
} catch (Throwable $throwable) {
|
|
Log::error('添加商品失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
|
|
throw $throwable;
|
|
}
|
|
}
|
|
|
|
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'];
|
|
}
|
|
|
|
DB::transaction(function () use ($goods, $itemIds, $noticeIdMapItemId) {
|
|
$items = Good::query()->whereIn('itemid', $itemIds)->with('item')->get();
|
|
$notices = [];
|
|
foreach ($items as $item) {
|
|
$item->fill($goods[$item->itemid]);
|
|
if ($item->isDirty()) {
|
|
$item->save();
|
|
}
|
|
$item->item->fill($goods[$item->itemid]);
|
|
if ($item->item->isDirty()) {
|
|
$item->item->save();
|
|
}
|
|
$notices[] = $noticeIdMapItemId[$item->itemid];
|
|
}
|
|
\App\Models\Notice::query()->whereIn('notice_id', $notices)
|
|
->update(['state' => 3]);
|
|
});
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable
|
|
*/
|
|
public function changeProductState(): void
|
|
{
|
|
$ids = $idss = [];
|
|
foreach ($this->data as $item) {
|
|
$idss[] = $item['notice_id'];
|
|
$ids[$item['result']['state']][] = $item['result']['itemId'];
|
|
}
|
|
$now = date('Y-m-d H:i:s');
|
|
try {
|
|
DB::transaction(function () use ($ids, $idss, $now) {
|
|
foreach ($ids as $state => $item) {
|
|
Good::query()->whereIn('itemid', $item)->update(['state' => $state, 'updated_at' => $now]);
|
|
}
|
|
\App\Models\Notice::query()->whereIn('notice_id', $idss)
|
|
->update(['state' => 3]);
|
|
});
|
|
} catch (Throwable $throwable) {
|
|
Log::error('修改状态失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
|
|
throw $throwable;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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'];
|
|
$now = date('Y-m-d H:i:s');
|
|
$updateData = array_map(function ($item) use ($priceType, $tablePriceType, $now) {
|
|
$item[$tablePriceType[$this->type]] = $item[$priceType[$this->type]];
|
|
unset($item[$priceType[$this->type]]);
|
|
$item['updated_at'] = $now;
|
|
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) {
|
|
Log::error('修改价格失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
|
|
throw $throwable;
|
|
}
|
|
}
|
|
}
|
|
|
|
|