CleanupPlan.php 7.1 KB

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