'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(), ]; } }