CleanupPlanContent.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace App\Module\Cleanup\Models;
  3. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  4. use UCore\ModelCore;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. /**
  7. * 计划内容模型
  8. *
  9. * 存储计划的具体内容,即计划具体处理哪些表,怎么清理
  10. * field start
  11. * @property int $id 主键ID
  12. * @property int $plan_id 计划ID
  13. * @property string $table_name 表名
  14. * @property string $model_class Model类名
  15. * @property int $cleanup_type 清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除
  16. * @property array $conditions 清理条件JSON配置
  17. * @property int $priority 清理优先级
  18. * @property int $batch_size 批处理大小
  19. * @property bool $is_enabled 是否启用
  20. * @property bool $backup_enabled 是否启用备份
  21. * @property string $notes 备注说明
  22. * @property \Carbon\Carbon $created_at 创建时间
  23. * @property \Carbon\Carbon $updated_at 更新时间
  24. * field end *
  25. */
  26. class CleanupPlanContent extends ModelCore
  27. {
  28. /**
  29. * 数据表名
  30. */
  31. protected $table = 'cleanup_plan_contents';
  32. // attrlist start
  33. protected $fillable = [
  34. 'id',
  35. 'plan_id',
  36. 'table_name',
  37. 'model_class',
  38. 'cleanup_type',
  39. 'conditions',
  40. 'priority',
  41. 'batch_size',
  42. 'is_enabled',
  43. 'backup_enabled',
  44. 'notes',
  45. ];
  46. // attrlist end
  47. /**
  48. * 字段类型转换
  49. */
  50. protected $casts = [
  51. 'plan_id' => 'integer',
  52. 'cleanup_type' => 'integer',
  53. 'conditions' => 'array',
  54. 'priority' => 'integer',
  55. 'batch_size' => 'integer',
  56. 'is_enabled' => 'boolean',
  57. 'backup_enabled' => 'boolean',
  58. 'created_at' => 'datetime',
  59. 'updated_at' => 'datetime',
  60. ];
  61. /**
  62. * 获取清理类型枚举
  63. */
  64. public function getCleanupTypeEnumAttribute(): CLEANUP_TYPE
  65. {
  66. return CLEANUP_TYPE::from($this->cleanup_type);
  67. }
  68. /**
  69. * 获取清理类型描述
  70. */
  71. public function getCleanupTypeNameAttribute(): string
  72. {
  73. return $this->getCleanupTypeEnumAttribute()->getDescription();
  74. }
  75. /**
  76. * 获取启用状态文本
  77. */
  78. public function getEnabledTextAttribute(): string
  79. {
  80. return $this->is_enabled ? '启用' : '禁用';
  81. }
  82. /**
  83. * 获取启用状态颜色
  84. */
  85. public function getEnabledColorAttribute(): string
  86. {
  87. return $this->is_enabled ? 'success' : 'secondary';
  88. }
  89. /**
  90. * 获取备份状态文本
  91. */
  92. public function getBackupTextAttribute(): string
  93. {
  94. return $this->backup_enabled ? '启用' : '禁用';
  95. }
  96. /**
  97. * 获取备份状态颜色
  98. */
  99. public function getBackupColorAttribute(): string
  100. {
  101. return $this->backup_enabled ? 'success' : 'warning';
  102. }
  103. /**
  104. * 获取优先级文本
  105. */
  106. public function getPriorityTextAttribute(): string
  107. {
  108. if ($this->priority <= 50) {
  109. return '高';
  110. } elseif ($this->priority <= 200) {
  111. return '中';
  112. } else {
  113. return '低';
  114. }
  115. }
  116. /**
  117. * 获取优先级颜色
  118. */
  119. public function getPriorityColorAttribute(): string
  120. {
  121. if ($this->priority <= 50) {
  122. return 'danger';
  123. } elseif ($this->priority <= 200) {
  124. return 'warning';
  125. } else {
  126. return 'info';
  127. }
  128. }
  129. /**
  130. * 判断是否需要条件配置
  131. */
  132. public function getNeedsConditionsAttribute(): bool
  133. {
  134. return $this->getCleanupTypeEnumAttribute()->needsConditions();
  135. }
  136. /**
  137. * 判断是否支持回滚
  138. */
  139. public function getIsRollbackableAttribute(): bool
  140. {
  141. return $this->getCleanupTypeEnumAttribute()->isRollbackable();
  142. }
  143. /**
  144. * 获取时间字段
  145. */
  146. public function getTimeFieldAttribute(): ?string
  147. {
  148. return $this->conditions['time_field'] ?? null;
  149. }
  150. /**
  151. * 获取时间条件
  152. */
  153. public function getTimeConditionAttribute(): ?string
  154. {
  155. return $this->conditions['time_condition'] ?? null;
  156. }
  157. /**
  158. * 获取时间值
  159. */
  160. public function getTimeValueAttribute(): ?int
  161. {
  162. return $this->conditions['time_value'] ?? null;
  163. }
  164. /**
  165. * 获取时间单位
  166. */
  167. public function getTimeUnitAttribute(): ?string
  168. {
  169. return $this->conditions['time_unit'] ?? null;
  170. }
  171. /**
  172. * 获取用户字段
  173. */
  174. public function getUserFieldAttribute(): ?string
  175. {
  176. return $this->conditions['user_field'] ?? null;
  177. }
  178. /**
  179. * 获取用户条件
  180. */
  181. public function getUserConditionAttribute(): ?string
  182. {
  183. return $this->conditions['user_condition'] ?? null;
  184. }
  185. /**
  186. * 获取用户值列表
  187. */
  188. public function getUserValuesAttribute(): array
  189. {
  190. return $this->conditions['user_values'] ?? [];
  191. }
  192. /**
  193. * 获取自定义条件
  194. */
  195. public function getCustomConditionsAttribute(): array
  196. {
  197. return $this->conditions['conditions'] ?? [];
  198. }
  199. /**
  200. * 获取条件逻辑
  201. */
  202. public function getConditionLogicAttribute(): string
  203. {
  204. return $this->conditions['logic'] ?? 'AND';
  205. }
  206. /**
  207. * 获取条件描述
  208. */
  209. public function getConditionsDescriptionAttribute(): string
  210. {
  211. if (!$this->needs_conditions || empty($this->conditions)) {
  212. return '无条件';
  213. }
  214. $descriptions = [];
  215. switch ($this->cleanup_type) {
  216. case CLEANUP_TYPE::DELETE_BY_TIME->value:
  217. if ($this->time_field && $this->time_value && $this->time_unit) {
  218. $descriptions[] = "删除 {$this->time_field} 字段 {$this->time_value} {$this->time_unit} 前的记录";
  219. }
  220. break;
  221. case CLEANUP_TYPE::DELETE_BY_USER->value:
  222. if ($this->user_field && !empty($this->user_values)) {
  223. $userList = implode(', ', array_slice($this->user_values, 0, 3));
  224. if (count($this->user_values) > 3) {
  225. $userList .= ' 等' . count($this->user_values) . '个用户';
  226. }
  227. $descriptions[] = "删除 {$this->user_field} 为 {$userList} 的记录";
  228. }
  229. break;
  230. case CLEANUP_TYPE::DELETE_BY_CONDITION->value:
  231. if (!empty($this->custom_conditions)) {
  232. $descriptions[] = "自定义条件:" . count($this->custom_conditions) . " 个条件";
  233. }
  234. break;
  235. }
  236. return empty($descriptions) ? '条件配置不完整' : implode('; ', $descriptions);
  237. }
  238. /**
  239. * 关联清理计划
  240. */
  241. public function plan(): BelongsTo
  242. {
  243. return $this->belongsTo(CleanupPlan::class, 'plan_id');
  244. }
  245. /**
  246. * 关联清理配置
  247. */
  248. public function config(): BelongsTo
  249. {
  250. return $this->belongsTo(CleanupConfig::class, 'table_name', 'table_name');
  251. }
  252. /**
  253. * 作用域:按计划筛选
  254. */
  255. public function scopeByPlan($query, int $planId)
  256. {
  257. return $query->where('plan_id', $planId);
  258. }
  259. /**
  260. * 作用域:按表名筛选
  261. */
  262. public function scopeByTable($query, string $tableName)
  263. {
  264. return $query->where('table_name', $tableName);
  265. }
  266. /**
  267. * 作用域:只查询启用的内容
  268. */
  269. public function scopeEnabled($query)
  270. {
  271. return $query->where('is_enabled', true);
  272. }
  273. /**
  274. * 作用域:按优先级排序
  275. */
  276. public function scopeOrderByPriority($query)
  277. {
  278. return $query->orderBy('priority')->orderBy('table_name');
  279. }
  280. /**
  281. * 作用域:按清理类型筛选
  282. */
  283. public function scopeByCleanupType($query, int $type)
  284. {
  285. return $query->where('cleanup_type', $type);
  286. }
  287. /**
  288. * 作用域:启用备份的内容
  289. */
  290. public function scopeBackupEnabled($query)
  291. {
  292. return $query->where('backup_enabled', true);
  293. }
  294. /**
  295. * 作用域:按表名搜索
  296. */
  297. public function scopeSearchTable($query, string $search)
  298. {
  299. return $query->where('table_name', 'like', "%{$search}%");
  300. }
  301. /**
  302. * 获取清理类型统计
  303. */
  304. public static function getCleanupTypeStats(int $planId = null): array
  305. {
  306. $query = static::selectRaw('cleanup_type, COUNT(*) as count')
  307. ->groupBy('cleanup_type');
  308. if ($planId) {
  309. $query->where('plan_id', $planId);
  310. }
  311. $stats = $query->get()->keyBy('cleanup_type')->toArray();
  312. $result = [];
  313. foreach (CLEANUP_TYPE::cases() as $type) {
  314. $result[$type->value] = [
  315. 'name' => $type->getDescription(),
  316. 'count' => $stats[$type->value]['count'] ?? 0,
  317. ];
  318. }
  319. return $result;
  320. }
  321. /**
  322. * 获取启用状态统计
  323. */
  324. public static function getEnabledStats(int $planId = null): array
  325. {
  326. $query = static::query();
  327. if ($planId) {
  328. $query->where('plan_id', $planId);
  329. }
  330. return [
  331. 'enabled' => $query->clone()->where('is_enabled', true)->count(),
  332. 'disabled' => $query->clone()->where('is_enabled', false)->count(),
  333. 'backup_enabled' => $query->clone()->where('backup_enabled', true)->count(),
  334. 'total' => $query->count(),
  335. ];
  336. }
  337. /**
  338. * 获取Model实例
  339. */
  340. public function getModelInstance()
  341. {
  342. if (empty($this->model_class)) {
  343. throw new \Exception("Model类名为空");
  344. }
  345. if (!class_exists($this->model_class)) {
  346. throw new \Exception("Model类不存在: {$this->model_class}");
  347. }
  348. return new $this->model_class();
  349. }
  350. /**
  351. * 获取实际表名(优先从Model获取)
  352. */
  353. public function getActualTableName(): string
  354. {
  355. if (!empty($this->model_class)) {
  356. try {
  357. return $this->getModelInstance()->getTable();
  358. } catch (\Exception $e) {
  359. // 如果Model有问题,回退到table_name
  360. }
  361. }
  362. return $this->table_name;
  363. }
  364. /**
  365. * 获取目标名称(Model类名或表名)
  366. */
  367. public function getTargetName(): string
  368. {
  369. return $this->model_class ?: $this->table_name;
  370. }
  371. /**
  372. * 检查是否基于Model类
  373. */
  374. public function isModelBased(): bool
  375. {
  376. return !empty($this->model_class);
  377. }
  378. /**
  379. * 检查Model是否支持软删除
  380. */
  381. public function supportsSoftDeletes(): bool
  382. {
  383. if (!$this->isModelBased()) {
  384. return false;
  385. }
  386. try {
  387. $model = $this->getModelInstance();
  388. return in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive($model));
  389. } catch (\Exception $e) {
  390. return false;
  391. }
  392. }
  393. /**
  394. * 获取记录数量(优先使用Model)
  395. */
  396. public function getRecordCount(): int
  397. {
  398. if ($this->isModelBased()) {
  399. try {
  400. $modelClass = $this->model_class;
  401. return $modelClass::count();
  402. } catch (\Exception $e) {
  403. // 如果Model有问题,回退到直接查询表
  404. }
  405. }
  406. return \DB::table($this->table_name)->count();
  407. }
  408. /**
  409. * 作用域:只查询有Model类的内容
  410. */
  411. public function scopeWithModel($query)
  412. {
  413. return $query->whereNotNull('model_class')->where('model_class', '!=', '');
  414. }
  415. /**
  416. * 作用域:只查询没有Model类的内容(旧数据)
  417. */
  418. public function scopeWithoutModel($query)
  419. {
  420. return $query->where(function($q) {
  421. $q->whereNull('model_class')->orWhere('model_class', '');
  422. });
  423. }
  424. /**
  425. * 作用域:按Model类筛选
  426. */
  427. public function scopeByModel($query, string $modelClass)
  428. {
  429. return $query->where('model_class', $modelClass);
  430. }
  431. }