diff --git a/app/Imports/SongDataImport.php b/app/Imports/SongDataImport.php index ab75b46..72305ee 100644 --- a/app/Imports/SongDataImport.php +++ b/app/Imports/SongDataImport.php @@ -33,54 +33,18 @@ class SongDataImport implements ToCollection, WithHeadingRow, WithChunkReading public function collection(Collection $rows) { - // 建立現有歌手名稱的查找表,避免重複建立 - static $existingIDs = null; - - if ($existingIDs === null) { - $existingIDs = array_flip(array_map('trim', Song::pluck('id')->all())); - } - - $ToInsert = []; - $artistMap = []; // [song_id => [artist_id]] - $categoryMap = []; // [song_id => [category_id]] - foreach ($rows as $row) { $songId = trim($row['編號'] ?? ''); if (!$songId) { continue; } - // 若資料庫已有該編號,跳過 - if (isset($existingIDs[$songId])) continue; - - // 字元處理 - $songName=trim($row['歌名'] ?? ''); - $simplified=ChineseNameConverter::convertToSimplified($songName);// 繁體轉簡體 - if (!$row->has('注音')) { - $phoneticAbbr = ChineseNameConverter::getKTVZhuyinAbbr($simplified);// 注音符號 - } else { - $phoneticAbbr = trim($row['注音']); + // 改為即時查詢是否已有此編號 + if (Song::where('id', $songId)->exists()) { + continue; } - if (!$row->has('拼音')) { - $pinyinAbbr = ChineseNameConverter::getKTVPinyinAbbr($simplified);// 拼音首字母 - } else { - $pinyinAbbr = trim($row['拼音']); - } - if (!$row->has('kk3')) {//歌名第一個字筆畫 - $chars = preg_split('//u', $songName, -1, PREG_SPLIT_NO_EMPTY); - $firstChar = $chars[0] ?? null; - $strokesAbbr=$firstChar ? ChineseStrokesConverter::getStrokes($firstChar) : null; - } else { - $strokesAbbr=trim($row['kk3'] ?? 0); - } - if (!$row->has('kk4')) {//歌名字數 - $songNumber = mb_strlen($songName, 'UTF-8'); - } else { - $songNumber=trim($row['kk4'] ?? 0); - } - // 準備 song 資料 - $ToInsert[] = [ + $song = new Song([ 'id' => $songId, 'name' => $songName, 'adddate' => trim($row['日期'] ?? null), @@ -96,16 +60,13 @@ class SongDataImport implements ToCollection, WithHeadingRow, WithChunkReading 'note03' => trim($row['版權05'] ?? ''), 'note04' => trim($row['版權06'] ?? ''), 'enable' => trim($row['狀態'] ?? 1), - - 'simplified' => $simplified, - 'phonetic_abbr' => $phoneticAbbr, - 'pinyin_abbr' => $pinyinAbbr, - 'strokes_abbr' => $strokesAbbr, - 'song_number' => $songNumber, - 'song_counts' => trim($row['點播次數'] ?? 0), - ]; + ]); + $song->save(); + + // 處理關聯 - 歌手 + $artistIds = []; foreach (['歌星A', '歌星B'] as $key) { $artistName = trim($row[$key] ?? ''); if ($artistName === '') continue; @@ -113,39 +74,20 @@ class SongDataImport implements ToCollection, WithHeadingRow, WithChunkReading // 若是歌星B,且與歌星A相同,則跳過 if ($key === '歌星B' && $artistName === trim($row['歌星A'] ?? '')) continue; - $artistId = $this->getOrCreateArtistId($artistName); - $artistMap[$songId][] = $artistId; + $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])) { - $categoryMap[$songId][] = $this->categoryMap[$code]; + $categoryIds[] = $this->categoryMap[$code]; } } - } - - // 新增到快取,避免後面重複匯入 - $existingIDs[$songId] = true; - } - - // 寫入資料庫 - Song::insert($ToInsert); - - // 同步關聯(建議可用事件或批次處理) - foreach ($artistMap as $songId => $artistIds) { - $song = Song::find($songId); - if ($song) { - $song->artists()->sync($artistIds); - } - } - - foreach ($categoryMap as $songId => $categoryIds) { - $song = Song::find($songId); - if ($song) { $song->categories()->sync($categoryIds); } }