|
|
<?php
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
use App\Models\ScheduleOverviewLocation;
|
|
|
use App\Models\ScheduleOverviewOwner;
|
|
|
use App\Models\ScheduleOverviewSchedule;
|
|
|
use App\Models\ScheduleOverviewScheduleGroup;
|
|
|
use App\Models\ScheduleOverviewScheduleModule;
|
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class MigrateScheduleOverviewLegacySchedules extends Command
|
|
|
{
|
|
|
protected $signature = 'schedule-overview:migrate-legacy-schedules {--year=} {--dry-run}';
|
|
|
|
|
|
protected $description = '将旧的 schedule_overview_schedules 数据迁移到 schedule_groups + modules(不删除旧数据)';
|
|
|
|
|
|
public function handle(): int
|
|
|
{
|
|
|
$year = $this->option('year');
|
|
|
$dryRun = (bool) $this->option('dry-run');
|
|
|
|
|
|
$query = ScheduleOverviewSchedule::query()->orderBy('id');
|
|
|
if (!empty($year)) {
|
|
|
$query->where('year', (string) $year);
|
|
|
}
|
|
|
|
|
|
$legacyList = $query->get();
|
|
|
if ($legacyList->isEmpty()) {
|
|
|
$this->info('未找到需要迁移的数据。');
|
|
|
return self::SUCCESS;
|
|
|
}
|
|
|
|
|
|
$this->info('待迁移条数:' . $legacyList->count() . ($dryRun ? '(dry-run)' : ''));
|
|
|
|
|
|
$createdGroups = 0;
|
|
|
$createdModules = 0;
|
|
|
$skippedModules = 0;
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
foreach ($legacyList as $legacy) {
|
|
|
$group = ScheduleOverviewScheduleGroup::query()
|
|
|
->where('year', $legacy->year)
|
|
|
->where('system_id', $legacy->system_id)
|
|
|
->where('course_id', $legacy->course_id)
|
|
|
->where('title', $legacy->title)
|
|
|
->first();
|
|
|
|
|
|
if (empty($group)) {
|
|
|
$group = new ScheduleOverviewScheduleGroup();
|
|
|
$group->year = (string) $legacy->year;
|
|
|
$group->system_id = (int) $legacy->system_id;
|
|
|
$group->course_id = (int) $legacy->course_id;
|
|
|
$group->title = (string) $legacy->title;
|
|
|
$group->admin_id = $legacy->admin_id;
|
|
|
$group->department_id = $legacy->department_id;
|
|
|
|
|
|
if (!$dryRun) {
|
|
|
$group->save();
|
|
|
}
|
|
|
$createdGroups++;
|
|
|
}
|
|
|
|
|
|
$locationName = trim((string) ($legacy->location ?? ''));
|
|
|
$ownerName = trim((string) ($legacy->owner ?? ''));
|
|
|
|
|
|
if ($locationName === '' || $ownerName === '') {
|
|
|
// 旧数据不完整时跳过,避免写入无法编辑的 module
|
|
|
$this->warn("跳过 legacy#{$legacy->id}:地点或负责人为空");
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
$location = ScheduleOverviewLocation::query()->where('name', $locationName)->first();
|
|
|
if (empty($location)) {
|
|
|
$location = new ScheduleOverviewLocation();
|
|
|
$location->name = $locationName;
|
|
|
$location->sort = 0;
|
|
|
$location->status = 1;
|
|
|
$location->admin_id = $legacy->admin_id;
|
|
|
$location->department_id = $legacy->department_id;
|
|
|
if (!$dryRun) {
|
|
|
$location->save();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$owner = ScheduleOverviewOwner::query()->where('name', $ownerName)->first();
|
|
|
if (empty($owner)) {
|
|
|
$owner = new ScheduleOverviewOwner();
|
|
|
$owner->name = $ownerName;
|
|
|
$owner->sort = 0;
|
|
|
$owner->status = 1;
|
|
|
$owner->admin_id = $legacy->admin_id;
|
|
|
$owner->department_id = $legacy->department_id;
|
|
|
if (!$dryRun) {
|
|
|
$owner->save();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$exists = ScheduleOverviewScheduleModule::query()
|
|
|
->where('group_id', $group->id)
|
|
|
->where('month', (int) $legacy->month)
|
|
|
->where('location_id', $location->id)
|
|
|
->where('owner_id', $owner->id)
|
|
|
->where('count_text', (string) ($legacy->count_text ?? ''))
|
|
|
->exists();
|
|
|
|
|
|
if ($exists) {
|
|
|
$skippedModules++;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
$module = new ScheduleOverviewScheduleModule();
|
|
|
$module->group_id = $group->id;
|
|
|
$module->name = null;
|
|
|
$module->month = (int) $legacy->month;
|
|
|
$module->location_id = $location->id;
|
|
|
$module->owner_id = $owner->id;
|
|
|
$module->count_text = (string) ($legacy->count_text ?? '');
|
|
|
$module->sort = 0;
|
|
|
$module->admin_id = $legacy->admin_id;
|
|
|
$module->department_id = $legacy->department_id;
|
|
|
|
|
|
if (!$dryRun) {
|
|
|
$module->save();
|
|
|
}
|
|
|
$createdModules++;
|
|
|
}
|
|
|
|
|
|
if ($dryRun) {
|
|
|
DB::rollBack();
|
|
|
} else {
|
|
|
DB::commit();
|
|
|
}
|
|
|
} catch (\Throwable $throwable) {
|
|
|
DB::rollBack();
|
|
|
$this->error($throwable->getMessage());
|
|
|
return self::FAILURE;
|
|
|
}
|
|
|
|
|
|
$this->info("迁移完成:新增 groups={$createdGroups},新增 modules={$createdModules},跳过重复 modules={$skippedModules}");
|
|
|
if ($dryRun) {
|
|
|
$this->info('dry-run 未写入数据库。');
|
|
|
}
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
}
|
|
|
}
|
|
|
|