2017-06-18 06:25:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Repository;
|
2017-06-18 06:25:36 +08:00
|
|
|
use App\Models\Flight;
|
2017-12-05 00:59:25 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Prettus\Repository\Contracts\CacheableInterface;
|
|
|
|
use Prettus\Repository\Traits\CacheableRepository;
|
2017-06-18 06:25:36 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class FlightRepository
|
|
|
|
*/
|
|
|
|
class FlightRepository extends Repository implements CacheableInterface
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
2017-12-01 10:28:45 +08:00
|
|
|
use CacheableRepository;
|
|
|
|
|
2017-06-18 06:25:36 +08:00
|
|
|
protected $fieldSearchable = [
|
2017-12-01 10:28:45 +08:00
|
|
|
'arr_airport_id',
|
2019-05-11 05:24:34 +08:00
|
|
|
'distance',
|
2017-06-18 06:25:36 +08:00
|
|
|
'dpt_airport_id',
|
2021-01-18 01:23:23 +08:00
|
|
|
'flight_type',
|
2017-12-01 10:28:45 +08:00
|
|
|
'flight_number' => 'like',
|
2019-05-11 05:24:34 +08:00
|
|
|
'route_code' => 'like',
|
|
|
|
'route_leg' => 'like',
|
2018-03-20 09:50:40 +08:00
|
|
|
'route' => 'like',
|
|
|
|
'notes' => 'like',
|
2017-06-18 06:25:36 +08:00
|
|
|
];
|
|
|
|
|
2019-05-11 05:24:34 +08:00
|
|
|
public function model(): string
|
2017-06-18 06:25:36 +08:00
|
|
|
{
|
|
|
|
return Flight::class;
|
|
|
|
}
|
2017-12-05 00:59:25 +08:00
|
|
|
|
2018-02-25 05:38:25 +08:00
|
|
|
/**
|
|
|
|
* Find a flight based on the given criterea
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $airline_id
|
|
|
|
* @param $flight_num
|
2018-03-02 06:20:13 +08:00
|
|
|
* @param null $route_code
|
|
|
|
* @param null $route_leg
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-25 05:38:25 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function findFlight($airline_id, $flight_num, $route_code = null, $route_leg = null)
|
2018-02-25 05:38:25 +08:00
|
|
|
{
|
|
|
|
$where = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'airline_id' => $airline_id,
|
2018-03-02 06:20:13 +08:00
|
|
|
'flight_number' => $flight_num,
|
2018-03-20 09:50:40 +08:00
|
|
|
'active' => true,
|
2018-02-25 05:38:25 +08:00
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($route_code)) {
|
2018-03-02 06:20:13 +08:00
|
|
|
$where['route_code'] = $route_code;
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (filled($route_leg)) {
|
2018-03-02 06:20:13 +08:00
|
|
|
$where['route_leg'] = $route_leg;
|
2018-02-25 05:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findWhere($where);
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
/**
|
|
|
|
* Create the search criteria and return this with the stuff pushed
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 05:11:27 +08:00
|
|
|
* @param Request $request
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param bool $only_active
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-05 00:59:25 +08:00
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return $this
|
2017-12-05 00:59:25 +08:00
|
|
|
*/
|
2019-05-11 05:24:34 +08:00
|
|
|
public function searchCriteria(Request $request, bool $only_active = true): self
|
2017-12-05 00:59:25 +08:00
|
|
|
{
|
2017-12-27 04:14:04 +08:00
|
|
|
$where = [];
|
2020-02-09 02:29:34 +08:00
|
|
|
$relations = [];
|
2017-12-27 04:14:04 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($only_active === true) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$where['active'] = $only_active;
|
2018-07-13 11:29:50 +08:00
|
|
|
$where['visible'] = $only_active;
|
2017-12-27 04:14:04 +08:00
|
|
|
}
|
2017-12-05 00:59:25 +08:00
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
if ($request->filled('flight_id')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where['id'] = $request->input('flight_id');
|
2017-12-12 21:25:11 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 02:05:31 +08:00
|
|
|
if ($request->filled('airline_id')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where['airline_id'] = $request->input('airline_id');
|
2017-12-05 00:59:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($request->filled('flight_number')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where['flight_number'] = $request->input('flight_number');
|
2017-12-05 00:59:25 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 22:21:39 +08:00
|
|
|
if ($request->filled('flight_type') && $request->input('flight_type') !== '0') {
|
2021-01-18 01:23:23 +08:00
|
|
|
$where['flight_type'] = $request->input('flight_type');
|
|
|
|
}
|
|
|
|
|
2017-12-05 02:05:31 +08:00
|
|
|
if ($request->filled('route_code')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where['route_code'] = $request->input('route_code');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('dpt_airport_id')) {
|
|
|
|
$where['dpt_airport_id'] = strtoupper($request->input('dpt_airport_id'));
|
2017-12-05 02:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('dep_icao')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where['dpt_airport_id'] = strtoupper($request->input('dep_icao'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('arr_airport_id')) {
|
|
|
|
$where['arr_airport_id'] = strtoupper($request->input('arr_airport_id'));
|
2017-12-05 02:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('arr_icao')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where['arr_airport_id'] = strtoupper($request->input('arr_icao'));
|
2017-12-05 00:59:25 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
// Distance, greater than
|
|
|
|
if ($request->filled('dgt')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where[] = ['distance', '>=', $request->input('dgt')];
|
2019-05-11 05:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Distance, less than
|
|
|
|
if ($request->filled('dlt')) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$where[] = ['distance', '<=', $request->input('dlt')];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a special query for finding the child subfleets
|
|
|
|
if ($request->filled('subfleet_id')) {
|
|
|
|
$relations['subfleets'] = [
|
|
|
|
'subfleets.id' => $request->input('subfleet_id'),
|
|
|
|
];
|
2019-05-11 05:03:04 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->pushCriteria(new WhereCriteria($request, $where, $relations));
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
return $this;
|
|
|
|
}
|
2017-06-18 06:25:36 +08:00
|
|
|
}
|