CleanupConfig.php 6.6 KB

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