'array', 'data_category' => 'integer', 'default_cleanup_type' => 'integer', 'default_conditions' => 'array', 'is_enabled' => 'boolean', 'priority' => 'integer', 'batch_size' => 'integer', 'last_cleanup_at' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 获取数据分类枚举 */ public function getDataCategoryEnumAttribute(): DATA_CATEGORY { return DATA_CATEGORY::from($this->data_category); } /** * 获取默认清理类型枚举 */ public function getDefaultCleanupTypeEnumAttribute(): CLEANUP_TYPE { return CLEANUP_TYPE::from($this->default_cleanup_type); } /** * 获取数据分类描述 */ public function getDataCategoryNameAttribute(): string { return $this->getDataCategoryEnumAttribute()->getDescription(); } /** * 获取默认清理类型描述 */ public function getDefaultCleanupTypeNameAttribute(): string { return $this->getDefaultCleanupTypeEnumAttribute()->getDescription(); } /** * 获取数据分类颜色 */ public function getDataCategoryColorAttribute(): string { return $this->getDataCategoryEnumAttribute()->getColor(); } /** * 获取启用状态文本 */ public function getEnabledTextAttribute(): string { return $this->is_enabled ? '启用' : '禁用'; } /** * 获取启用状态颜色 */ public function getEnabledColorAttribute(): string { return $this->is_enabled ? 'success' : 'secondary'; } /** * 获取优先级文本 */ public function getPriorityTextAttribute(): string { if ($this->priority <= 50) { return '高'; } elseif ($this->priority <= 200) { return '中'; } else { return '低'; } } /** * 获取优先级颜色 */ public function getPriorityColorAttribute(): string { if ($this->priority <= 50) { return 'danger'; } elseif ($this->priority <= 200) { return 'warning'; } else { return 'info'; } } /** * 判断是否需要条件配置 */ public function getNeedsConditionsAttribute(): bool { return $this->getDefaultCleanupTypeEnumAttribute()->needsConditions(); } /** * 判断是否支持回滚 */ public function getIsRollbackableAttribute(): bool { return $this->getDefaultCleanupTypeEnumAttribute()->isRollbackable(); } /** * 获取格式化的最后清理时间 */ public function getLastCleanupAtFormattedAttribute(): ?string { return $this->last_cleanup_at ? $this->last_cleanup_at->format('Y-m-d H:i:s') : null; } /** * 获取最后清理时间的相对时间 */ public function getLastCleanupAtHumanAttribute(): ?string { return $this->last_cleanup_at ? $this->last_cleanup_at->diffForHumans() : '从未清理'; } /** * 作用域:按模块筛选 */ public function scopeByModule($query, string $moduleName) { return $query->where('module_name', $moduleName); } /** * 作用域:按数据分类筛选 */ public function scopeByCategory($query, int $category) { return $query->where('data_category', $category); } /** * 作用域:只查询启用的配置 */ public function scopeEnabled($query) { return $query->where('is_enabled', true); } /** * 作用域:按优先级排序 */ public function scopeOrderByPriority($query) { return $query->orderBy('priority')->orderBy('table_name'); } /** * 作用域:按表名搜索 */ public function scopeSearchTable($query, string $search) { return $query->where('table_name', 'like', "%{$search}%"); } /** * 作用域:排除配置数据 */ public function scopeExcludeConfig($query) { return $query->where('data_category', '!=', DATA_CATEGORY::CONFIG_DATA->value); } /** * 获取模块列表 */ public static function getModuleList(): array { return static::distinct('module_name') ->orderBy('module_name') ->pluck('module_name') ->toArray(); } /** * 获取数据分类统计 */ public static function getCategoryStats(): array { $stats = static::selectRaw('data_category, COUNT(*) as count') ->groupBy('data_category') ->get() ->keyBy('data_category') ->toArray(); $result = []; foreach (DATA_CATEGORY::cases() as $category) { $result[$category->value] = [ 'name' => $category->getDescription(), 'count' => $stats[$category->value]['count'] ?? 0, 'color' => $category->getColor(), ]; } return $result; } /** * 获取启用状态统计 */ public static function getEnabledStats(): array { return [ 'enabled' => static::where('is_enabled', true)->count(), 'disabled' => static::where('is_enabled', false)->count(), 'total' => static::count(), ]; } /** * 获取Model实例 */ public function getModelInstance() { if (empty($this->model_class)) { throw new \Exception("Model类名为空"); } if (!class_exists($this->model_class)) { throw new \Exception("Model类不存在: {$this->model_class}"); } return new $this->model_class(); } /** * 获取实际表名(优先从Model获取) */ public function getActualTableName(): string { if (!empty($this->model_class)) { try { return $this->getModelInstance()->getTable(); } catch (\Exception $e) { // 如果Model有问题,回退到table_name } } return $this->table_name; } /** * 获取记录数量(优先使用Model) */ public function getRecordCount(): int { if (!empty($this->model_class)) { try { $modelClass = $this->model_class; return $modelClass::count(); } catch (\Exception $e) { // 如果Model有问题,回退到直接查询表 } } return \DB::table($this->table_name)->count(); } /** * 检查Model是否支持软删除 */ public function supportsSoftDeletes(): bool { if (empty($this->model_class)) { return false; } try { $model = $this->getModelInstance(); return in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive($model)); } catch (\Exception $e) { return false; } } /** * 获取Model信息摘要 */ public function getModelSummary(): array { if (empty($this->model_class)) { return [ 'has_model' => false, 'table_name' => $this->table_name, 'record_count' => $this->getRecordCount(), ]; } try { $model = $this->getModelInstance(); return [ 'has_model' => true, 'model_class' => $this->model_class, 'table_name' => $model->getTable(), 'primary_key' => $model->getKeyName(), 'timestamps' => $model->timestamps, 'soft_deletes' => $this->supportsSoftDeletes(), 'record_count' => $this->getRecordCount(), 'fillable_count' => count($model->getFillable()), 'casts_count' => count($model->getCasts()), ]; } catch (\Exception $e) { return [ 'has_model' => false, 'error' => $e->getMessage(), 'table_name' => $this->table_name, 'record_count' => 0, ]; } } /** * 作用域:只查询有Model类的配置 */ public function scopeWithModel($query) { return $query->whereNotNull('model_class')->where('model_class', '!=', ''); } /** * 作用域:只查询没有Model类的配置(旧数据) */ public function scopeWithoutModel($query) { return $query->where(function($q) { $q->whereNull('model_class')->orWhere('model_class', ''); }); } }