This commit is contained in:
xing 2024-05-26 22:49:03 +08:00
parent 074b630679
commit 535c2bee95
2 changed files with 9 additions and 4 deletions

View File

@ -66,6 +66,7 @@ public function addGoods(): void
$goods = $goodsItems = $noticeIds = [];
$this->client = new Client();
$apiToken = env('API_TOKEN');
$now = date('Y-m-d H:i:s');
foreach ($this->data as $item) {
$product = apiRequest::requests($this->client, $this->goodsUrl, [
'token' => $apiToken,
@ -79,18 +80,22 @@ public function addGoods(): void
$product['itemid'] = $product['itemId'];
$good = new Good();
$good->fill($product);
$goods[] = $good->toArray();
$goodsData = $good->toArray();
$goodsData['created_at'] = $now;
$goods[] = $goodsData;
$goodsItem = new GoodsItem();
$product['item_id'] = $product['itemid'];
$product['specifications'] = json_encode($product['specifications']);
$goodsItem->fill($product);
$goodsItems[] = $goodsItem->toArray();
$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) {
\App\Models\Notice::query()->whereIn('notice_id', $noticeIds)

View File

@ -18,7 +18,7 @@ public function up()
$table->integer('notice_id')->unique()->comment('通知id');
$table->integer('notice_type')->comment('通知type');
$table->text('raw_content')->comment('原始消息内容');
$table->string('err_message')->default('')->comment('处理时错误信息');
$table->text('err_message')->comment('处理时错误信息');
$table->tinyInteger('state')->default(1)->comment('消息状态1待消费2消费进行中,3处理完成');
$table->timestamps();
});