CleanupPlan.php 8.3 KB

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