phpvms/app/Repositories/AcarsRepository.php

90 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Repositories;
use App\Contracts\Repository;
use App\Models\Acars;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepState;
2018-02-21 12:33:09 +08:00
use App\Models\Pirep;
use Carbon\Carbon;
class AcarsRepository extends Repository
{
/**
* @return string
*/
public function model()
{
return Acars::class;
}
2018-01-02 06:01:01 +08:00
/**
* @param $pirep_id
* @param $type
2018-08-27 00:40:04 +08:00
*
2018-01-02 06:01:01 +08:00
* @return mixed
*/
public function forPirep($pirep_id, $type)
{
2018-01-02 06:01:01 +08:00
$where = [
'pirep_id' => $pirep_id,
'type' => $type,
2018-01-02 06:01:01 +08:00
];
switch ($type) {
default:
case AcarsType::FLIGHT_PATH:
case AcarsType::LOG:
$order_by = 'created_at';
break;
case AcarsType::ROUTE:
$order_by = 'order';
break;
}
return $this->orderBy($order_by, 'asc')->findWhere($where);
}
/**
* Get all of the PIREPS that are in-progress, and then
* get the latest update for those flights
2018-08-27 00:40:04 +08:00
*
* @param int $live_time Age in hours of the oldest flights to show
2018-08-27 00:40:04 +08:00
*
* @return Pirep
*/
public function getPositions($live_time = 0)
{
$with = [
'aircraft',
'airline',
'arr_airport',
'dpt_airport',
'position',
'user',
];
$q = Pirep::with($with)
->where(['state' => PirepState::IN_PROGRESS]);
2018-08-27 00:40:04 +08:00
if ($live_time !== null && $live_time > 0) {
$st = Carbon::now('UTC')->subHours($live_time);
$q = $q->whereDate('created_at', '>=', $st);
}
$q = $q->orderBy('created_at', 'desc');
return $q->get();
}
/**
* @return $this
*/
public function getAllAcarsPoints()
{
return Pirep::with('acars')->where([
2018-08-27 00:40:04 +08:00
'state' => PirepState::IN_PROGRESS,
]);
}
}