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\Aircraft;
|
|
|
|
use App\Models\Airline;
|
|
|
|
use App\Models\Subfleet;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Modules\Importer\Services\BaseImporter;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
class AircraftImporter extends BaseImporter
|
|
|
|
{
|
2020-02-24 08:48:28 +08:00
|
|
|
protected $table = 'aircraft';
|
2019-12-02 22:57:35 +08:00
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
public function run($start = 0)
|
|
|
|
{
|
|
|
|
$this->comment('--- AIRCRAFT IMPORT ---');
|
|
|
|
|
2020-03-10 01:40:10 +08:00
|
|
|
$fields = [
|
|
|
|
'id',
|
|
|
|
'icao',
|
|
|
|
'name',
|
|
|
|
'fullname',
|
|
|
|
'registration',
|
|
|
|
'enabled',
|
|
|
|
];
|
|
|
|
|
|
|
|
// See if there is an airline column
|
|
|
|
$columns = $this->db->getColumns($this->table);
|
|
|
|
if (in_array('airline', $columns)) {
|
|
|
|
$fields[] = 'airline';
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2020-03-10 01:40:10 +08:00
|
|
|
if (in_array('location', $columns)) {
|
|
|
|
$fields[] = 'location';
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
$count = 0;
|
2020-03-10 01:40:10 +08:00
|
|
|
$rows = $this->db->readRows($this->table, $this->idField, $start, $fields);
|
2020-02-24 08:48:28 +08:00
|
|
|
foreach ($rows as $row) {
|
2020-03-10 01:40:10 +08:00
|
|
|
$airline_id = null;
|
|
|
|
if (!empty($row->airline)) {
|
|
|
|
$airline_id = $this->idMapper->getMapping('airlines', $row->airline_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$subfleet = $this->getSubfleet($row->icao, $airline_id);
|
|
|
|
$this->info('Subfleet ID is '.$subfleet->id);
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
$where = [
|
|
|
|
'registration' => $row->registration,
|
|
|
|
];
|
|
|
|
|
2020-03-10 01:40:10 +08:00
|
|
|
$cols = [
|
2019-11-27 22:19:20 +08:00
|
|
|
'icao' => $row->icao,
|
2020-03-10 01:40:10 +08:00
|
|
|
'name' => $row->fullname,
|
2019-11-27 22:19:20 +08:00
|
|
|
'subfleet_id' => $subfleet->id,
|
|
|
|
'active' => $row->enabled,
|
2020-03-10 01:40:10 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($row->location)) {
|
|
|
|
$cols['airport_id'] = $row->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
$aircraft = Aircraft::firstOrCreate($where, $cols);
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
$this->idMapper->addMapping('aircraft', $row->id, $aircraft->id);
|
|
|
|
|
|
|
|
if ($aircraft->wasRecentlyCreated) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Imported '.$count.' aircraft');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the subfleet
|
|
|
|
*
|
2020-03-10 01:40:10 +08:00
|
|
|
* @param string $icao ICAO of the subfleet
|
|
|
|
* @param int $airline_id
|
|
|
|
*
|
2019-11-27 22:19:20 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-03-10 01:40:10 +08:00
|
|
|
protected function getSubfleet($icao, $airline_id = null)
|
2019-11-27 22:19:20 +08:00
|
|
|
{
|
2020-03-10 01:40:10 +08:00
|
|
|
if ($airline_id === null) {
|
|
|
|
$airline = Airline::first();
|
|
|
|
$airline_id = $airline->id;
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2020-03-10 01:40:10 +08:00
|
|
|
return Subfleet::firstOrCreate([
|
|
|
|
'airline_id' => $airline_id,
|
|
|
|
'name' => $icao,
|
|
|
|
], ['type' => $icao]);
|
2019-11-27 22:19:20 +08:00
|
|
|
}
|
|
|
|
}
|