CleanupConfig.php 10 KB

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