停车场管理系统
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.
 
 

309 lines
10 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\AdminFloor;
use App\Models\AdminFloorRegion;
use App\Models\Parking;
use App\Models\ParkingSpace;
use App\Services\AdminFloorService;
use App\Services\AdminTranslationService;
use App\Services\ApiResponseService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Psr\SimpleCache\InvalidArgumentException;
class FloorController extends BaseController
{
protected string $menuUri = 'floors';
/**
* @var AdminFloorService
*/
protected AdminFloorService $AdminFloorService;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param AdminFloorService $AdminFloorService
*/
public function __construct(
ApiResponseService $responseService,
AdminFloorService $AdminFloorService
) {
parent::__construct($responseService);
$this->AdminFloorService = $AdminFloorService;
}
/**
* @param Request $request
* @return JsonResponse
*/
public function index(Request $request): JsonResponse
{
try {
$query = AdminFloor::query();
if ($request->has('building_floor')) {
$building_floor = $request->input('building_floor');
if ($building_floor) {
$query->where('building_floor', $building_floor);
}
}
if ($request->has('name')) {
$name = $request->input('name');
if ($name) {
$query->where('name', 'like', "%{$name}%");
}
}
if ($request->has('create_start_time')
&& $request->has(
'create_end_time'
)
) {
$create_start_time = $request->input('create_start_time');
$create_end_time = $request->input('create_end_time');
if ($create_start_time && $create_end_time) {
$query->whereBetween('created_at', [
$create_start_time . ' 00:00:00',
$create_end_time . ' 23:59:59'
]);
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$statusArr = AdminFloorService::getStatus();
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->get()->each(
function ($item) use($statusArr) {
$item['name']
= AdminTranslationService::getTranslationTypeName(
$item['id'],
4,
$item['name']
);
$item['parking'] = Parking::getName(
$item['building_floor']
);
$item['status_str'] = $statusArr[$item['status']];
$item['parking_space_count'] = ParkingSpace::getFloorCount($item['id']);;
$open_time_res = option_time($item['open_time']);
$item['open_time'] = $open_time_res['time'];
$item['open_time_str'] = $open_time_res['str'];
$close_time_res = option_time($item['close_time']);
$item['close_time'] = $close_time_res['time'];
$item['close_time_str'] = $close_time_res['str'];
return $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()
);
}
}
/**
* @param Request $request
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function store(Request $request): JsonResponse
{
try {
$this->saveValidator($request->all());
$this->AdminFloorService->createModel($request->all());
return $this->responseService->success(
null,
__('admin.save_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_floor.create_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param array $data
* @param int $id
* @return void
* @throws ValidationException
*/
protected function saveValidator(array $data, int $id = 0): void
{
$rules = [
'name' => 'required|max:50',
'open_time' => 'required|size:5',
'close_time' => 'required|size:5',
];
$messages = [
'name.required' => __('validation.admin_floor.n_empty'),
'name.max' => __('validation.admin_floor.n_max'),
'open_time.required' => __(
'validation.parking_management.o_empty'
),
'open_time.size' => __(
'validation.parking_management.o_length'
),
'close_time.required' => __(
'validation.parking_management.c_empty'
),
'close_time.size' => __(
'validation.parking_management.c_length'
)
];
if ($id) {
$this->validateId($id, AdminFloor::class);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
}
/**
* @param string $id
* @return JsonResponse
*/
public function edit(string $id): JsonResponse
{
try {
$this->validateId($id, AdminFloor::class);
$data = [
'region_list' => AdminFloorRegion::getFloorRegion($id),
'building_floor_list' => get_select_data(
AdminFloorService::getBuildingFloor()
),
'item' => AdminFloorService::getFloorData($id)
];
$Translation = AdminTranslationService::getTranslation(
$data['item'][0]['id'],
4
);
$data['item'][0]['en_name'] = $Translation['en'] ?? '';
$data['item'][0]['tw_name'] = $Translation['zh_tw'] ?? '';
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @param Request $request
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function update(Request $request, string $id): JsonResponse
{
try {
$this->saveValidator($request->all(), $id);
$this->AdminFloorService->updateModel($request->all(), $id);
return $this->responseService->success(
null,
__('admin.update_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_floor.update_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, AdminFloor::class);
$this->AdminFloorService->deleteModel($id);
return $this->responseService->success(
null,
__('admin.delete_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_floor.destroy_failed') . ':'
. $e->getMessage()
);
}
}
/**
* 列表搜索数据
* @return JsonResponse
*/
public function search(): JsonResponse
{
try {
$data = [
'building_floor_list' => get_select_data(
AdminFloorService::getBuildingFloor()
)
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
/**
* 楼层编号查询区域
* @param Request $request
* @return JsonResponse
*/
public function floorRegionList(Request $request): JsonResponse
{
try {
$floor_id = $request->get('floor_id', '');
$this->validateId($floor_id, AdminFloor::class);
$data = [
'floor_region_list' => AdminFloorRegion::getFloorRegion($floor_id)
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
}