Lae/app/Models/Host.php

151 lines
3.3 KiB
PHP
Raw Normal View History

2022-08-16 10:44:16 +00:00
<?php
namespace App\Models;
use App\Models\Module\Module;
use App\Exceptions\CommonException;
2022-08-16 10:44:16 +00:00
use App\Models\WorkOrder\WorkOrder;
use Illuminate\Support\Facades\Cache;
2022-08-16 10:44:16 +00:00
use Illuminate\Database\Eloquent\Model;
2022-08-26 14:37:20 +00:00
// use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2022-08-16 10:44:16 +00:00
class Host extends Model
{
2022-08-26 14:37:20 +00:00
use HasFactory;
2022-08-16 10:44:16 +00:00
protected $table = 'hosts';
protected $fillable = [
'name',
'module_id',
'user_id',
'price',
'configuration',
'status',
'managed_price',
];
protected $casts = [
'configuration' => 'array'
];
// user
public function user()
{
2022-08-16 10:44:16 +00:00
return $this->belongsTo(User::class);
}
// module
public function module()
{
2022-08-16 10:44:16 +00:00
return $this->belongsTo(Module::class);
}
// workOrders
public function workOrders()
{
2022-08-16 10:44:16 +00:00
return $this->hasMany(WorkOrder::class);
}
// module 远程一对一
// public function module() {
// return $this->hasOneThrough(Module::class, ProviderModule::class);
// }
// scope
public function scopeActive($query)
{
2022-08-16 10:44:16 +00:00
return $query->where('status', 'running')->where('price', '!=', 0);
}
public function scopeThisUser($query, $module = null)
{
2022-08-19 15:27:57 +00:00
if ($module) {
return $query->where('user_id', auth()->id())->where('module_id', $module);
} else {
return $query->where('user_id', auth()->id());
}
}
// cost
public function cost($price = null)
{
$cache_key = 'user_' . $this->user_id;
// if cache has user
if (Cache::has($cache_key)) {
// if user is not instances of Model
$user = Cache::get($cache_key);
if (!($user instanceof User)) {
$user = Cache::put($cache_key, $this->user, now()->addDay());
}
} else {
$user = Cache::put($cache_key, $this->user, now()->addDay());
}
// Log::debug($user);
if ($price !== null) {
$this->managed_price = $price;
}
2022-08-26 14:37:20 +00:00
if ($this->managed_price) {
$this->price = $this->managed_price;
}
$user->drops -= $this->price;
// update cache
Cache::put($cache_key, $user, now()->addDay());
return true;
}
2022-08-16 10:44:16 +00:00
// on create
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
// if sanctum
if (auth('sanctum')->check()) {
$model->user_id = auth('sanctum')->id();
} else {
// if user_id is null
// check user_id is exists
throw_if(!User::find($model->user_id), CommonException::class, 'user is not exists');
}
2022-08-26 14:37:20 +00:00
// set price to 0
$model->price = 0;
2022-08-16 10:44:16 +00:00
// $model->load('module');
// $model->module->load(['provider', 'module']);
// add to queue
});
// when Updated
static::updated(function ($model) {
dispatch(new \App\Jobs\Remote\Host($model, 'put'));
});
// when delete
static::deleting(function ($model) {
2022-08-26 14:37:20 +00:00
// return false;
dispatch(new \App\Jobs\Remote\Host($model, 'delete'));
});
2022-08-16 10:44:16 +00:00
}
}