2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
namespace Modules\Importer\Services\Importers;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
use App\Models\Airport;
|
2019-12-02 22:57:35 +08:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Modules\Importer\Services\BaseImporter;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
class AirportImporter extends BaseImporter
|
|
|
|
{
|
2019-12-02 22:57:35 +08:00
|
|
|
protected $table = 'airports';
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
public function run($start = 0)
|
|
|
|
{
|
|
|
|
$this->comment('--- AIRPORT IMPORT ---');
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
$fields = [
|
|
|
|
'icao',
|
|
|
|
'name',
|
|
|
|
'country',
|
|
|
|
'lat',
|
|
|
|
'lng',
|
|
|
|
'hub',
|
|
|
|
];
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
$count = 0;
|
2019-12-02 22:57:35 +08:00
|
|
|
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
|
2019-11-27 22:19:20 +08:00
|
|
|
$attrs = [
|
|
|
|
'id' => trim($row->icao),
|
|
|
|
'icao' => trim($row->icao),
|
|
|
|
'name' => $row->name,
|
|
|
|
'country' => $row->country,
|
|
|
|
'lat' => $row->lat,
|
|
|
|
'lon' => $row->lng,
|
|
|
|
'hub' => $row->hub,
|
|
|
|
];
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
$w = ['id' => $attrs['id']];
|
|
|
|
//$airport = Airport::updateOrCreate($w, $attrs);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$airport = Airport::create(array_merge($w, $attrs));
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
$sqlState = $e->errorInfo[0];
|
|
|
|
$errorCode = $e->errorInfo[1];
|
|
|
|
if ($sqlState === '23000' && $errorCode === 1062) {
|
|
|
|
Log::info('Found duplicate for '.$row->icao.', ignoring');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
if ($airport->wasRecentlyCreated) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Imported '.$count.' airports');
|
|
|
|
}
|
|
|
|
}
|