|
|
|
@ -639,6 +639,22 @@ class OrderOnlineService implements OrderOnlineServiceInterface |
|
|
|
|
|
|
|
$orderMain = $this->check($globalOrderId, $userId, OrderState::REFUNDING); |
|
|
|
|
|
|
|
$orderIds = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->pluck('id'); |
|
|
|
$orderGoods = OrderGoods::query()->whereIn('order_id', $orderIds)->get()->toArray(); |
|
|
|
|
|
|
|
// 释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, intval($goodsItem['number'])); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 微信退款
|
|
|
|
if ($orderMain->pay_type == Payment::WECHAT) { |
|
|
|
return $this->paymentService->undo($orderMain->global_order_id, $userId); |
|
|
|
@ -647,51 +663,55 @@ class OrderOnlineService implements OrderOnlineServiceInterface |
|
|
|
|
|
|
|
public function autoCancel() |
|
|
|
{ |
|
|
|
$orderMains = OrderMain::query() |
|
|
|
->select(['global_order_id', 'user_id']) |
|
|
|
->where(['state' => OrderState::UNPAID]) |
|
|
|
->where('created_at', '<', time()-900) |
|
|
|
->get(); |
|
|
|
|
|
|
|
foreach ($orderMains as $key => &$orderMain) { |
|
|
|
try { |
|
|
|
|
|
|
|
// 订单待支付
|
|
|
|
$orderMain->state = OrderState::CANCELED; |
|
|
|
if (!$orderMain->save()) { |
|
|
|
throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE); |
|
|
|
} |
|
|
|
$orderMains = OrderMain::query() |
|
|
|
->where(['state' => OrderState::UNPAID]) |
|
|
|
->where('created_at', '<', time()-900) |
|
|
|
->get(); |
|
|
|
|
|
|
|
// 退还优惠券
|
|
|
|
$this->couponService->orderRefundCoupons($orderMain->global_order_id); |
|
|
|
foreach ($orderMains as $key => &$orderMain) { |
|
|
|
|
|
|
|
// 撤销活动商品购买记录
|
|
|
|
$orders = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->get()->toArray(); |
|
|
|
$orderGoods = OrderGoods::query() |
|
|
|
->whereIn('order_id', array_values(array_column($orders, 'id'))) |
|
|
|
->get() |
|
|
|
->toArray(); |
|
|
|
foreach ($orderGoods as $key => &$goods) { |
|
|
|
if ($goods['activity_type'] == 2) { |
|
|
|
$this->goodsActivityService->clearCacheRecord($goods['goods_id'], $goods['number'], $orderMain->user_id); |
|
|
|
// 订单取消
|
|
|
|
OrderMain::query()->where(['id' => $orderMain->id])->update(['state' => OrderState::CANCELED]); |
|
|
|
|
|
|
|
// 退还优惠券
|
|
|
|
$this->couponService->orderRefundCoupons($orderMain->global_order_id); |
|
|
|
|
|
|
|
// 撤销活动商品购买记录
|
|
|
|
$orders = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->get()->toArray(); |
|
|
|
$orderGoods = OrderGoods::query() |
|
|
|
->whereIn('order_id', array_values(array_column($orders, 'id'))) |
|
|
|
->get() |
|
|
|
->toArray(); |
|
|
|
foreach ($orderGoods as $key => &$goods) { |
|
|
|
if ($goods['activity_type'] == 2) { |
|
|
|
$this->goodsActivityService->clearCacheRecord($goods['goods_id'], $goods['number'], $orderMain->user_id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 释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, intval($goodsItem['number'])); |
|
|
|
// 释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, intval($goodsItem['number'])); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 记录badge
|
|
|
|
$orderChildIds = array_values(array_column($orders, 'store_id')); |
|
|
|
$this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::CANCELED); |
|
|
|
} |
|
|
|
|
|
|
|
// 记录badge
|
|
|
|
$orderChildIds = array_values(array_column($orders, 'store_id')); |
|
|
|
$this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::CANCELED); |
|
|
|
} catch (Exception $e) { |
|
|
|
$this->log->event(LogLabel::ORDER_AUTO_CANCEL_FAIL_LOG, ['message' => $e->getMessage()]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |