86 lines
2.0 KiB
PHP
86 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\DeviceStatus;
|
|
use App\Enums\DeviceType;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Peidikeji\Keywords\Models\Keywords;
|
|
|
|
class Device extends Model
|
|
{
|
|
use HasFactory, Filterable, HasDateTimeFormatter;
|
|
|
|
protected $attributes = [
|
|
'status' => DeviceStatus::Offline,
|
|
];
|
|
|
|
protected $casts = [
|
|
'type' => DeviceType::class,
|
|
'status' => DeviceStatus::class,
|
|
'extends' => 'json',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'agricultural_base_id',
|
|
'is_recommend',
|
|
'sn',
|
|
'name',
|
|
'model',
|
|
'monitoring_point',
|
|
'type',
|
|
'status',
|
|
'extends',
|
|
'created_by',
|
|
'updated_by',
|
|
'sort',
|
|
'supplier_key',
|
|
'project_key',
|
|
];
|
|
|
|
public function scopeSupplierBy(Builder $query, string $supplier): void
|
|
{
|
|
$query->whereHas('supplier', fn ($query) => $query->where('supplier_key', $supplier));
|
|
}
|
|
|
|
public function base()
|
|
{
|
|
return $this->belongsTo(AgriculturalBase::class, 'agricultural_base_id');
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(AdminUser::class, 'created_by');
|
|
}
|
|
|
|
public function updatedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(AdminUser::class, 'updated_by');
|
|
}
|
|
|
|
public function supplier(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Keywords::class, 'supplier_key', 'key');
|
|
}
|
|
|
|
public function project(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Keywords::class, 'project_key', 'key');
|
|
}
|
|
|
|
public function isTypeSoil(): bool
|
|
{
|
|
return $this->type === DeviceType::Soil;
|
|
}
|
|
|
|
public function isTypeMeteorological(): bool
|
|
{
|
|
return $this->type === DeviceType::Meteorological;
|
|
}
|
|
}
|