From e748b7ff3d7ce450f847742b573b511b183bab75 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Tue, 6 May 2025 18:09:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BB=99=E5=85=A5=E5=A4=A7=E9=87=8F=E6=AD=8C?= =?UTF-8?q?=E6=89=8B=E6=9C=83=E6=9C=89=E5=95=8F=E9=A1=8C=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=2020250506?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 6148 bytes app/.DS_Store | Bin 0 -> 6148 bytes app/Enums/.DS_Store | Bin 0 -> 6148 bytes app/Imports/ArtistDataImport.php | 75 ++++++++++++------ app/Jobs/ImportArtistJob.php | 42 ++++++++++ app/Livewire/Admin/ArtistImportData.php | 18 +++-- .../admin/artist-import-data.blade.php | 39 ++++++--- 開發手冊.ini | 13 ++- 8 files changed, 139 insertions(+), 48 deletions(-) create mode 100644 app/.DS_Store create mode 100644 app/Enums/.DS_Store create mode 100644 app/Jobs/ImportArtistJob.php diff --git a/.DS_Store b/.DS_Store index 6549c6a504cfc2cd03d4d068b774ecb94962d052..7df85645a3b8cc461f760d01d828cb61dc2536b2 100644 GIT binary patch delta 126 zcmZoMXfc=|#>B)qu~2NHo}vH?0|Nsi1A_oVa!yiyeh%YAeTmHiOj8&QK@!Xii3|k{ z1u4Z)NuUr1Lkdu^1c;M>xR@aoF1eY9@igmZb`E|Hpy`_#nZ7em<`=Q#02&TN3=EqC IM7A&k0Me-&FaQ7m delta 84 zcmZoMXfc=|#>B`mu~2NHo}wrd0|Nsi1A_nqLq0=MQh9MfQcix-#KPs14MbQr+cTYD l-PmBjxS5@Up9838vmx_$=E?jbmK;D`AZ<*W14Onk0{~-;6RH3J diff --git a/app/.DS_Store b/app/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a5cbae3cd00bb764c3a2918bf7e78df797a57537 GIT binary patch literal 6148 zcmeHKyH3ME5S)d8BBe=5dB4CPSW!|^h z(@o*^0PX~u+0f;Mx z!#Iy#g4jGj>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0failCount++; - return null; - } + $toInsert = []; - try { - Artist::firstOrCreate( - ['name' => $name], - ['category' => ArtistCategory::tryFrom($category) ?? ArtistCategory::Unset], - ['enable' => $enable], - ); - $this->successCount++; - } catch (\Throwable $e) { - $this->failCount++; + foreach ($rows as $row) { + $name=trim($row['歌手姓名'] ?? ''); + if (empty($name)) continue; + + // 字元處理 + $simplified = ChineseNameConverter::convertToSimplified($name); + if (!array_key_exists('歌手注音', $row)) { + $phoneticAbbr = ChineseNameConverter::getKTVZhuyinAbbr($simplified); + } else { + $phoneticAbbr = trim($row['歌手注音']); + } + $pinyinAbbr = ChineseNameConverter::getKTVPinyinAbbr($simplified); + if (!array_key_exists('歌手注音', $row)) { + $chars = preg_split('//u', $name, -1, PREG_SPLIT_NO_EMPTY); + $firstChar = $chars[0] ?? null; + $strokesAbbr = $firstChar ? ChineseStrokesConverter::getStrokes($firstChar) : null; + } else { + $strokesAbbr = trim($row['歌手筆畫']); + } + // 準備 song 資料 + $toInsert[] = [ + 'name' => $name, + 'category' => ArtistCategory::tryFrom(trim($row['歌手分類'] ?? '未定義')) ?? ArtistCategory::Unset, + 'simplified' => $simplified, + 'phonetic_abbr' => $phoneticAbbr, + 'pinyin_abbr' => $pinyinAbbr, + 'strokes_abbr' => $strokesAbbr, + 'enable' =>trim($row['狀態'] ?? 1) + ]; } - - return null; + Artist::insert($toInsert); } + + public function chunkSize(): int + { + return 100; + } + public function batchSize(): int + { + return 100; + } + public function headingRow(): int { return 1; diff --git a/app/Jobs/ImportArtistJob.php b/app/Jobs/ImportArtistJob.php new file mode 100644 index 0000000..1ed62af --- /dev/null +++ b/app/Jobs/ImportArtistJob.php @@ -0,0 +1,42 @@ +filePath = $filePath; + } + + /** + * Execute the job. + */ + public function handle(): void + { + Excel::import(new ArtistDataImport, $this->filePath); + // 匯入完成後刪除檔案 + if (Storage::exists($this->filePath)) { + Storage::delete($this->filePath); + } + } +} diff --git a/app/Livewire/Admin/ArtistImportData.php b/app/Livewire/Admin/ArtistImportData.php index d14b4d4..9b94084 100644 --- a/app/Livewire/Admin/ArtistImportData.php +++ b/app/Livewire/Admin/ArtistImportData.php @@ -5,8 +5,8 @@ namespace App\Livewire\Admin; use Illuminate\Support\Facades\Auth; use Livewire\Component; use Livewire\WithFileUploads; -use Maatwebsite\Excel\Facades\Excel; -use App\Imports\ArtistDataImport; +use App\Jobs\ImportArtistJob; +use Illuminate\Support\Facades\Storage; class ArtistImportData extends Component { @@ -41,13 +41,15 @@ class ArtistImportData extends Component 'file' => 'required|file|mimes:csv,xlsx,xls' ]); if ($this->canCreate) { - $import = new ArtistDataImport(); - Excel::import($import, $this->file); - $success = $import->successCount; - $fail = $import->failCount; + // 儲存檔案至 storage + $path = $this->file->storeAs('imports', uniqid() . '_' . $this->file->getClientOriginalName()); + + // 丟到 queue 執行 + ImportArtistJob::dispatch($path); + $this->reset('file'); - $this->showModal =false; - session()->flash('message', '匯入完成:成功 $success 筆,失敗 $fail 筆。'); + $this->showModal = false; + session()->flash('message', '已排入背景匯入作業,請稍候查看結果'); } } diff --git a/resources/views/livewire/admin/artist-import-data.blade.php b/resources/views/livewire/admin/artist-import-data.blade.php index ae378cf..5793060 100644 --- a/resources/views/livewire/admin/artist-import-data.blade.php +++ b/resources/views/livewire/admin/artist-import-data.blade.php @@ -3,33 +3,48 @@ {{-- 說明區塊 --}}

匯入格式說明

-

請依下列表格格式準備 CSV 檔案:

+

請依下列表格格式準備 Excel 或 CSV 檔案:

-
- +
+
- - + + + - - + + + + + + + + + + - + + + + + + + + + +
類別名稱欄位名稱說明範例
某男歌手姓名歌手姓名唯一田馥甄
歌手注音歌手注音第一字碼ㄊㄈㄓ
歌手分類男、女、團、外、其他 某女
狀態是否啟用(1:啟用,0:停用)1
歌手筆畫字首筆劃5
- -

- ※ 類別欄位僅可使用:其他。 -

+ {{-- 檔案上傳 --}} diff --git a/開發手冊.ini b/開發手冊.ini index aeb9722..cb71e1a 100644 --- a/開發手冊.ini +++ b/開發手冊.ini @@ -82,6 +82,13 @@ php artisan make:livewire Admin/ArtistForm php artisan make:livewire Admin/SongForm -php artisan make:model Branches -m -php artisan make:model Rooms -m -php artisan make:model RoomStatusLogs -m \ No newline at end of file +php artisan make:model OperationLog -m + +php artisan make:observer GenericObserver --model=OperationLog + +php artisan make:model Branch -m +php artisan make:model Room -m +php artisan make:model RoomStatusLog -m + +php artisan make:observer RoomObserver --model=Room +