11 changed files with 495 additions and 9 deletions
@ -0,0 +1,254 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers\Admin; |
|||
|
|||
use App\Exceptions\CustomException; |
|||
use App\Models\ParkingAdminUser; |
|||
use App\Services\ApiResponseService; |
|||
use App\Services\ParkingAdminUserService; |
|||
use Exception; |
|||
use Illuminate\Http\JsonResponse; |
|||
use Illuminate\Http\Request; |
|||
use Illuminate\Support\Facades\Validator; |
|||
use Illuminate\Validation\ValidationException; |
|||
|
|||
class parkingAttendantController extends BaseController |
|||
{ |
|||
protected string $menuUri = 'parkingAttendant'; |
|||
|
|||
/** |
|||
* @var ParkingAdminUserService |
|||
*/ |
|||
protected ParkingAdminUserService $service; |
|||
|
|||
/** |
|||
* @param ApiResponseService $responseService |
|||
* @param ParkingAdminUserService $service |
|||
*/ |
|||
public function __construct( |
|||
ApiResponseService $responseService, |
|||
ParkingAdminUserService $service |
|||
) { |
|||
parent::__construct($responseService); |
|||
$this->service = $service; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* @return JsonResponse |
|||
*/ |
|||
public function search(): JsonResponse |
|||
{ |
|||
try { |
|||
$data = [ |
|||
'member_type_list' => get_select_data( |
|||
ParkingAdminUserService::$memberType |
|||
) |
|||
]; |
|||
return $this->responseService->success($data); |
|||
} catch (Exception $e) { |
|||
$m_prefix = __('exception.exception_handler.resource'); |
|||
return $this->responseService->systemError( |
|||
$m_prefix . ':' . $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
public function index(Request $request): JsonResponse |
|||
{ |
|||
try { |
|||
$query = ParkingAdminUser::query(); |
|||
|
|||
if ($request->has('name')) { |
|||
$name = $request->input('name'); |
|||
if (!empty($name)) { |
|||
$query->where('name', 'like', "%{$name}%"); |
|||
} |
|||
} |
|||
|
|||
if ($request->has('phone')) { |
|||
$phone = $request->input('phone'); |
|||
if (!empty($phone)) { |
|||
$query->where('phone', $phone); |
|||
} |
|||
} |
|||
|
|||
if ($request->has('member_type')) { |
|||
$member_type = $request->input('member_type'); |
|||
if (!empty($member_type)) { |
|||
$query->where('member_type', $member_type); |
|||
} |
|||
} |
|||
|
|||
// 分页 |
|||
$page = $request->input('page', 1); |
|||
$perPage = $request->input('per_page', 10); |
|||
$query->orderBy('id'); |
|||
$total = $query->count(); |
|||
$memberType = ParkingAdminUserService::$memberType; |
|||
|
|||
$items = $query->latest()->forPage($page, $perPage)->get()->each( |
|||
function ($item) use ($memberType) { |
|||
$item['member_type'] = $memberType[$item['member_type']]; |
|||
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 |
|||
*/ |
|||
protected function saveValidator(array $data, int $id = 0): void |
|||
{ |
|||
$rules = [ |
|||
'name' => 'required', |
|||
'phone' => 'required', |
|||
'member_type' => 'required', |
|||
]; |
|||
$messages = [ |
|||
'name.required' => __( |
|||
'validation.parking_attendant.n_empty' |
|||
), |
|||
'phone.required' => __( |
|||
'validation.parking_attendant.p_empty' |
|||
), |
|||
'member_type.required' => __( |
|||
'validation.parking_attendant.m_empty' |
|||
), |
|||
]; |
|||
$model = ParkingAdminUser::query(); |
|||
|
|||
if ($id) { |
|||
$this->validateId($id, ParkingAdminUser::class); |
|||
$model->where([ |
|||
['name', '=', $data['name']], |
|||
['id', '<>', $id] |
|||
]); |
|||
} else { |
|||
$model->where('name', $data['name']); |
|||
} |
|||
|
|||
if ($model->exists()) { |
|||
throw new CustomException( |
|||
__('validation.parking_attendant.name_exists') |
|||
); |
|||
} |
|||
|
|||
$validator = Validator::make($data, $rules, $messages); |
|||
|
|||
if ($validator->fails()) { |
|||
throw new ValidationException($validator); |
|||
} |
|||
} |
|||
|
|||
public function edit($id): JsonResponse |
|||
{ |
|||
try { |
|||
$this->validateId($id, ParkingAdminUser::class); |
|||
$item = ParkingAdminUser::query()->findOrFail($id); |
|||
$data = [ |
|||
'item' => $item, |
|||
'member_type_list' => get_select_data( |
|||
ParkingAdminUserService::$memberType |
|||
), |
|||
]; |
|||
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); |
|||
$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( |
|||
__('admin.operation_failed') . ':' |
|||
. $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* @param string $id |
|||
* @return JsonResponse |
|||
* @throws ValidationException |
|||
*/ |
|||
public function destroy(string $id): JsonResponse |
|||
{ |
|||
try { |
|||
$this->validateId($id, ParkingAdminUser::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( |
|||
__('admin.operation_failed') . ':' |
|||
. $e->getMessage() |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<?php |
|||
|
|||
namespace App\Models; |
|||
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Database\Eloquent\SoftDeletes; |
|||
|
|||
class ParkingAdminUser extends Model |
|||
{ |
|||
use HasFactory, SoftDeletes; |
|||
|
|||
protected $table = 'parking_admin_user'; |
|||
|
|||
protected $fillable |
|||
= [ |
|||
'name', |
|||
'phone', |
|||
'member_type', |
|||
'status' |
|||
]; |
|||
|
|||
protected $hidden |
|||
= [ |
|||
'updated_at', |
|||
'deleted_at' |
|||
]; |
|||
|
|||
public function getCreatedAtAttribute($value): string |
|||
{ |
|||
return $value ? date("Y-m-d H:i:s", strtotime($value)) : $value; |
|||
} |
|||
} |
|||
@ -0,0 +1,105 @@ |
|||
<?php |
|||
|
|||
namespace App\Services; |
|||
|
|||
|
|||
use App\Models\ParkingAdminUser; |
|||
use Exception; |
|||
use Illuminate\Support\Facades\DB; |
|||
|
|||
class ParkingAdminUserService extends BaseService |
|||
{ |
|||
|
|||
public static array $memberType |
|||
= [ |
|||
1 => 'DIS', |
|||
2 => 'OMHK', |
|||
3 => 'MBHK', |
|||
4 => 'GBA', |
|||
5 => 'YUC', |
|||
6 => 'VMHK', |
|||
7 => 'ST', |
|||
8 => 'RCIM', |
|||
9 => 'OMCN', |
|||
10 => 'MBCN', |
|||
11 => 'PUBLIC' |
|||
]; |
|||
protected string $menuTitle = 'parking_attendant'; |
|||
|
|||
// 创建 |
|||
|
|||
public function createModel(array $data) |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$model = ParkingAdminUser::query()->create([ |
|||
'name' => $data['name'], |
|||
'phone' => $data['phone'], |
|||
'member_type' => $data['member_type'], |
|||
'created_at' => get_datetime() |
|||
]); |
|||
|
|||
$this->logService->logCreated($model, 'parking_attendant.create'); |
|||
|
|||
DB::commit(); |
|||
return $model; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
// 创建 |
|||
public function updateModel(array $data, $id) |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$model = ParkingAdminUser::query()->findOrFail($id); |
|||
$oldValue = $model->toArray(); |
|||
|
|||
$update = [ |
|||
'name' => $data['name'], |
|||
'phone' => $data['phone'], |
|||
'member_type' => $data['member_type'], |
|||
'status' => $data['status'] ?? 1, |
|||
'updated_at' => get_datetime() |
|||
]; |
|||
$model->update($update); |
|||
|
|||
$this->logService->logUpdated( |
|||
$model, |
|||
$oldValue, |
|||
'parking_attendant.update' |
|||
); |
|||
|
|||
DB::commit(); |
|||
return $model; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
public function deleteModel(int $id): bool |
|||
{ |
|||
try { |
|||
DB::beginTransaction(); |
|||
|
|||
$model = ParkingAdminUser::query()->findOrFail($id); |
|||
|
|||
$this->logService->logDeleted( |
|||
$model, |
|||
'parking_attendant.delete' |
|||
); |
|||
|
|||
$model->delete(); |
|||
DB::commit(); |
|||
return true; |
|||
} catch (Exception $e) { |
|||
DB::rollBack(); |
|||
throw $e; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
<?php |
|||
|
|||
use Illuminate\Database\Migrations\Migration; |
|||
use Illuminate\Database\Schema\Blueprint; |
|||
use Illuminate\Support\Facades\Schema; |
|||
|
|||
return new class extends Migration |
|||
{ |
|||
/** |
|||
* Run the migrations. |
|||
*/ |
|||
public function up(): void |
|||
{ |
|||
Schema::create('parking_admin_user', function (Blueprint $table) { |
|||
$table->id(); |
|||
$table->string('name', 50)->default('')->comment('管理人员名称'); |
|||
$table->string('phone', 20)->default('')->comment('手机号'); |
|||
$table->tinyInteger('member_type')->comment('会员类型'); |
|||
$table->tinyInteger('status')->default(1)->comment('状态'); |
|||
$table->timestamps(); |
|||
$table->softDeletes(); |
|||
$table->innoDb(); |
|||
$table->comment('停车场管理人员'); |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* Reverse the migrations. |
|||
*/ |
|||
public function down(): void |
|||
{ |
|||
Schema::dropIfExists('parking_admin_user'); |
|||
} |
|||
}; |
|||
Loading…
Reference in new issue