Skip to content

Instantly share code, notes, and snippets.

@ab-budaev
Created November 22, 2018 20:32
Show Gist options
  • Save ab-budaev/aeeb2307faa28e2ccf70a4f3f7b55a2b to your computer and use it in GitHub Desktop.
Save ab-budaev/aeeb2307faa28e2ccf70a4f3f7b55a2b to your computer and use it in GitHub Desktop.
Sync study groups console command
<?php
namespace App\Console\Commands;
use App\Models\Course;
use App\Models\StudyGroup;
use App\Models\Teacher;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class SyncStudyGroups extends Command
{
protected $signature = 'sync:groups';
protected $description = 'Синхронизация учебных групп';
public function handle()
{
$remoteGroups = DB::connection('tma')
->table('studygroup')
->get();
StudyGroup::query()->truncate();
foreach ($remoteGroups as $remoteGroup) {
$course = Course::query()->where('remote_id', $remoteGroup->Curse)->first();
$teacher = Teacher::query()->where('remote_id', $remoteGroup->Teacher)->first();
if (!$course)
continue;
switch ($remoteGroup->Type) {
case 1:
$type = 'Утро';
break;
case 2:
$type = 'День';
break;
case 3:
$type = 'Вечер';
break;
default:
$type = 'Выходные дни';
break;
}
$groupTimeData = json_decode($remoteGroup->descr_json, true);
StudyGroup::query()->updateOrCreate([
'id' => $remoteGroup->Id,
], [
'code' => $remoteGroup->Code,
'course_id' => $course->id,
'teacher_id' => $teacher
? $teacher->id
: null,
'is_public' => $remoteGroup->is_public
?: false,
'start_at' => $remoteGroup->DateStart,
'finish_at' => $remoteGroup->DateEnd,
'type' => $type,
'days' => isset($groupTimeData['days'])
? $groupTimeData['days']
: null,
'hours' => isset($groupTimeData['hours'])
? $groupTimeData['hours']
: null,
]);
}
$this->output->success('Учебные группы успешно синхронизированы');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment