forked from summerblue/laravel-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d5850f
commit 56ad650
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use App\Models\Order; | ||
|
||
// 代表这个类需要被放到队列中执行,而不是触发时立即执行 | ||
class CloseOrder implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
protected $order; | ||
|
||
public function __construct(Order $order, $delay) | ||
{ | ||
$this->order = $order; | ||
// 设置延迟的时间,delay() 方法的参数代表多少秒之后执行 | ||
$this->delay($delay); | ||
} | ||
|
||
// 定义这个任务类具体的执行逻辑 | ||
// 当队列处理器从队列中取出任务时,会调用 handle() 方法 | ||
public function handle() | ||
{ | ||
// 判断对应的订单是否已经被支付 | ||
// 如果已经支付则不需要关闭订单,直接退出 | ||
if ($this->order->paid_at) { | ||
return; | ||
} | ||
// 通过事务执行 sql | ||
\DB::transaction(function() { | ||
// 将订单的 closed 字段标记为 true,即关闭订单 | ||
$this->order->update(['closed' => true]); | ||
// 循环遍历订单中的商品 SKU,将订单中的数量加回到 SKU 的库存中去 | ||
foreach ($this->order->items as $item) { | ||
$item->productSku->addStock($item->amount); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters