60 lines
1.2 KiB
PHP
60 lines
1.2 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
||
class PrintPluginInstallation extends Model
|
||
{
|
||
use HasFactory;
|
||
|
||
protected $fillable = [
|
||
'user_id',
|
||
'plugin_id',
|
||
'installed_version',
|
||
'device_id',
|
||
'device_name',
|
||
'os_version',
|
||
'install_status',
|
||
'error_message',
|
||
'last_heartbeat',
|
||
'installed_at',
|
||
];
|
||
|
||
protected $casts = [
|
||
'last_heartbeat' => 'datetime',
|
||
'installed_at' => 'datetime',
|
||
];
|
||
|
||
public function user(): BelongsTo
|
||
{
|
||
return $this->belongsTo(User::class);
|
||
}
|
||
|
||
public function plugin(): BelongsTo
|
||
{
|
||
return $this->belongsTo(PrintPlugin::class, 'plugin_id');
|
||
}
|
||
|
||
/**
|
||
* 检查心跳是否超时(超过5分钟视为离线)
|
||
*/
|
||
public function isOnline(): bool
|
||
{
|
||
if (!$this->last_heartbeat) {
|
||
return false;
|
||
}
|
||
return $this->last_heartbeat->diffInMinutes(now()) < 5;
|
||
}
|
||
|
||
/**
|
||
* 记录心跳
|
||
*/
|
||
public function recordHeartbeat(): void
|
||
{
|
||
$this->update(['last_heartbeat' => now()]);
|
||
}
|
||
}
|