Created
October 4, 2018 10:11
-
-
Save mishazapl/1428d86821510ef9b0b892dc3f04d672 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 11.08.18 | |
* Time: 12:48 | |
*/ | |
namespace App\Services\Excel\Import\Price; | |
use App\Services\ImportFactory; | |
use Illuminate\Http\UploadedFile; | |
use Jenssegers\Mongodb\Eloquent\Model; | |
class ExecuteStrategy extends ImportFactory | |
{ | |
/** | |
* @param UploadedFile $file | |
* @param string $providerId | |
* @param Model $model | |
* @param int $countRow | |
* @return mixed|void | |
*/ | |
protected function excel(UploadedFile $file, string $providerId, Model $model): void | |
{ | |
$class = 'App\Services\Excel\Import\Price\\'.class_basename($model); | |
$excel = new $class($model); | |
$excel->start($file, $providerId); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 14.08.18 | |
* Time: 14:10 | |
*/ | |
namespace App\Services\Excel\Import\Price; | |
use App\Services\Contracts\HandBook; | |
use App\Services\Contracts\Import; | |
use App\Services\HandBookService; | |
use App\Services\Helpers\Facades\Helper; | |
use App\Services\Registry; | |
use Illuminate\Http\UploadedFile; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\Facades\Storage; | |
use Jenssegers\Mongodb\Eloquent\Model; | |
abstract class ImportExcel implements Import, HandBook | |
{ | |
/** | |
* @var boolean $save | |
* @var integer $providerId | |
* @var Model $model | |
* @var integer $countRow | |
*/ | |
protected $save = false; | |
protected $providerId; | |
protected $model; | |
protected $countRow; | |
protected $vars; | |
/** | |
* ImportExcel constructor. | |
* @param Model $model | |
*/ | |
public function __construct(Model $model) | |
{ | |
$this->model = $model; | |
} | |
/** | |
* Вызывает метод export, для валидации, после | |
* если ошибок нету вызывает снова меняя ключ на true | |
* и метод export сохраняет. | |
* | |
* @param UploadedFile $file | |
* @param string $providerId | |
* @throws \Exception | |
*/ | |
public function start(UploadedFile $file, string $providerId): void | |
{ | |
$this->providerId = $providerId; | |
$this->export($file); | |
$errors = Registry::get('import-error'); | |
if (!empty($errors)) { | |
$errors['file'] = $file->getClientOriginalName(); | |
$randomArt = str_random(10) . random_int(10, 100) . str_random(10); | |
$this->model->fill([ | |
'system_article' => $randomArt, | |
'provider_article' => $randomArt, | |
'errors' => $errors | |
]) | |
->save(); | |
return; | |
} | |
$this->save = true; | |
$this->export($file); | |
} | |
/** | |
* Считывает файл и вызывает метод для валидации каждой строки. | |
* если ключ установлен в true сохраняет. | |
* | |
* @param $file | |
* @return array | |
*/ | |
public function export(UploadedFile $file): void | |
{ | |
/** | |
* Generate | |
* | |
* system_article | |
* | |
* | |
*/ | |
\Maatwebsite\Excel\Facades\Excel::filter('chunk') | |
->load($file->getRealPath()) | |
->chunk(100, function ($reader) { | |
if (!$this->save) { | |
foreach ($reader as $num => $row) { | |
$this->validateRow($row, $num + 2); | |
} | |
} else { | |
$materials = collect(); | |
foreach ($reader as $row) { | |
$materials->push(new \App\Models\Product\Material($row->combine($this->model::getExportCombineFillable()) | |
->flip()->merge([ | |
'system_article' => Helper::articleGenerate(), | |
'provider_id' => $this->providerId, | |
'status' => 0 | |
])->toArray())); | |
} | |
$this->save($materials->toArray()); | |
$this->handBook($materials); | |
} | |
}, false); | |
} | |
/** | |
* Валидирует каждую строку Collection[] | |
* | |
* @param Collection $row | |
* @param int $num | |
* @return void | |
*/ | |
public function validateRow(Collection $row, int $num): void | |
{ | |
$row = $row->reject(function ($value) { | |
return empty($value); | |
}); | |
if ($row->count() < $this->countRow) { | |
$error = Registry::get('import-error'); | |
array_push($error, "Строка $num содержит меньше {$this->countRow} полей или одна из ячеек была пустой."); | |
Registry::set('import-error', $error); | |
} | |
} | |
/** | |
* @param array $data | |
*/ | |
public function save(array $data): void | |
{ | |
foreach ($data as $item) { | |
$this->model->where('provider_article', $item['provider_article']) | |
->update($item, ['upsert' => true]); | |
} | |
} | |
public function handBook(Collection $materials): void | |
{ | |
$data = collect(); | |
$save = collect(); | |
$data->push($materials->map(function ($item) { | |
return array_only($item->toArray(), $this->model::getHandBookFillable()); | |
})); | |
foreach ($this->model::getHandBookFillable() as $fillable) { | |
foreach ($data[0] as $key => $arr) { | |
$save->push(['handbook' => | |
[ | |
'provider_id' => $this->providerId, | |
'modelName' => strtolower(class_basename($this->model)), | |
'key' => $fillable, | |
'value' => $arr[$fillable] | |
] | |
]); | |
} | |
} | |
HandBookService::save($save->unique()->values()->toArray()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 14.08.18 | |
* Time: 15:14 | |
*/ | |
namespace App\Services\Excel\Import\Price; | |
class Material extends ImportExcel | |
{ | |
protected $countRow = 20; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 14.08.18 | |
* Time: 15:14 | |
*/ | |
namespace App\Services\Excel\Import\Price; | |
class Product extends ImportExcel | |
{ | |
protected $countRow = 15; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment