|
|
<?php
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use App\Models\CourseContent;
|
|
|
use App\Models\Teacher;
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
class SyncTeacherDirection extends Command
|
|
|
{
|
|
|
/**
|
|
|
* The name and signature of the console command.
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $signature = 'sync:teacher_direction';
|
|
|
|
|
|
/**
|
|
|
* The console command description.
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $description = '从courseContents同步课程方向到teachers表的direction字段';
|
|
|
|
|
|
/**
|
|
|
* Create a new command instance.
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function __construct()
|
|
|
{
|
|
|
parent::__construct();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Execute the console command.
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
$this->info('开始同步老师课程方向...');
|
|
|
|
|
|
// 获取所有有课程内容的老师
|
|
|
$teachers = Teacher::whereHas('courseContents', function ($query) {
|
|
|
$query->whereNotNull('direction')
|
|
|
->where('direction', '!=', '');
|
|
|
})->get();
|
|
|
|
|
|
$total = $teachers->count();
|
|
|
if ($total == 0) {
|
|
|
return $this->info('没有需要同步的老师');
|
|
|
}
|
|
|
|
|
|
$this->info("共找到 {$total} 个老师需要同步");
|
|
|
$bar = $this->output->createProgressBar($total);
|
|
|
$bar->start();
|
|
|
|
|
|
$updatedCount = 0;
|
|
|
foreach ($teachers as $teacher) {
|
|
|
// 获取该老师所有课程内容的direction
|
|
|
$courseContents = CourseContent::where('teacher_id', $teacher->id)
|
|
|
->whereNotNull('direction')
|
|
|
->where('direction', '!=', '')
|
|
|
->get();
|
|
|
|
|
|
if ($courseContents->isEmpty()) {
|
|
|
$bar->advance();
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 收集所有direction值
|
|
|
$allDirections = [];
|
|
|
foreach ($courseContents as $content) {
|
|
|
if (!empty($content->direction)) {
|
|
|
// 将逗号分隔的字符串拆分成数组
|
|
|
$directions = explode(',', $content->direction);
|
|
|
foreach ($directions as $direction) {
|
|
|
$direction = trim($direction);
|
|
|
if (!empty($direction)) {
|
|
|
$allDirections[] = $direction;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (empty($allDirections)) {
|
|
|
$bar->advance();
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 去重并合并现有direction
|
|
|
$existingDirections = [];
|
|
|
if (!empty($teacher->direction)) {
|
|
|
$existingDirections = array_map('trim', explode(',', $teacher->direction));
|
|
|
$existingDirections = array_filter($existingDirections);
|
|
|
}
|
|
|
|
|
|
// 合并并去重
|
|
|
$mergedDirections = array_unique(array_merge($existingDirections, $allDirections));
|
|
|
$mergedDirections = array_filter($mergedDirections); // 移除空值
|
|
|
|
|
|
// 排序并重新组合
|
|
|
sort($mergedDirections);
|
|
|
$newDirection = implode(',', $mergedDirections);
|
|
|
|
|
|
// 如果direction有变化,则更新
|
|
|
if ($teacher->direction !== $newDirection) {
|
|
|
$teacher->direction = $newDirection;
|
|
|
$teacher->save();
|
|
|
$updatedCount++;
|
|
|
}
|
|
|
|
|
|
$bar->advance();
|
|
|
}
|
|
|
|
|
|
$bar->finish();
|
|
|
$this->newLine();
|
|
|
$this->info("同步完成,共更新 {$updatedCount} 个老师的课程方向");
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|