123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace App\Models;
- use Slowlyo\OwlAdmin\Models\AdminUser;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- /**
- * 系统异常记录模型
- */
- class SystemException extends Model
- {
- use SoftDeletes;
- protected $table = 'system_exceptions';
- protected $guarded = [];
- protected $casts = [
- 'request_data' => 'json',
- 'handled_at' => 'datetime',
- 'status' => 'integer',
- ];
- /**
- * 状态常量
- */
- const STATUS_PENDING = 0; // 未处理
- const STATUS_PROCESSING = 1; // 处理中
- const STATUS_HANDLED = 2; // 已处理
- const STATUS_IGNORED = 3; // 已忽略
- /**
- * 获取状态文本
- *
- * @return string
- */
- public function getStatusTextAttribute(): string
- {
- return match ($this->status) {
- self::STATUS_PENDING => '未处理',
- self::STATUS_PROCESSING => '处理中',
- self::STATUS_HANDLED => '已处理',
- self::STATUS_IGNORED => '已忽略',
- default => '未知状态',
- };
- }
- /**
- * 获取处理人关联
- */
- public function handlerUser()
- {
- return $this->belongsTo(AdminUser::class, 'handler', 'name');
- }
- /**
- * 获取相关用户
- */
- public function user()
- {
- return $this->belongsTo(MemberUser::class, 'user_id');
- }
- /**
- * 创建异常记录
- *
- * @param \Throwable $exception 异常对象
- * @param array $requestData 请求数据
- * @param int|null $userId 用户ID
- * @return static
- */
- public static function createFromException(\Throwable $exception, array $requestData = [], ?int $userId = null): static
- {
- return static::create([
- 'type' => get_class($exception),
- 'message' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString(),
- 'request_data' => $requestData,
- 'user_id' => $userId,
- 'status' => self::STATUS_PENDING,
- ]);
- }
- /**
- * 标记为处理中
- *
- * @return bool
- */
- public function markAsProcessing(): bool
- {
- return $this->update(['status' => self::STATUS_PROCESSING]);
- }
- /**
- * 标记为已处理
- *
- * @param string $handler 处理人
- * @return bool
- */
- public function markAsHandled(string $handler): bool
- {
- return $this->update([
- 'status' => self::STATUS_HANDLED,
- 'handler' => $handler,
- 'handled_at' => now(),
- ]);
- }
- /**
- * 标记为已忽略
- *
- * @param string $handler 处理人
- * @return bool
- */
- public function markAsIgnored(string $handler): bool
- {
- return $this->update([
- 'status' => self::STATUS_IGNORED,
- 'handler' => $handler,
- 'handled_at' => now(),
- ]);
- }
- /**
- * 更新AI解决方案
- *
- * @param string $solution AI提供的解决方案
- * @return bool
- */
- public function updateSolution(string $solution): bool
- {
- return $this->update(['solution' => $solution]);
- }
- /**
- * 获取未处理的异常
- *
- * @param int $limit 限制数量
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function getPending(int $limit = 10)
- {
- return static::where('status', self::STATUS_PENDING)
- ->latest()
- ->limit($limit)
- ->get();
- }
- /**
- * 获取处理中的异常
- *
- * @param int $limit 限制数量
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function getProcessing(int $limit = 10)
- {
- return static::where('status', self::STATUS_PROCESSING)
- ->latest()
- ->limit($limit)
- ->get();
- }
- }
|