From ebb87f09df998843ef635054992697f5e281363d Mon Sep 17 00:00:00 2001 From: zhouchangcheng Date: Mon, 27 May 2024 10:21:38 +0800 Subject: [PATCH] modify table --- app/Console/Commands/HandleNotice.php | 3 ++- app/Jobs/notice.php | 24 +++++++++++++------ .../2024_05_24_034644_create_goods_table.php | 4 ++-- ..._05_24_034726_create_goods_items_table.php | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/Console/Commands/HandleNotice.php b/app/Console/Commands/HandleNotice.php index e5452db..4ef0548 100644 --- a/app/Console/Commands/HandleNotice.php +++ b/app/Console/Commands/HandleNotice.php @@ -74,8 +74,9 @@ public function handle(): int foreach ($newIds as $id) { $notices[] = [ 'notice_id' => $id, - 'notice_type'=>$data[$id]['type'], + 'notice_type' => $data[$id]['type'], 'raw_content' => json_encode($data[$id]), + 'err_message' => '', 'created_at' => $time, ]; if (isset($typeIds[$data[$id]['type']])) { diff --git a/app/Jobs/notice.php b/app/Jobs/notice.php index 2ffe9fe..c02a8c3 100644 --- a/app/Jobs/notice.php +++ b/app/Jobs/notice.php @@ -47,12 +47,12 @@ public function handle(): void case 2: case 3: - $this->changeProductState(); - break; + $this->changeProductState(); + break; case 4: case 5: - $this->changePrice(); - break; + $this->changePrice(); + break; } } @@ -67,7 +67,17 @@ public function addGoods(): void $this->client = new Client(); $apiToken = env('API_TOKEN'); $now = date('Y-m-d H:i:s'); - foreach ($this->data as $item) { + $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]; $product = apiRequest::requests($this->client, $this->goodsUrl, [ 'token' => $apiToken, 'itemId' => $item['result']['itemId'], @@ -78,12 +88,12 @@ public function addGoods(): void $noticeIds[] = $item['notice_id']; $product = $product['data']['product']; $product['itemid'] = $product['itemId']; - $good = new Good(); + $good->fill($product); $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); diff --git a/database/migrations/2024_05_24_034644_create_goods_table.php b/database/migrations/2024_05_24_034644_create_goods_table.php index 63cfa14..cf189db 100644 --- a/database/migrations/2024_05_24_034644_create_goods_table.php +++ b/database/migrations/2024_05_24_034644_create_goods_table.php @@ -15,8 +15,8 @@ public function up() { Schema::create('goods', function (Blueprint $table) { $table->id(); - $table->integer('itemid')->comment('商品id'); - $table->integer('typeid')->comment(''); + $table->integer('itemid')->comment('商品id')->unique(); + $table->integer('typeid')->comment('类型'); $table->string('product_name')->comment('商品名'); $table->string('product_code')->index()->comment('商品code'); $table->string('product_group')->comment('商品组'); diff --git a/database/migrations/2024_05_24_034726_create_goods_items_table.php b/database/migrations/2024_05_24_034726_create_goods_items_table.php index f53cb27..19538cc 100644 --- a/database/migrations/2024_05_24_034726_create_goods_items_table.php +++ b/database/migrations/2024_05_24_034726_create_goods_items_table.php @@ -15,7 +15,7 @@ public function up() { Schema::create('goods_items', function (Blueprint $table) { $table->id(); - $table->integer('item_id')->index(); + $table->integer('item_id')->unique(); $table->string('brand')->default('')->comment('品牌'); $table->string('color_name')->default('')->comment('颜色'); $table->text('sell_point')->comment('卖点');