Lae/app/Models/Host.php

194 lines
5.0 KiB
PHP
Raw Normal View History

2022-08-16 10:44:16 +00:00
<?php
namespace App\Models;
2022-09-22 06:00:03 +00:00
use App\Events\UserEvent;
2022-09-15 04:13:37 +00:00
use App\Models\Transaction;
2022-08-16 10:44:16 +00:00
use App\Models\Module\Module;
2022-08-26 14:37:20 +00:00
// use Illuminate\Database\Eloquent\SoftDeletes;
2022-09-22 06:00:03 +00:00
use App\Models\WorkOrder\WorkOrder;
2022-09-15 04:13:37 +00:00
use Illuminate\Support\Facades\Cache;
2022-08-30 09:20:45 +00:00
use Illuminate\Database\Eloquent\Model;
use App\Exceptions\User\BalanceNotEnoughException;
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
];
2022-09-10 04:03:20 +00:00
// get user hosts
public function getUserHosts($user_id = null)
{
return Cache::remember('user_hosts_' . $user_id ?? auth()->id(), 3600, function () use ($user_id) {
return $this->where('user_id', $user_id)->with('module', function ($query) {
2022-09-10 04:03:20 +00:00
$query->select(['id', 'name']);
})->get();
});
}
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
2022-09-15 12:57:36 +00:00
public function cost($price = null, $auto = true)
{
2022-08-30 09:20:45 +00:00
$this->load('user');
2022-09-15 04:13:37 +00:00
$transaction = new Transaction();
$drops = $transaction->getDrops($this->user_id);
if ($price !== null) {
2022-09-09 12:18:17 +00:00
$this->price = $price;
}
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-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;
2022-09-10 03:25:59 +00:00
$month_cache_key = 'user_' . $this->user_id . '_month_' . $month . '_hosts_drops';
2022-09-09 13:42:12 +00:00
$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-15 12:57:36 +00:00
$transaction->reduceDrops($this->user_id, $this->id, $this->module_id, $auto, $this->price);
2022-08-30 09:20:45 +00:00
2022-09-22 06:00:03 +00:00
broadcast(new UserEvent($this->user_id, 'balances.drops.reduced', $this->user));
2022-09-28 05:24:23 +00:00
// 检测用户余额是否足够
if ($this->user->balance < 0) {
$this->update([
'status' => 'suspended',
]);
}
return true;
}
2022-08-29 10:59:32 +00:00
protected static function boot()
{
parent::boot();
2022-08-16 10:44:16 +00:00
2022-09-22 06:00:03 +00:00
static::created(function ($model) {
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
});
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-09-22 06:00:03 +00:00
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
2022-08-29 10:59:32 +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-09-10 04:05:28 +00:00
Cache::forget('user_hosts_' . $model->user_id);
2022-09-22 06:00:03 +00:00
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
2022-08-29 10:59:32 +00:00
});
2022-08-28 17:17:57 +00:00
2022-09-22 06:00:03 +00:00
//
2022-08-29 10:59:32 +00:00
// static::deleting(function ($model) {
2022-09-22 06:00:03 +00:00
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
2022-08-29 10:59:32 +00:00
// });
2022-09-10 04:05:28 +00:00
static::deleted(function ($model) {
2022-09-22 06:00:03 +00:00
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
2022-09-10 04:05:28 +00:00
Cache::forget('user_hosts_' . $model->user_id);
});
2022-08-29 10:59:32 +00:00
}
2022-08-16 10:44:16 +00:00
}