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',
|
2020-10-21 03:25:13 +08:00
|
|
|
'ground_handling_cost',
|
|
|
|
'fuel_jeta_cost',
|
2019-12-02 22:57:35 +08:00
|
|
|
];
|
|
|
|
|
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) {
|
2020-10-21 03:25:13 +08:00
|
|
|
$ground_handling_cost = (float) $row->ground_handling_cost;
|
|
|
|
$fuel_jetA_cost = (float) $row->fuel_jeta_cost;
|
|
|
|
|
2021-03-20 04:42:36 +08:00
|
|
|
if (empty($ground_handling_cost)) {
|
|
|
|
$ground_handling_cost = 0;
|
2020-10-21 03:25:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-20 04:42:36 +08:00
|
|
|
if (empty($fuel_jetA_cost)) {
|
|
|
|
$fuel_jetA_cost = 0;
|
2020-10-21 03:25:13 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
$attrs = [
|
2020-10-21 03:25:13 +08:00
|
|
|
'id' => trim($row->icao),
|
|
|
|
'icao' => trim($row->icao),
|
|
|
|
'name' => $row->name,
|
|
|
|
'country' => $row->country,
|
|
|
|
'lat' => $row->lat,
|
|
|
|
'lon' => $row->lng,
|
|
|
|
'hub' => $row->hub,
|
|
|
|
'ground_handling_cost' => $ground_handling_cost,
|
|
|
|
'fuel_jeta_cost' => $fuel_jetA_cost,
|
2019-11-27 22:19:20 +08:00
|
|
|
];
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|