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.

71 lines
2.3 KiB

<?php
namespace Database\Seeders;
use App\Models\Competition;
use App\Models\CompetitionTrack;
use App\Support\DefaultTrackScoringSheets;
use App\Support\SignupFieldI18n;
use Illuminate\Database\Seeder;
/**
* 与最新原型 user-form / event-site 四个细分赛道对齐。
*
* php artisan db:seed --class=CompetitionDefaultTracksSeeder
*/
class CompetitionDefaultTracksSeeder extends Seeder
{
/** 历史赛道编码,同步时停用 */
private const LEGACY_TRACK_CODES = [
'tese_xiaofei',
'jiankang_xiaofei',
'shang_wen_lv',
'xiaofei_ai',
];
public function run(): void
{
$envId = (int) env('DEFAULT_TRACKS_COMPETITION_ID', 0);
$competitions = $envId > 0
? Competition::query()->whereKey($envId)->get()
: Competition::query()->orderBy('id')->get();
if ($competitions->isEmpty()) {
$this->command?->warn('No competition found, skip tracks seed.');
return;
}
foreach ($competitions as $competition) {
CompetitionTrack::query()
->where('competition_id', $competition->id)
->whereIn('track_code', self::LEGACY_TRACK_CODES)
->update(['is_enabled' => false]);
foreach (SignupFieldI18n::prototypeTracks() as $row) {
$sheet = DefaultTrackScoringSheets::forTrackCode($row['track_code'], $row['title']);
CompetitionTrack::query()->updateOrCreate(
[
'competition_id' => $competition->id,
'track_code' => $row['track_code'],
],
[
'title' => $row['title'],
'title_en' => SignupFieldI18n::trackTitlesEn()[$row['title']] ?? null,
'description' => $row['description'],
'description_en' => SignupFieldI18n::trackDescriptionsEn()[$row['title']] ?? null,
'sort' => $row['sort'],
'is_enabled' => true,
'scoring_sheet_json' => $sheet,
]
);
}
$this->command?->info(
'Synced '.count(SignupFieldI18n::prototypeTracks())." tracks for competition #{$competition->id}."
);
}
}
}