Lae/app/Models/Host.php

177 lines
4.1 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 = [
2022-08-29 10:59:32 +00:00
// 'configuration' => 'array',
'suspended_at' => 'datetime',
2022-08-16 10:44:16 +00:00
];
// 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());
}
2022-08-29 10:59:32 +00:00
// 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;
}
2022-08-29 10:59:32 +00:00
$user->drops -= (int) $this->price;
// update cache
Cache::put($cache_key, $user, now()->addDay());
2022-08-29 16:04:50 +00:00
// if $user->drops <= 0
if ($user->drops <= 0) {
$this->update([
'status' => 'suspended',
]);
}
return true;
}
2022-08-28 17:17:57 +00:00
/**
* 创建主机
*
* 在此之后,所有的主机都将由 module 创建,并且主机的数据仅被用作计费。
*
* 废弃
* @deprecated
*/
2022-08-16 10:44:16 +00:00
// on create
2022-08-29 10:59:32 +00:00
protected static function boot()
{
parent::boot();
2022-08-16 10:44:16 +00:00
2022-08-29 10:59:32 +00:00
// 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
2022-08-29 10:59:32 +00:00
// // // set price to 0
// // $model->price = 0;
2022-08-16 10:44:16 +00:00
2022-08-29 10:59:32 +00:00
// // $model->load('module');
// // $model->module->load(['provider', 'module']);
2022-08-16 10:44:16 +00:00
2022-08-29 10:59:32 +00:00
// // add to queue
2022-08-29 10:59:32 +00:00
// });
2022-08-29 10:59:32 +00:00
static::updating(function ($model) {
if ($model->status == 'suspended') {
$model->suspended_at = now();
} else if ($model->status == 'running') {
$model->suspended_at = null;
}
});
2022-08-29 10:59:32 +00:00
// when Updated
static::updated(function ($model) {
dispatch(new \App\Jobs\Remote\Host($model, 'patch'));
});
2022-08-28 17:17:57 +00:00
2022-08-29 10:59:32 +00:00
// // when delete
// static::deleting(function ($model) {
// // return false;
// // dispatch(new \App\Jobs\Remote\Host($model, 'delete'));
// });
}
2022-08-16 10:44:16 +00:00
}