CleanupPlan.php 7.1 KB

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