KTV/app/Imports/SongDataImport.php

119 lines
4.0 KiB
PHP
Raw Normal View History

2025-05-05 16:54:21 +08:00
<?php
namespace App\Imports;
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 App\Helpers\ChineseNameConverter;
use App\Helpers\ChineseStrokesConverter;
2025-05-05 16:54:21 +08:00
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
2025-05-05 16:54:21 +08:00
class SongDataImport implements ToCollection, WithHeadingRow, WithChunkReading
{
protected array $artistCache = [];
protected array $categoryMap = [];
public function __construct()
{
// 關閉 heading row 格式化
HeadingRowFormatter::default('none');
2025-05-05 16:54:21 +08:00
// 快取分類代碼對應 ID
$this->categoryMap = SongCategory::pluck('id', 'code')->toArray();
}
public function collection(Collection $rows)
{
foreach ($rows as $row) {
$songId = trim($row['編號'] ?? '');
if (!$songId) {
continue;
}
2025-05-08 14:25:58 +08:00
// 改為即時查詢是否已有此編號
if (Song::where('id', $songId)->exists()) {
continue;
}
2025-05-05 16:54:21 +08:00
// 準備 song 資料
2025-05-08 14:25:58 +08:00
$song = new Song([
2025-05-05 16:54:21 +08:00
'id' => $songId,
'name' => $songName,
2025-05-05 16:54:21 +08:00
'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,//情境
2025-05-05 16:54:21 +08:00
'copyright01' => trim($row['版權01'] ?? ''),
'copyright02' => trim($row['版權02'] ?? ''),
'note01' => trim($row['版權03'] ?? ''),
'note02' => trim($row['版權04'] ?? ''),
'note03' => trim($row['版權05'] ?? ''),
'note04' => trim($row['版權06'] ?? ''),
2025-05-05 16:54:21 +08:00
'enable' => trim($row['狀態'] ?? 1),
'song_counts' => trim($row['點播次數'] ?? 0),
2025-05-08 14:25:58 +08:00
]);
$song->save();
2025-05-05 16:54:21 +08:00
2025-05-08 14:25:58 +08:00
// 處理關聯 - 歌手
$artistIds = [];
2025-05-05 16:54:21 +08:00
foreach (['歌星A', '歌星B'] as $key) {
$artistName = trim($row[$key] ?? '');
if ($artistName === '') continue;
// 若是歌星B且與歌星A相同則跳過
if ($key === '歌星B' && $artistName === trim($row['歌星A'] ?? '')) continue;
2025-05-08 14:25:58 +08:00
$artistIds[] = $this->getOrCreateArtistId($artistName);
2025-05-05 16:54:21 +08:00
}
2025-05-08 14:25:58 +08:00
$song->artists()->sync($artistIds);
2025-05-05 16:54:21 +08:00
// 分類處理(多個用 , 分隔)
if (!empty($row['分類'])) {
2025-05-08 14:25:58 +08:00
$categoryIds = [];
2025-05-05 16:54:21 +08:00
$codes = explode(',', $row['分類']);
foreach ($codes as $code) {
$code = trim($code);
if (isset($this->categoryMap[$code])) {
2025-05-08 14:25:58 +08:00
$categoryIds[] = $this->categoryMap[$code];
2025-05-05 16:54:21 +08:00
}
}
$song->categories()->sync($categoryIds);
}
}
}
protected function getOrCreateArtistId(string $name): int
{
if (isset($this->artistCache[$name])) {
return $this->artistCache[$name];
}
$artist = Artist::firstOrCreate(
['name' => $name],
['category' => ArtistCategory::Unset]
);
return $this->artistCache[$name] = $artist->id;
}
public function chunkSize(): int
{
return 100;
2025-05-05 16:54:21 +08:00
}
public function headingRow(): int
{
return 1;
}
}