每个订单时间超过 20 分钟时更新状态

Update Status when each order time past 20 minutes

我使用 Laravel Lumen 框架和 jenssegers/laravel-mongodb 包,我在项目中的查询是:

    $time_5_min_ago = Carbon::now()->subMinute(5);
    $time_10_min_ago = Carbon::now()->subMinute(10);
    $time_15_min_ago = Carbon::now()->subMinute(15);
    $time_20_min_ago = Carbon::now()->subMinute(20);



    return Order::where(function ($query)  use ($maxLat_try_one,$minLat_try_one,$maxLon_try_one,$minLon_try_one,$time_5_min_ago,$time_10_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
            ->whereBetween('source_latitude', [$minLat_try_one,$maxLat_try_one])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_5_min_ago)
            ->where('created_at', '>=', $time_10_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_two,$minLat_try_two,$maxLon_try_two,$minLon_try_two,$time_10_min_ago,$time_15_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_two, $maxLon_try_two])
            ->whereBetween('source_latitude', [$minLat_try_two,$maxLat_try_two])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_10_min_ago)
            ->where('created_at', '>=', $time_15_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_three,$minLat_try_three,$maxLon_try_three,$minLon_try_three,$time_15_min_ago,$time_20_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_three, $maxLon_try_three])
            ->whereBetween('source_latitude', [$minLat_try_three,$maxLat_try_three])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_15_min_ago)
            ->where('created_at', '>=', $time_20_min_ago);
    })->get($fields);

我想在顶部查询和最后一个 orWehere 查询中存在任何订单,订单 created_at 是 < 20 分钟前,状态仍然是挂起,订单状态更新为暂停

我认为你应该使用 Eloquen 属性而不是每分钟更新一次数据库。

public function getStatusAttribute() {
 if($this->created > ....) {
   $status = "pending";
 } else if(...) {
   ....
 }
 return $status;
}

使用Laravel的命令结合时间表,你会得到你想要的。

详细解释如下:

第 1 步:创建名为 "ChangePendingToSuspended"

的命令

打开控制台然后执行

php artisan make:console ChangePendingToSuspended

第 2 步:打开 ChangePendingToSuspended.php

您可以在 app/Console/Commands/ 目录中找到它并篡改它的参数,例如添加 描述

protected $description = 'Changes the Requests which has been in pending status for a period of time to suspended status.';

和一个签名

protected $signature = 'requests:clear-pending';

好的,在你问之前 "What's a signature?" 签名是一种从控制台执行命令的方式,例如现在您可以从 artisan 手动启动 ChangePendingToSuspended 命令,例如

php artisan requests:clear-pending

第 3 步:定义我们的命令

现在您将代码放入 handle 方法中,在您的情况下它可能是以下上下文中的内容:

public function handle(){
    \DB::table('requests')
        ->where('created_at','<',\Carbon\Carbon::now()->addMinutes(-20))
        ->update(['status'=>'suspended']);
}

只需使用您喜欢的任何方法来更改该命令中的状态。

第 4 步:将命令添加到计划

打开在 app\Console\ 目录中找到的 Kernel.php 您将看到一个名为 $commands 的数组,将我们的 class 添加到其中

 protected $commands = [
        Commands\Inspire::class,
        Commands\ChangePendingToSuspended::class,
    ];

现在转到调度方法并调度您新创建的命令

protected function schedule(Schedule $schedule)
    {
...
        $schedule->command('requests:change-pending-to-investigate')->everyFiveMinutes();
...
    }

好的,现在,每五分钟,调度程序将每五分钟执行一次我们的命令 ChangePendingToSuspended, 但还有 1 个步骤,我们需要通过将它的 cron 作业添加到我们的系统来使计划打勾。

第 5 步:将计划 cron 条目添加到您的服务器

这因服务器和版本而异,无论您使用的是 windows、linux 还是 osx

但这是 cron 条目

对于 linux:

* * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

for windows(使用任务调度程序):

* * * * path/to/php path/to/artisan schedule:run 1>> NUL 2>&1