42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Models\ImportJob;
|
|
use App\Models\ImportJobLog;
|
|
|
|
class ImportService
|
|
{
|
|
protected $driver;
|
|
|
|
public function import(ImportJob $job)
|
|
{
|
|
if ($job->status == 1) {
|
|
$this->driver = new $job->type();
|
|
$res = $this->driver->readFileByUrl($job->file);
|
|
// dd($res);
|
|
if ($res) {
|
|
$job->update([
|
|
'status'=>2,
|
|
'success' => $res['success']??0,
|
|
'fails'=> $res['fails']??0,
|
|
]);
|
|
}
|
|
if (isset($res['errors']) && count($res['errors']) > 0) {
|
|
$this->createErrorLogs($job, $res['errors']);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function createErrorLogs(ImportJob $job, array $errors)
|
|
{
|
|
ImportJobLog::insert(array_map(function ($value) use ($job) {
|
|
return array_merge($value, [
|
|
'job_id'=>$job->id,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}, $errors));
|
|
}
|
|
}
|