'integer', 'cleanup_type' => 'integer', 'conditions' => 'array', 'priority' => 'integer', 'batch_size' => 'integer', 'is_enabled' => 'boolean', 'backup_enabled' => 'boolean', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 获取清理类型枚举 */ public function getCleanupTypeEnumAttribute(): CLEANUP_TYPE { return CLEANUP_TYPE::from($this->cleanup_type); } /** * 获取清理类型描述 */ public function getCleanupTypeNameAttribute(): string { return $this->getCleanupTypeEnumAttribute()->getDescription(); } /** * 获取启用状态文本 */ public function getEnabledTextAttribute(): string { return $this->is_enabled ? '启用' : '禁用'; } /** * 获取启用状态颜色 */ public function getEnabledColorAttribute(): string { return $this->is_enabled ? 'success' : 'secondary'; } /** * 获取备份状态文本 */ public function getBackupTextAttribute(): string { return $this->backup_enabled ? '启用' : '禁用'; } /** * 获取备份状态颜色 */ public function getBackupColorAttribute(): string { return $this->backup_enabled ? 'success' : 'warning'; } /** * 获取优先级文本 */ 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->getCleanupTypeEnumAttribute()->needsConditions(); } /** * 判断是否支持回滚 */ public function getIsRollbackableAttribute(): bool { return $this->getCleanupTypeEnumAttribute()->isRollbackable(); } /** * 获取时间字段 */ public function getTimeFieldAttribute(): ?string { return $this->conditions['time_field'] ?? null; } /** * 获取时间条件 */ public function getTimeConditionAttribute(): ?string { return $this->conditions['time_condition'] ?? null; } /** * 获取时间值 */ public function getTimeValueAttribute(): ?int { return $this->conditions['time_value'] ?? null; } /** * 获取时间单位 */ public function getTimeUnitAttribute(): ?string { return $this->conditions['time_unit'] ?? null; } /** * 获取用户字段 */ public function getUserFieldAttribute(): ?string { return $this->conditions['user_field'] ?? null; } /** * 获取用户条件 */ public function getUserConditionAttribute(): ?string { return $this->conditions['user_condition'] ?? null; } /** * 获取用户值列表 */ public function getUserValuesAttribute(): array { return $this->conditions['user_values'] ?? []; } /** * 获取自定义条件 */ public function getCustomConditionsAttribute(): array { return $this->conditions['conditions'] ?? []; } /** * 获取条件逻辑 */ public function getConditionLogicAttribute(): string { return $this->conditions['logic'] ?? 'AND'; } /** * 获取条件描述 */ public function getConditionsDescriptionAttribute(): string { if (!$this->needs_conditions || empty($this->conditions)) { return '无条件'; } $descriptions = []; switch ($this->cleanup_type) { case CLEANUP_TYPE::DELETE_BY_TIME->value: if ($this->time_field && $this->time_value && $this->time_unit) { $descriptions[] = "删除 {$this->time_field} 字段 {$this->time_value} {$this->time_unit} 前的记录"; } break; case CLEANUP_TYPE::DELETE_BY_USER->value: if ($this->user_field && !empty($this->user_values)) { $userList = implode(', ', array_slice($this->user_values, 0, 3)); if (count($this->user_values) > 3) { $userList .= ' 等' . count($this->user_values) . '个用户'; } $descriptions[] = "删除 {$this->user_field} 为 {$userList} 的记录"; } break; case CLEANUP_TYPE::DELETE_BY_CONDITION->value: if (!empty($this->custom_conditions)) { $descriptions[] = "自定义条件:" . count($this->custom_conditions) . " 个条件"; } break; } return empty($descriptions) ? '条件配置不完整' : implode('; ', $descriptions); } /** * 关联清理计划 */ public function plan(): BelongsTo { return $this->belongsTo(CleanupPlan::class, 'plan_id'); } /** * 关联清理配置 */ public function config(): BelongsTo { return $this->belongsTo(CleanupConfig::class, 'table_name', 'table_name'); } /** * 作用域:按计划筛选 */ public function scopeByPlan($query, int $planId) { return $query->where('plan_id', $planId); } /** * 作用域:按表名筛选 */ public function scopeByTable($query, string $tableName) { return $query->where('table_name', $tableName); } /** * 作用域:只查询启用的内容 */ public function scopeEnabled($query) { return $query->where('is_enabled', true); } /** * 作用域:按优先级排序 */ public function scopeOrderByPriority($query) { return $query->orderBy('priority')->orderBy('table_name'); } /** * 作用域:按清理类型筛选 */ public function scopeByCleanupType($query, int $type) { return $query->where('cleanup_type', $type); } /** * 作用域:启用备份的内容 */ public function scopeBackupEnabled($query) { return $query->where('backup_enabled', true); } /** * 作用域:按表名搜索 */ public function scopeSearchTable($query, string $search) { return $query->where('table_name', 'like', "%{$search}%"); } /** * 获取清理类型统计 */ public static function getCleanupTypeStats(int $planId = null): array { $query = static::selectRaw('cleanup_type, COUNT(*) as count') ->groupBy('cleanup_type'); if ($planId) { $query->where('plan_id', $planId); } $stats = $query->get()->keyBy('cleanup_type')->toArray(); $result = []; foreach (CLEANUP_TYPE::cases() as $type) { $result[$type->value] = [ 'name' => $type->getDescription(), 'count' => $stats[$type->value]['count'] ?? 0, ]; } return $result; } /** * 获取启用状态统计 */ public static function getEnabledStats(int $planId = null): array { $query = static::query(); if ($planId) { $query->where('plan_id', $planId); } return [ 'enabled' => $query->clone()->where('is_enabled', true)->count(), 'disabled' => $query->clone()->where('is_enabled', false)->count(), 'backup_enabled' => $query->clone()->where('backup_enabled', true)->count(), 'total' => $query->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 getTargetName(): string { return $this->model_class ?: $this->table_name; } /** * 检查是否基于Model类 */ public function isModelBased(): bool { return !empty($this->model_class); } /** * 检查Model是否支持软删除 */ public function supportsSoftDeletes(): bool { if (!$this->isModelBased()) { 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 getRecordCount(): int { if ($this->isModelBased()) { try { $modelClass = $this->model_class; return $modelClass::count(); } catch (\Exception $e) { // 如果Model有问题,回退到直接查询表 } } return \DB::table($this->table_name)->count(); } /** * 作用域:只查询有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', ''); }); } /** * 作用域:按Model类筛选 */ public function scopeByModel($query, string $modelClass) { return $query->where('model_class', $modelClass); } }