102 lines
3.7 KiB
PHP
102 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
use App\Models\Song;
|
||
use App\Models\Artist;
|
||
use App\Models\SongCategory;
|
||
use App\Enums\ArtistCategory;
|
||
use App\Enums\SongLanguageType;
|
||
use App\Enums\SongSituation;
|
||
|
||
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 ImportSongChunkJob implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
protected Collection $rows;
|
||
protected array $categoryMap = [];
|
||
|
||
public function __construct(Collection $rows)
|
||
{
|
||
$this->rows = $rows;
|
||
$this->categoryMap = SongCategory::pluck('id', 'code')->toArray();
|
||
}
|
||
|
||
public function handle(): void
|
||
{
|
||
foreach ($this->rows as $index => $row) {
|
||
$songId = trim($row['編號'] ?? '');
|
||
|
||
if (!$songId) {
|
||
continue;
|
||
}
|
||
|
||
// 改為即時查詢是否已有此編號
|
||
if (Song::where('id', $songId)->exists()) {
|
||
continue;
|
||
}
|
||
|
||
try {
|
||
// 準備 song 資料
|
||
$song = new Song([
|
||
'id' => $songId,
|
||
'name' => trim($row['歌名'] ?? ''),
|
||
'adddate' => trim($row['日期'] ?? null),
|
||
'filename' => trim($row['檔名'] ?? ''),
|
||
'language_type' => SongLanguageType::tryFrom(trim($row['語別'] ?? '')) ?? SongLanguageType::Unset,
|
||
'db_change' => trim($row['kk2'] ?? 0),//分貝增減
|
||
'vocal' => trim($row['kk6'] ?? 0),//人聲
|
||
'situation' => SongSituation::tryFrom(trim($row['kk7'] ?? '')) ?? SongSituation::Unset,//情境
|
||
'copyright01' => trim($row['版權01'] ?? ''),
|
||
'copyright02' => trim($row['版權02'] ?? ''),
|
||
'note01' => trim($row['版權03'] ?? ''),
|
||
'note02' => trim($row['版權04'] ?? ''),
|
||
'note03' => trim($row['版權05'] ?? ''),
|
||
'note04' => trim($row['版權06'] ?? ''),
|
||
'enable' => trim($row['狀態'] ?? 1),
|
||
'song_counts' => trim($row['點播次數'] ?? 0),
|
||
]);
|
||
|
||
$song->save();
|
||
|
||
// 處理關聯 - 歌手
|
||
$artistIds = [];
|
||
foreach (['歌星A', '歌星B'] as $key) {
|
||
$artistName = trim($row[$key] ?? '');
|
||
if ($artistName === '') continue;
|
||
|
||
// 若是歌星B,且與歌星A相同,則跳過
|
||
if ($key === '歌星B' && $artistName === trim($row['歌星A'] ?? '')) continue;
|
||
|
||
$artistIds[] = $this->getOrCreateArtistId($artistName);
|
||
}
|
||
$song->artists()->sync($artistIds);
|
||
|
||
// 分類處理(多個用 , 分隔)
|
||
if (!empty($row['分類'])) {
|
||
$categoryIds = [];
|
||
$codes = explode(',', $row['分類']);
|
||
foreach ($codes as $code) {
|
||
$code = trim($code);
|
||
if (isset($this->categoryMap[$code])) {
|
||
$categoryIds[] = $this->categoryMap[$code];
|
||
}
|
||
}
|
||
$song->categories()->sync($categoryIds);
|
||
}
|
||
} catch (\Throwable $e) {
|
||
\Log::error("Row {$index} failed: {$e->getMessage()}", [
|
||
'row' => $row,
|
||
'trace' => $e->getTraceAsString()
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
} |