4 changed files with 432 additions and 0 deletions
@ -0,0 +1,197 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers\Admin; |
|||
|
|||
use App\Exceptions\CustomException; |
|||
use App\Models\AdminFloor; |
|||
use App\Models\AdminFloorRegion; |
|||
use App\Services\AdminFloorService; |
|||
use App\Services\ApiResponseService; |
|||
use Exception; |
|||
use Illuminate\Http\JsonResponse; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\Validator; |
|||
use Illuminate\Validation\ValidationException; |
|||
|
|||
class FloorController extends BaseController |
|||
{ |
|||
/** |
|||
* @var ApiResponseService |
|||
*/ |
|||
protected ApiResponseService $responseService; |
|||
|
|||
/** |
|||
* @var AdminFloorService |
|||
*/ |
|||
protected AdminFloorService $AdminFloorService; |
|||
|
|||
/** |
|||
* 构造函数 |
|||
* @param ApiResponseService $responseService |
|||
* @param AdminFloorService $AdminFloorService |
|||
*/ |
|||
public function __construct( |
|||
ApiResponseService $responseService, |
|||
AdminFloorService $AdminFloorService, |
|||
) { |
|||
$this->responseService = $responseService; |
|||
$this->AdminFloorService = $AdminFloorService; |
|||
} |
|||
|
|||
/** |
|||
* @param Request $request |
|||
* @return JsonResponse |
|||
*/ |
|||
public function index(Request $request): JsonResponse |
|||
{ |
|||
try { |
|||
$query = AdminFloor::query(); |
|||
|
|||
// 分页 |
|||
$page = $request->input('page', 1); |
|||
$perPage = $request->input('per_page', 10); |
|||
|
|||
$total = $query->count(); |
|||
$items = $query->latest()->forPage($page, $perPage)->get(); |
|||
|
|||
return $this->responseService->success([ |
|||
'items' => $items, |
|||
'total' => $total, |
|||
'page' => $page, |
|||
'per_page' => $perPage, |
|||
'last_page' => ceil($total / $perPage), |
|||
]); |
|||
} catch (Exception $e) { |
|||
$m_prefix = __('exception.get_user_info_list_failed'); |
|||
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->AdminFloorService->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 |
|||
*/ |
|||
public function saveValidator(array $data, int $id = 0): void |
|||
{ |
|||
$rules = [ |
|||
'name' => 'required|max:50', |
|||
'region_data' => 'array' |
|||
]; |
|||
$messages = [ |
|||
'name.required' => __('validation.admin_floor.n_empty'), |
|||
'name.max' => __('validation.admin_floor.n_max'), |
|||
'region_data.array' => __('validation.admin_floor.r_array') |
|||
]; |
|||
if ($id) { |
|||
$this->validateId($id, AdminFloor::class); |
|||
} |
|||
$validator = Validator::make($data, $rules, $messages); |
|||
|
|||
if ($validator->fails()) { |
|||
throw new ValidationException($validator); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Show the form for editing the specified resource. |
|||
*/ |
|||
public function edit(string $id): JsonResponse |
|||
{ |
|||
try { |
|||
$this->validateId($id, AdminFloor::class); |
|||
$data = [ |
|||
'region_list' => AdminFloorRegion::getFloorRegion($id), |
|||
'item' => AdminFloor::query() |
|||
->where('id', $id) |
|||
->get() |
|||
->toArray() |
|||
]; |
|||
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->AdminFloorService->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, AdminFloor::class); |
|||
$this->AdminFloorService->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,22 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Database\Eloquent\SoftDeletes; |
|||
|
|||
class AdminFloor extends Model |
|||
{ |
|||
use HasFactory, SoftDeletes; |
|||
|
|||
protected $table = 'admin_floor'; |
|||
|
|||
protected $hidden = [ |
|||
'updated_at', |
|||
'created_at', |
|||
'deleted_at' |
|||
]; |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
|
|||
class AdminFloorRegion extends Model |
|||
{ |
|||
use HasFactory; |
|||
|
|||
protected $table = 'admin_floor_region'; |
|||
|
|||
protected $hidden |
|||
= [ |
|||
'created_at', |
|||
'updated_at', |
|||
'deleted_at' |
|||
]; |
|||
|
|||
/** |
|||
* @param int $floor_id |
|||
* @return array |
|||
*/ |
|||
public static function getFloorRegion(int $floor_id): array |
|||
{ |
|||
$columns = ['id', 'name']; |
|||
return self::query()->where('floor_id', $floor_id)->select($columns) |
|||
->get()->toArray(); |
|||
} |
|||
|
|||
public static function existsRoleMenu($floor_id, $nameArr): bool |
|||
{ |
|||
$count = count($nameArr); |
|||
$existsCount = self::query()->where('floor_id', $floor_id) |
|||
->whereIn('name', $nameArr)->count(); |
|||
return $count != $existsCount; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,173 @@ |
|||
<?php |
|||
|
|||
namespace App\Services; |
|||
|
|||
use App\Models\AdminFloor; |
|||
use App\Models\AdminFloorRegion; |
|||
use Exception; |
|||
use Illuminate\Database\Eloquent\Builder; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class AdminFloorService |
|||
{ |
|||
|
|||
/** |
|||
* @var OperationLogService |
|||
*/ |
|||
private OperationLogService $logService; |
|||
|
|||
/** |
|||
* 构造函数 |
|||
* @param OperationLogService $logService |
|||
*/ |
|||
public function __construct(OperationLogService $logService) |
|||
{ |
|||
$this->logService = $logService; |
|||
} |
|||
|
|||
/** |
|||
* 创建角色 |
|||
* @param array $data |
|||
* @return Model|Builder |
|||
* @throws Exception |
|||
*/ |
|||
public function createModel(array $data): Model|Builder |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
$region_data = $data['region_data']; |
|||
|
|||
if (AdminFloor::query()->where('name', $data['name'])->exists()) { |
|||
throw new Exception(__('service.admin_floor.name_exists')); |
|||
} |
|||
|
|||
$model = AdminFloor::query()->create([ |
|||
'name' => $data['name'], |
|||
'image_url' => $data['image_url'], |
|||
'created_at' => get_datetime() |
|||
]); |
|||
|
|||
$this->logService->logCreated($model, '创建楼层'); |
|||
|
|||
$this->addAdminFloorRegion($model->id, $region_data); |
|||
|
|||
DB::commit(); |
|||
return $model; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 同步添加区域关联数据 |
|||
* @param int $floor_id |
|||
* @param array $regionData |
|||
* @return void |
|||
*/ |
|||
private function addAdminFloorRegion(int $floor_id, array $regionData): void |
|||
{ |
|||
$regionSaveData = []; |
|||
foreach ($regionData as $value) { |
|||
$regionSaveData[] = [ |
|||
'floor_id' => $floor_id, |
|||
'name' => $value['name'], |
|||
'created_at' => get_datetime() |
|||
]; |
|||
} |
|||
AdminFloorRegion::query()->insert($regionSaveData); |
|||
$AdminRoleMenuData = AdminFloorRegion::getFloorRegion($floor_id); |
|||
$this->logService->logCreatedData( |
|||
new AdminFloorRegion(), |
|||
'创建楼层关联区域', |
|||
$AdminRoleMenuData |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* @param array $data |
|||
* @param int $id |
|||
* @return Model|Builder |
|||
* @throws Exception |
|||
*/ |
|||
public function updateModel(array $data, int $id): Model|Builder |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
$region_data = $data['region_data']; |
|||
|
|||
// 验证 |
|||
$existsWhere = [ |
|||
['name', '=', $data['name']], |
|||
['id', '<>', $id] |
|||
]; |
|||
if (AdminFloor::query()->where($existsWhere)->exists()) { |
|||
throw new Exception(__('service.admin_role.name_exists')); |
|||
} |
|||
|
|||
// 更新 |
|||
$model = AdminFloor::query()->findOrFail($id); |
|||
$oldValues = $model->toArray(); |
|||
|
|||
$model->update([ |
|||
'name' => $data['name'], |
|||
'image_url' => $data['image_url'], |
|||
'updated_at' => get_datetime() |
|||
]); |
|||
|
|||
$this->logService->logUpdated($model, $oldValues, '更新楼层'); |
|||
|
|||
// 删除再创建关联 |
|||
$nameArr = array_column($region_data, 'name'); |
|||
if (AdminFloorRegion::existsRoleMenu($id, $nameArr)) { |
|||
$this->delAdminFloorRegion($id); |
|||
$this->addAdminFloorRegion($id, $region_data); |
|||
} |
|||
|
|||
DB::commit(); |
|||
return $model; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
protected function delAdminFloorRegion($floor_id): void |
|||
{ |
|||
$oldData = AdminFloorRegion::query()->where('floor_id', $floor_id) |
|||
->select()->get()->toArray(); |
|||
$this->logService->logDeletedData( |
|||
new AdminFloorRegion(), |
|||
'删除角色关联菜单', |
|||
$oldData |
|||
); |
|||
AdminFloorRegion::query()->where('floor_id', $floor_id)->delete(); |
|||
} |
|||
|
|||
/** |
|||
* @param $id |
|||
* @return bool |
|||
* @throws Exception |
|||
*/ |
|||
public function deleteModel($id): bool |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$model = AdminFloor::query()->findOrFail($id); |
|||
|
|||
$this->logService->logDeleted($model, '删除楼层'); |
|||
|
|||
$model->delete(); |
|||
|
|||
$this->delAdminFloorRegion($id); |
|||
|
|||
DB::commit(); |
|||
return true; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue