goodsUrl = env('GOODS_URL'); } public string $goodsUrl; /** * Execute the job. * @throws GuzzleException * @throws Throwable */ public function handle(): void { switch ($this->type) { case 1: $this->addGoods(); break; case 2: case 3: $this->changeProductState(); break; case 4: case 5: $this->changePrice(); break; } } /** * @throws GuzzleException * @throws Exception * @throws Throwable */ public function addGoods(): void { $goods = $goodsItems = $noticeIds = []; $this->client = new Client(); $apiToken = env('API_TOKEN'); foreach ($this->data as $item) { $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'] ?? ''); } $noticeIds[] = $item['notice_id']; $product = $product['data']['product']; $product['itemid'] = $product['itemId']; $good = new Good(); $good->fill($product); $goods[] = $good->toArray(); $goodsItem = new GoodsItem(); $product['item_id'] = $product['itemid']; $product['specifications'] = json_encode($product['specifications']); $goodsItem->fill($product); $goodsItems[] = $goodsItem->toArray(); } 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) ->update(['err_message' => $throwable->getMessage()]); throw new $throwable; } } /** * @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; } } /** * @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; } } }