You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
247 lines
8.0 KiB
247 lines
8.0 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exports\ParkingSpaceExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminFloor;
|
|
use App\Models\ParkingLicensePlate;
|
|
use App\Models\ParkingSpace;
|
|
use App\Models\ParkingSpaceAttributes;
|
|
use App\Models\ParkingSpaceType;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\ParkingSpaceService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class ParkingSpaceController extends Controller
|
|
{
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
|
|
/**
|
|
* @var ParkingSpaceService
|
|
*/
|
|
protected ParkingSpaceService $service;
|
|
|
|
/**
|
|
* @param ApiResponseService $responseService
|
|
* @param ParkingSpaceService $service
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
ParkingSpaceService $service
|
|
) {
|
|
$this->responseService = $responseService;
|
|
$this->service = $service;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = ParkingSpace::query();
|
|
|
|
if ($request->has('license_plate')) {
|
|
$license_plate = $request->input('license_plate');
|
|
if (!empty($license_plate)) {
|
|
$license_plate_id_arr = ParkingLicensePlate::query()->where(
|
|
'number',
|
|
'like',
|
|
"%{$license_plate}%"
|
|
)->pluck('id');
|
|
$license_plate_id_arr ? $query->whereIn(
|
|
'license_plate_id',
|
|
$license_plate_id_arr
|
|
) : $query->where('id', 0);
|
|
}
|
|
}
|
|
|
|
if ($request->has('floor')) {
|
|
$floor = $request->input('floor');
|
|
if (!empty($floor)) {
|
|
$query->where('floor_id', $floor);
|
|
}
|
|
}
|
|
|
|
if ($request->has('number')) {
|
|
$number = $request->input('number');
|
|
if (!empty($number)) {
|
|
$query->where('number', 'like', "%{$number}%");
|
|
}
|
|
}
|
|
|
|
if ($request->has('space_type')) {
|
|
$space_type = $request->input('space_type');
|
|
if (!empty($space_type)) {
|
|
$query->where('space_type_id', $space_type);
|
|
}
|
|
}
|
|
|
|
if ($request->has('space_attr')) {
|
|
$space_attr = $request->input('space_attr');
|
|
if (!empty($space_attr)) {
|
|
$query->where('space_attr_id', $space_attr);
|
|
}
|
|
}
|
|
|
|
if ($request->has('status')) {
|
|
$status = $request->input('status');
|
|
if (!empty($status)) {
|
|
$query->where('status', $status);
|
|
}
|
|
}
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
$query->orderBy('id');
|
|
$total = $query->count();
|
|
$_this = $this;
|
|
$items = $query->latest()->forPage($page, $perPage)->get()->each(
|
|
function ($item) use ($_this) {
|
|
return $_this->service->optionItems($item);
|
|
}
|
|
);
|
|
|
|
return $this->responseService->success([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'last_page' => ceil($total / $perPage),
|
|
]);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return JsonResponse
|
|
*/
|
|
public function search(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'floor_data' => AdminFloor::query()->select(['id', 'name'])
|
|
->get()->toArray(),
|
|
'space_type_data' => ParkingSpaceType::query()->select(
|
|
['id', 'name']
|
|
)->get()->toArray(),
|
|
'space_attr_data' => ParkingSpaceAttributes::query()->select(
|
|
['id', 'attributes']
|
|
)->get()->toArray(),
|
|
'status_data' => get_select_data(
|
|
$this->service->getStatusArr()
|
|
)
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function updateType(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$rules = [
|
|
'space_type_id' => 'required|numeric',
|
|
'space_ids' => 'required|array'
|
|
];
|
|
$messages = [
|
|
'space_type_id.required' => __(
|
|
'validation.parking_space.type_id_empty'
|
|
),
|
|
'space_type_id.numeric' => __(
|
|
'validation.parking_space.type_id_numeric'
|
|
),
|
|
'space_ids.required' => __(
|
|
'validation.parking_space.ids_empty'
|
|
),
|
|
'space_ids.array' => __(
|
|
'validation.parking_space.ids_array'
|
|
)
|
|
];
|
|
$validator = Validator::make($request->all(), $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
$this->service->updateData($request->all());
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.update_succeeded')
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.parking_space.update_type_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function updateAttr(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$rules = [
|
|
'space_attr_id' => 'required|numeric',
|
|
'space_ids' => 'required|array'
|
|
];
|
|
$messages = [
|
|
'space_attr_id.required' => __(
|
|
'validation.parking_space.attr_id_empty'
|
|
),
|
|
'space_attr_id.numeric' => __(
|
|
'validation.parking_space.attr_id_numeric'
|
|
),
|
|
'space_ids.required' => __(
|
|
'validation.parking_space.ids_empty'
|
|
),
|
|
'space_ids.array' => __(
|
|
'validation.parking_space.ids_array'
|
|
)
|
|
];
|
|
$validator = Validator::make($request->all(), $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
$this->service->updateData($request->all(), 'space_attr_id');
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.update_succeeded')
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.parking_space.update_attr_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function export(): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new ParkingSpaceExport(),
|
|
__('exports.parking_space.list') . time() . '.xlsx'
|
|
);
|
|
}
|
|
}
|
|
|