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.

85 lines
2.3 KiB

<?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)
// 方式3: 或者课程已结束
->orWhere('course_status', 40);
})
->get();
$totalUpdated = 0;
foreach ($courses as $course) {
// 获取报名通过的学员
$courseSigns = CourseSign::where('course_id', $course->id)->where('status', 1)->get();
if ($courseSigns->isEmpty()) {
continue;
}
// 只更新还不是校友的学员,避免重复处理
$updated = User::whereIn('id', $courseSigns->pluck('user_id'))
// ->where('is_schoolmate', '!=', 1)
//->where('is_black',0)
->update(['is_schoolmate' => 1]);
$totalUpdated += $updated;
}
return $this->info("更新完成,共处理 {$totalUpdated} 位学员");
}
}