2022-08-16 10:44:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Models\Module\Module;
|
2022-08-23 09:36:10 +00:00
|
|
|
use App\Exceptions\CommonException;
|
2022-08-16 10:44:16 +00:00
|
|
|
use App\Models\WorkOrder\WorkOrder;
|
2022-08-23 09:36:10 +00:00
|
|
|
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;
|
2022-08-23 09:36:10 +00:00
|
|
|
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
|
2022-08-23 09:36:10 +00:00
|
|
|
public function user()
|
|
|
|
{
|
2022-08-16 10:44:16 +00:00
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// module
|
2022-08-23 09:36:10 +00:00
|
|
|
public function module()
|
|
|
|
{
|
2022-08-16 10:44:16 +00:00
|
|
|
return $this->belongsTo(Module::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// workOrders
|
2022-08-23 09:36:10 +00:00
|
|
|
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
|
2022-08-23 09:36:10 +00:00
|
|
|
public function scopeActive($query)
|
|
|
|
{
|
2022-08-16 10:44:16 +00:00
|
|
|
return $query->where('status', 'running')->where('price', '!=', 0);
|
|
|
|
}
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
// Log::debug($user);
|
|
|
|
|
|
|
|
if ($price !== null) {
|
|
|
|
$this->managed_price = $price;
|
|
|
|
}
|
2022-08-26 14:37:20 +00:00
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
if ($this->managed_price) {
|
|
|
|
$this->price = $this->managed_price;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-29 10:59:32 +00:00
|
|
|
$user->drops -= (int) $this->price;
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
// 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',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
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-23 09:36:10 +00:00
|
|
|
|
2022-08-29 10:59:32 +00:00
|
|
|
// });
|
2022-08-23 09:36:10 +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-23 09:36:10 +00:00
|
|
|
|
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
|
|
|
}
|