| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?php
- namespace App\Module\Cleanup\Models;
- use App\Module\Cleanup\Enums\CLEANUP_TYPE;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 计划内容模型
- *
- * 存储计划的具体内容,即计划具体处理哪些表,怎么清理
- */
- class CleanupPlanContent extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'cleanup_plan_contents';
- // field start
- /**
- * @property int $id 主键ID
- * @property int $plan_id 计划ID
- * @property string $table_name 表名
- * @property int $cleanup_type 清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除
- * @property array $conditions 清理条件JSON配置
- * @property int $priority 清理优先级
- * @property int $batch_size 批处理大小
- * @property bool $is_enabled 是否启用
- * @property bool $backup_enabled 是否启用备份
- * @property string $notes 备注说明
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- 'plan_id' => '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(),
- ];
- }
- }
|