Lae/app/Jobs/Host/SendRenewNotificationJob.php

48 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2023-02-12 18:21:09 +00:00
<?php
namespace App\Jobs\Host;
use App\Models\Host;
use App\Notifications\User\UserNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendRenewNotificationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected ?Host $host;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(?Host $host)
{
$this->host = $host;
}
/**
* Execute the job.
*/
public function handle(): void
{
2023-02-12 18:28:06 +00:00
if (! $this->host) {
2023-02-12 18:21:09 +00:00
// 获取 Host距离今天刚好 7 天的 Host
2023-02-12 19:06:27 +00:00
(new Host)->where('next_due_at', '>', now()->addDays(7)->startOfDay())
2023-02-12 18:21:09 +00:00
->where('next_due_at', '<', now()->addDays(7)->endOfDay())
->chunk(100, function ($hosts) {
foreach ($hosts as $host) {
dispatch(new self($host));
}
});
}
2023-02-12 18:28:06 +00:00
$this->host?->user->notify(new UserNotification('续费提醒', "您的 {$this->host->name} 将在 7 天后到期,请及时续费。", true));
2023-02-12 18:21:09 +00:00
}
}