303 lines
9.8 KiB
PHP
303 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Maatwebsite\Excel\Row;
|
|
use Maatwebsite\Excel\Concerns\OnEachRow;
|
|
use Maatwebsite\Excel\Events\AfterImport;
|
|
use Maatwebsite\Excel\Events\BeforeImport;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Maatwebsite\Excel\Concerns\WithChunkReading;
|
|
use Maatwebsite\Excel\Concerns\WithBatchInserts;
|
|
use Maatwebsite\Excel\Concerns\RemembersChunkOffset;
|
|
use Maatwebsite\Excel\Concerns\WithProgressBar;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Maatwebsite\Excel\Concerns\Importable;
|
|
use Carbon\Carbon;
|
|
use Cache;
|
|
use \PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
|
|
class ImportExcel implements
|
|
ToCollection,
|
|
WithProgressBar,
|
|
WithChunkReading,
|
|
ShouldQueue,
|
|
WithBatchInserts,
|
|
WithEvents
|
|
{
|
|
public $tableName;
|
|
use RemembersChunkOffset;
|
|
use Importable;
|
|
public $firstRow = null;
|
|
public $columnTypes = [];
|
|
public $id;
|
|
|
|
|
|
|
|
public function __construct(string $tableName, int $id)
|
|
{
|
|
$this->tableName = $tableName;
|
|
$this->id = $id;
|
|
}
|
|
|
|
|
|
|
|
public function unsetColumns($refactoringRow) {
|
|
$unsetColumns = [
|
|
// 'id'
|
|
|
|
];
|
|
foreach($unsetColumns AS $unsetColumn) {
|
|
unset($refactoringRow[$unsetColumn]);
|
|
}
|
|
return $refactoringRow;
|
|
}
|
|
|
|
public function getNameAndBirthToUserId($welderName, $welderNameRu, $yearOfBirth) {
|
|
//dump($yearOfBirth);
|
|
$user = db("users")
|
|
->where("name", $welderName)
|
|
->where("name_ru", $welderNameRu)
|
|
// ->where("date_of_birth", $yearOfBirth)
|
|
->first();
|
|
//dd($user);
|
|
if($user) {
|
|
return $user->id;
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
BeforeImport::class => function (BeforeImport $event) {
|
|
$totalRows = $event->getReader()->getTotalRows();
|
|
|
|
if (filled($totalRows)) {
|
|
cache()->forever("total_rows_{$this->id}", array_values($totalRows)[0]);
|
|
cache()->forever("start_date_{$this->id}", now()->unix());
|
|
}
|
|
},
|
|
AfterImport::class => function (AfterImport $event) {
|
|
cache(["end_date_{$this->id}" => now()], now()->addMinute());
|
|
cache()->forget("total_rows_{$this->id}");
|
|
cache()->forget("start_date_{$this->id}");
|
|
cache()->forget("current_row_{$this->id}");
|
|
},
|
|
];
|
|
}
|
|
|
|
public function onRow(Row $row)
|
|
{
|
|
$rowIndex = $row->getIndex();
|
|
$row = array_map('trim', $row->toArray());
|
|
cache()->forever("current_row_{$this->id}", $rowIndex);
|
|
}
|
|
|
|
/**
|
|
* @param Collection $collection
|
|
*/
|
|
public function collection(Collection $collection)
|
|
{
|
|
set_time_limit(-1);
|
|
DB::beginTransaction();
|
|
try {
|
|
$chunkOffset = $this->getChunkOffset();
|
|
$insertArray = [];
|
|
|
|
$totalRowCount = count($collection);
|
|
|
|
|
|
$excelPrefix = "excel_process";
|
|
$excelLogPath = "$excelPrefix.txt";
|
|
|
|
$excelColumnPrefix = $this->tableName . "_excel_columns";
|
|
$excelColumnTypesPrefix = $this->tableName . "_excel_column_types";
|
|
if(!Cache::has($excelColumnPrefix)) {
|
|
$firstRow = $collection[0];
|
|
$this->firstRow = $firstRow;
|
|
Cache::put($excelColumnPrefix, $firstRow);
|
|
$otherRow = $collection;
|
|
|
|
|
|
$data = [];
|
|
|
|
foreach($firstRow AS $column) {
|
|
if(!is_null($column)) {
|
|
$columnType = table_column_type($this->tableName, $column);
|
|
$columnTypes[$column] = $columnType;
|
|
}
|
|
|
|
}
|
|
Cache::put($excelColumnTypesPrefix, $columnTypes);
|
|
|
|
$this->columnTypes = $columnTypes;
|
|
} else {
|
|
$otherRow = $collection;
|
|
$firstRow = Cache::get($excelColumnPrefix);//$this->firstRow;
|
|
$columnTypes = Cache::get($excelColumnTypesPrefix);//$this->columnTypes;
|
|
}
|
|
unset($otherRow[0]);
|
|
|
|
|
|
|
|
$processRowCount=0;
|
|
$error = false;
|
|
$added = 0;
|
|
$updated = 0;
|
|
$updateOrAdded = 0;
|
|
|
|
foreach($otherRow AS $row) {
|
|
|
|
if($this->tableName== "naks_welders") {
|
|
|
|
if(is_null($row[1])) {
|
|
$getUserId = $this->getNameAndBirthToUserId($row[3], $row[2], $row[4]);
|
|
if(!is_null($getUserId)) {
|
|
$row[1] = $getUserId;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
$columnKey = 0;
|
|
$refactoringRow = [];
|
|
foreach($firstRow AS $column) {
|
|
|
|
if($column!="") {
|
|
$columnType = $columnTypes[$column];
|
|
|
|
if($columnType=="date") {
|
|
|
|
if($row[$columnKey] == "1970-01-01") {
|
|
$refactoringRow[$column] = null;
|
|
} else {
|
|
|
|
try {
|
|
if(!is_null($row[$columnKey])) {
|
|
$refactoringRow[$column] = Date::excelToDateTimeObject($row[$columnKey]);
|
|
} else {
|
|
$refactoringRow[$column] = null;
|
|
}
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$refactoringRow[$column] = null;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
} elseif($columnType=="integer") {
|
|
$refactoringRow[$column] = (int) $row[$columnKey];
|
|
} elseif($columnType=="float") {
|
|
$refactoringRow[$column] = (float) $row[$columnKey];
|
|
} else {
|
|
$refactoringRow[$column] = $row[$columnKey];
|
|
}
|
|
|
|
$columnKey++;
|
|
}
|
|
}
|
|
$refactoringRow['created_at'] = simdi();
|
|
$refactoringRow['updated_at'] = simdi();
|
|
|
|
if(!isset($refactoringRow['id'])) {
|
|
$refactoringRow['id']=="";
|
|
}
|
|
if($refactoringRow['id']=="") {
|
|
unset($refactoringRow['id']);
|
|
// $insertArray[] = $refactoringRow;
|
|
|
|
if($this->tableName == "m_t_o_s") {
|
|
|
|
foreach($insertArray AS $insertItem) {
|
|
$insertItem['created_at'] = simdi();
|
|
$insertItem['updated_at'] = simdi();
|
|
db($this->tableName)->updateOrInsert([
|
|
'line' => $insertItem['line'],
|
|
'component_code_id' => $insertItem['component_code_id'],
|
|
], $insertItem);
|
|
|
|
$updateOrAdded++;
|
|
}
|
|
|
|
} else {
|
|
$updateOrInsert = false;
|
|
if(array_key_exists("iso_number", $refactoringRow)) {
|
|
if(array_key_exists("no_of_the_joint_as_per_as_built_survey", $refactoringRow)) {
|
|
$updateOrInsert = true;
|
|
|
|
}
|
|
}
|
|
|
|
if($updateOrInsert) {
|
|
db($this->tableName)->updateOrInsert([
|
|
'no_of_the_joint_as_per_as_built_survey' => $refactoringRow['no_of_the_joint_as_per_as_built_survey'],
|
|
'iso_number' => $refactoringRow['iso_number'],
|
|
],
|
|
$refactoringRow);
|
|
|
|
$updateOrAdded++;
|
|
} else {
|
|
db($this->tableName)->insert($refactoringRow);
|
|
$added++;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
$updateOrInsertData = $this->unsetColumns($refactoringRow);
|
|
db($this->tableName)->updateOrInsert([
|
|
'id' => $refactoringRow['id']
|
|
], $updateOrInsertData);
|
|
$updateOrAdded++;
|
|
|
|
}
|
|
|
|
$processRowCount++;
|
|
}
|
|
|
|
|
|
DB::commit();
|
|
} catch (\Throwable $th) {
|
|
DB::rollback();
|
|
$error = $firstRow . $th->getMessage();
|
|
Cache::forget($excelColumnPrefix);
|
|
}
|
|
|
|
$response = [
|
|
'processRow' => $processRowCount,
|
|
'added' => $added,
|
|
'updateOrAdded' => $updateOrAdded,
|
|
'error' => $error
|
|
];
|
|
Cache::forget($excelColumnPrefix);
|
|
|
|
echo json_encode_tr($response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public function chunkSize(): int
|
|
{
|
|
return 100000;
|
|
}
|
|
public function batchSize(): int
|
|
{
|
|
return 10000;
|
|
}
|
|
}
|