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;
|
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;
|
2022-11-19 05:08:39 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-06 11:28:22 +00:00
|
|
|
|
|
|
|
// 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-18 11:39:30 +00:00
|
|
|
return $query->whereIn('status', ['running', 'stopped'])->where('price', '!=', 0)->where('managed_price', '!=', 0);
|
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-06 11:28:22 +00:00
|
|
|
public function cost($price = null, $auto = true): bool
|
2022-08-23 09:36:10 +00:00
|
|
|
{
|
2022-08-30 09:20:45 +00:00
|
|
|
$this->load('user');
|
2022-08-23 09:36:10 +00:00
|
|
|
|
2022-09-15 04:13:37 +00:00
|
|
|
$transaction = new Transaction();
|
|
|
|
|
|
|
|
$drops = $transaction->getDrops($this->user_id);
|
2022-08-23 09:36:10 +00:00
|
|
|
|
2022-10-23 05:00:41 +00:00
|
|
|
if ($price !== null) {
|
|
|
|
$real_price = $price;
|
|
|
|
} else {
|
2022-11-18 11:39:30 +00:00
|
|
|
if ($this->managed_price === null) {
|
|
|
|
$real_price = $this->price;
|
|
|
|
} else {
|
|
|
|
$real_price = $this->managed_price;
|
|
|
|
|
|
|
|
}
|
2022-08-23 09:36:10 +00:00
|
|
|
}
|
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-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;
|
|
|
|
|
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-11-19 04:50:48 +00:00
|
|
|
$this->addLog('drops', $real_price);
|
2022-11-19 04:37:18 +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',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-08-23 09:36:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-16 05:16:56 +00:00
|
|
|
public function costBalance($amount = 1): bool
|
2022-10-07 05:15:08 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2022-11-19 04:50:48 +00:00
|
|
|
$this->addLog('balance', $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-11-19 04:50:48 +00:00
|
|
|
|
|
|
|
|
2022-11-19 05:08:39 +00:00
|
|
|
public function addLog($type = 'drops', float $amount = 0)
|
2022-11-19 04:50:48 +00:00
|
|
|
{
|
2022-11-19 05:08:39 +00:00
|
|
|
if ($amount == 0) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
$rate = (int)config('drops.rate');
|
|
|
|
$commission = (float)config('drops.commission');
|
|
|
|
|
|
|
|
|
|
|
|
if ($type == 'drops') {
|
|
|
|
// 换成 余额
|
|
|
|
|
2022-11-19 05:08:39 +00:00
|
|
|
// 如果小于 0.00,则不四舍五入
|
2022-11-19 05:11:22 +00:00
|
|
|
// $c = $amount / $rate;
|
|
|
|
//
|
|
|
|
// if ($c > 0.01) {
|
|
|
|
// $amount = $amount / $rate;
|
|
|
|
// }
|
2022-11-19 05:08:39 +00:00
|
|
|
|
2022-11-19 05:11:22 +00:00
|
|
|
$amount = $amount / $rate;
|
2022-11-19 04:50:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 05:08:39 +00:00
|
|
|
$amount = round($amount, 2);
|
|
|
|
|
|
|
|
|
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 ($type == 'drops') {
|
|
|
|
$drops = $amount;
|
|
|
|
} else {
|
|
|
|
$drops = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($earnings[$current_year][$current_month])) {
|
|
|
|
$earnings[$current_year][$current_month]['balance'] += $amount;
|
|
|
|
$earnings[$current_year][$current_month]['should_balance'] += $should_balance;
|
|
|
|
$earnings[$current_year][$current_month]['drops'] += $drops;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$earnings[$current_year][$current_month] = [
|
|
|
|
'balance' => $amount,
|
|
|
|
// 应得(交了手续费)
|
|
|
|
'should_balance' => $should_balance,
|
|
|
|
'drops' => $drops
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 删除 前 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-08-16 10:44:16 +00:00
|
|
|
}
|