77 lines
1.6 KiB
PHP
77 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;
|
|
|
|
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',
|
|
];
|
|
|
|
/**
|
|
* @return BelongsTo<LlmProvider>
|
|
*/
|
|
public function llmProvider(): BelongsTo
|
|
{
|
|
return $this->belongsTo(LlmProvider::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany<Admin>
|
|
*/
|
|
public function admins(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Admin::class, 'admin_client')
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* 检查客户是否处于活跃状态
|
|
*/
|
|
public function isActive(): bool
|
|
{
|
|
return $this->status === self::STATUS_ACTIVE;
|
|
}
|
|
|
|
/**
|
|
* 检查是否可以发送LLM请求
|
|
*/
|
|
public function canSendLlmRequest(): bool
|
|
{
|
|
/** @var LlmProvider|null $provider */
|
|
$provider = $this->llmProvider;
|
|
return $this->isActive() && $provider && $provider->isActive();
|
|
}
|
|
}
|