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

202 lines
6.6 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Exceptions\CustomException;
use App\Models\Parking;
use App\Models\ParkingAbnormal;
use App\Services\ApiResponseService;
use App\Services\ParkingAbnormalService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class ParkingAbnormalController extends BaseController
{
protected string $menuUri = 'abnormalResourceUsage';
/**
* @var ParkingAbnormalService
*/
protected ParkingAbnormalService $service;
/**
* 构造函数
* @param ApiResponseService $responseService
* @param ParkingAbnormalService $service
*/
public function __construct(
ApiResponseService $responseService,
ParkingAbnormalService $service
) {
parent::__construct($responseService);
$this->service = $service;
}
/**
* @param Request $request
* @return JsonResponse
*/
public function index(Request $request): JsonResponse
{
try {
$query = ParkingAbnormal::query();
if ($request->has('parking_id')) {
$parking_id = $request->input('parking_id');
if ($parking_id) {
$query->where('parking_id', $parking_id);
}
}
if ($request->has('date_type')) {
$date_type = $request->input('date_type');
$start_date = '';
if ($date_type == 1) {
$start_date = date('Y-m-d 00:00:00', time());
} else {
if ($date_type == 2) {
$start_date = date(
'Y-m-d 00:00:00',
strtotime('-3 day')
);
} else {
if ($date_type == 3) {
$start_date = date(
'Y-m-d 00:00:00',
strtotime('-7 day')
);
} else {
if ($date_type == 4) {
$start_date = date(
'Y-m-d 00:00:00',
strtotime('-1 month')
);
} else {
if ($date_type == 5) {
$start_date = date(
'Y-m-d 00:00:00',
strtotime('-3 month')
);
}
}
}
}
}
if ($start_date) {
$end_date = date('Y-m-d 23:59:59', time());
$query->whereBetween('enter_at', [$start_date, $end_date]);
}
}
if ($request->has('status')) {
$status = $request->input('status');
if ($status) {
$query->where('status', $status);
}
}
if ($request->has('operation_type')) {
$operation_type = $request->input('operation_type');
if ($operation_type) {
$query->where('operation_type', $operation_type);
}
}
// 分页
$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()
);
}
}
public function search(): JsonResponse
{
try {
$data = [
'parking_list' => Parking::getData(),
'date_list' => get_select_data(
$this->service->getDateArr()
),
'status_list' => get_select_data(
$this->service->getStatus()
),
'operation_list' => get_select_data(
$this->service->getOperationType()
)
];
return $this->responseService->success($data);
} catch (Exception $e) {
$m_prefix = __('exception.exception_handler.resource');
return $this->responseService->systemError(
$m_prefix . ':' . $e->getMessage()
);
}
}
public function handle(Request $request): JsonResponse
{
try {
$data = $request->all();
$this->validateId($data['id'] ?? 0, ParkingAbnormal::class);
$this->service->updateOperationType(
$data['id'],
$data['reason'] ?? ''
);
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()
);
}
}
public function ignore(Request $request): JsonResponse
{
try {
$data = $request->all();
$this->validateId($data['id'] ?? 0, ParkingAbnormal::class);
$this->service->updateOperationType(
$data['id'],
$data['reason'] ?? '',
2
);
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()
);
}
}
}