2022-08-16 10:44:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-01-10 12:45:07 +00:00
|
|
|
use App\Events\Users;
|
2022-12-28 13:19:40 +00:00
|
|
|
use App\Jobs\Module\HostJob;
|
2022-11-06 11:28:22 +00:00
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
2022-11-20 14:35:53 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
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-08-16 10:44:16 +00:00
|
|
|
class Host extends Model
|
|
|
|
{
|
2022-12-12 03:55:08 +00:00
|
|
|
use 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-11-30 12:27:54 +00:00
|
|
|
'price' => 'decimal:2',
|
|
|
|
'managed_price' => 'decimal:2',
|
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 07:49:22 +00:00
|
|
|
$model->minute_at = now()->minute;
|
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) {
|
2023-01-10 12:45:07 +00:00
|
|
|
broadcast(new Users($model->user_id, 'hosts.created', $model));
|
2022-11-19 04:38:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
static::updating(function ($model) {
|
|
|
|
if ($model->isDirty('status')) {
|
|
|
|
if ($model->status == 'suspended') {
|
|
|
|
$model->suspended_at = now();
|
|
|
|
} else {
|
|
|
|
$model->suspended_at = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 12:27:54 +00:00
|
|
|
// if ($model->isDirty('price')) {
|
|
|
|
// $model->price = round($model->price, 2);
|
|
|
|
// }
|
2022-11-20 12:34:13 +00:00
|
|
|
|
2022-11-30 12:27:54 +00:00
|
|
|
// if ($model->isDirty('managed_price') && $model->managed_price !== null) {
|
|
|
|
// $model->managed_price = round($model->managed_price, 2);
|
|
|
|
// }
|
2022-11-20 12:34:13 +00:00
|
|
|
|
2023-01-10 12:45:07 +00:00
|
|
|
broadcast(new Users($model->user_id, 'hosts.updating', $model));
|
2022-11-19 04:38:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// when Updated
|
|
|
|
static::updated(function ($model) {
|
2022-12-28 13:19:40 +00:00
|
|
|
dispatch(new HostJob($model, 'patch'));
|
2022-11-19 04:38:26 +00:00
|
|
|
|
|
|
|
Cache::forget('user_hosts_' . $model->user_id);
|
|
|
|
Cache::forget('user_tasks_' . $model->user_id);
|
|
|
|
|
2023-01-10 12:45:07 +00:00
|
|
|
broadcast(new Users($model->user_id, 'hosts.updated', $model));
|
2022-11-19 04:38:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// static::deleting(function ($model) {
|
2023-01-10 12:45:07 +00:00
|
|
|
// broadcast(new Users($model->user_id, 'hosts.deleting', $model));
|
2022-11-19 04:38:26 +00:00
|
|
|
// });
|
|
|
|
|
|
|
|
static::deleting(function ($model) {
|
|
|
|
Cache::forget('user_tasks_' . $model->user_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
static::deleted(function ($model) {
|
2023-01-10 12:45:07 +00:00
|
|
|
broadcast(new Users($model->user_id, 'hosts.deleted', $model));
|
2022-11-19 04:38:26 +00:00
|
|
|
Cache::forget('user_tasks_' . $model->user_id);
|
|
|
|
Cache::forget('user_hosts_' . $model->user_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:42:27 +00:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
2022-11-20 14:35:53 +00:00
|
|
|
public function getUserHosts($user_id = null): array|Collection
|
2022-09-19 13:24:14 +00:00
|
|
|
{
|
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-23 09:36:10 +00:00
|
|
|
{
|
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-23 09:36:10 +00:00
|
|
|
{
|
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
|
|
|
|
2022-11-23 07:49:22 +00:00
|
|
|
public function getPrice(): float
|
|
|
|
{
|
2022-11-23 03:58:21 +00:00
|
|
|
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
|
|
|
|
2022-11-19 02:11:56 +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
|
|
|
}
|
|
|
|
|
2022-12-28 13:19:40 +00:00
|
|
|
dispatch(new HostJob($this, 'delete'));
|
2022-11-19 02:11:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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');
|
2022-11-26 13:52:30 +00:00
|
|
|
$user = $this->user;
|
|
|
|
$user->load('user_group');
|
|
|
|
$user_group = $user->user_group;
|
|
|
|
|
|
|
|
if ($user_group) {
|
|
|
|
if ($user_group->exempt) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-11-19 14:42:47 +00:00
|
|
|
|
|
|
|
$real_price = $amount ?? $this->price;
|
|
|
|
|
|
|
|
if (!$amount) {
|
|
|
|
if ($this->managed_price) {
|
|
|
|
$real_price = $this->managed_price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:01:24 +00:00
|
|
|
$append_description = '';
|
2022-11-26 13:52:30 +00:00
|
|
|
if ($user_group) {
|
|
|
|
if ($user_group->discount !== 100 && $user_group->discount !== null) {
|
|
|
|
$real_price = $real_price * ($user_group->discount / 100);
|
2022-11-26 14:01:24 +00:00
|
|
|
$append_description = ' (折扣 ' . $user_group->discount . '%)';
|
2022-11-26 13:52:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-12-05 05:00:48 +00:00
|
|
|
// 如果太小,则重置为 0.0001
|
2022-11-24 00:13:38 +00:00
|
|
|
if ($real_price < 0.0001) {
|
2022-11-24 01:26:33 +00:00
|
|
|
$real_price = 0.0001;
|
2022-11-23 13:27:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 13:23:33 +00:00
|
|
|
$real_price = round($real_price ?? 0, 4);
|
2022-11-20 12:34:13 +00:00
|
|
|
|
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 13:21:56 +00:00
|
|
|
$hosts_balances[$this->id] = round($hosts_balances[$this->id], 4);
|
2022-11-23 04:04:56 +00:00
|
|
|
|
|
|
|
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-26 14:01:24 +00:00
|
|
|
if ($append_description) {
|
|
|
|
$description .= $append_description;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-01-10 12:45:07 +00:00
|
|
|
broadcast(new Users($this->user, 'balances.amount.reduced', $this->user));
|
2022-11-19 06:04:42 +00:00
|
|
|
|
|
|
|
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 年
|
2022-12-12 03:55:08 +00:00
|
|
|
Cache::forever($cache_key, $earnings);
|
2022-11-20 12:34:13 +00:00
|
|
|
|
|
|
|
/** 统计收益结束 */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-16 10:44:16 +00:00
|
|
|
}
|