businessGoodSku = $businessGoodSku->toArray(); $this->num = $num; $updateResult = false; //暂时设定重试3次 for ($i = 0; $i < 3; $i++) { if ($this->updateStock()) { $updateResult = true; break; } usleep(140); } if (!$updateResult) { Log::error("sku 业务更新失败", (array)$this->businessGoodSku); } } catch (\Exception $exception) { Log::error("sku 业务更新发生异常", ["error" => $exception->getMessage()]); } } private function updateStock() { $this->goodsSku = GoodsSku::query() ->where('external_sku_id', $this->businessGoodSku['external_sku_id']) ->first(); if (is_null($this->goodsSku)) { return true; } $oldStock = $this->goodsSku->stock; $stock = $this->goodsSku->stock + $this->num; $saleStock = $this->goodsSku->sale_stock + $this->num; $saleStock = ($saleStock > 0) ? $saleStock : 0; $updateParam = ["stock" => $stock, "sale_stock" => $saleStock]; if (0 >= $saleStock) { $updateParam['status'] = GoodsSku::$STATUS_DOWN; } else { $updateParam['status'] = GoodsSku::$STATUS_ON_SALE; } Log::info("sku 业务订单库存更新", $updateParam); //乐观锁更新 return GoodsSku::query()->where('external_sku_id', $this->businessGoodSku['external_sku_id'])-> where("stock", "=", $oldStock)->update($updateParam); } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('channel-name'); } }