diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index ad53e63..b3aa83c 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -25,7 +25,7 @@ class ErrorCode extends AbstractConstants /** * 线上下单失败 - * @Message("线上下单失败") + * @Message("下单失败") */ const ORDER_ONLINE_FAIL = 601; @@ -234,7 +234,7 @@ class ErrorCode extends AbstractConstants /************************************/ /** * 已卖光 - * @Message("该商品已卖光") + * @Message("有商品已卖光") */ const GOODS_INVENTORY_ERROR = 1101; @@ -262,7 +262,7 @@ class ErrorCode extends AbstractConstants /** * 当前特价商品已被购买过 - * @Message("该活动商品您已买过") + * @Message("有活动商品您已买过") */ const GOODS_ACTIVITY_BUY = 1151; @@ -274,7 +274,7 @@ class ErrorCode extends AbstractConstants /** * 已卖光 - * @Message("该商品已卖光") + * @Message("有商品已卖光") */ const GOODS_ACTIVITY_INVENTORY_ERROR = 1153; diff --git a/app/Service/v3/Implementations/OrderOnlineService.php b/app/Service/v3/Implementations/OrderOnlineService.php index 88963d5..862cec6 100644 --- a/app/Service/v3/Implementations/OrderOnlineService.php +++ b/app/Service/v3/Implementations/OrderOnlineService.php @@ -117,6 +117,7 @@ class OrderOnlineService implements OrderOnlineServiceInterface public function do($marketId, $userId, $userAddrId, $storeList, $totalMoney, $deliveryTimeNote='尽快送达', $serviceMoney=0, $receiveCouponIds=null, $plat=''){ $redis = ApplicationContext::getContainer()->get(Redis::class); + $isCacheInventory = false; $carts = []; Db::beginTransaction(); try { @@ -189,7 +190,7 @@ class OrderOnlineService implements OrderOnlineServiceInterface } $check = $this->goodsService->check($goods, $cart->num); if (true !== $check) { - throw new ErrorCodeException($check, '['.$goods->name.'/'.$goods->goods_unit.']'); + throw new ErrorCodeException($check, '['.$goods->name.']'); } } elseif ($cart->activity_type == 2) { $goods = GoodsActivity::query()->lockForUpdate()->with('store')->find($cart->goods_id); @@ -198,7 +199,7 @@ class OrderOnlineService implements OrderOnlineServiceInterface } $check = $this->goodsActivityService->check($goods, $cart->num, $userId); if (true !== $check) { - throw new ErrorCodeException($check, '['.$goods->name.'/'.$goods->goods_unit.']'); + throw new ErrorCodeException($check, '['.$goods->name.']'); } } @@ -211,6 +212,7 @@ class OrderOnlineService implements OrderOnlineServiceInterface } else { $redis->incrBy($inventoryKey, intval($cart->num)); } + $isCacheInventory = true; // 算金额 $goodsAmount = bcmul((string)$goods->price, (string)$cart->num); # 当前商品的金额 @@ -359,19 +361,27 @@ class OrderOnlineService implements OrderOnlineServiceInterface return $this->paymentService->do($globalOrderId, $totalAmount, $userId, config('wechat.notify_url.online')); } catch (Exception $e) { Db::rollBack(); - // 释redis库存 - foreach ($carts as $k => &$cart) { - // 拼接activity_type和goods_id - $inventoryKey = 'goods_inventory_sold_'.$cart->activity_type.'_'.$cart->goods_id; - // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 - if (!$redis->exists($inventoryKey)) { - $redis->set($inventoryKey, 0); - } else { - $redis->decrBy($inventoryKey, intval($cart->num)); + + if ($isCacheInventory) { + // 释redis库存 + foreach ($carts as $k => &$cart) { + // 拼接activity_type和goods_id + $inventoryKey = 'goods_inventory_sold_'.$cart->activity_type.'_'.$cart->goods_id; + // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 + if (!$redis->exists($inventoryKey)) { + $redis->set($inventoryKey, 0); + } else { + $redis->decrBy($inventoryKey, intval($cart->num)); + } } } $this->log->event(LogLabel::ORDER_ONLINE_LOG, ['msg' => $e->getMessage()]); - throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL); + + $message = ''; + if ($e instanceof ErrorCodeException) { + $message = $e->getMessage(); + } + throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL, $message); } }