Lae/app/Models/Host.php

237 lines
6.3 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-08-30 09:20:45 +00:00
use App\Exceptions\User\BalanceNotEnoughException;
2022-11-06 11:28:22 +00:00
use App\Models\WorkOrder\WorkOrder;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2022-11-06 11:28:22 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo as BelongsToAlias;
use Illuminate\Database\Eloquent\Relations\HasMany as HasManyAlias;
use Illuminate\Support\Facades\Cache;
// use Illuminate\Database\Eloquent\SoftDeletes;
2022-08-16 10:44:16 +00:00
class Host extends Model
{
2022-11-06 11:28:22 +00:00
use HasFactory, Cachable;
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)
{
2022-11-06 11:28:22 +00:00
return $this->where('user_id', $user_id)->with('module', function ($query) {
$query->select(['id', 'name']);
})->get();
2022-09-10 04:03:20 +00:00
}
2022-08-16 10:44:16 +00:00
// user
2022-11-06 11:28:22 +00:00
public function user(): BelongsToAlias
{
2022-08-16 10:44:16 +00:00
return $this->belongsTo(User::class);
}
// module
2022-11-06 11:28:22 +00:00
public function module(): BelongsToAlias
{
2022-08-16 10:44:16 +00:00
return $this->belongsTo(Module::class);
}
// workOrders
2022-11-06 11:28:22 +00:00
public function workOrders(): HasManyAlias
{
2022-08-16 10:44:16 +00:00
return $this->hasMany(WorkOrder::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-11-06 11:28:22 +00:00
public function cost($price = null, $auto = true): bool
{
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);
2022-10-23 05:00:41 +00:00
if ($price !== null) {
$real_price = $price;
} else {
2022-10-23 04:58:18 +00:00
$real_price = $this->price;
}
2022-08-26 14:37:20 +00:00
2022-10-23 05:00:41 +00:00
if ($real_price == 0) {
2022-10-23 04:58:18 +00:00
return true;
2022-10-23 04:52:17 +00:00
}
$real_price = round($real_price ?? 0, 8);
2022-10-15 01:54:46 +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
2022-10-16 09:25:04 +00:00
if ($drops < $real_price) {
2022-08-30 09:20:45 +00:00
try {
2022-09-07 17:55:07 +00:00
// 算出需要补充多少 Drops
2022-10-16 09:25:04 +00:00
$need = $real_price - $drops;
2022-09-07 17:55:07 +00:00
// 算出需要补充多少余额
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])) {
2022-10-16 09:25:04 +00:00
$hosts_drops[$this->id] += $real_price;
2022-09-09 13:42:12 +00:00
} else {
2022-10-16 09:25:04 +00:00
$hosts_drops[$this->id] = $real_price;
2022-09-09 13:42:12 +00:00
}
Cache::put($month_cache_key, $hosts_drops, 604800);
2022-10-16 09:25:04 +00:00
$transaction->reduceDrops($this->user_id, $this->id, $this->module_id, $auto, $real_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-10-07 05:15:08 +00:00
public function costBalance($amount = 1)
{
$transaction = new Transaction();
2022-10-09 06:53:25 +00:00
$month = now()->month;
$month_cache_key = 'user_' . $this->user_id . '_month_' . $month . '_hosts_balances';
$hosts_drops = Cache::get($month_cache_key, []);
// 统计 Host 消耗的 Drops
if (isset($hosts_drops[$this->id])) {
$hosts_drops[$this->id] += $amount;
} else {
$hosts_drops[$this->id] = $amount;
}
Cache::put($month_cache_key, $hosts_drops, 604800);
2022-10-09 07:01:21 +00:00
$left = $transaction->reduceHostAmount($this->user_id, $this->id, $this->module_id, $amount);
2022-10-07 05:15:08 +00:00
broadcast(new UserEvent($this->user_id, 'balances.amount.reduced', $this->user));
if ($left < 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-10-28 08:22:45 +00:00
Cache::forget('user_tasks_' . $model->user_id);
2022-09-22 06:00:03 +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-10-28 08:22:45 +00:00
Cache::forget('user_tasks_' . $model->user_id);
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-10-28 08:22:45 +00:00
Cache::forget('user_tasks_' . $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
2022-10-28 08:22:45 +00:00
static::deleting(function ($model) {
Cache::forget('user_tasks_' . $model->user_id);
});
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-10-28 08:22:45 +00:00
Cache::forget('user_tasks_' . $model->user_id);
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
}