SystemException.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Models;
  3. use Slowlyo\OwlAdmin\Models\AdminUser;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. /**
  7. * 系统异常记录模型
  8. */
  9. class SystemException extends Model
  10. {
  11. use SoftDeletes;
  12. protected $table = 'system_exceptions';
  13. protected $guarded = [];
  14. protected $casts = [
  15. 'request_data' => 'json',
  16. 'handled_at' => 'datetime',
  17. 'status' => 'integer',
  18. ];
  19. /**
  20. * 状态常量
  21. */
  22. const STATUS_PENDING = 0; // 未处理
  23. const STATUS_PROCESSING = 1; // 处理中
  24. const STATUS_HANDLED = 2; // 已处理
  25. const STATUS_IGNORED = 3; // 已忽略
  26. /**
  27. * 获取状态文本
  28. *
  29. * @return string
  30. */
  31. public function getStatusTextAttribute(): string
  32. {
  33. return match ($this->status) {
  34. self::STATUS_PENDING => '未处理',
  35. self::STATUS_PROCESSING => '处理中',
  36. self::STATUS_HANDLED => '已处理',
  37. self::STATUS_IGNORED => '已忽略',
  38. default => '未知状态',
  39. };
  40. }
  41. /**
  42. * 获取处理人关联
  43. */
  44. public function handlerUser()
  45. {
  46. return $this->belongsTo(AdminUser::class, 'handler', 'name');
  47. }
  48. /**
  49. * 获取相关用户
  50. */
  51. public function user()
  52. {
  53. return $this->belongsTo(MemberUser::class, 'user_id');
  54. }
  55. /**
  56. * 创建异常记录
  57. *
  58. * @param \Throwable $exception 异常对象
  59. * @param array $requestData 请求数据
  60. * @param int|null $userId 用户ID
  61. * @return static
  62. */
  63. public static function createFromException(\Throwable $exception, array $requestData = [], ?int $userId = null): static
  64. {
  65. return static::create([
  66. 'type' => get_class($exception),
  67. 'message' => $exception->getMessage(),
  68. 'file' => $exception->getFile(),
  69. 'line' => $exception->getLine(),
  70. 'trace' => $exception->getTraceAsString(),
  71. 'request_data' => $requestData,
  72. 'user_id' => $userId,
  73. 'status' => self::STATUS_PENDING,
  74. ]);
  75. }
  76. /**
  77. * 标记为处理中
  78. *
  79. * @return bool
  80. */
  81. public function markAsProcessing(): bool
  82. {
  83. return $this->update(['status' => self::STATUS_PROCESSING]);
  84. }
  85. /**
  86. * 标记为已处理
  87. *
  88. * @param string $handler 处理人
  89. * @return bool
  90. */
  91. public function markAsHandled(string $handler): bool
  92. {
  93. return $this->update([
  94. 'status' => self::STATUS_HANDLED,
  95. 'handler' => $handler,
  96. 'handled_at' => now(),
  97. ]);
  98. }
  99. /**
  100. * 标记为已忽略
  101. *
  102. * @param string $handler 处理人
  103. * @return bool
  104. */
  105. public function markAsIgnored(string $handler): bool
  106. {
  107. return $this->update([
  108. 'status' => self::STATUS_IGNORED,
  109. 'handler' => $handler,
  110. 'handled_at' => now(),
  111. ]);
  112. }
  113. /**
  114. * 更新AI解决方案
  115. *
  116. * @param string $solution AI提供的解决方案
  117. * @return bool
  118. */
  119. public function updateSolution(string $solution): bool
  120. {
  121. return $this->update(['solution' => $solution]);
  122. }
  123. /**
  124. * 获取未处理的异常
  125. *
  126. * @param int $limit 限制数量
  127. * @return \Illuminate\Database\Eloquent\Collection
  128. */
  129. public static function getPending(int $limit = 10)
  130. {
  131. return static::where('status', self::STATUS_PENDING)
  132. ->latest()
  133. ->limit($limit)
  134. ->get();
  135. }
  136. /**
  137. * 获取处理中的异常
  138. *
  139. * @param int $limit 限制数量
  140. * @return \Illuminate\Database\Eloquent\Collection
  141. */
  142. public static function getProcessing(int $limit = 10)
  143. {
  144. return static::where('status', self::STATUS_PROCESSING)
  145. ->latest()
  146. ->limit($limit)
  147. ->get();
  148. }
  149. }