HYJ 8 months ago
parent bc65a79818
commit 019a95d00a

@ -16,62 +16,57 @@ class CancelUnPayOrder implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $order; protected $order; // Property to hold the order instance
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @param Order $order * @param Order $order The order that needs to be processed
*/ */
public function __construct(Order $order) public function __construct(Order $order)
{ {
$this->order = $order; $this->order = $order; // Initialize the order property
} }
/** /**
* Execute the job. * Execute the job.
* *
* This method is called when the job is processed. It checks the status of the order
* and cancels it if it remains unpaid.
*
* @return void * @return void
*/ */
public function handle() public function handle()
{ {
// 查询数据库最新状态 // Refresh the order instance from the database to get the latest status
$nowOrder = $this->order->refresh(); $nowOrder = $this->order->refresh();
// 如果现在还是没有付款,那么则取消订单 // Check if the current status is UN_PAY (unpaid)
if ($nowOrder->status == OrderStatusEnum::UN_PAY) { if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
// 如果订单还是没有付款 // Update the order status to UN_PAY_CANCEL (cancelled due to non-payment)
// 未付款设置为取消状态,
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL; $this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
$this->order->save(); $this->order->save(); // Save the updated order status
// 回退优惠券 // Process coupon rollback if a coupon was used
if ( if (
!is_null($this->order->coupon_id) && !is_null($this->order->coupon_id) && // Ensure a coupon ID exists
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) $coupon = UserHasCoupon::query()->find($this->order->coupon_id) // Find the coupon in the database
) { ) {
// Reset the coupon usage timestamp
$coupon->used_at = null; $coupon->used_at = null;
$coupon->save(); $coupon->save(); // Save changes to the coupon
} }
// 回滚库存 // Rollback inventory for each order detail
$this->order $this->order
->details() ->details() // Get the order details
->with('product') ->with('product') // Eager load the associated product
->get() ->get() // Fetch the details
->map(function (OrderDetail $detail) { ->map(function (OrderDetail $detail) {
// For each order detail, increment the product's count by the ordered quantity
$product = $detail->product; // Get the associated product
$product->increment('count', $detail->number); // Increase product count
// 不回滚出售数量
$product = $detail->product;
$product->increment('count', $detail->number);
}); });
} }
} }

Loading…
Cancel
Save