logService = $logService; $this->logService->menuTitle = 'license_plate_management'; } /** * 创建车位属性 * @param array $data * @throws Exception */ public function createModel(array $data) { try { DB::beginTransaction(); if (ParkingSpaceAttributes::query()->where( 'attributes', $data['attributes'] )->exists() ) { throw new Exception( __('service.space_attributes.attributes_exists') ); } $model = ParkingSpaceAttributes::query()->create([ 'attributes' => $data['attributes'], 'import_diagram' => $data['import_diagram'], 'created_at' => get_datetime() ]); $this->logService->logCreated($model, 'space_attributes.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 = [ ['attributes', '=', $data['attributes']], ['id', '<>', $id] ]; if ( ParkingSpaceAttributes::query()->where($existsWhere)->exists() ) { throw new Exception( __('service.space_attributes.attributes_exists') ); } // 更新 $model = ParkingSpaceAttributes::query()->findOrFail($id); $oldValues = $model->toArray(); $model->update([ 'attributes' => $data['attributes'], 'import_diagram' => $data['import_diagram'], 'updated_at' => get_datetime() ]); $this->logService->logUpdated($model, $oldValues, 'space_attributes.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 = ParkingSpaceAttributes::query()->findOrFail($id); $this->logService->logDeleted($model, 'space_attributes.delete'); $model->delete(); DB::commit(); return true; } catch (Exception $e) { DB::rollBack(); throw $e; } } }