17 changed files with 339 additions and 26 deletions
@ -0,0 +1,182 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers\Admin; |
|||
|
|||
use App\Models\AdminFloor; |
|||
use App\Models\Parking; |
|||
use App\Models\ParkingLicensePlate; |
|||
use App\Models\ParkingSpace; |
|||
use App\Models\ParkingSpaceAttributes; |
|||
use App\Services\ApiResponseService; |
|||
use App\Services\ParkingSpaceService; |
|||
use Exception; |
|||
use Illuminate\Http\JsonResponse; |
|||
use Illuminate\Http\Request; |
|||
|
|||
class ParkingManagementListController |
|||
{ |
|||
/** |
|||
* @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; |
|||
} |
|||
|
|||
/** |
|||
* @return JsonResponse |
|||
*/ |
|||
public function search(): JsonResponse |
|||
{ |
|||
try { |
|||
$data = [ |
|||
'parking_list' => Parking::getData(), |
|||
'floor_list' => AdminFloor::getData(), |
|||
'space_attr_data' => ParkingSpaceAttributes::getData(), |
|||
'status_data' => get_select_child_data( |
|||
$this->service->getStatusArr(true) |
|||
) |
|||
]; |
|||
return $this->responseService->success($data); |
|||
} catch (Exception $e) { |
|||
$m_prefix = __('exception.exception_handler.resource'); |
|||
return $this->responseService->systemError( |
|||
$m_prefix . ':' . $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
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('parking_id')) { |
|||
$parking_id = $request->input('parking_id'); |
|||
if (!empty($parking_id)) { |
|||
$floorIds = AdminFloor::query()->where( |
|||
'building_floor', |
|||
$parking_id |
|||
)->pluck('id'); |
|||
if ($floorIds) { |
|||
$query->whereIn('floor_id', $floorIds); |
|||
} else { |
|||
$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_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)) { |
|||
$where = [['status', '=', 1]]; |
|||
if ($status == 3) { |
|||
$where[] = ['license_plate_id', '=', 0]; |
|||
$query->where($where); |
|||
} elseif ($status == 4) { |
|||
$where[] = ['license_plate_id', '>=', 1]; |
|||
$query->where($where); |
|||
} else { |
|||
$query->where('status', $status); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if ($request->has('customer_id')) { |
|||
$customer_id = $request->input('customer_id'); |
|||
if (!empty($customer_id)) { |
|||
$query->where('customer_id', $customer_id); |
|||
} |
|||
} |
|||
|
|||
if ($request->has('customer_id')) { |
|||
$customer_id = $request->input('customer_id'); |
|||
if (!empty($customer_id)) { |
|||
$query->where('customer_id', $customer_id); |
|||
} |
|||
} |
|||
|
|||
if ($request->has('member_id')) { |
|||
$member_id = $request->input('member_id'); |
|||
if (!empty($member_id)) { |
|||
$query->where('member_id', $member_id); |
|||
} |
|||
} |
|||
|
|||
// 分页 |
|||
$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) { |
|||
$item['parking_name'] = Parking::queryName($item['floor_id']); |
|||
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() |
|||
); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue