'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(); } }