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.
469 lines
15 KiB
469 lines
15 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Exports\ParkingSpaceRepairTemplateExport;
|
|
use App\Imports\ParkingSpaceRepairImport;
|
|
use App\Models\AdminFloor;
|
|
use App\Models\AdminUsers;
|
|
use App\Models\Parking;
|
|
use App\Models\ParkingSpace;
|
|
use App\Models\ParkingSpaceRepair;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\ParkingSpaceRepairService;
|
|
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 ParkingSpaceRepairController extends BaseController
|
|
{
|
|
|
|
protected string $menuUri = 'parkingRepair';
|
|
|
|
/**
|
|
* @var ParkingSpaceRepairService
|
|
*/
|
|
protected ParkingSpaceRepairService $service;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param ParkingSpaceRepairService $service
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
ParkingSpaceRepairService $service
|
|
) {
|
|
parent::__construct($responseService);
|
|
$this->service = $service;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = ParkingSpaceRepair::query();
|
|
|
|
if ($request->has('parking_space_number')) {
|
|
$number = $request->input('parking_space_number');
|
|
if ($number) {
|
|
$space_id = ParkingSpace::getValueId($number);
|
|
if ($space_id) {
|
|
$query->where('space_id', $space_id);
|
|
} else {
|
|
$query->where('id', 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($request->has('parking_id')) {
|
|
$parking_id = $request->input('parking_id');
|
|
$floor_id = $request->input('floor_id');
|
|
if ($parking_id && !$floor_id) {
|
|
$floor_ids = AdminFloor::query()->where(
|
|
'building_floor',
|
|
$parking_id
|
|
)->pluck('id')->toArray();
|
|
if ($floor_ids) {
|
|
$space_ids = ParkingSpace::query()->whereIn(
|
|
'floor_id',
|
|
$floor_ids
|
|
)->pluck('id')->toArray();
|
|
if ($space_ids) {
|
|
$query->whereIn('space_id', $space_ids);
|
|
} else {
|
|
$query->where('id', 0);
|
|
}
|
|
} else {
|
|
$query->where('id', 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($request->has('floor_id')) {
|
|
$floor_id = $request->input('floor_id');
|
|
if ($floor_id) {
|
|
$space_ids = ParkingSpace::query()->where(
|
|
'floor_id',
|
|
$floor_id
|
|
)->pluck('id')->toArray();
|
|
if ($space_ids) {
|
|
$query->whereIn('space_id', $space_ids);
|
|
} else {
|
|
$query->where('id', 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($request->has('start_date')) {
|
|
$start_date = $request->input('start_date');
|
|
if ($start_date) {
|
|
$query->where('start_at', '>=', $start_date . ' 00:00:00');
|
|
}
|
|
}
|
|
|
|
if ($request->has('end_date')) {
|
|
$end_date = $request->input('end_date');
|
|
if ($end_date) {
|
|
$query->where('end_at', '<=', $end_date . ' 23:59:59');
|
|
}
|
|
}
|
|
|
|
if ($request->has('admin_user')) {
|
|
$admin_user = $request->input('admin_user');
|
|
if ($admin_user) {
|
|
$admin_user_ids = AdminUsers::getIds($admin_user);
|
|
if ($admin_user_ids) {
|
|
$query->whereIn('admin_user_id', $admin_user_ids);
|
|
} 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 search(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'parking_list' => Parking::getData(),
|
|
'floor_list' => AdminFloor::getData()
|
|
];
|
|
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 = [
|
|
'parking_space_list' => ParkingSpace::getFloorData()
|
|
];
|
|
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, ParkingSpaceRepair::class);
|
|
$data = [
|
|
'parking_space_list' => ParkingSpace::getFloorData(),
|
|
'item' => ParkingSpaceRepair::query()->find($id, [
|
|
'id',
|
|
'space_id',
|
|
'start_at',
|
|
'end_at'
|
|
])
|
|
];
|
|
$ParkingSpace = ParkingSpace::query()->find(
|
|
$data['item']['space_id']
|
|
);
|
|
$data['item']['parking_space_name'] = $ParkingSpace['number'];
|
|
$data['item']['floor'] = AdminFloor::getName(
|
|
$ParkingSpace['floor_id']
|
|
);
|
|
unset($data['item']['space_id']);
|
|
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);
|
|
$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 CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
protected function saveValidator(array $data, int $id = 0): void
|
|
{
|
|
$rules = [
|
|
'parking_space_name' => 'required',
|
|
'start_at' => 'required',
|
|
'end_at' => 'required'
|
|
];
|
|
$messages = [
|
|
'parking_space_name.required' => __validation(
|
|
'parking_repair_list.psn_empty'
|
|
),
|
|
'start_at.required' => __validation(
|
|
'parking_repair_list.s_empty'
|
|
),
|
|
'end_at.required' => __validation(
|
|
'parking_repair_list.e_empty'
|
|
)
|
|
];
|
|
if ($id) {
|
|
$this->validateId($id, ParkingSpaceRepair::class);
|
|
}
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
$start_at_times = strtotime($data['start_at']);
|
|
$end_at_times = strtotime($data['end_at']);
|
|
if ($start_at_times >= $end_at_times) {
|
|
throw new CustomException(
|
|
__validation('parking_repair_list.date_error')
|
|
);
|
|
}
|
|
|
|
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, ParkingSpaceRepair::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()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function batchDelete(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$data = $request->all();
|
|
$rules = [
|
|
'ids' => 'required|array',
|
|
];
|
|
$messages = [
|
|
'ids.required' => __validation(
|
|
'parking_repair_list.ids_empty'
|
|
),
|
|
'ids.array' => __validation(
|
|
'parking_repair_list.ids_array'
|
|
),
|
|
];
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
$ids = $data['ids'];
|
|
$this->service->batchDeleteModel($ids);
|
|
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 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 ParkingSpaceRepairImport($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 downloadTemplate()
|
|
{
|
|
return Excel::download(
|
|
new ParkingSpaceRepairTemplateExport(),
|
|
set_space_underline(__exports('parking_repair_list.list'))
|
|
. get_datetime('date_')
|
|
. '.xlsx'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function synchronizeList(): JsonResponse
|
|
{
|
|
try {
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.operation_successful')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('admin.operation_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|