Browse Source

活动商品库存

master
weigang 5 years ago
parent
commit
f541149c75
  1. 8
      app/Constants/v3/ErrorCode.php
  2. 16
      app/Service/v3/Implementations/OrderOnlineService.php

8
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;

16
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,6 +361,8 @@ class OrderOnlineService implements OrderOnlineServiceInterface
return $this->paymentService->do($globalOrderId, $totalAmount, $userId, config('wechat.notify_url.online'));
} catch (Exception $e) {
Db::rollBack();
if ($isCacheInventory) {
// 释redis库存
foreach ($carts as $k => &$cart) {
// 拼接activity_type和goods_id
@ -370,8 +374,14 @@ class OrderOnlineService implements OrderOnlineServiceInterface
$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);
}
}

Loading…
Cancel
Save