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.

82 lines
2.4 KiB

<?php
namespace App\Console\Commands;
use App\Jobs\SendCourseCar;
use App\Models\CourseSign;
use App\Models\ThirdAppointmentLog;
use Illuminate\Console\Command;
class SyncCourseCarAppointments extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_course_car_appointments';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步课程未预约的新增车牌号';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$today = date('Y-m-d');
$total = 0;
// 1. 找出:审核通过、课程已发布、课程填写了开始日期且未到开始时间、用户填写了车牌号的报名记录
$courseSigns = CourseSign::with(['course', 'user'])
->where('status', 1)
->whereHas('course', function ($query) use ($today) {
$query->where('status', 1)
->whereNotNull('start_date')
->where('start_date', '>', $today);
})->whereHas('user', function ($query) {
$query->whereNotNull('plate')
->where('plate', '!=', '');
})->get();
foreach ($courseSigns as $courseSign) {
// 2. 一个用户可能有多个车牌,按逗号拆开逐个处理
$plates = $this->parsePlates($courseSign->user->plate);
foreach ($plates as $plate) {
// 3. 如果这个车牌已经预约成功,就不用重复预约
$hasAppointment = ThirdAppointmentLog::where('course_sign_id', $courseSign->id)
->where('plate', $plate)
->where('plate_status', 1)
->exists();
if ($hasAppointment) {
continue;
}
// 4. 没有预约成功记录,提交车牌预约任务
dispatch(new SendCourseCar($courseSign, $plate));
$total++;
}
}
$this->info("课程车牌同步完成,共提交 {$total} 条车牌预约");
return self::SUCCESS;
}
/**
* 解析用户车牌,兼容空格和重复车牌。
*/
protected function parsePlates($plate)
{
return array_values(array_unique(array_filter(array_map('trim', explode(',', $plate)))));
}
}