| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace App\Module\Cleanup\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- /**
- * 清理计划模型
- *
- * 存储清理计划信息,如"农场模块清理"
- */
- class CleanupPlan extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'cleanup_plans';
- // field start
- /**
- * @property int $id 主键ID
- * @property string $plan_name 计划名称
- * @property array $selected_tables 选择的Model类列表
- * @property array $global_conditions 全局清理条件
- * @property array $backup_config 备份配置
- * @property bool $is_template 是否为模板
- * @property bool $is_enabled 是否启用
- * @property string $description 计划描述
- * @property int $created_by 创建者用户ID
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- 'selected_tables' => 'array',
- 'global_conditions' => 'array',
- 'backup_config' => 'array',
- 'is_template' => 'boolean',
- 'is_enabled' => 'boolean',
- 'created_by' => 'integer',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 获取计划类型颜色
- */
- public function getPlanTypeColorAttribute(): string
- {
- return $this->getPlanTypeEnumAttribute()->getColor();
- }
- /**
- * 获取启用状态文本
- */
- public function getEnabledTextAttribute(): string
- {
- return $this->is_enabled ? '启用' : '禁用';
- }
- /**
- * 获取启用状态颜色
- */
- public function getEnabledColorAttribute(): string
- {
- return $this->is_enabled ? 'success' : 'secondary';
- }
- /**
- * 获取模板状态文本
- */
- public function getTemplateTextAttribute(): string
- {
- return $this->is_template ? '模板' : '计划';
- }
- /**
- * 获取模板状态颜色
- */
- public function getTemplateColorAttribute(): string
- {
- return $this->is_template ? 'info' : 'primary';
- }
- /**
- * 判断是否需要目标选择配置
- */
- public function getNeedsTargetSelectionAttribute(): bool
- {
- return $this->getPlanTypeEnumAttribute()->needsTargetSelection();
- }
- /**
- * 获取目标选择类型
- */
- public function getSelectionTypeAttribute(): ?string
- {
- return $this->target_selection['selection_type'] ?? null;
- }
- /**
- * 获取目标模块列表
- */
- public function getTargetModulesAttribute(): array
- {
- return $this->target_selection['modules'] ?? [];
- }
- /**
- * 获取目标分类列表
- */
- public function getTargetCategoriesAttribute(): array
- {
- return $this->target_selection['categories'] ?? [];
- }
- /**
- * 获取目标表列表
- */
- public function getTargetTablesAttribute(): array
- {
- return $this->target_selection['tables'] ?? [];
- }
- /**
- * 获取排除表列表
- */
- public function getExcludeTablesAttribute(): array
- {
- return $this->target_selection['exclude_tables'] ?? [];
- }
- /**
- * 获取排除模块列表
- */
- public function getExcludeModulesAttribute(): array
- {
- return $this->target_selection['exclude_modules'] ?? [];
- }
- /**
- * 获取排除分类列表
- */
- public function getExcludeCategoriesAttribute(): array
- {
- return $this->target_selection['exclude_categories'] ?? [];
- }
- /**
- * 获取备份类型
- */
- public function getBackupTypeAttribute(): ?int
- {
- return $this->backup_config['backup_type'] ?? null;
- }
- /**
- * 获取压缩类型
- */
- public function getCompressionTypeAttribute(): ?int
- {
- return $this->backup_config['compression_type'] ?? null;
- }
- /**
- * 判断是否包含表结构
- */
- public function getIncludeStructureAttribute(): bool
- {
- return $this->backup_config['include_structure'] ?? true;
- }
- /**
- * 关联计划内容
- */
- public function contents(): HasMany
- {
- return $this->hasMany(CleanupPlanContent::class, 'plan_id');
- }
- /**
- * 关联启用的计划内容
- */
- public function enabledContents(): HasMany
- {
- return $this->contents()->where('is_enabled', true);
- }
- /**
- * 关联清理任务
- */
- public function tasks(): HasMany
- {
- return $this->hasMany(CleanupTask::class, 'plan_id');
- }
- /**
- * 关联备份记录
- */
- public function backups(): HasMany
- {
- return $this->hasMany(CleanupBackup::class, 'plan_id');
- }
- /**
- * 作用域:按计划类型筛选
- */
- public function scopeByType($query, int $type)
- {
- return $query->where('plan_type', $type);
- }
- /**
- * 作用域:只查询启用的计划
- */
- public function scopeEnabled($query)
- {
- return $query->where('is_enabled', true);
- }
- /**
- * 作用域:只查询模板
- */
- public function scopeTemplates($query)
- {
- return $query->where('is_template', true);
- }
- /**
- * 作用域:只查询非模板
- */
- public function scopeNonTemplates($query)
- {
- return $query->where('is_template', false);
- }
- /**
- * 作用域:按计划名称搜索
- */
- public function scopeSearchName($query, string $search)
- {
- return $query->where('plan_name', 'like', "%{$search}%");
- }
- /**
- * 作用域:按创建者筛选
- */
- public function scopeByCreator($query, int $createdBy)
- {
- return $query->where('created_by', $createdBy);
- }
- /**
- * 获取计划内容数量
- */
- public function getContentsCountAttribute(): int
- {
- return $this->contents()->count();
- }
- /**
- * 获取启用的内容数量
- */
- public function getEnabledContentsCountAttribute(): int
- {
- return $this->enabledContents()->count();
- }
- /**
- * 获取任务数量
- */
- public function getTasksCountAttribute(): int
- {
- return $this->tasks()->count();
- }
- /**
- * 获取备份数量
- */
- public function getBackupsCountAttribute(): int
- {
- return $this->backups()->count();
- }
- /**
- * 获取最后执行时间
- */
- public function getLastExecutedAtAttribute(): ?string
- {
- $lastTask = $this->tasks()
- ->whereNotNull('completed_at')
- ->orderBy('completed_at', 'desc')
- ->first();
- return $lastTask ? $lastTask->completed_at->format('Y-m-d H:i:s') : null;
- }
- /**
- * 获取启用状态统计
- */
- public static function getEnabledStats(): array
- {
- return [
- 'enabled' => static::where('is_enabled', true)->count(),
- 'disabled' => static::where('is_enabled', false)->count(),
- 'templates' => static::where('is_template', true)->count(),
- 'total' => static::count(),
- ];
- }
- }
|