2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
namespace App\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-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;
|
2020-02-24 08:48:28 +08:00
|
|
|
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
|
|
|
|
foreach ($rows 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');
|
2020-10-19 22:10:28 +08:00
|
|
|
return true;
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
}
|