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.

87 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use App\Models\Course;
use App\Models\CourseSign;
use App\Models\EmailRecord;
use App\Models\EmailRecordUser;
use App\Models\User;
use App\Repositories\MeetRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class AutoSchoolmate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'auto_schoolmate';
/**
* The console command description.
*
* @var string
*/
protected $description = '已审核学员自动进入校友库';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// 获取所有已开始且需要自动加入校友库的课程
$today = date('Y-m-d');
$courses = Course::where('auto_schoolmate', 1)
->where(function ($query) use ($today) {
// 方式1: start_date 已填写且 <= 今天
$query->where(function ($q) use ($today) {
$q->whereNotNull('start_date')
->where('start_date', '<=', $today);
})
// 方式2: 或者课程状态为进行中(即使 start_date 未及时填写)
->orWhere('course_status', 10);
})
->get();
$totalUpdated = 0;
foreach ($courses as $course) {
// 获取报名通过的学员
$courseSigns = CourseSign::where('course_id', $course->id)->where('status', 1)->get();
if ($courseSigns->isEmpty()) {
continue;
}
// 只更新还不是校友的学员,避免重复处理
// 注意is_schoolmate != 1 不会匹配 NULL 值,需要显式包含 NULL
$updated = User::whereIn('id', $courseSigns->pluck('user_id'))
->where(function ($query) {
$query->where('is_schoolmate', '!=', 1)
->orWhereNull('is_schoolmate');
})
//->where('is_black',0)
->update(['is_schoolmate' => 1, 'schoolmate_time' => now()]);
$totalUpdated += $updated;
}
return $this->info("更新完成,共处理 {$totalUpdated} 位学员");
}
}