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.
418 lines
13 KiB
418 lines
13 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Exports\EventCalendarTemplateExport;
|
|
use App\Imports\EventCalendarImport;
|
|
use App\Models\AdminUsers;
|
|
use App\Models\EventCalendar;
|
|
use App\Models\ParkingPattern;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\EventCalendarService;
|
|
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;
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class EventCalendarController extends BaseController
|
|
{
|
|
protected EventCalendarService $service;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param EventCalendarService $service
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
EventCalendarService $service
|
|
) {
|
|
parent::__construct($responseService);
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = EventCalendar::query();
|
|
|
|
if ($request->has('pattern_id')) {
|
|
$pattern_id = $request->input('pattern_id');
|
|
if ($pattern_id) {
|
|
$query->where('pattern_id', $pattern_id);
|
|
}
|
|
}
|
|
|
|
if ($request->has('status')) {
|
|
$status = $request->input('status');
|
|
if ($status) {
|
|
$query->where('status', $status);
|
|
}
|
|
}
|
|
|
|
if ($request->has('start_time')) {
|
|
$start_time = $request->input('start_time');
|
|
if ($start_time && strtotime($start_time)) {
|
|
$query->where('start_time', '>=', $start_time);
|
|
}
|
|
}
|
|
|
|
if ($request->has('end_time')) {
|
|
$end_time = $request->input('end_time');
|
|
if ($end_time && strtotime($end_time)) {
|
|
$query->where('end_time', '<=', $end_time);
|
|
}
|
|
}
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$total = $query->count();
|
|
$statusArr = EventCalendarService::getStatus();
|
|
$items = $query->latest()->forPage($page, $perPage)->select()
|
|
->get()->each(function ($item) use ($statusArr) {
|
|
$item['pattern_name'] = ParkingPattern::getName(
|
|
$item['pattern_id']
|
|
);
|
|
$item['admin_username'] = AdminUsers::getUsername(
|
|
$item['admin_user_id']
|
|
);
|
|
$item['status_index'] = $item['status'];
|
|
$item['status'] = $statusArr[$item['status']] ?? '';
|
|
$item['remake'] = '';
|
|
unset($item['pattern_id'], $item['admin_user_id']);
|
|
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()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function search(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'pattern_list' => ParkingPattern::getData(),
|
|
'status_list' => get_select_data(
|
|
EventCalendarService::getStatus()
|
|
)
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function create(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'pattern_list' => ParkingPattern::getData()
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function changeMode(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$data = $request->all();
|
|
$this->saveValidator($data, 0, true);
|
|
$this->service->changeModel($data, $this->adminUserId);
|
|
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()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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(), $this->adminUserId);
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.save_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.event_calendar.create_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @return void
|
|
* @throws ValidationException
|
|
*/
|
|
protected function saveValidator(array $data, int $id = 0, $is = false): void
|
|
{
|
|
$rules = [
|
|
'pattern_id' => 'required',
|
|
'start_time' => 'required',
|
|
'end_time' => 'required'
|
|
];
|
|
$messages = [
|
|
'pattern_id.required' => __(
|
|
'validation.event_calendar.p_empty'
|
|
),
|
|
'start_time.required' => __(
|
|
'validation.event_calendar.s_empty'
|
|
),
|
|
'end_time.required' => __(
|
|
'validation.event_calendar.e_empty'
|
|
)
|
|
];
|
|
if ($id) {
|
|
$this->validateId($id, EventCalendar::class);
|
|
}
|
|
if ($is) {
|
|
unset($rules['start_time'], $messages['start_time.required']);
|
|
}
|
|
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
}
|
|
|
|
|
|
public function edit($id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, EventCalendar::class);
|
|
$item = EventCalendar::query()->findOrFail($id);
|
|
$data = [
|
|
'pattern_list' => ParkingPattern::getData()
|
|
];
|
|
$data['item'] = $item;
|
|
$statusArr = $item['status'] == 0 ? EventCalendarService::getStatus(
|
|
2
|
|
) : EventCalendarService::getStatus();
|
|
$data['status_list'] = get_select_data($statusArr);
|
|
unset($item['admin_user_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, $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(
|
|
__('exception.event_calendar.update_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, EventCalendar::class);
|
|
$this->service->deleteModel($id);
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.delete_succeeded')
|
|
);
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.event_calendar.destroy_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function end(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, EventCalendar::class);
|
|
$this->service->endModel($id);
|
|
return $this->responseService->success(
|
|
null,
|
|
__('exception.event_calendar.end_succeeded')
|
|
);
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.event_calendar.end_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function importTemplate(): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new EventCalendarTemplateExport(),
|
|
__('exports.event_calendar.list') . time() . '.xlsx'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 创建导入
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function import(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
// 1. 验证上传的文件
|
|
$data = $request->all();
|
|
$request->validate([
|
|
'file' => 'required|mimes:xlsx,xls,csv|max:2048'
|
|
]);
|
|
$validator = Validator::make($data, [
|
|
'file' => 'required|mimes:xlsx,xls,csv|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 EventCalendarImport($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()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return JsonResponse
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function rule(): JsonResponse
|
|
{
|
|
try {
|
|
return $this->responseService->success(
|
|
$this->methodShow('eventCalendar', ['import'])
|
|
);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function targetMode()
|
|
{
|
|
try {
|
|
$data = [
|
|
'model' => EventCalendarService::targetModel()
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|