CleanupConfig.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. // attrlist start
  18. /**
  19. * 可批量赋值的属性
  20. */
  21. protected $fillable = [
  22. 'model_class',
  23. 'table_name',
  24. 'model_info',
  25. 'module_name',
  26. 'data_category',
  27. 'default_cleanup_type',
  28. 'default_conditions',
  29. 'is_enabled',
  30. 'priority',
  31. 'batch_size',
  32. 'description',
  33. 'last_cleanup_at',
  34. ];
  35. // attrlist end
  36. // field start
  37. /**
  38. * @property int $id 主键ID
  39. * @property string $model_class Model类名
  40. * @property string $table_name 表名(兼容性保留)
  41. * @property array $model_info Model类信息JSON
  42. * @property string $module_name 模块名称
  43. * @property int $data_category 数据分类:1用户数据,2日志数据,3交易数据,4缓存数据,5配置数据
  44. * @property int $default_cleanup_type 默认清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除
  45. * @property array $default_conditions 默认清理条件JSON配置
  46. * @property bool $is_enabled 是否启用清理
  47. * @property int $priority 清理优先级(数字越小优先级越高)
  48. * @property int $batch_size 批处理大小
  49. * @property string $description 配置描述
  50. * @property \Carbon\Carbon $last_cleanup_at 最后清理时间
  51. * @property \Carbon\Carbon $created_at 创建时间
  52. * @property \Carbon\Carbon $updated_at 更新时间
  53. */
  54. // field end
  55. /**
  56. * 字段类型转换
  57. */
  58. protected $casts = [
  59. 'model_info' => 'array',
  60. 'data_category' => 'integer',
  61. 'default_cleanup_type' => 'integer',
  62. 'default_conditions' => 'array',
  63. 'is_enabled' => 'boolean',
  64. 'priority' => 'integer',
  65. 'batch_size' => 'integer',
  66. 'last_cleanup_at' => 'datetime',
  67. 'created_at' => 'datetime',
  68. 'updated_at' => 'datetime',
  69. ];
  70. /**
  71. * 获取数据分类枚举
  72. */
  73. public function getDataCategoryEnumAttribute(): DATA_CATEGORY
  74. {
  75. return DATA_CATEGORY::from($this->data_category);
  76. }
  77. /**
  78. * 获取默认清理类型枚举
  79. */
  80. public function getDefaultCleanupTypeEnumAttribute(): CLEANUP_TYPE
  81. {
  82. return CLEANUP_TYPE::from($this->default_cleanup_type);
  83. }
  84. /**
  85. * 获取数据分类描述
  86. */
  87. public function getDataCategoryNameAttribute(): string
  88. {
  89. return $this->getDataCategoryEnumAttribute()->getDescription();
  90. }
  91. /**
  92. * 获取默认清理类型描述
  93. */
  94. public function getDefaultCleanupTypeNameAttribute(): string
  95. {
  96. return $this->getDefaultCleanupTypeEnumAttribute()->getDescription();
  97. }
  98. /**
  99. * 获取数据分类颜色
  100. */
  101. public function getDataCategoryColorAttribute(): string
  102. {
  103. return $this->getDataCategoryEnumAttribute()->getColor();
  104. }
  105. /**
  106. * 获取启用状态文本
  107. */
  108. public function getEnabledTextAttribute(): string
  109. {
  110. return $this->is_enabled ? '启用' : '禁用';
  111. }
  112. /**
  113. * 获取启用状态颜色
  114. */
  115. public function getEnabledColorAttribute(): string
  116. {
  117. return $this->is_enabled ? 'success' : 'secondary';
  118. }
  119. /**
  120. * 获取优先级文本
  121. */
  122. public function getPriorityTextAttribute(): string
  123. {
  124. if ($this->priority <= 50) {
  125. return '高';
  126. } elseif ($this->priority <= 200) {
  127. return '中';
  128. } else {
  129. return '低';
  130. }
  131. }
  132. /**
  133. * 获取优先级颜色
  134. */
  135. public function getPriorityColorAttribute(): string
  136. {
  137. if ($this->priority <= 50) {
  138. return 'danger';
  139. } elseif ($this->priority <= 200) {
  140. return 'warning';
  141. } else {
  142. return 'info';
  143. }
  144. }
  145. /**
  146. * 判断是否需要条件配置
  147. */
  148. public function getNeedsConditionsAttribute(): bool
  149. {
  150. return $this->getDefaultCleanupTypeEnumAttribute()->needsConditions();
  151. }
  152. /**
  153. * 判断是否支持回滚
  154. */
  155. public function getIsRollbackableAttribute(): bool
  156. {
  157. return $this->getDefaultCleanupTypeEnumAttribute()->isRollbackable();
  158. }
  159. /**
  160. * 获取格式化的最后清理时间
  161. */
  162. public function getLastCleanupAtFormattedAttribute(): ?string
  163. {
  164. return $this->last_cleanup_at ? $this->last_cleanup_at->format('Y-m-d H:i:s') : null;
  165. }
  166. /**
  167. * 获取最后清理时间的相对时间
  168. */
  169. public function getLastCleanupAtHumanAttribute(): ?string
  170. {
  171. return $this->last_cleanup_at ? $this->last_cleanup_at->diffForHumans() : '从未清理';
  172. }
  173. /**
  174. * 作用域:按模块筛选
  175. */
  176. public function scopeByModule($query, string $moduleName)
  177. {
  178. return $query->where('module_name', $moduleName);
  179. }
  180. /**
  181. * 作用域:按数据分类筛选
  182. */
  183. public function scopeByCategory($query, int $category)
  184. {
  185. return $query->where('data_category', $category);
  186. }
  187. /**
  188. * 作用域:只查询启用的配置
  189. */
  190. public function scopeEnabled($query)
  191. {
  192. return $query->where('is_enabled', true);
  193. }
  194. /**
  195. * 作用域:按优先级排序
  196. */
  197. public function scopeOrderByPriority($query)
  198. {
  199. return $query->orderBy('priority')->orderBy('table_name');
  200. }
  201. /**
  202. * 作用域:按表名搜索
  203. */
  204. public function scopeSearchTable($query, string $search)
  205. {
  206. return $query->where('table_name', 'like', "%{$search}%");
  207. }
  208. /**
  209. * 作用域:排除配置数据
  210. */
  211. public function scopeExcludeConfig($query)
  212. {
  213. return $query->where('data_category', '!=', DATA_CATEGORY::CONFIG_DATA->value);
  214. }
  215. /**
  216. * 获取模块列表
  217. */
  218. public static function getModuleList(): array
  219. {
  220. return static::distinct('module_name')
  221. ->orderBy('module_name')
  222. ->pluck('module_name')
  223. ->toArray();
  224. }
  225. /**
  226. * 获取数据分类统计
  227. */
  228. public static function getCategoryStats(): array
  229. {
  230. $stats = static::selectRaw('data_category, COUNT(*) as count')
  231. ->groupBy('data_category')
  232. ->get()
  233. ->keyBy('data_category')
  234. ->toArray();
  235. $result = [];
  236. foreach (DATA_CATEGORY::cases() as $category) {
  237. $result[$category->value] = [
  238. 'name' => $category->getDescription(),
  239. 'count' => $stats[$category->value]['count'] ?? 0,
  240. 'color' => $category->getColor(),
  241. ];
  242. }
  243. return $result;
  244. }
  245. /**
  246. * 获取启用状态统计
  247. */
  248. public static function getEnabledStats(): array
  249. {
  250. return [
  251. 'enabled' => static::where('is_enabled', true)->count(),
  252. 'disabled' => static::where('is_enabled', false)->count(),
  253. 'total' => static::count(),
  254. ];
  255. }
  256. /**
  257. * 获取Model实例
  258. */
  259. public function getModelInstance()
  260. {
  261. if (empty($this->model_class)) {
  262. throw new \Exception("Model类名为空");
  263. }
  264. if (!class_exists($this->model_class)) {
  265. throw new \Exception("Model类不存在: {$this->model_class}");
  266. }
  267. return new $this->model_class();
  268. }
  269. /**
  270. * 获取实际表名(优先从Model获取)
  271. */
  272. public function getActualTableName(): string
  273. {
  274. if (!empty($this->model_class)) {
  275. try {
  276. return $this->getModelInstance()->getTable();
  277. } catch (\Exception $e) {
  278. // 如果Model有问题,回退到table_name
  279. }
  280. }
  281. return $this->table_name;
  282. }
  283. /**
  284. * 获取记录数量(优先使用Model)
  285. */
  286. public function getRecordCount(): int
  287. {
  288. if (!empty($this->model_class)) {
  289. try {
  290. $modelClass = $this->model_class;
  291. return $modelClass::count();
  292. } catch (\Exception $e) {
  293. // 如果Model有问题,回退到直接查询表
  294. }
  295. }
  296. return \DB::table($this->table_name)->count();
  297. }
  298. /**
  299. * 检查Model是否支持软删除
  300. */
  301. public function supportsSoftDeletes(): bool
  302. {
  303. if (empty($this->model_class)) {
  304. return false;
  305. }
  306. try {
  307. $model = $this->getModelInstance();
  308. return in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive($model));
  309. } catch (\Exception $e) {
  310. return false;
  311. }
  312. }
  313. /**
  314. * 获取Model信息摘要
  315. */
  316. public function getModelSummary(): array
  317. {
  318. if (empty($this->model_class)) {
  319. return [
  320. 'has_model' => false,
  321. 'table_name' => $this->table_name,
  322. 'record_count' => $this->getRecordCount(),
  323. ];
  324. }
  325. try {
  326. $model = $this->getModelInstance();
  327. return [
  328. 'has_model' => true,
  329. 'model_class' => $this->model_class,
  330. 'table_name' => $model->getTable(),
  331. 'primary_key' => $model->getKeyName(),
  332. 'timestamps' => $model->timestamps,
  333. 'soft_deletes' => $this->supportsSoftDeletes(),
  334. 'record_count' => $this->getRecordCount(),
  335. 'fillable_count' => count($model->getFillable()),
  336. 'casts_count' => count($model->getCasts()),
  337. ];
  338. } catch (\Exception $e) {
  339. return [
  340. 'has_model' => false,
  341. 'error' => $e->getMessage(),
  342. 'table_name' => $this->table_name,
  343. 'record_count' => 0,
  344. ];
  345. }
  346. }
  347. /**
  348. * 作用域:只查询有Model类的配置
  349. */
  350. public function scopeWithModel($query)
  351. {
  352. return $query->whereNotNull('model_class')->where('model_class', '!=', '');
  353. }
  354. /**
  355. * 作用域:只查询没有Model类的配置(旧数据)
  356. */
  357. public function scopeWithoutModel($query)
  358. {
  359. return $query->where(function($q) {
  360. $q->whereNull('model_class')->orWhere('model_class', '');
  361. });
  362. }
  363. }