You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.7 KiB
81 lines
2.7 KiB
1 month ago
|
<?php
|
||
|
|
||
|
namespace App\Console\Commands;
|
||
|
|
||
|
use App\Dictionary\SendMessage\Type;
|
||
|
use App\Dictionary\WeChat\MiniProgram\SubscribeId;
|
||
|
use App\Jobs\SendWeChatMessageJob;
|
||
|
use App\Models\RegistrationRecord;
|
||
|
use App\Models\SendMessageJob;
|
||
|
use App\Utils\Traits\SendSubscribeMessage;
|
||
|
use Exception;
|
||
|
use Illuminate\Console\Command;
|
||
|
use Illuminate\Support\Facades\Log;
|
||
|
use \Symfony\Component\Console\Command\Command as BaseCommand;
|
||
|
use Throwable;
|
||
|
|
||
|
/**
|
||
|
* Appointment Visit Reminders 发送预约就诊提示
|
||
|
* @example
|
||
|
*/
|
||
|
class SendAppointmentReminders extends Command
|
||
|
{
|
||
|
use SendSubscribeMessage;
|
||
|
|
||
|
/**
|
||
|
* The name and signature of the console command.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $signature = 'app:send-appointment-reminders';
|
||
|
|
||
|
/**
|
||
|
* The console command description.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $description = 'Send appointment reminders to users via WeChat subscription messages';
|
||
|
|
||
|
/**
|
||
|
* Execute the console command.
|
||
|
*/
|
||
|
public function handle(): int
|
||
|
{
|
||
|
$this->info('Starting to send appointment reminders...');
|
||
|
|
||
|
// 查询即将到期的预约记录(8小时后的预约)
|
||
|
$appointments = RegistrationRecord::where('visit_date', now()->toDate())
|
||
|
->where('begin_date', now()->subHours(8))
|
||
|
->where('reminder_sent', 0)
|
||
|
->get();
|
||
|
|
||
|
if ($appointments->isEmpty()) {
|
||
|
$this->info('No appointments to send reminders for.');
|
||
|
return BaseCommand::SUCCESS;
|
||
|
}
|
||
|
|
||
|
$message = new SendMessageJob();
|
||
|
$success_records = [];
|
||
|
foreach ($appointments as $appointment) {
|
||
|
try {
|
||
|
$this->sendAppointmentReminderMessage($appointment);
|
||
|
|
||
|
// 标记提醒已发送
|
||
|
$appointment->update(['reminder_sent' => 1]);
|
||
|
$success_records[] = $appointment->id;
|
||
|
|
||
|
$this->info("Reminder sent for appointment ID: {$appointment->id}");
|
||
|
Log::channel('send_wechat_message')->info('Reminder sent for appointment ID: '. $appointment->id);
|
||
|
} catch (Exception|Throwable $e) {
|
||
|
// 记录错误日志
|
||
|
$err_msg = "{$e->getMessage()} ON {$e->getFile()}:{$e->getLine()}";
|
||
|
$this->error("Failed to send reminder for appointment ID: {$appointment->id}, Err msg: {$err_msg}");
|
||
|
Log::channel('send_wechat_message')->error('Failed to send reminder for appointment ID: '. $appointment->id, ['error' => $err_msg]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$this->info( '['. date('Y-m-d H:i:s').'] Appointment reminders sent successfully. send Record:'. implode(',', $success_records));
|
||
|
return BaseCommand::SUCCESS;
|
||
|
}
|
||
|
}
|