2022-08-16 10:44:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Models\Module\Module;
|
|
|
|
use App\Models\WorkOrder\WorkOrder;
|
2022-08-23 09:36:10 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-08-26 14:37:20 +00:00
|
|
|
// use Illuminate\Database\Eloquent\SoftDeletes;
|
2022-08-30 09:20:45 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use App\Exceptions\User\BalanceNotEnoughException;
|
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)
|
|
|
|
{
|
2022-08-30 09:20:45 +00:00
|
|
|
$this->load('user');
|
2022-08-23 09:36:10 +00:00
|
|
|
|
2022-09-09 13:04:09 +00:00
|
|
|
$drops = getDrops($this->user_id);
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
if ($price !== null) {
|
2022-09-09 12:18:17 +00:00
|
|
|
$this->price = $price;
|
2022-08-23 09:36:10 +00:00
|
|
|
}
|
2022-08-26 14:37:20 +00:00
|
|
|
|
2022-09-09 13:04:09 +00:00
|
|
|
$amount = $price / config('drops.rate') + 1;
|
2022-09-03 17:32:50 +00:00
|
|
|
|
2022-08-30 09:20:45 +00:00
|
|
|
// if drops <= price
|
|
|
|
if ($drops < $this->price) {
|
|
|
|
try {
|
2022-09-07 17:55:07 +00:00
|
|
|
// 算出需要补充多少 Drops
|
|
|
|
$need = $this->price - $drops;
|
|
|
|
|
|
|
|
// 算出需要补充多少余额
|
2022-09-09 13:04:09 +00:00
|
|
|
$need_amount = $need / config('drops.rate') + 1;
|
2022-09-07 17:55:07 +00:00
|
|
|
|
|
|
|
$this->user->toDrops($amount + $need_amount);
|
2022-08-30 09:20:45 +00:00
|
|
|
} catch (BalanceNotEnoughException) {
|
|
|
|
$this->update([
|
|
|
|
'status' => 'suspended',
|
|
|
|
]);
|
2022-08-23 09:36:10 +00:00
|
|
|
|
2022-08-30 09:20:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-30 06:36:59 +00:00
|
|
|
} else if ($this->status == 'suspended') {
|
|
|
|
$this->update([
|
2022-08-30 09:20:45 +00:00
|
|
|
'status' => 'stopped',
|
2022-08-30 06:36:59 +00:00
|
|
|
]);
|
2022-08-29 16:04:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:42:12 +00:00
|
|
|
$month = now()->month;
|
|
|
|
|
|
|
|
$month_cache_key = 'user_' . $this->user_id . '_month_' . $month . 'hosts_drops';
|
|
|
|
$hosts_drops = Cache::get($month_cache_key, []);
|
|
|
|
|
|
|
|
// 统计 Host 消耗的 Drops
|
|
|
|
if (isset($hosts_drops[$this->id])) {
|
|
|
|
$hosts_drops[$this->id] += $this->price;
|
|
|
|
} else {
|
|
|
|
$hosts_drops[$this->id] = $this->price;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::put($month_cache_key, $hosts_drops, 604800);
|
|
|
|
|
2022-09-09 13:04:09 +00:00
|
|
|
reduceDrops($this->user_id, $this->price);
|
2022-08-30 09:20:45 +00:00
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:17:57 +00:00
|
|
|
/**
|
|
|
|
* 创建主机
|
2022-09-01 15:46:42 +00:00
|
|
|
*
|
2022-08-28 17:17:57 +00:00
|
|
|
* 在此之后,所有的主机都将由 module 创建,并且主机的数据仅被用作计费。
|
2022-09-01 15:46:42 +00:00
|
|
|
*
|
2022-08-28 17:17:57 +00:00
|
|
|
* 废弃
|
|
|
|
* @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
|
2022-09-08 16:12:02 +00:00
|
|
|
// // if (auth('api')->check()) {
|
|
|
|
// // $model->user_id = auth('api')->id();
|
2022-08-29 10:59:32 +00:00
|
|
|
// // } 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
|
|
|
}
|