調整 滙入 歌手 中有全形或 英文小寫問題
20250512
This commit is contained in:
parent
7092566c8d
commit
d61eee5d91
@ -48,23 +48,28 @@ class TransferSqliteToMysql extends Command
|
|||||||
$this->info("📦 Transferring table: {$table}");
|
$this->info("📦 Transferring table: {$table}");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 從 SQLite 資料庫讀取資料
|
// 用 cursor 來避免一次性佔用過多記憶體
|
||||||
$records = DB::connection('sqlite')->table($table)->get();
|
$rows = DB::connection('sqlite')->table($table)->cursor();
|
||||||
|
|
||||||
if ($records->isEmpty()) {
|
$buffer = [];
|
||||||
$this->warn("⚠️ Table {$table} has no data.");
|
$count = 0;
|
||||||
continue;
|
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$buffer[] = (array) $row;
|
||||||
|
$count++;
|
||||||
|
|
||||||
|
if ($count % 500 === 0) {
|
||||||
|
DB::connection($mysqlConnection)->table($table)->insert($buffer);
|
||||||
|
$buffer = []; // 清空 buffer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 資料分批處理,避免一次插入過多資料造成效能問題
|
// 插入剩下的資料
|
||||||
$chunks = $records->chunk(500);
|
if (!empty($buffer)) {
|
||||||
foreach ($chunks as $chunk) {
|
DB::connection($mysqlConnection)->table($table)->insert($buffer);
|
||||||
DB::connection($mysqlConnection)->table($table)->insert(
|
|
||||||
$chunk->map(fn ($row) => (array) $row)->toArray()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info("✅ Done: {$table}");
|
$this->info("✅ Done: {$table} ({$count} records)");
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->error("❌ Failed to transfer {$table}: " . $e->getMessage());
|
$this->error("❌ Failed to transfer {$table}: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ class ImportArtistChunkJob implements ShouldQueue
|
|||||||
$now = now();
|
$now = now();
|
||||||
foreach ($this->rows as $index => $row) {
|
foreach ($this->rows as $index => $row) {
|
||||||
try {
|
try {
|
||||||
$name = trim($row['歌手姓名'] ?? '');
|
$name = $this->normalizeName($row['歌手姓名'] ?? '');
|
||||||
|
|
||||||
if (empty($name) || Artist::where('name', $name)->exists()) {
|
if (empty($name) || Artist::where('name', $name)->exists()) {
|
||||||
continue;
|
continue;
|
||||||
@ -79,4 +79,9 @@ class ImportArtistChunkJob implements ShouldQueue
|
|||||||
}
|
}
|
||||||
Artist::insert($toInsert);
|
Artist::insert($toInsert);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function normalizeName(?string $str): string
|
||||||
|
{
|
||||||
|
return strtoupper(mb_convert_kana(trim($str ?? ''), 'as'));
|
||||||
|
}
|
||||||
}
|
}
|
@ -52,7 +52,7 @@ class ImportSongChunkJob implements ShouldQueue
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 字元處理
|
// 字元處理
|
||||||
$songName=trim($row['歌名'] ?? '');
|
$songName=$this->normalizeName($row['歌名'] ?? '');
|
||||||
$simplified=ChineseNameConverter::convertToSimplified($songName);// 繁體轉簡體
|
$simplified=ChineseNameConverter::convertToSimplified($songName);// 繁體轉簡體
|
||||||
if (!$row->has('注音')) {
|
if (!$row->has('注音')) {
|
||||||
$phoneticAbbr = ChineseNameConverter::getKTVZhuyinAbbr($simplified);// 注音符號
|
$phoneticAbbr = ChineseNameConverter::getKTVZhuyinAbbr($simplified);// 注音符號
|
||||||
@ -104,11 +104,11 @@ class ImportSongChunkJob implements ShouldQueue
|
|||||||
// 處理關聯 - 歌手
|
// 處理關聯 - 歌手
|
||||||
$artistIds = [];
|
$artistIds = [];
|
||||||
foreach (['歌星A', '歌星B'] as $key) {
|
foreach (['歌星A', '歌星B'] as $key) {
|
||||||
$artistName = trim($row[$key] ?? '');
|
$artistName = $this->normalizeName($row[$key] ?? '');
|
||||||
if ($artistName === '') continue;
|
if ($artistName === '') continue;
|
||||||
|
|
||||||
// 若是歌星B,且與歌星A相同,則跳過
|
// 若是歌星B,且與歌星A相同,則跳過
|
||||||
if ($key === '歌星B' && $artistName === trim($row['歌星A'] ?? '')) continue;
|
if ($key === '歌星B' && $artistName === $this->normalizeName($row['歌星A'] ?? '')) continue;
|
||||||
|
|
||||||
$artistMap[$songId][] = $this->getOrCreateArtistId($artistName);
|
$artistMap[$songId][] = $this->getOrCreateArtistId($artistName);
|
||||||
}
|
}
|
||||||
@ -160,6 +160,12 @@ class ImportSongChunkJob implements ShouldQueue
|
|||||||
|
|
||||||
return $this->artistCache[$name] = $artist->id;
|
return $this->artistCache[$name] = $artist->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function normalizeName(?string $str): string
|
||||||
|
{
|
||||||
|
return strtoupper(mb_convert_kana(trim($str ?? ''), 'as'));
|
||||||
|
}
|
||||||
|
|
||||||
protected function formatText($value)
|
protected function formatText($value)
|
||||||
{
|
{
|
||||||
if (is_numeric($value) && $value < 1 && $value > 0) {
|
if (is_numeric($value) && $value < 1 && $value > 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user