67 lines
2.1 KiB
PHP
67 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\User;
|
|
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;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ImportUserChunkJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected Collection $rows;
|
|
protected String $id;
|
|
|
|
public function __construct(Collection $rows,String $id)
|
|
{
|
|
$this->rows = $rows;
|
|
$this->id = $id;
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
Log::warning('匯入啟動', [
|
|
'model' => "ImportUserChunkJob",
|
|
'rows_id' =>$this->id,
|
|
]);
|
|
$now = now();
|
|
foreach ($this->rows as $index => $row) {
|
|
try {
|
|
$name = $this->normalizeName($row['歌手姓名'] ?? '');
|
|
|
|
if (empty($name) || User::where('name', $name)->exists()) {
|
|
continue;
|
|
}
|
|
// 準備 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),
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
];
|
|
} catch (\Throwable $e) {
|
|
\Log::error("Row {$index} failed: {$e->getMessage()}", [
|
|
'row' => $row,
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
}
|
|
}
|
|
User::insert($toInsert);
|
|
}
|
|
|
|
public function normalizeName(?string $str): string
|
|
{
|
|
return strtoupper(mb_convert_kana(trim($str ?? ''), 'as'));
|
|
}
|
|
} |