| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?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';
- // attrlist start
- /**
- * 可批量赋值的属性
- */
- protected $fillable = [
- 'model_class',
- 'table_name',
- 'model_info',
- 'module_name',
- 'data_category',
- 'default_cleanup_type',
- 'default_conditions',
- 'is_enabled',
- 'priority',
- 'batch_size',
- 'description',
- 'last_cleanup_at',
- ];
- // attrlist end
- // field start
- /**
- * @property int $id 主键ID
- * @property string $model_class Model类名
- * @property string $table_name 表名(兼容性保留)
- * @property array $model_info Model类信息JSON
- * @property string $module_name 模块名称
- * @property int $data_category 数据分类:1用户数据,2日志数据,3交易数据,4缓存数据,5配置数据
- * @property int $default_cleanup_type 默认清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除
- * @property array $default_conditions 默认清理条件JSON配置
- * @property bool $is_enabled 是否启用清理
- * @property int $priority 清理优先级(数字越小优先级越高)
- * @property int $batch_size 批处理大小
- * @property string $description 配置描述
- * @property \Carbon\Carbon $last_cleanup_at 最后清理时间
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- 'model_info' => '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', '');
- });
- }
- }
|