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

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

View File

@ -149,6 +149,10 @@
"type": "timestamp", "type": "timestamp",
"nullable": true, "nullable": true,
"comment": "Дата удаления" "comment": "Дата удаления"
},
"photo_id": {
"type": "int",
"validation": "nullable|int"
} }
} }
}, },
@ -300,6 +304,14 @@
"name": "owner_id", "name": "owner_id",
"label": "Владелец" "label": "Владелец"
} }
},
{
"size": 12,
"input": {
"name": "photo_id",
"label": "Фото",
"type": "file"
}
} }
] ]
} }
@ -384,6 +396,14 @@
"name": "owner_id", "name": "owner_id",
"label": "Владелец" "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(); $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": { "name": {
"type": "string" "type": "string"
},
"code": {
"type": "string"
} }
} }
}, },
@ -27,6 +30,10 @@
{ {
"name": "name", "name": "name",
"caption": "Название цвета" "caption": "Название цвета"
},
{
"name": "code",
"caption": "Код"
} }
], ],
"action": { "action": {
@ -49,6 +56,13 @@
"name": "name", "name": "name",
"label": "Название цвета" "label": "Название цвета"
} }
},
{
"size": 6,
"input": {
"name": "code",
"label": "Код"
}
} }
] ]
} }
@ -66,11 +80,18 @@
{ {
"cols": [ "cols": [
{ {
"size": 12, "size": 6,
"input": { "input": {
"name": "name", "name": "name",
"label": "Название" "label": "Название"
} }
},
{
"size": 6,
"input": {
"name": "code",
"label": "Код"
}
} }
] ]
} }
@ -86,11 +107,18 @@
{ {
"cols": [ "cols": [
{ {
"size": 12, "size": 6,
"input": { "input": {
"name": "name", "name": "name",
"label": "Название" "label": "Название"
} }
},
{
"size": 6,
"input": {
"name": "code",
"label": "Код"
}
} }
] ]
} }

View File

@ -33,6 +33,12 @@ return new class extends \A7kz\Platform\Commands\InstallScript {
$table->softDeletes(); $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", "module": "pipicar",
"name": "pipicar.auto_colors", "name": "pipicar.auto_tariffs",
"type": "crud", "type": "crud",
"title": "Цвета Автомобилей", "title": "Цвета Автомобилей",
"withHeader": false, "withHeader": false,
"data": { "data": {
"table": "pipi_auto_colors", "table": "pipi_auto_tariffs",
"pk": "id", "pk": "id",
"limit": 25, "limit": 25,
"segment": true, "segment": true,