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

332 lines
10 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Exports\ParkingWhitelistExport;
use App\Exports\ParkingWhitelistTemplateExport;
use App\Imports\ParkingWhitelistImport;
use App\Models\Parking;
use App\Models\ParkingChannel;
use App\Models\ParkingLicensePlate;
use App\Models\ParkingSpaceType;
use App\Models\ParkingWhitelist;
use App\Services\ApiResponseService;
use App\Services\ParkingWhitelistService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Maatwebsite\Excel\Facades\Excel;
class ParkingWhitelistController extends BaseController
{
protected string $menuUri = 'whitelist';
/**
* @var ParkingWhitelistService
*/
protected ParkingWhitelistService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingWhitelistService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingWhitelistService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
/**
* @param Request $request
* @return JsonResponse
*/
public function index(Request $request): JsonResponse
{
try {
$query = ParkingWhitelist::query();
if ($request->has('license_plate')) {
$license_plate = $request->input('license_plate');
if ($license_plate) {
$license_plate_id = ParkingLicensePlate::getValueId(
$license_plate
);
if ($license_plate_id) {
$query->where('license_plate_id', $license_plate_id);
} else {
$query->where('id', 0);
}
}
}
// 分页
$page = $request->input('page', 1);
$perPage = $request->input('per_page', 10);
$total = $query->count();
$items = $query->latest()->forPage($page, $perPage)->get()->each(
function ($item) {
return $this->service->getItem($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 create(): JsonResponse
{
try {
$data = [
'parking_list' => Parking::getData(),
'member_type_list' => ParkingSpaceType::getData(),
'channel_list' => ParkingChannel::getData()
];
return $this->responseService->success($data);
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.get_data_failed') . ':' . $e->getMessage()
);
}
}
/**
* @param string $id
* @return JsonResponse
*/
public function edit(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingWhitelist::class);
$data = [
'parking_list' => Parking::getData(),
'member_type_list' => ParkingSpaceType::getData(),
'channel_list' => ParkingChannel::getData()
];
$item = ParkingWhitelist::query()->find($id);
$item['license_plate'] = ParkingLicensePlate::getNumber(
$item['license_plate_id']
);
unset(
$item['license_plate_id'],
$item['admin_user_id'],
$item['created_at'],
$item['status']
);
$data['item'] = $item;
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 {
$data = $request->all();
$this->saveValidator($data, $id);
$data['admin_user_id'] = $this->adminUserId;
$this->service->updateModel($data, $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 array $data
* @param int $id
* @return void
* @throws ValidationException
*/
protected function saveValidator(array $data, int $id = 0): void
{
$rules = [
'license_plate' => 'required',
'parking_id' => 'required',
'reason' => 'required',
'channel_ids' => 'required|array'
];
$messages = [
'license_plate.required' => __(
'validation.channel_management.l_empty'
),
'parking_id.required' => __(
'validation.channel_management.pa_empty'
),
'reason.required' => __(
'validation.channel_management.n_empty'
),
'channel_ids.required' => __(
'validation.channel_permissions.c_empty'
),
'channel_ids.array' => __(
'validation.channel_permissions.c_array'
)
];
if ($id) {
$this->validateId($id, ParkingWhitelist::class);
}
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails()) {
throw new ValidationException($validator);
}
}
/**
* @param string $id
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function destroy(string $id): JsonResponse
{
try {
$this->validateId($id, ParkingWhitelist::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()
);
}
}
public function downloadTemplate()
{
return Excel::download(
new ParkingWhitelistTemplateExport(),
set_space_underline(__exports('whitelist.list'))
. get_datetime('date_')
. '.xlsx'
);
}
public function batchImport(Request $request)
{
try {
// 1. 验证上传的文件
$data = $request->all();
$validator = Validator::make($data, [
'file' => 'required|mimes:xlsx,xls,csv,txt|max:2048'
], [
'file.required' => __validation('admin_list_vip.file_empty'),
'file.mimes' => __validation('admin_list_vip.file_mimes'),
'file.max' => __validation('admin_list_vip.file_max')
]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
// 2. 获取上传的文件
$file = $request->file('file');
// 3. 正确的做法:先存储文件,再获取绝对路径进行导入
$path = $file->store('imports');
// 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径
Excel::import(
new ParkingWhitelistImport($this->adminUserId),
storage_path('app/' . $path)
);
// 5. (可选)导入完成后删除临时文件
Storage::delete($path);
return $this->responseService->success(
__('controller.import.success')
);
} catch (ValidationException $e) {
throw $e;
} catch (Exception $e) {
return $this->responseService->systemError(
__('exception.admin_vip_list.import_failed') . ':'
. $e->getMessage()
);
}
}
/**
* @param Request $request
* @return JsonResponse
* @throws CustomException
* @throws ValidationException
*/
public function store(Request $request): JsonResponse
{
try {
$data = $request->all();
$this->saveValidator($data);
$data['admin_user_id'] = $this->adminUserId;
$this->service->createModel($data);
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()
);
}
}
public function export()
{
return Excel::download(
new ParkingWhitelistExport(),
set_space_underline(__exports('whitelist.export_list'))
. get_datetime('date_')
. '.xlsx'
);
}
}