KTV/app/Jobs/ImportArtistChunkJob.php

52 lines
1.4 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Artist;
use App\Enums\ArtistCategory;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Collection;
class ImportArtistChunkJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Collection $rows;
public function __construct(Collection $rows)
{
$this->rows = $rows;
}
public function handle(): void
{
foreach ($this->rows as $index => $row) {
try {
$name = trim($row['歌手姓名'] ?? '');
if (empty($name)) {
continue;
}
if (Artist::where('name', $name)->exists()) {
continue;
}
Artist::create([
'name' => $name,
'category' => ArtistCategory::tryFrom(trim($row['歌手分類'] ?? '未定義')) ?? ArtistCategory::Unset,
'enable' => trim($row['狀態'] ?? 1),
]);
} catch (\Throwable $e) {
\Log::error("Row {$index} failed: {$e->getMessage()}", [
'row' => $row,
'trace' => $e->getTraceAsString()
]);
}
}
}
}