Lae/app/Models/Host.php

408 lines
14 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-11-06 11:28:22 +00:00
use App\Models\WorkOrder\WorkOrder;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2022-11-20 14:35:53 +00:00
use Illuminate\Database\Eloquent\Collection;
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\Support\Facades\Cache;
2022-11-20 12:34:13 +00:00
/**
* App\Models\Host
*
2022-11-20 14:35:53 +00:00
* @property int $id
* @property string $name
* @property string $module_id
* @property int $user_id
* @property float $price
* @property float|null $managed_price
* @property mixed|null $configuration
* @property string $status
* @property int|null $hour
* @property \Illuminate\Support\Carbon|null $suspended_at
* @property string|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\Module $module
* @property-read \App\Models\User $user
* @property-read Collection|WorkOrder[] $workOrders
* @property-read int|null $work_orders_count
2022-11-20 12:34:13 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host active()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host all($columns = [])
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host avg($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host cache(array $tags = [])
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host cachedValue(array $arguments, string $cacheKey)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host count($columns = '*')
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host disableCache()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host disableModelCaching()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host exists()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host flushCache(array $tags = [])
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host
* getModelCacheCooldown(\Illuminate\Database\Eloquent\Model $instance)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host inRandomOrder($seed = '')
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host insert(array $values)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host isCachable()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host max($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host min($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host newModelQuery()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host newQuery()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host query()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host sum($column)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host thisUser($module = null)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host truncate()
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereConfiguration($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereCreatedAt($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereDeletedAt($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereHour($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereId($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereManagedPrice($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereModuleId($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereName($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host wherePrice($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereStatus($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereSuspendedAt($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereUpdatedAt($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host whereUserId($value)
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|Host withCacheCooldownSeconds(?int $seconds = null)
* @mixin \Eloquent
*/
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-19 04:38:26 +00:00
protected static function boot()
{
parent::boot();
2022-11-20 12:34:13 +00:00
static::creating(function ($model) {
$model->hour_at = now()->hour;
2022-11-23 02:44:24 +00:00
$model->minute_at = now()->minute_at;
2022-11-20 12:34:13 +00:00
if ($model->price !== null) {
2022-11-20 13:22:34 +00:00
$model->price = round($model->price, 2);
2022-11-20 12:34:13 +00:00
}
if ($model->managed_price !== null) {
$model->managed_price = round($model->managed_price, 2);
}
});
2022-11-19 04:38:26 +00:00
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;
}
}
2022-11-20 12:34:13 +00:00
if ($model->isDirty('price')) {
$model->price = round($model->price, 2);
}
if ($model->isDirty('managed_price') && $model->managed_price !== null) {
$model->managed_price = round($model->managed_price, 2);
}
2022-11-19 04:38:26 +00:00
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);
});
}
2022-11-20 14:35:53 +00:00
public function getUserHosts($user_id = null): array|Collection
{
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-06 11:28:22 +00:00
public function user(): BelongsToAlias
{
2022-08-16 10:44:16 +00:00
return $this->belongsTo(User::class);
}
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);
}
2022-11-20 13:10:46 +00:00
// public function workOrders(): HasManyAlias
// {
// return $this->hasMany(WorkOrder::class);
// }
2022-08-16 10:44:16 +00:00
2022-11-19 04:38:26 +00:00
public function getPrice() {
return $this->managed_price ?? $this->price;
}
2022-11-19 04:38:26 +00:00
2022-11-20 14:35:53 +00:00
public function scopeActive($query)
{
return $query->whereIn('status', ['running', 'stopped']);
}
public function scopeThisUser($query, $module = null)
{
if ($module) {
return $query->where('user_id', auth()->id())->where('module_id', $module);
} else {
return $query->where('user_id', auth()->id());
}
}
2022-08-19 15:27:57 +00:00
public function safeDelete(): bool
{
2022-11-22 11:31:41 +00:00
// 如果创建时间大于大于 1 小时
if ($this->created_at->diffInHours(now()) > 1) {
2022-11-23 02:39:35 +00:00
// 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。
if (now()->minute < $this->minute_at) {
$this->cost();
}
2022-11-22 11:31:41 +00:00
}
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 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;
}
}
2022-11-20 12:34:13 +00:00
if ($auto) {
// 获取本月天数
$days = now()->daysInMonth;
// 本月每天的每小时的价格
$real_price = $real_price / $days / 24;
}
if ($real_price == 0) {
2022-11-19 14:42:47 +00:00
return true;
}
2022-11-20 12:34:13 +00:00
$real_price = round($real_price ?? 0, 8);
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';
2022-11-23 04:04:56 +00:00
$hosts_balances = Cache::get($month_cache_key, []);
2022-11-19 06:04:42 +00:00
2022-11-19 14:42:47 +00:00
// 统计 Host 消耗的 Balance
2022-11-23 04:04:56 +00:00
if (isset($hosts_balances[$this->id])) {
$hosts_balances[$this->id] += $real_price;
2022-11-19 06:04:42 +00:00
} else {
2022-11-23 04:04:56 +00:00
$hosts_balances[$this->id] = $real_price;
2022-11-19 06:04:42 +00:00
}
2022-11-23 04:04:56 +00:00
$hosts_balances[$this->id] = round($hosts_balances[$this->id], 8);
Cache::put($month_cache_key, $hosts_balances, 604800);
2022-11-19 06:04:42 +00:00
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-20 12:34:13 +00:00
$this->addLog($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-11-20 12:34:13 +00:00
public function addLog(float|null $amount = 0): bool
{
if ($amount === 0 || $amount === null) {
return false;
}
/** 统计收益开始 */
$current_month = now()->month;
$current_year = now()->year;
$cache_key = 'module_earning_' . $this->module_id;
$commission = (float)config('billing.commission');
$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);
/** 统计收益结束 */
return true;
}
2022-08-16 10:44:16 +00:00
}