logService = $logService; $this->logService->menuTitle = 'vip_list'; } /** * @param array $data * @throws Exception */ public function createModel(array $data) { try { DB::beginTransaction(); if (ParkingVipList::query()->where('license', $data['license']) ->exists() ) { throw new Exception( __('service.admin_vip_list.license_exists') ); } $model = ParkingVipList::query()->create([ 'license' => $data['license'], 'user_id' => Auth::guard('sanctum')->user()['id'], 'created_at' => get_datetime() ]); $this->logService->logCreated($model, 'vip_list.create'); 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 = [ ['license', '=', $data['license']], ['id', '<>', $id] ]; if (ParkingVipList::query()->where($existsWhere)->exists()) { throw new Exception(__('service.admin_vip_list.license_exists')); } // 更新 $model = ParkingVipList::query()->findOrFail($id); $oldValues = $model->toArray(); $model->update([ 'license' => $data['license'], 'user_id' => Auth::guard('sanctum')->user()['id'], 'updated_at' => get_datetime() ]); $this->logService->logUpdated($model, $oldValues, 'vip_list.update'); 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 = ParkingVipList::query()->findOrFail($id); $this->logService->logDeleted($model, 'vip_list.delete'); $model->delete(); DB::commit(); return true; } catch (Exception $e) { DB::rollBack(); throw $e; } } }