CleanupPlan.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace App\Module\Cleanup\Models;
  3. use App\Module\Cleanup\Enums\PLAN_TYPE;
  4. use UCore\ModelCore;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. /**
  7. * 清理计划模型
  8. *
  9. * 存储清理计划信息,如"农场模块清理"
  10. */
  11. class CleanupPlan extends ModelCore
  12. {
  13. /**
  14. * 数据表名
  15. */
  16. protected $table = 'cleanup_plans';
  17. // field start
  18. /**
  19. * @property int $id 主键ID
  20. * @property string $plan_name 计划名称
  21. * @property int $plan_type 计划类型:1全量清理,2模块清理,3分类清理,4自定义清理,5混合清理
  22. * @property array $target_selection 目标选择配置
  23. * @property array $global_conditions 全局清理条件
  24. * @property array $backup_config 备份配置
  25. * @property bool $is_template 是否为模板
  26. * @property bool $is_enabled 是否启用
  27. * @property string $description 计划描述
  28. * @property int $created_by 创建者用户ID
  29. * @property \Carbon\Carbon $created_at 创建时间
  30. * @property \Carbon\Carbon $updated_at 更新时间
  31. */
  32. // field end
  33. /**
  34. * 字段类型转换
  35. */
  36. protected $casts = [
  37. 'plan_type' => 'integer',
  38. 'target_selection' => 'array',
  39. 'global_conditions' => 'array',
  40. 'backup_config' => 'array',
  41. 'is_template' => 'boolean',
  42. 'is_enabled' => 'boolean',
  43. 'created_by' => 'integer',
  44. 'created_at' => 'datetime',
  45. 'updated_at' => 'datetime',
  46. ];
  47. /**
  48. * 获取计划类型枚举
  49. */
  50. public function getPlanTypeEnumAttribute(): PLAN_TYPE
  51. {
  52. return PLAN_TYPE::from($this->plan_type);
  53. }
  54. /**
  55. * 获取计划类型描述
  56. */
  57. public function getPlanTypeNameAttribute(): string
  58. {
  59. return $this->getPlanTypeEnumAttribute()->getDescription();
  60. }
  61. /**
  62. * 获取计划类型图标
  63. */
  64. public function getPlanTypeIconAttribute(): string
  65. {
  66. return $this->getPlanTypeEnumAttribute()->getIcon();
  67. }
  68. /**
  69. * 获取计划类型颜色
  70. */
  71. public function getPlanTypeColorAttribute(): string
  72. {
  73. return $this->getPlanTypeEnumAttribute()->getColor();
  74. }
  75. /**
  76. * 获取启用状态文本
  77. */
  78. public function getEnabledTextAttribute(): string
  79. {
  80. return $this->is_enabled ? '启用' : '禁用';
  81. }
  82. /**
  83. * 获取启用状态颜色
  84. */
  85. public function getEnabledColorAttribute(): string
  86. {
  87. return $this->is_enabled ? 'success' : 'secondary';
  88. }
  89. /**
  90. * 获取模板状态文本
  91. */
  92. public function getTemplateTextAttribute(): string
  93. {
  94. return $this->is_template ? '模板' : '计划';
  95. }
  96. /**
  97. * 获取模板状态颜色
  98. */
  99. public function getTemplateColorAttribute(): string
  100. {
  101. return $this->is_template ? 'info' : 'primary';
  102. }
  103. /**
  104. * 判断是否需要目标选择配置
  105. */
  106. public function getNeedsTargetSelectionAttribute(): bool
  107. {
  108. return $this->getPlanTypeEnumAttribute()->needsTargetSelection();
  109. }
  110. /**
  111. * 获取目标选择类型
  112. */
  113. public function getSelectionTypeAttribute(): ?string
  114. {
  115. return $this->target_selection['selection_type'] ?? null;
  116. }
  117. /**
  118. * 获取目标模块列表
  119. */
  120. public function getTargetModulesAttribute(): array
  121. {
  122. return $this->target_selection['modules'] ?? [];
  123. }
  124. /**
  125. * 获取目标分类列表
  126. */
  127. public function getTargetCategoriesAttribute(): array
  128. {
  129. return $this->target_selection['categories'] ?? [];
  130. }
  131. /**
  132. * 获取目标表列表
  133. */
  134. public function getTargetTablesAttribute(): array
  135. {
  136. return $this->target_selection['tables'] ?? [];
  137. }
  138. /**
  139. * 获取排除表列表
  140. */
  141. public function getExcludeTablesAttribute(): array
  142. {
  143. return $this->target_selection['exclude_tables'] ?? [];
  144. }
  145. /**
  146. * 获取排除模块列表
  147. */
  148. public function getExcludeModulesAttribute(): array
  149. {
  150. return $this->target_selection['exclude_modules'] ?? [];
  151. }
  152. /**
  153. * 获取排除分类列表
  154. */
  155. public function getExcludeCategoriesAttribute(): array
  156. {
  157. return $this->target_selection['exclude_categories'] ?? [];
  158. }
  159. /**
  160. * 获取备份类型
  161. */
  162. public function getBackupTypeAttribute(): ?int
  163. {
  164. return $this->backup_config['backup_type'] ?? null;
  165. }
  166. /**
  167. * 获取压缩类型
  168. */
  169. public function getCompressionTypeAttribute(): ?int
  170. {
  171. return $this->backup_config['compression_type'] ?? null;
  172. }
  173. /**
  174. * 判断是否包含表结构
  175. */
  176. public function getIncludeStructureAttribute(): bool
  177. {
  178. return $this->backup_config['include_structure'] ?? true;
  179. }
  180. /**
  181. * 关联计划内容
  182. */
  183. public function contents(): HasMany
  184. {
  185. return $this->hasMany(CleanupPlanContent::class, 'plan_id');
  186. }
  187. /**
  188. * 关联启用的计划内容
  189. */
  190. public function enabledContents(): HasMany
  191. {
  192. return $this->contents()->where('is_enabled', true);
  193. }
  194. /**
  195. * 关联清理任务
  196. */
  197. public function tasks(): HasMany
  198. {
  199. return $this->hasMany(CleanupTask::class, 'plan_id');
  200. }
  201. /**
  202. * 关联备份记录
  203. */
  204. public function backups(): HasMany
  205. {
  206. return $this->hasMany(CleanupBackup::class, 'plan_id');
  207. }
  208. /**
  209. * 作用域:按计划类型筛选
  210. */
  211. public function scopeByType($query, int $type)
  212. {
  213. return $query->where('plan_type', $type);
  214. }
  215. /**
  216. * 作用域:只查询启用的计划
  217. */
  218. public function scopeEnabled($query)
  219. {
  220. return $query->where('is_enabled', true);
  221. }
  222. /**
  223. * 作用域:只查询模板
  224. */
  225. public function scopeTemplates($query)
  226. {
  227. return $query->where('is_template', true);
  228. }
  229. /**
  230. * 作用域:只查询非模板
  231. */
  232. public function scopeNonTemplates($query)
  233. {
  234. return $query->where('is_template', false);
  235. }
  236. /**
  237. * 作用域:按计划名称搜索
  238. */
  239. public function scopeSearchName($query, string $search)
  240. {
  241. return $query->where('plan_name', 'like', "%{$search}%");
  242. }
  243. /**
  244. * 作用域:按创建者筛选
  245. */
  246. public function scopeByCreator($query, int $createdBy)
  247. {
  248. return $query->where('created_by', $createdBy);
  249. }
  250. /**
  251. * 获取计划内容数量
  252. */
  253. public function getContentsCountAttribute(): int
  254. {
  255. return $this->contents()->count();
  256. }
  257. /**
  258. * 获取启用的内容数量
  259. */
  260. public function getEnabledContentsCountAttribute(): int
  261. {
  262. return $this->enabledContents()->count();
  263. }
  264. /**
  265. * 获取任务数量
  266. */
  267. public function getTasksCountAttribute(): int
  268. {
  269. return $this->tasks()->count();
  270. }
  271. /**
  272. * 获取备份数量
  273. */
  274. public function getBackupsCountAttribute(): int
  275. {
  276. return $this->backups()->count();
  277. }
  278. /**
  279. * 获取最后执行时间
  280. */
  281. public function getLastExecutedAtAttribute(): ?string
  282. {
  283. $lastTask = $this->tasks()
  284. ->whereNotNull('completed_at')
  285. ->orderBy('completed_at', 'desc')
  286. ->first();
  287. return $lastTask ? $lastTask->completed_at->format('Y-m-d H:i:s') : null;
  288. }
  289. /**
  290. * 获取计划类型统计
  291. */
  292. public static function getTypeStats(): array
  293. {
  294. $stats = static::selectRaw('plan_type, COUNT(*) as count')
  295. ->groupBy('plan_type')
  296. ->get()
  297. ->keyBy('plan_type')
  298. ->toArray();
  299. $result = [];
  300. foreach (PLAN_TYPE::cases() as $type) {
  301. $result[$type->value] = [
  302. 'name' => $type->getDescription(),
  303. 'count' => $stats[$type->value]['count'] ?? 0,
  304. 'color' => $type->getColor(),
  305. 'icon' => $type->getIcon(),
  306. ];
  307. }
  308. return $result;
  309. }
  310. /**
  311. * 获取启用状态统计
  312. */
  313. public static function getEnabledStats(): array
  314. {
  315. return [
  316. 'enabled' => static::where('is_enabled', true)->count(),
  317. 'disabled' => static::where('is_enabled', false)->count(),
  318. 'templates' => static::where('is_template', true)->count(),
  319. 'total' => static::count(),
  320. ];
  321. }
  322. }