| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Module\Cleanup\Models;
- use App\Module\Cleanup\Enums\CLEANUP_TYPE;
- use App\Module\Cleanup\Enums\DATA_CATEGORY;
- use UCore\ModelCore;
- /**
- * 清理配置模型
- *
- * 存储每个数据表的基础清理配置信息
- */
- class CleanupConfig extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'cleanup_configs';
- // field start
- /**
- * 可批量赋值的字段
- */
- protected $fillable = [
- 'table_name',
- 'module_name',
- 'data_category',
- 'default_cleanup_type',
- 'default_conditions',
- 'is_enabled',
- 'priority',
- 'batch_size',
- 'description',
- 'last_cleanup_at',
- ];
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- '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(),
- ];
- }
- }
|