45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Maatwebsite\Excel\Concerns\WithChunkReading;
|
|
use Illuminate\Support\Collection;
|
|
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Jobs\ImportUserChunkJob;
|
|
|
|
class DataImport implements ToCollection, WithHeadingRow, WithChunkReading
|
|
{
|
|
protected int $con=0;
|
|
protected string $modelName;
|
|
public function __construct(string $modelName)
|
|
{
|
|
HeadingRowFormatter::default('none');
|
|
$this->modelName= $modelName;
|
|
}
|
|
public function collection(Collection $rows)
|
|
{
|
|
|
|
Log::warning('匯入啟動', [
|
|
'model' => $this->modelName,
|
|
'rows_id' =>++$this->con,
|
|
'rows_count' => $rows->count()
|
|
]);
|
|
if($this->modelName=='User'){
|
|
ImportUserChunkJob::dispatch($rows,$this->con);
|
|
}else{
|
|
Log::warning('未知的 modelName', ['model' => $this->modelName]);
|
|
}
|
|
}
|
|
public function chunkSize(): int
|
|
{
|
|
return 1000;
|
|
}
|
|
|
|
public function headingRow(): int
|
|
{
|
|
return 1;
|
|
}
|
|
} |