75 lines
1.6 KiB
PHP
75 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Client extends Model
|
|
{
|
|
/**
|
|
* 客户状态常量
|
|
*/
|
|
public const STATUS_ACTIVE = 'active';
|
|
public const STATUS_INACTIVE = 'inactive';
|
|
|
|
/**
|
|
* 有效的状态列表
|
|
*/
|
|
public const VALID_STATUSES = [
|
|
self::STATUS_ACTIVE,
|
|
self::STATUS_INACTIVE,
|
|
];
|
|
|
|
protected $table = 'clients';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'llm_provider_id',
|
|
'rate_limit',
|
|
'timeout',
|
|
'status',
|
|
];
|
|
|
|
protected $casts = [
|
|
'rate_limit' => 'integer',
|
|
'timeout' => 'integer',
|
|
'status' => 'string',
|
|
];
|
|
|
|
public function llmProvider(): BelongsTo
|
|
{
|
|
return $this->belongsTo(LlmProvider::class);
|
|
}
|
|
|
|
public function admins(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Admin::class, 'admin_client')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function authTokens(): HasMany
|
|
{
|
|
return $this->hasMany(AuthToken::class);
|
|
}
|
|
|
|
/**
|
|
* 检查客户是否处于活跃状态
|
|
*/
|
|
public function isActive(): bool
|
|
{
|
|
return $this->status === self::STATUS_ACTIVE;
|
|
}
|
|
|
|
/**
|
|
* 检查是否可以发送LLM请求
|
|
*/
|
|
public function canSendLlmRequest(): bool
|
|
{
|
|
return $this->isActive() && $this->llmProvider->isActive();
|
|
}
|
|
}
|