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

350 lines
11 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\Parking;
use App\Models\ParkingChannel;
use App\Models\ParkingEquipment;
use App\Services\AdminTranslationService;
use App\Services\ApiResponseService;
use App\Services\ParkingEquipmentService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class ParkingEquipmentController extends BaseController
{
protected string $menuUri = 'equipmentManagement';
/**
* @var ParkingEquipmentService
*/
protected ParkingEquipmentService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingEquipmentService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingEquipmentService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
/**
* @param Request $request
* @return JsonResponse
*/
public function index(Request $request): JsonResponse
{
try {
$query = ParkingEquipment::query();
if ($request->has('parking_id')) {
$parking_id = $request->input('parking_id');
if ($parking_id) {
$channel_ids = ParkingChannel::getIds($parking_id);
if ($channel_ids) {
$query->whereIn('channel_id', $channel_ids);
} else {
$query->where('id', 0);
}
}
}
if ($request->has('name')) {
$name = $request->input('name');
if ($name) {
$query->where('name', 'like', "%{$name}%");
}
}
if ($request->has('type')) {
$type = $request->input('type');
if ($type) {
$query->where('type', $type);
}
}
if ($request->has('status')) {
$status = $request->input('status');
if ($status) {
$query->where('status', $status);
}
}
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);
$typeArr = ParkingEquipmentService::getType();
$statusArr = ParkingEquipmentService::getStatus();
$purposeArr = ParkingEquipmentService::getPurpose();
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->get()->each(
function ($item) use ($typeArr, $statusArr, $purposeArr) {
$parking_id = ParkingChannel::getParkingId(
$item['channel_id']
);
$item['parking_name'] = Parking::getName($parking_id);
$item['name']
= AdminTranslationService::getTranslationTypeName(
$item['id'],
9,
$item['name']
);
$item['type_str'] = $typeArr[$item['type']];
$item['purpose_str'] = $purposeArr[$item['purpose']] ?? '';
$item['channel_name'] = ParkingChannel::getName(
$item['channel_id']
);
$item['status_str'] = $statusArr[$item['status']];
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()
);
}
}
/**
* 列表搜索数据
* @return JsonResponse
*/
public function search(): JsonResponse
{
try {
$data = [
'parking_list' => Parking::getData(),
'type_list' => get_select_data(
ParkingEquipmentService::getType()
),
'status_list' => get_select_data(
ParkingEquipmentService::getStatus()
)
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
/**
* @return JsonResponse
*/
public function create(): JsonResponse
{
try {
$data = [
'type_list' => get_select_data(
ParkingEquipmentService::getType()
),
'purpose_list' => get_select_data(
ParkingEquipmentService::getPurpose()
),
'channel_list' => ParkingChannel::getData()
];
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @param Request $request
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function store(Request $request): JsonResponse
{
try {
$this->saveValidator($request->all());
$this->service->createModel($request->all());
return $this->responseService->success(
null,
__('admin.save_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('admin.operation_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',
'type' => 'required',
'channel_id' => 'required',
'purpose' => 'required'
];
$messages = [
'name.required' => __validation(
'equipment_management.n_empty'
),
'type.required' => __validation(
'equipment_management.t_empty'
),
'channel_id.required' => __validation(
'equipment_management.ch_empty'
),
'purpose.required' => __validation(
'equipment_management.pu_empty'
)
];
if ($id) {
$this->validateId($id, ParkingEquipment::class);
}
if (isset($data['type'])) {
$type = $data['type'];
if (in_array($type, [2, 5])) {
$rules['ip'] = 'required|ip';
$messages['ip.required']
= __validation('equipment_management.ip_empty');
$messages['ip.ip'] = __validation('equipment_management.ip');
}
if (in_array($type, [3, 4, 5])) {
$rules['sn'] = 'required';
$messages['sn.required']
= __validation('equipment_management.sn_empty');
}
}
$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, ParkingEquipment::class);
$data = [
'type_list' => get_select_data(
ParkingEquipmentService::getType()
),
'purpose_list' => get_select_data(
ParkingEquipmentService::getPurpose()
),
'channel_list' => ParkingChannel::getData(),
'item' => ParkingEquipment::query()->find($id)
];
$Translation = AdminTranslationService::getTranslation(
$data['item']['id'],
9
);
$data['item']['en_name'] = $Translation['en'] ?? '';
$data['item']['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->service->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(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingEquipment::class);
$this->service->deleteModel($id);
return $this->responseService->success(
null,
__('admin.delete_succeeded')
);
} catch (ValidationException|CustomException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('admin.operation_failed') . ':'
. $e->getMessage()
);
}
}
}