14 changed files with 502 additions and 70 deletions
@ -0,0 +1,282 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers\Admin; |
|||
|
|||
use App\Exceptions\CustomException; |
|||
use App\Models\AdminFloor; |
|||
use App\Models\AdminFloorRegion; |
|||
use App\Models\ParkingSpace; |
|||
use App\Services\AdminFloorRegionService; |
|||
use App\Services\AdminFloorService; |
|||
use App\Services\AdminTranslationService; |
|||
use App\Services\ApiResponseService; |
|||
use Exception; |
|||
use Illuminate\Http\JsonResponse; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\Validator; |
|||
use Illuminate\Validation\ValidationException; |
|||
|
|||
class RegionalManagementController extends BaseController |
|||
{ |
|||
|
|||
protected string $menuUri = 'regionalManagement'; |
|||
|
|||
/** |
|||
* @var AdminFloorRegionService |
|||
*/ |
|||
protected AdminFloorRegionService $service; |
|||
|
|||
/** |
|||
* 构造函数 |
|||
* @param ApiResponseService $responseService |
|||
* @param AdminFloorRegionService $service |
|||
*/ |
|||
public function __construct( |
|||
ApiResponseService $responseService, |
|||
AdminFloorRegionService $service |
|||
) { |
|||
parent::__construct($responseService); |
|||
$this->service = $service; |
|||
} |
|||
|
|||
/** |
|||
* @param Request $request |
|||
* @return JsonResponse |
|||
*/ |
|||
public function index(Request $request): JsonResponse |
|||
{ |
|||
try { |
|||
$query = AdminFloorRegion::query(); |
|||
|
|||
if ($request->has('floor_id')) { |
|||
$floor_id = $request->input('floor_id'); |
|||
if ($floor_id) { |
|||
$query->where('floor_id', $floor_id); |
|||
} |
|||
} |
|||
|
|||
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); |
|||
$statusArr = AdminFloorRegionService::getStatus(); |
|||
$total = $query->count(); |
|||
$items = $query->latest()->forPage($page, $perPage)->get()->each( |
|||
function ($item) use ($statusArr) { |
|||
$item['name'] |
|||
= AdminTranslationService::getTranslationTypeName( |
|||
$item['id'], |
|||
5, |
|||
$item['name'] |
|||
); |
|||
$item['floor'] = AdminFloor::getName($item['floor_id']); |
|||
$item['status_str'] = $statusArr[$item['status']]; |
|||
$item['parking_space_count'] = ParkingSpace::getFloorCount( |
|||
$item['floor_id'] |
|||
); |
|||
$open_time_res = option_time($item['open_time']); |
|||
$item['open_time'] = $open_time_res['time']; |
|||
$item['open_time_str'] = $open_time_res['str']; |
|||
$close_time_res = option_time($item['close_time']); |
|||
$item['close_time'] = $close_time_res['time']; |
|||
$item['close_time_str'] = $close_time_res['str']; |
|||
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() |
|||
); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 列表搜索数据 |
|||
* @return JsonResponse |
|||
*/ |
|||
public function search(): JsonResponse |
|||
{ |
|||
try { |
|||
$data = [ |
|||
'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() |
|||
); |
|||
} |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @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( |
|||
__('exception.admin_floor.create_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|max:50', |
|||
'open_time' => 'required|size:5', |
|||
'close_time' => 'required|size:5', |
|||
]; |
|||
$messages = [ |
|||
'name.required' => __('validation.admin_floor.n_empty'), |
|||
'name.max' => __('validation.admin_floor.n_max'), |
|||
'open_time.required' => __( |
|||
'validation.parking_management.o_empty' |
|||
), |
|||
'open_time.size' => __( |
|||
'validation.parking_management.o_length' |
|||
), |
|||
'close_time.required' => __( |
|||
'validation.parking_management.c_empty' |
|||
), |
|||
'close_time.size' => __( |
|||
'validation.parking_management.c_length' |
|||
) |
|||
]; |
|||
if ($id) { |
|||
$this->validateId($id, AdminFloorRegion::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, AdminFloorRegion::class); |
|||
$data = [ |
|||
'building_floor_list' => get_select_data( |
|||
AdminFloorService::getBuildingFloor() |
|||
), |
|||
'floor_list' => AdminFloor::getData(), |
|||
'item' => AdminFloorRegion::query()->find($id) |
|||
]; |
|||
$AdminFloor = AdminFloor::getFirst($data['item']['floor_id']); |
|||
$data['item']['building_floor'] = $AdminFloor['building_floor'] ?? ''; |
|||
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( |
|||
__('exception.admin_floor.update_failed') . ':' |
|||
. $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @param string $id |
|||
* @return JsonResponse |
|||
* @throws CustomException |
|||
* @throws ValidationException |
|||
*/ |
|||
public function destroy(string $id): JsonResponse |
|||
{ |
|||
try { |
|||
$this->validateId($id, AdminFloorRegion::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,148 @@ |
|||
<?php |
|||
|
|||
namespace App\Services; |
|||
|
|||
use App\Models\AdminFloorRegion; |
|||
use Exception; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class AdminFloorRegionService extends BaseService |
|||
{ |
|||
|
|||
private static array $statusArr = ['disabled', 'enable']; |
|||
protected string $menuTitle = 'regional_management'; |
|||
|
|||
/** |
|||
* @return array|string[] |
|||
*/ |
|||
public static function getStatus(): array |
|||
{ |
|||
$statusArr = self::$statusArr; |
|||
foreach ($statusArr as $key => $value) { |
|||
$statusArr[$key] = __('admin.' . $value); |
|||
} |
|||
return $statusArr; |
|||
} |
|||
|
|||
/** |
|||
* 创建角色 |
|||
* @param array $data |
|||
* @throws Exception |
|||
*/ |
|||
public function createModel(array $data) |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$existsWhere = [ |
|||
['name', '=', $data['name']], |
|||
['floor_id', '=', $data['floor_id']] |
|||
]; |
|||
if (AdminFloorRegion::query()->where($existsWhere)->exists()) { |
|||
throw new Exception(__('service.admin_floor.name_exists')); |
|||
} |
|||
|
|||
$model = AdminFloorRegion::query()->create([ |
|||
'name' => $data['name'], |
|||
'floor_id' => $data['floor_id'], |
|||
'open_time' => $data['open_time'], |
|||
'close_time' => $data['close_time'], |
|||
'created_at' => get_datetime() |
|||
]); |
|||
|
|||
$this->logService->logCreated($model, 'admin_floor.region_create'); |
|||
|
|||
AdminTranslationService::saveTranslation( |
|||
$data['name'], |
|||
$data['en_name'] ?? '', |
|||
$data['tw_name'] ?? '', |
|||
$model->id, |
|||
5 |
|||
); |
|||
|
|||
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']], |
|||
['floor_id', '=', $data['floor_id']], |
|||
['id', '<>', $id] |
|||
]; |
|||
if (AdminFloorRegion::query()->where($existsWhere)->exists()) { |
|||
throw new Exception(__('service.admin_role.name_exists')); |
|||
} |
|||
|
|||
// 更新 |
|||
$model = AdminFloorRegion::query()->findOrFail($id); |
|||
$oldValues = $model->toArray(); |
|||
|
|||
$model->update([ |
|||
'name' => $data['name'], |
|||
'floor_id' => $data['floor_id'], |
|||
'open_time' => $data['open_time'], |
|||
'close_time' => $data['close_time'], |
|||
'updated_at' => get_datetime() |
|||
]); |
|||
|
|||
$this->logService->logUpdated( |
|||
$model, |
|||
$oldValues, |
|||
'admin_floor.region_update' |
|||
); |
|||
|
|||
AdminTranslationService::saveTranslation( |
|||
$data['name'], |
|||
$data['en_name'] ?? '', |
|||
$data['tw_name'] ?? '', |
|||
$id, |
|||
5 |
|||
); |
|||
|
|||
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 = AdminFloorRegion::query()->findOrFail($id); |
|||
|
|||
$this->logService->logDeleted($model, 'admin_floor.region_delete'); |
|||
|
|||
$model->delete(); |
|||
|
|||
DB::commit(); |
|||
return true; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue