今天在做一个订单更新状态的功能时,选择使用laravel自带的 任务调度 功能来实现。
首先,先建立有一个新的Artisan命令,可以使用 Artisan 命令 make:command。这个命令会在 app/Console/Commands 目录中创建一个新的命令类。php artisan make:command OrderStatusCommand
Command命令结构命令生成后,应先填写类的 signature 和 description 属性,这会在使用 list 命令的时候显示出来。执行命令时会调用 handle 方法,你可以在这个方法中放置命令逻辑。<?php
namespace App\Console\Commands;
use App\Models\CommonConfig;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Throwable;
class OrderStatusCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order:update';
/**
* The console command description.
*
* @var string
*/
protected $description = '定时更新订单状态';
protected $model;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->model = new Order();
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
* @throws Throwable
*/
public function handle()
{
$config = (new CommonConfig)->get();
$this->model->getConnection()->transaction(function () use ($config) {
// 未支付订单自动关闭
$this->close($config->close_days);
// 已发货订单自动确认收货
$this->receive($config->receive_days);
});
$this->info('更新订单状态成功.');
}
/**
* 未支付订单自动关闭
* @param $close_days
* @return $this|bool
*/
private function close($close_days)
{
// 取消n天以前的的未付款订单
if ($close_days < 1) {
return false;
}
// 截止时间
$deadlineTime = Carbon::now()->subDays($close_days)->toDateTimeString();
// 查询截止时间未支付的订单
$orderIds = $this->model
->where(['pay_status' => 10,'order_status' => 10])
->where('created_at', '
->pluck('id')->toArray();
//更新订单状态
if ($orderIds) {
return $this->model->whereIn('id', $orderIds)->update(['order_status' => 20]);
}
return false;
}
/**
* 已发货订单自动确认收货
* @param $receive_days
* @return bool|false|int
*/
private function receive($receive_days)
{
if ($receive_days < 1) {
return false;
}
// 截止时间
$deadlineTime = Carbon::now()->subDays($receive_days)->toDateTimeString();
// 订单id集
$orderIds = $this->model
->where(['pay_status' => 20,'delivery_status' => 20, 'receipt_status' => 10])
->where('delivery_time', '
->pluck('id')->toArray();
// 更新订单收货状态
return $this->model->whereIn('id', $orderIds)->update([
'receipt_status' => 20,
'receipt_time' => Carbon::now()->toDateTimeString(),
'order_status' => 30
]);
}
}
闭包命令基于闭包的命令提供一个用类替代定义控制台命令的方法。同样的,路由闭包是控制器的一种替代方法,而命令闭包可以视为命令类的替代方法。在 app/Console/Kernel.php 文件的 commands 方法中, Laravel 加载了 routes/console.php 文件:<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\OrderStatusCommand::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('order:update')->everyMinute();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
测试一下php artisan order:update
php artisan schedule:run
转载声明:本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
欢迎捐赠赞赏