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.

61 lines
2.7 KiB

15 hours ago
<?php
namespace App\Console\Commands;
use App\Models\Course;
use App\Services\SchoolmateEnrollmentService;
use Illuminate\Console\Command;
class BackfillSchoolmate extends Command
{
protected $signature = 'schoolmate:backfill
{course_ids : 课程ID,多个用英文逗号分隔}
{--execute : 正式补齐;不传时只检查并输出遗漏名单}';
protected $description = '按指定课程检查或补齐审核通过学员的校友资格';
public function handle(SchoolmateEnrollmentService $service)
{
$raw = (string) $this->argument('course_ids');
if (!preg_match('/^[1-9][0-9]*(,[1-9][0-9]*)*$/D', $raw)) {
$this->error('课程ID必须为正整数,多个用英文逗号分隔。');
return 1;
}
$ids = array_values(array_unique(explode(',', $raw)));
$courses = Course::whereIn('id', $ids);
$found = (clone $courses)->get(['id', 'name', 'start_date']);
$missingIds = array_diff($ids, $found->pluck('id')->all());
if ($missingIds) {
$this->error('课程不存在或已删除:' . implode(',', $missingIds));
return 1;
}
// 缺少开课日期时无法确定历史入库时间,整批停止,避免只补一部分。
foreach ($found as $course) {
if (!$course->start_date) {
$this->error("课程 {$course->id}【{$course->name}】缺少开课日期,请先补全。");
return 1;
}
}
$this->table(['课程ID', '课程名称', '开课日期'], $found->map(function ($course) {
return [$course->id, $course->name, $course->start_date];
})->all());
$execute = (bool) $this->option('execute');
$this->info($execute ? '正式执行:仅补齐尚非校友的审核通过学员。' : '检查模式:不会修改任何学员数据。');
$total = 0;
$updated = 0;
$service->pendingUsers($courses)->chunkById(200, function ($users) use ($service, $execute, &$total, &$updated) {
$rows = [];
foreach ($users as $user) {
$total++;
$rows[] = [$user->id, $user->name, $user->mobile, $user->source_start_date];
if ($execute) {
$updated += $service->promote($user->id, $user->source_start_date);
}
}
$this->table(['学员ID', '姓名', '手机号', '拟入库日期(所选课程最早开课日)'], $rows);
}, 'users.id', 'id');
$this->info("发现遗漏 {$total} 人" . ($execute ? ",实际补齐 {$updated} 人。" : ';正式补齐请追加 --execute。'));
return 0;
}
}