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-11-06 11:28:22 +00:00
|
|
|
use App\Models\WorkOrder\WorkOrder;
|
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
2022-08-23 09:36:10 +00:00
|
|
|
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',
|
2022-11-18 09:16:30 +00:00
|
|
|
'suspended_at',
|
2022-08-16 10:44:16 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
2022-08-29 10:59:32 +00:00
|
|
|
// 'configuration' => 'array',
|
|
|
|
'suspended_at' => 'datetime',
|
|
|
|
|
2022-08-16 10:44:16 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
2022-11-16 05:16:56 +00:00
|
|
|
// user
|
|
|
|
|
2022-11-19 04:38:26 +00:00
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::created(function ($model) {
|
|
|
|
broadcast(new UserEvent($model->user_id, 'hosts.created', $model));
|
|
|
|
});
|
|
|
|
|
|
|
|
static::updating(function ($model) {
|
|
|
|
|
|
|
|
if ($model->isDirty('status')) {
|
|
|
|
if ($model->status == 'suspended') {
|
|
|
|
$model->suspended_at = now();
|
|
|
|
} else {
|
|
|
|
$model->suspended_at = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcast(new UserEvent($model->user_id, 'hosts.updating', $model));
|
|
|
|
});
|
|
|
|
|
|
|
|
// when Updated
|
|
|
|
static::updated(function ($model) {
|
|
|
|
dispatch(new \App\Jobs\Module\Host($model, 'patch'));
|
|
|
|
|
|
|
|
Cache::forget('user_hosts_' . $model->user_id);
|
|
|
|
Cache::forget('user_tasks_' . $model->user_id);
|
|
|
|
|
|
|
|
|
|
|
|
broadcast(new UserEvent($model->user_id, 'hosts.updated', $model));
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// static::deleting(function ($model) {
|
|
|
|
// broadcast(new UserEvent($model->user_id, 'hosts.deleting', $model));
|
|
|
|
// });
|
|
|
|
|
|
|
|
static::deleting(function ($model) {
|
|
|
|
Cache::forget('user_tasks_' . $model->user_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
static::deleted(function ($model) {
|
|
|
|
broadcast(new UserEvent($model->user_id, 'hosts.deleted', $model));
|
|
|
|
Cache::forget('user_tasks_' . $model->user_id);
|
|
|
|
Cache::forget('user_hosts_' . $model->user_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// module
|
|
|
|
|
2022-09-19 13:24:14 +00:00
|
|
|
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-11-19 04:38:26 +00:00
|
|
|
// workOrders
|
2022-09-10 04:03:20 +00:00
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
public function user(): BelongsToAlias
|
2022-08-23 09:36:10 +00:00
|
|
|
{
|
2022-08-16 10:44:16 +00:00
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
2022-11-19 04:38:26 +00:00
|
|
|
// scope
|
2022-11-16 05:16:56 +00:00
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
public function module(): BelongsToAlias
|
2022-08-23 09:36:10 +00:00
|
|
|
{
|
2022-08-16 10:44:16 +00:00
|
|
|
return $this->belongsTo(Module::class);
|
|
|
|
}
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
public function workOrders(): HasManyAlias
|
2022-08-23 09:36:10 +00:00
|
|
|
{
|
2022-08-16 10:44:16 +00:00
|
|
|
return $this->hasMany(WorkOrder::class);
|
|
|
|
}
|
|
|
|
|
2022-11-19 04:38:26 +00:00
|
|
|
|
|
|
|
// cost
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
public function scopeActive($query)
|
|
|
|
{
|
2022-11-19 13:18:21 +00:00
|
|
|
return $query->whereIn('status', ['running', 'stopped']);
|
2022-08-16 10:44:16 +00:00
|
|
|
}
|
|
|
|
|
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-11-19 02:11:56 +00:00
|
|
|
public function safeDelete(): bool
|
|
|
|
{
|
|
|
|
dispatch(new \App\Jobs\Module\Host($this, 'delete'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
// public function cost($price = null, $auto = true): bool
|
|
|
|
// {
|
|
|
|
// $this->load('user');
|
|
|
|
//
|
|
|
|
// $transaction = new Transaction();
|
|
|
|
//
|
|
|
|
// $drops = $transaction->getDrops($this->user_id);
|
|
|
|
//
|
|
|
|
// $real_price = $price ?? $this->price;
|
|
|
|
//
|
|
|
|
// if (!$price) {
|
|
|
|
//
|
|
|
|
// if ($this->managed_price) {
|
|
|
|
// $real_price = $this->managed_price;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if ($real_price == 0) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// $real_price = round($real_price ?? 0, 8);
|
|
|
|
//
|
|
|
|
// $amount = $price / config('drops.rate') + 1;
|
|
|
|
//
|
|
|
|
// // if drops <= price
|
|
|
|
// if ($drops < $real_price) {
|
|
|
|
// try {
|
|
|
|
// // 算出需要补充多少 Drops
|
|
|
|
// $need = $real_price - $drops;
|
|
|
|
//
|
|
|
|
// // 算出需要补充多少余额
|
|
|
|
// $need_amount = $need / config('drops.rate') + 1;
|
|
|
|
//
|
|
|
|
// $this->user->toDrops($amount + $need_amount);
|
|
|
|
// } catch (BalanceNotEnoughException) {
|
|
|
|
// $this->update([
|
|
|
|
// 'status' => 'suspended',
|
|
|
|
// ]);
|
|
|
|
//
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// } else if ($this->status == 'suspended') {
|
|
|
|
// $this->update([
|
|
|
|
// 'status' => 'stopped',
|
|
|
|
// ]);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// $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] += $real_price;
|
|
|
|
// } else {
|
|
|
|
// $hosts_drops[$this->id] = $real_price;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Cache::put($month_cache_key, $hosts_drops, 604800);
|
|
|
|
//
|
|
|
|
// $transaction->reduceDrops($this->user_id, $this->id, $this->module_id, $auto, $real_price);
|
|
|
|
//
|
|
|
|
// $this->addLog('drops', $real_price);
|
|
|
|
//
|
|
|
|
// broadcast(new UserEvent($this->user_id, 'balances.drops.reduced', $this->user));
|
|
|
|
//
|
|
|
|
// // 检测用户余额是否足够
|
|
|
|
// if ($this->user->balance < 0) {
|
|
|
|
// $this->update([
|
|
|
|
// 'status' => 'suspended',
|
|
|
|
// ]);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
public function addLog($type = 'drops', float|null $amount = 0): bool
|
2022-08-23 09:36:10 +00:00
|
|
|
{
|
2022-11-19 14:42:47 +00:00
|
|
|
if ($amount === 0 || $amount === null) {
|
2022-11-19 05:08:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-19 04:50:48 +00:00
|
|
|
/** 统计收益开始 */
|
|
|
|
$current_month = now()->month;
|
|
|
|
$current_year = now()->year;
|
|
|
|
|
|
|
|
$cache_key = 'module_earning_' . $this->module_id;
|
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
$commission = (float)config('billing.commission');
|
2022-11-19 05:08:39 +00:00
|
|
|
|
2022-11-19 04:50:48 +00:00
|
|
|
$should_amount = round($amount * $commission, 2);
|
|
|
|
|
|
|
|
// 应得的余额
|
|
|
|
$should_balance = $amount - $should_amount;
|
|
|
|
|
|
|
|
$earnings = Cache::get($cache_key, []);
|
|
|
|
|
|
|
|
if (!isset($earnings[$current_year])) {
|
|
|
|
$earnings[$current_year] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($earnings[$current_year][$current_month])) {
|
|
|
|
$earnings[$current_year][$current_month]['balance'] += $amount;
|
|
|
|
$earnings[$current_year][$current_month]['should_balance'] += $should_balance;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$earnings[$current_year][$current_month] = [
|
|
|
|
'balance' => $amount,
|
|
|
|
// 应得(交了手续费)
|
|
|
|
'should_balance' => $should_balance,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除 前 3 年的数据
|
|
|
|
if (count($earnings) > 3) {
|
|
|
|
$earnings = array_slice($earnings, -3, 3, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 保存 1 年
|
|
|
|
Cache::put($cache_key, $earnings, 24 * 60 * 60 * 365);
|
|
|
|
|
|
|
|
/** 统计收益结束 */
|
2022-11-19 05:08:58 +00:00
|
|
|
|
|
|
|
return true;
|
2022-11-19 04:50:48 +00:00
|
|
|
}
|
2022-11-19 06:04:42 +00:00
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
public function cost($amount = null, $auto = true): bool
|
2022-11-19 06:04:42 +00:00
|
|
|
{
|
2022-11-19 14:42:47 +00:00
|
|
|
|
|
|
|
$this->load('user');
|
|
|
|
|
|
|
|
$real_price = $amount ?? $this->price;
|
|
|
|
|
|
|
|
if (!$amount) {
|
|
|
|
if ($this->managed_price) {
|
|
|
|
$real_price = $this->managed_price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($real_price == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$real_price = round($real_price ?? 0, 2);
|
|
|
|
|
|
|
|
if ($real_price < 0.01) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-19 06:04:42 +00:00
|
|
|
$transaction = new Transaction();
|
|
|
|
|
|
|
|
$month = now()->month;
|
|
|
|
|
|
|
|
$month_cache_key = 'user_' . $this->user_id . '_month_' . $month . '_hosts_balances';
|
|
|
|
$hosts_drops = Cache::get($month_cache_key, []);
|
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
// 统计 Host 消耗的 Balance
|
2022-11-19 06:04:42 +00:00
|
|
|
if (isset($hosts_drops[$this->id])) {
|
2022-11-19 14:42:47 +00:00
|
|
|
$hosts_drops[$this->id] += $real_price;
|
2022-11-19 06:04:42 +00:00
|
|
|
} else {
|
2022-11-19 14:42:47 +00:00
|
|
|
$hosts_drops[$this->id] = $real_price;
|
2022-11-19 06:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Cache::put($month_cache_key, $hosts_drops, 604800);
|
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
$description = '模块发起的扣费。';
|
|
|
|
|
|
|
|
if ($auto) {
|
|
|
|
$description = '自动扣费。';
|
|
|
|
}
|
2022-11-19 06:04:42 +00:00
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
$left = $transaction->reduceHostAmount($this->user_id, $this->id, $this->module_id, $real_price, $description);
|
2022-11-19 06:04:42 +00:00
|
|
|
|
2022-11-19 14:42:47 +00:00
|
|
|
$this->addLog('balance', $real_price);
|
2022-11-19 06:04:42 +00:00
|
|
|
|
|
|
|
broadcast(new UserEvent($this->user_id, 'balances.amount.reduced', $this->user));
|
|
|
|
|
|
|
|
if ($left < 0) {
|
|
|
|
$this->update([
|
|
|
|
'status' => 'suspended',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-16 10:44:16 +00:00
|
|
|
}
|