CleanupConfig.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace App\Module\Cleanup\Models;
  3. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  4. use App\Module\Cleanup\Enums\DATA_CATEGORY;
  5. use UCore\ModelCore;
  6. /**
  7. * 清理配置模型
  8. *
  9. * 存储每个数据表的基础清理配置信息
  10. */
  11. class CleanupConfig extends ModelCore
  12. {
  13. /**
  14. * 数据表名
  15. */
  16. protected $table = 'cleanup_configs';
  17. // field start
  18. /**
  19. * 可批量赋值的字段
  20. */
  21. protected $fillable = [
  22. 'table_name',
  23. 'module_name',
  24. 'data_category',
  25. 'default_cleanup_type',
  26. 'default_conditions',
  27. 'is_enabled',
  28. 'priority',
  29. 'batch_size',
  30. 'description',
  31. 'last_cleanup_at',
  32. ];
  33. // field end
  34. /**
  35. * 字段类型转换
  36. */
  37. protected $casts = [
  38. 'data_category' => 'integer',
  39. 'default_cleanup_type' => 'integer',
  40. 'default_conditions' => 'array',
  41. 'is_enabled' => 'boolean',
  42. 'priority' => 'integer',
  43. 'batch_size' => 'integer',
  44. 'last_cleanup_at' => 'datetime',
  45. 'created_at' => 'datetime',
  46. 'updated_at' => 'datetime',
  47. ];
  48. /**
  49. * 获取数据分类枚举
  50. */
  51. public function getDataCategoryEnumAttribute(): DATA_CATEGORY
  52. {
  53. return DATA_CATEGORY::from($this->data_category);
  54. }
  55. /**
  56. * 获取默认清理类型枚举
  57. */
  58. public function getDefaultCleanupTypeEnumAttribute(): CLEANUP_TYPE
  59. {
  60. return CLEANUP_TYPE::from($this->default_cleanup_type);
  61. }
  62. /**
  63. * 获取数据分类描述
  64. */
  65. public function getDataCategoryNameAttribute(): string
  66. {
  67. return $this->getDataCategoryEnumAttribute()->getDescription();
  68. }
  69. /**
  70. * 获取默认清理类型描述
  71. */
  72. public function getDefaultCleanupTypeNameAttribute(): string
  73. {
  74. return $this->getDefaultCleanupTypeEnumAttribute()->getDescription();
  75. }
  76. /**
  77. * 获取数据分类颜色
  78. */
  79. public function getDataCategoryColorAttribute(): string
  80. {
  81. return $this->getDataCategoryEnumAttribute()->getColor();
  82. }
  83. /**
  84. * 获取启用状态文本
  85. */
  86. public function getEnabledTextAttribute(): string
  87. {
  88. return $this->is_enabled ? '启用' : '禁用';
  89. }
  90. /**
  91. * 获取启用状态颜色
  92. */
  93. public function getEnabledColorAttribute(): string
  94. {
  95. return $this->is_enabled ? 'success' : 'secondary';
  96. }
  97. /**
  98. * 获取优先级文本
  99. */
  100. public function getPriorityTextAttribute(): string
  101. {
  102. if ($this->priority <= 50) {
  103. return '高';
  104. } elseif ($this->priority <= 200) {
  105. return '中';
  106. } else {
  107. return '低';
  108. }
  109. }
  110. /**
  111. * 获取优先级颜色
  112. */
  113. public function getPriorityColorAttribute(): string
  114. {
  115. if ($this->priority <= 50) {
  116. return 'danger';
  117. } elseif ($this->priority <= 200) {
  118. return 'warning';
  119. } else {
  120. return 'info';
  121. }
  122. }
  123. /**
  124. * 判断是否需要条件配置
  125. */
  126. public function getNeedsConditionsAttribute(): bool
  127. {
  128. return $this->getDefaultCleanupTypeEnumAttribute()->needsConditions();
  129. }
  130. /**
  131. * 判断是否支持回滚
  132. */
  133. public function getIsRollbackableAttribute(): bool
  134. {
  135. return $this->getDefaultCleanupTypeEnumAttribute()->isRollbackable();
  136. }
  137. /**
  138. * 获取格式化的最后清理时间
  139. */
  140. public function getLastCleanupAtFormattedAttribute(): ?string
  141. {
  142. return $this->last_cleanup_at ? $this->last_cleanup_at->format('Y-m-d H:i:s') : null;
  143. }
  144. /**
  145. * 获取最后清理时间的相对时间
  146. */
  147. public function getLastCleanupAtHumanAttribute(): ?string
  148. {
  149. return $this->last_cleanup_at ? $this->last_cleanup_at->diffForHumans() : '从未清理';
  150. }
  151. /**
  152. * 作用域:按模块筛选
  153. */
  154. public function scopeByModule($query, string $moduleName)
  155. {
  156. return $query->where('module_name', $moduleName);
  157. }
  158. /**
  159. * 作用域:按数据分类筛选
  160. */
  161. public function scopeByCategory($query, int $category)
  162. {
  163. return $query->where('data_category', $category);
  164. }
  165. /**
  166. * 作用域:只查询启用的配置
  167. */
  168. public function scopeEnabled($query)
  169. {
  170. return $query->where('is_enabled', true);
  171. }
  172. /**
  173. * 作用域:按优先级排序
  174. */
  175. public function scopeOrderByPriority($query)
  176. {
  177. return $query->orderBy('priority')->orderBy('table_name');
  178. }
  179. /**
  180. * 作用域:按表名搜索
  181. */
  182. public function scopeSearchTable($query, string $search)
  183. {
  184. return $query->where('table_name', 'like', "%{$search}%");
  185. }
  186. /**
  187. * 作用域:排除配置数据
  188. */
  189. public function scopeExcludeConfig($query)
  190. {
  191. return $query->where('data_category', '!=', DATA_CATEGORY::CONFIG_DATA->value);
  192. }
  193. /**
  194. * 获取模块列表
  195. */
  196. public static function getModuleList(): array
  197. {
  198. return static::distinct('module_name')
  199. ->orderBy('module_name')
  200. ->pluck('module_name')
  201. ->toArray();
  202. }
  203. /**
  204. * 获取数据分类统计
  205. */
  206. public static function getCategoryStats(): array
  207. {
  208. $stats = static::selectRaw('data_category, COUNT(*) as count')
  209. ->groupBy('data_category')
  210. ->get()
  211. ->keyBy('data_category')
  212. ->toArray();
  213. $result = [];
  214. foreach (DATA_CATEGORY::cases() as $category) {
  215. $result[$category->value] = [
  216. 'name' => $category->getDescription(),
  217. 'count' => $stats[$category->value]['count'] ?? 0,
  218. 'color' => $category->getColor(),
  219. ];
  220. }
  221. return $result;
  222. }
  223. /**
  224. * 获取启用状态统计
  225. */
  226. public static function getEnabledStats(): array
  227. {
  228. return [
  229. 'enabled' => static::where('is_enabled', true)->count(),
  230. 'disabled' => static::where('is_enabled', false)->count(),
  231. 'total' => static::count(),
  232. ];
  233. }
  234. }