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.

79 lines
2.2 KiB

8 months ago
<?php
namespace App\Console\Commands;
use App\Models\Course;
use App\Models\CourseSign;
use App\Models\User;
use Illuminate\Console\Command;
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()
{
3 months ago
// 获取所有已开始且需要自动加入校友库的课程
8 months ago
$today = date('Y-m-d');
3 months ago
$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 未及时填写)
3 months ago
->orWhere('course_status', 10);
3 months ago
})
->get();
$totalUpdated = 0;
8 months ago
foreach ($courses as $course) {
// 获取报名通过的学员
$courseSigns = CourseSign::where('course_id', $course->id)->where('status', 1)->get();
3 months ago
if ($courseSigns->isEmpty()) {
continue;
}
3 months ago
// 只更新还不是校友的学员;从 非校友→校友 时顺带写入 schoolmate_time
$userIds = $courseSigns->pluck('user_id')->unique()->values();
$updated = User::whereIn('id', $userIds)
->whereRaw('COALESCE(is_schoolmate, 0) != 1')
3 months ago
->update(['is_schoolmate' => 1, 'schoolmate_time' => now()]);
3 months ago
$totalUpdated += $updated;
8 months ago
}
3 months ago
return $this->info("更新完成,共处理 {$totalUpdated} 位学员");
8 months ago
}
}