|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
<?php
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
use App\Enums\OrderStatusEnum;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDetail;
|
|
|
use App\Models\UserHasCoupon;
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
class CancelUnPayOrder implements ShouldQueue
|
|
|
{
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected $order; // 存储订单实例的属性
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的任务实例。
|
|
|
*
|
|
|
* @param Order $order 需要处理的订单
|
|
|
*/
|
|
|
public function __construct(Order $order)
|
|
|
{
|
|
|
$this->order = $order; // 初始化订单属性
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行任务。
|
|
|
*
|
|
|
* 此方法在处理任务时调用,检查订单状态并在未付款时取消订单。
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
// 从数据库刷新订单实例,以获取最新状态
|
|
|
$nowOrder = $this->order->refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查当前状态是否为 UN_PAY(未付款)
|
|
|
if ($nowOrder->status == OrderStatusEnum::UN_PAY) {
|
|
|
|
|
|
// 将订单状态更新为 UN_PAY_CANCEL(因未付款而取消)
|
|
|
$this->order->status = OrderStatusEnum::UN_PAY_CANCEL;
|
|
|
$this->order->save(); // 保存更新后的订单状态
|
|
|
|
|
|
// 如果使用了优惠券,则处理优惠券回滚
|
|
|
if (
|
|
|
!is_null($this->order->coupon_id) && // 确保存在优惠券 ID
|
|
|
$coupon = UserHasCoupon::query()->find($this->order->coupon_id) // 在数据库中查找优惠券
|
|
|
) {
|
|
|
// 重置优惠券的使用时间戳
|
|
|
$coupon->used_at = null;
|
|
|
$coupon->save(); // 保存对优惠券的更改
|
|
|
}
|
|
|
|
|
|
// 对每个订单详情回滚库存
|
|
|
$this->order
|
|
|
->details() // 获取订单详情
|
|
|
->with('product') // 预加载相关产品
|
|
|
->get() // 获取详情
|
|
|
->map(function (OrderDetail $detail) {
|
|
|
// 对于每个订单详情,根据订购数量增加产品库存
|
|
|
$product = $detail->product; // 获取相关产品
|
|
|
$product->increment('count', $detail->number); // 增加产品库存
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|