14 changed files with 408 additions and 4 deletions
@ -0,0 +1,227 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers\Admin; |
||||
|
|
||||
|
use App\Exceptions\CustomException; |
||||
|
use App\Models\ParkingGuardBooth; |
||||
|
use App\Services\AdminTranslationService; |
||||
|
use App\Services\ApiResponseService; |
||||
|
use App\Services\ParkingGuardBoothService; |
||||
|
use Exception; |
||||
|
use Illuminate\Http\JsonResponse; |
||||
|
use Illuminate\Http\Request; |
||||
|
use Illuminate\Support\Facades\Validator; |
||||
|
use Illuminate\Validation\ValidationException; |
||||
|
|
||||
|
class GuardBoothManagementController extends BaseController |
||||
|
{ |
||||
|
protected string $menuUri = 'guardBoothManagement'; |
||||
|
|
||||
|
/** |
||||
|
* @var ParkingGuardBoothService |
||||
|
*/ |
||||
|
protected ParkingGuardBoothService $service; |
||||
|
|
||||
|
/** |
||||
|
* 构造函数 |
||||
|
* @param ApiResponseService $responseService |
||||
|
* @param ParkingGuardBoothService $service |
||||
|
*/ |
||||
|
public function __construct( |
||||
|
ApiResponseService $responseService, |
||||
|
ParkingGuardBoothService $service |
||||
|
) { |
||||
|
parent::__construct($responseService); |
||||
|
$this->service = $service; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Request $request |
||||
|
* @return JsonResponse |
||||
|
*/ |
||||
|
public function index(Request $request): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$query = ParkingGuardBooth::query(); |
||||
|
|
||||
|
if ($request->has('name')) { |
||||
|
$name = $request->input('name'); |
||||
|
if ($name) { |
||||
|
$query->where('name', 'like', "%{$name}%"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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); |
||||
|
$total = $query->count(); |
||||
|
$items = $query->latest()->forPage($page, $perPage)->get()->each( |
||||
|
function ($item) { |
||||
|
$item['name'] |
||||
|
= AdminTranslationService::getTranslationTypeName( |
||||
|
$item['id'], |
||||
|
7, |
||||
|
$item['name'] |
||||
|
); |
||||
|
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() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @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' |
||||
|
]; |
||||
|
$messages = [ |
||||
|
'name.required' => __( |
||||
|
'validation.guard_booth_management.n_empty' |
||||
|
) |
||||
|
]; |
||||
|
if ($id) { |
||||
|
$this->validateId($id, ParkingGuardBooth::class); |
||||
|
} |
||||
|
$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, ParkingGuardBooth::class); |
||||
|
$data = [ |
||||
|
'item' => ParkingGuardBooth::query()->find($id) |
||||
|
]; |
||||
|
$Translation = AdminTranslationService::getTranslation( |
||||
|
$data['item']['id'], |
||||
|
7 |
||||
|
); |
||||
|
$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, ParkingGuardBooth::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( |
||||
|
__('exception.admin_floor.destroy_failed') . ':' |
||||
|
. $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,136 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Services; |
||||
|
|
||||
|
use App\Models\ParkingGuardBooth; |
||||
|
use Exception; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
class ParkingGuardBoothService extends BaseService |
||||
|
{ |
||||
|
protected string $menuTitle = 'guard_booth_management'; |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function createModel(array $data) |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
$existsWhere = [ |
||||
|
['name', '=', $data['name']] |
||||
|
]; |
||||
|
if (ParkingGuardBooth::query()->where($existsWhere)->exists()) { |
||||
|
throw new Exception( |
||||
|
__('service.guard_booth_management.name_exists') |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
$model = ParkingGuardBooth::query()->create([ |
||||
|
'name' => $data['name'], |
||||
|
'remake' => $data['remake'] ?? '', |
||||
|
'created_at' => get_datetime() |
||||
|
]); |
||||
|
|
||||
|
$this->logService->logCreated( |
||||
|
$model, |
||||
|
'guard_booth_management.create' |
||||
|
); |
||||
|
|
||||
|
AdminTranslationService::saveTranslation( |
||||
|
$data['name'], |
||||
|
$data['en_name'] ?? '', |
||||
|
$data['tw_name'] ?? '', |
||||
|
$model->id, |
||||
|
7 |
||||
|
); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return $model; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @param int $id |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function updateModel(array $data, int $id) |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
// 验证 |
||||
|
$existsWhere = [ |
||||
|
['name', '=', $data['name']], |
||||
|
['id', '<>', $id] |
||||
|
]; |
||||
|
if (ParkingGuardBooth::query()->where($existsWhere)->exists()) { |
||||
|
throw new Exception( |
||||
|
__('service.guard_booth_management.name_exists') |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
// 更新 |
||||
|
$model = ParkingGuardBooth::query()->findOrFail($id); |
||||
|
$oldValues = $model->toArray(); |
||||
|
|
||||
|
$model->update([ |
||||
|
'name' => $data['name'], |
||||
|
'remake' => $data['remake'] ?? '', |
||||
|
'updated_at' => get_datetime() |
||||
|
]); |
||||
|
|
||||
|
$this->logService->logUpdated( |
||||
|
$model, |
||||
|
$oldValues, |
||||
|
'guard_booth_management.update' |
||||
|
); |
||||
|
|
||||
|
AdminTranslationService::saveTranslation( |
||||
|
$data['name'], |
||||
|
$data['en_name'] ?? '', |
||||
|
$data['tw_name'] ?? '', |
||||
|
$id, |
||||
|
7 |
||||
|
); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return $model; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $id |
||||
|
* @return bool |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function deleteModel($id): bool |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
$model = ParkingGuardBooth::query()->findOrFail($id); |
||||
|
|
||||
|
$this->logService->logDeleted($model, 'guard_booth_management.delete'); |
||||
|
|
||||
|
$model->delete(); |
||||
|
|
||||
|
AdminTranslationService::syncDelete($id, 7); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return true; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue