Доработка апи

pull/7/head
Rustem 2025-03-31 23:51:34 +05:00
parent a156e81a8b
commit ee0f52a4c7
6 changed files with 123 additions and 34 deletions

View File

@ -64,7 +64,8 @@ class MobileApiController extends Controller
$data['car_id'] = null;
if ($request->header('Authorization')) {
$user = auth()->guard('api')->user();
$data['user_id'] = $user ? $user->id : null;
$data['user_id'] = $user?->id;
$authToken = $request->header('Authorization');
} else {
$user = UniModel::model('core_users')
->where('email', $data['email'])
@ -85,18 +86,19 @@ class MobileApiController extends Controller
'message' => 'Заявка создана',
];
if ($authToken) {
$car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id']);
if (isEmpty($car)) {
if ($data['user_id']) {
$car = $this->getAvailableCar($data['started_at'], $data['ended_at'], $data['mark_id'], $data['color_code']);
if (!isset($car)) {
return response()->json('Нет свободных машин');
}
$data['car_id'] = $car->id;
$period = CarbonPeriod::create($data['started_at'], $data['ended_at']);
$dates = array_map(fn($date) => $date->toDateString(), iterator_to_array($period));
foreach ($dates as $date) {
UniModel::model('pipi_auto_calendar')->create([
'auto_id' => $car->id,
'auto_id' => $data['car_id'],
'date' => $date,
'status' => AutoStatusEnums::Rent->name
]);
@ -200,14 +202,22 @@ class MobileApiController extends Controller
$bodywork_id = UniModel::model('pipi_auto_bodywork')->where('name', $bodywork)->first()->id;
$marks = $marks->where('bodywork_id', $bodywork_id);
}
foreach ($marks as $mark) {
if ($mark->name === 'Ввод остатков') {
continue;
}
//
// if (!$this->checkAvailableCar($started_at, $ended_at, $mark->id)) {
// continue;
// }
if (!$this->checkAvailableCar($started_at, $ended_at, $mark->id)) {
continue;
}
$cars = UniModel::model('pipi_auto')->where('model_id', $mark->id)->get();
if ($cars->isEmpty()) {
continue;
}
$brand = UniModel::model('pipi_auto_brands')->find($mark->brand_id);
$tariffs = UniModel::model('pipi_auto_tariffs')->where('model_id', $mark->id)->get();
@ -220,6 +230,7 @@ class MobileApiController extends Controller
if ($photo) {
$path = url('api/files/file/' . ltrim($photo->path, '/'));
}
$tariffs_new = [];
foreach ($tariffs as $tariff) {
$tariffs_new[] = [
@ -229,25 +240,39 @@ class MobileApiController extends Controller
'max' => $tariff?->day_range_end,
];
}
$carsByColor = $cars->groupBy('color_id');
$availableMarks[$mark->name . '-' . $mark->year] = [
'id' => $mark->id,
'brand' => $brand?->name,
'mark' => $mark->name,
'year' => $mark->year,
'configuration' => $equipment?->name,
'people' => $mark?->people ?? '5',
'actuator' => $mark?->actuator ?? 'Передний',
'fuel_type' => $mark?->fuel_type ?? 'АКПП',
'hp' => $mark?->hp ?? '1.6',
'engine_capacity' => $mark?->engine_capacity ?? '1591',
'fuel_tank' => $mark?->fuel_tank ?? '50',
'class' => $class->name ?? 'Эконом',
'bodywork' => $bodywork->name ?? 'Седан',
'conditioner' => $mark?->conditioner,
'photo' => $path,
'tariffs' => $tariffs_new
];
foreach ($carsByColor as $carColor => $carsOfColor) {
$carColor = UniModel::model('pipi_auto_colors')->find($carColor)->code;
$colorPath = $path;
if ($carsOfColor->first()->photo_id) {
$photo = UniModel::model('core_files')->find($carsOfColor->first()->photo_id);
$colorPath = url('api/files/file/' . ltrim($photo->path, '/'));
}
$key = $mark->name . '-' . $mark->year . '-' . $carColor;
$availableMarks[$key] = [
'id' => $mark->id,
'brand' => $brand?->name,
'mark' => $mark->name,
'year' => $mark->year,
'color' => $carColor,
'configuration' => $equipment?->name,
'people' => $mark?->people ?? '5',
'actuator' => $mark?->actuator ?? 'Передний',
'fuel_type' => $mark?->fuel_type ?? 'АКПП',
'hp' => $mark?->hp ?? '1.6',
'engine_capacity' => $mark?->engine_capacity ?? '1591',
'fuel_tank' => $mark?->fuel_tank ?? '50',
'class' => $class->name ?? 'Эконом',
'bodywork' => $bodywork->name ?? 'Седан',
'conditioner' => $mark?->conditioner,
'photo' => $colorPath,
'tariffs' => $tariffs_new,
];
}
}
if (empty($availableMarks)) {
@ -256,11 +281,15 @@ class MobileApiController extends Controller
return response()->json($availableMarks);
}
public function getAvailableCar($started_at, $ended_at, $modelId)
public function getAvailableCar($started_at, $ended_at, $modelId, $color)
{
$started_at = Carbon::parse($started_at)->format('Y-m-d');
$ended_at = Carbon::parse($ended_at)->format('Y-m-d');
$cars = UniModel::model('pipi_auto')->where('model_id', $modelId)->get();
$color = UniModel::model('pipi_auto_colors')->where('code', $color)->first()->id;
$cars = UniModel::model('pipi_auto')
->where('color_id', $color)
->where('model_id', $modelId)
->get();
$busyCars = UniModel::model('pipi_auto_calendar')
->whereBetween('date', [$started_at, $ended_at])
->pluck('auto_id')
@ -271,7 +300,7 @@ class MobileApiController extends Controller
if ($availableCars->isNotEmpty()) {
return $availableCars->first();
} else {
return (object) [];
return null;
}
}
}

View File

@ -149,6 +149,10 @@
"type": "timestamp",
"nullable": true,
"comment": "Дата удаления"
},
"photo_id": {
"type": "int",
"validation": "nullable|int"
}
}
},
@ -300,6 +304,14 @@
"name": "owner_id",
"label": "Владелец"
}
},
{
"size": 12,
"input": {
"name": "photo_id",
"label": "Фото",
"type": "file"
}
}
]
}
@ -384,6 +396,14 @@
"name": "owner_id",
"label": "Владелец"
}
},
{
"size": 12,
"input": {
"name": "photo_id",
"label": "Фото",
"type": "file"
}
}
]
}

View File

@ -139,6 +139,12 @@ return new class extends \A7kz\Platform\Commands\InstallScript {
$table->string('guid')->unique()->nullable();
});
}
if (!Schema::connection($segment->connector)->hasColumn('pipi_auto', 'photo_id')) {
Schema::connection($segment->connector)->table('pipi_auto', static function (Blueprint $table) {
$table->unsignedBigInteger('photo_id')->nullable();
});
}
}
}

View File

@ -16,6 +16,9 @@
},
"name": {
"type": "string"
},
"code": {
"type": "string"
}
}
},
@ -27,6 +30,10 @@
{
"name": "name",
"caption": "Название цвета"
},
{
"name": "code",
"caption": "Код"
}
],
"action": {
@ -49,6 +56,13 @@
"name": "name",
"label": "Название цвета"
}
},
{
"size": 6,
"input": {
"name": "code",
"label": "Код"
}
}
]
}
@ -66,11 +80,18 @@
{
"cols": [
{
"size": 12,
"size": 6,
"input": {
"name": "name",
"label": "Название"
}
},
{
"size": 6,
"input": {
"name": "code",
"label": "Код"
}
}
]
}
@ -86,11 +107,18 @@
{
"cols": [
{
"size": 12,
"size": 6,
"input": {
"name": "name",
"label": "Название"
}
},
{
"size": 6,
"input": {
"name": "code",
"label": "Код"
}
}
]
}

View File

@ -33,6 +33,12 @@ return new class extends \A7kz\Platform\Commands\InstallScript {
$table->softDeletes();
});
}
if (!Schema::connection($segment->connector)->hasColumn('pipi_auto_colors', 'code')) {
Schema::connection($segment->connector)->table('pipi_auto_colors', static function (Blueprint $table) {
$table->string('code')->nullable();
});
}
}
}
};

View File

@ -1,11 +1,11 @@
{
"module": "pipicar",
"name": "pipicar.auto_colors",
"name": "pipicar.auto_tariffs",
"type": "crud",
"title": "Цвета Автомобилей",
"withHeader": false,
"data": {
"table": "pipi_auto_colors",
"table": "pipi_auto_tariffs",
"pk": "id",
"limit": 25,
"segment": true,