|
|
|
@ -34,6 +34,7 @@ use Hyperf\Database\Model\Model; |
|
|
|
use Hyperf\DbConnection\Db; |
|
|
|
use Hyperf\Di\Annotation\Inject; |
|
|
|
use App\Service\v3\Interfaces\OrderOnlineServiceInterface; |
|
|
|
use Hyperf\Redis\Redis; |
|
|
|
use Hyperf\Snowflake\IdGeneratorInterface; |
|
|
|
use Hyperf\Utils\ApplicationContext; |
|
|
|
|
|
|
|
@ -115,6 +116,8 @@ class OrderOnlineService implements OrderOnlineServiceInterface |
|
|
|
*/ |
|
|
|
public function do($marketId, $userId, $userAddrId, $storeList, $totalMoney, $deliveryTimeNote='尽快送达', $serviceMoney=0, $receiveCouponIds=null, $plat=''){ |
|
|
|
|
|
|
|
$redis = ApplicationContext::getContainer()->get(Redis::class); |
|
|
|
$carts = []; |
|
|
|
Db::beginTransaction(); |
|
|
|
try { |
|
|
|
|
|
|
|
@ -335,6 +338,18 @@ class OrderOnlineService implements OrderOnlineServiceInterface |
|
|
|
|
|
|
|
Db::commit(); |
|
|
|
|
|
|
|
// 压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, $cart->num); |
|
|
|
} else { |
|
|
|
$redis->incrBy($inventoryKey, $cart->num); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 清除购物车
|
|
|
|
$this->shopCartUpdateService->doClear($userId, $marketId); |
|
|
|
|
|
|
|
@ -345,6 +360,17 @@ 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, $cart->num); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->log->event(LogLabel::ORDER_ONLINE_LOG, ['msg' => $e->getMessage()]); |
|
|
|
throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL); |
|
|
|
} |
|
|
|
@ -388,15 +414,31 @@ class OrderOnlineService implements OrderOnlineServiceInterface |
|
|
|
// 撤销活动商品购买记录
|
|
|
|
$orders = Order::query()->where(['order_main_id' => $globalOrderId])->get()->toArray(); |
|
|
|
$orderGoods = OrderGoods::query() |
|
|
|
->where('activity_type', 2) |
|
|
|
// ->where('activity_type', 2)
|
|
|
|
->whereIn('order_id', array_values(array_column($orders, 'id'))) |
|
|
|
->get(); |
|
|
|
->get() |
|
|
|
->toArray(); |
|
|
|
foreach ($orderGoods as $key => &$goods) { |
|
|
|
$this->goodsActivityService->clearCacheRecord($goods->goods_id, $goods->number, $orderMain->user_id); |
|
|
|
if ($goods['activity_type'] == 2) { |
|
|
|
$this->goodsActivityService->clearCacheRecord($goods['goods_id'], $goods['number'], $orderMain['user_id']); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Db::commit(); |
|
|
|
|
|
|
|
// 释redis库存
|
|
|
|
$redis = ApplicationContext::getContainer()->get(Redis::class); |
|
|
|
foreach ($orderGoods as $k => &$goodsItem) { |
|
|
|
// 拼接activity_type和goods_id
|
|
|
|
$inventoryKey = 'goods_inventory_sold_'.$goodsItem['activity_type'].'_'.$goodsItem['goods_id']; |
|
|
|
// redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放
|
|
|
|
if (!$redis->exists($inventoryKey)) { |
|
|
|
$redis->set($inventoryKey, 0); |
|
|
|
} else { |
|
|
|
$redis->decrBy($inventoryKey, $goodsItem['number']); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 记录badge
|
|
|
|
$orderChildIds = array_values(array_column($orders, 'store_id')); |
|
|
|
$this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::CANCELED); |
|
|
|
@ -479,6 +521,19 @@ class OrderOnlineService implements OrderOnlineServiceInterface |
|
|
|
|
|
|
|
Db::commit(); |
|
|
|
|
|
|
|
// 释redis库存
|
|
|
|
$redis = ApplicationContext::getContainer()->get(Redis::class); |
|
|
|
foreach ($orderGoods as $k => &$goodsItem) { |
|
|
|
// 拼接activity_type和goods_id
|
|
|
|
$inventoryKey = 'goods_inventory_sold_'.$goodsItem['activity_type'].'_'.$goodsItem['goods_id']; |
|
|
|
// redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放
|
|
|
|
if (!$redis->exists($inventoryKey)) { |
|
|
|
$redis->set($inventoryKey, 0); |
|
|
|
} else { |
|
|
|
$redis->decrBy($inventoryKey, $goodsItem['number']); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 月销流水
|
|
|
|
$statistics = []; |
|
|
|
foreach ($orders as $key => &$order) { |
|
|
|
|