CleanupPlanContent.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. */
  11. class CleanupPlanContent extends ModelCore
  12. {
  13. /**
  14. * 数据表名
  15. */
  16. protected $table = 'cleanup_plan_contents';
  17. // field start
  18. /**
  19. * 可批量赋值的字段
  20. */
  21. protected $fillable = [
  22. 'plan_id',
  23. 'table_name',
  24. 'cleanup_type',
  25. 'conditions',
  26. 'priority',
  27. 'batch_size',
  28. 'is_enabled',
  29. 'backup_enabled',
  30. 'notes',
  31. ];
  32. // field end
  33. /**
  34. * 字段类型转换
  35. */
  36. protected $casts = [
  37. 'plan_id' => 'integer',
  38. 'cleanup_type' => 'integer',
  39. 'conditions' => 'array',
  40. 'priority' => 'integer',
  41. 'batch_size' => 'integer',
  42. 'is_enabled' => 'boolean',
  43. 'backup_enabled' => 'boolean',
  44. 'created_at' => 'datetime',
  45. 'updated_at' => 'datetime',
  46. ];
  47. /**
  48. * 获取清理类型枚举
  49. */
  50. public function getCleanupTypeEnumAttribute(): CLEANUP_TYPE
  51. {
  52. return CLEANUP_TYPE::from($this->cleanup_type);
  53. }
  54. /**
  55. * 获取清理类型描述
  56. */
  57. public function getCleanupTypeNameAttribute(): string
  58. {
  59. return $this->getCleanupTypeEnumAttribute()->getDescription();
  60. }
  61. /**
  62. * 获取启用状态文本
  63. */
  64. public function getEnabledTextAttribute(): string
  65. {
  66. return $this->is_enabled ? '启用' : '禁用';
  67. }
  68. /**
  69. * 获取启用状态颜色
  70. */
  71. public function getEnabledColorAttribute(): string
  72. {
  73. return $this->is_enabled ? 'success' : 'secondary';
  74. }
  75. /**
  76. * 获取备份状态文本
  77. */
  78. public function getBackupTextAttribute(): string
  79. {
  80. return $this->backup_enabled ? '启用' : '禁用';
  81. }
  82. /**
  83. * 获取备份状态颜色
  84. */
  85. public function getBackupColorAttribute(): string
  86. {
  87. return $this->backup_enabled ? 'success' : 'warning';
  88. }
  89. /**
  90. * 获取优先级文本
  91. */
  92. public function getPriorityTextAttribute(): string
  93. {
  94. if ($this->priority <= 50) {
  95. return '高';
  96. } elseif ($this->priority <= 200) {
  97. return '中';
  98. } else {
  99. return '低';
  100. }
  101. }
  102. /**
  103. * 获取优先级颜色
  104. */
  105. public function getPriorityColorAttribute(): string
  106. {
  107. if ($this->priority <= 50) {
  108. return 'danger';
  109. } elseif ($this->priority <= 200) {
  110. return 'warning';
  111. } else {
  112. return 'info';
  113. }
  114. }
  115. /**
  116. * 判断是否需要条件配置
  117. */
  118. public function getNeedsConditionsAttribute(): bool
  119. {
  120. return $this->getCleanupTypeEnumAttribute()->needsConditions();
  121. }
  122. /**
  123. * 判断是否支持回滚
  124. */
  125. public function getIsRollbackableAttribute(): bool
  126. {
  127. return $this->getCleanupTypeEnumAttribute()->isRollbackable();
  128. }
  129. /**
  130. * 获取时间字段
  131. */
  132. public function getTimeFieldAttribute(): ?string
  133. {
  134. return $this->conditions['time_field'] ?? null;
  135. }
  136. /**
  137. * 获取时间条件
  138. */
  139. public function getTimeConditionAttribute(): ?string
  140. {
  141. return $this->conditions['time_condition'] ?? null;
  142. }
  143. /**
  144. * 获取时间值
  145. */
  146. public function getTimeValueAttribute(): ?int
  147. {
  148. return $this->conditions['time_value'] ?? null;
  149. }
  150. /**
  151. * 获取时间单位
  152. */
  153. public function getTimeUnitAttribute(): ?string
  154. {
  155. return $this->conditions['time_unit'] ?? null;
  156. }
  157. /**
  158. * 获取用户字段
  159. */
  160. public function getUserFieldAttribute(): ?string
  161. {
  162. return $this->conditions['user_field'] ?? null;
  163. }
  164. /**
  165. * 获取用户条件
  166. */
  167. public function getUserConditionAttribute(): ?string
  168. {
  169. return $this->conditions['user_condition'] ?? null;
  170. }
  171. /**
  172. * 获取用户值列表
  173. */
  174. public function getUserValuesAttribute(): array
  175. {
  176. return $this->conditions['user_values'] ?? [];
  177. }
  178. /**
  179. * 获取自定义条件
  180. */
  181. public function getCustomConditionsAttribute(): array
  182. {
  183. return $this->conditions['conditions'] ?? [];
  184. }
  185. /**
  186. * 获取条件逻辑
  187. */
  188. public function getConditionLogicAttribute(): string
  189. {
  190. return $this->conditions['logic'] ?? 'AND';
  191. }
  192. /**
  193. * 获取条件描述
  194. */
  195. public function getConditionsDescriptionAttribute(): string
  196. {
  197. if (!$this->needs_conditions || empty($this->conditions)) {
  198. return '无条件';
  199. }
  200. $descriptions = [];
  201. switch ($this->cleanup_type) {
  202. case CLEANUP_TYPE::DELETE_BY_TIME->value:
  203. if ($this->time_field && $this->time_value && $this->time_unit) {
  204. $descriptions[] = "删除 {$this->time_field} 字段 {$this->time_value} {$this->time_unit} 前的记录";
  205. }
  206. break;
  207. case CLEANUP_TYPE::DELETE_BY_USER->value:
  208. if ($this->user_field && !empty($this->user_values)) {
  209. $userList = implode(', ', array_slice($this->user_values, 0, 3));
  210. if (count($this->user_values) > 3) {
  211. $userList .= ' 等' . count($this->user_values) . '个用户';
  212. }
  213. $descriptions[] = "删除 {$this->user_field} 为 {$userList} 的记录";
  214. }
  215. break;
  216. case CLEANUP_TYPE::DELETE_BY_CONDITION->value:
  217. if (!empty($this->custom_conditions)) {
  218. $descriptions[] = "自定义条件:" . count($this->custom_conditions) . " 个条件";
  219. }
  220. break;
  221. }
  222. return empty($descriptions) ? '条件配置不完整' : implode('; ', $descriptions);
  223. }
  224. /**
  225. * 关联清理计划
  226. */
  227. public function plan(): BelongsTo
  228. {
  229. return $this->belongsTo(CleanupPlan::class, 'plan_id');
  230. }
  231. /**
  232. * 关联清理配置
  233. */
  234. public function config(): BelongsTo
  235. {
  236. return $this->belongsTo(CleanupConfig::class, 'table_name', 'table_name');
  237. }
  238. /**
  239. * 作用域:按计划筛选
  240. */
  241. public function scopeByPlan($query, int $planId)
  242. {
  243. return $query->where('plan_id', $planId);
  244. }
  245. /**
  246. * 作用域:按表名筛选
  247. */
  248. public function scopeByTable($query, string $tableName)
  249. {
  250. return $query->where('table_name', $tableName);
  251. }
  252. /**
  253. * 作用域:只查询启用的内容
  254. */
  255. public function scopeEnabled($query)
  256. {
  257. return $query->where('is_enabled', true);
  258. }
  259. /**
  260. * 作用域:按优先级排序
  261. */
  262. public function scopeOrderByPriority($query)
  263. {
  264. return $query->orderBy('priority')->orderBy('table_name');
  265. }
  266. /**
  267. * 作用域:按清理类型筛选
  268. */
  269. public function scopeByCleanupType($query, int $type)
  270. {
  271. return $query->where('cleanup_type', $type);
  272. }
  273. /**
  274. * 作用域:启用备份的内容
  275. */
  276. public function scopeBackupEnabled($query)
  277. {
  278. return $query->where('backup_enabled', true);
  279. }
  280. /**
  281. * 作用域:按表名搜索
  282. */
  283. public function scopeSearchTable($query, string $search)
  284. {
  285. return $query->where('table_name', 'like', "%{$search}%");
  286. }
  287. /**
  288. * 获取清理类型统计
  289. */
  290. public static function getCleanupTypeStats(int $planId = null): array
  291. {
  292. $query = static::selectRaw('cleanup_type, COUNT(*) as count')
  293. ->groupBy('cleanup_type');
  294. if ($planId) {
  295. $query->where('plan_id', $planId);
  296. }
  297. $stats = $query->get()->keyBy('cleanup_type')->toArray();
  298. $result = [];
  299. foreach (CLEANUP_TYPE::cases() as $type) {
  300. $result[$type->value] = [
  301. 'name' => $type->getDescription(),
  302. 'count' => $stats[$type->value]['count'] ?? 0,
  303. ];
  304. }
  305. return $result;
  306. }
  307. /**
  308. * 获取启用状态统计
  309. */
  310. public static function getEnabledStats(int $planId = null): array
  311. {
  312. $query = static::query();
  313. if ($planId) {
  314. $query->where('plan_id', $planId);
  315. }
  316. return [
  317. 'enabled' => $query->clone()->where('is_enabled', true)->count(),
  318. 'disabled' => $query->clone()->where('is_enabled', false)->count(),
  319. 'backup_enabled' => $query->clone()->where('backup_enabled', true)->count(),
  320. 'total' => $query->count(),
  321. ];
  322. }
  323. }