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;
-
- 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');
- }
-
- 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,
- ]);
- }
-
- public function markAsProcessing(): bool
- {
- return $this->update(['status' => self::STATUS_PROCESSING]);
- }
-
- public function markAsHandled(string $handler): bool
- {
- return $this->update([
- 'status' => self::STATUS_HANDLED,
- 'handler' => $handler,
- 'handled_at' => now(),
- ]);
- }
-
- public function markAsIgnored(string $handler): bool
- {
- return $this->update([
- 'status' => self::STATUS_IGNORED,
- 'handler' => $handler,
- 'handled_at' => now(),
- ]);
- }
-
- public function updateSolution(string $solution): bool
- {
- return $this->update(['solution' => $solution]);
- }
-
- public static function getPending(int $limit = 10)
- {
- return static::where('status', self::STATUS_PENDING)
- ->latest()
- ->limit($limit)
- ->get();
- }
-
- public static function getProcessing(int $limit = 10)
- {
- return static::where('status', self::STATUS_PROCESSING)
- ->latest()
- ->limit($limit)
- ->get();
- }
- }
|