Lae/app/Models/Host.php

460 lines
12 KiB
PHP
Raw Normal View History

2022-08-16 10:44:16 +00:00
<?php
namespace App\Models;
2023-02-12 18:22:12 +00:00
use App\Exceptions\User\BalanceNotEnoughException;
2023-01-13 14:13:46 +00:00
use App\Jobs\Host\HostJob;
use App\Jobs\Host\UpdateOrDeleteHostJob;
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',
2023-02-12 18:22:12 +00:00
'managed_price',
2022-08-16 10:44:16 +00:00
'configuration',
'status',
2023-02-12 18:22:12 +00:00
'billing_cycle',
'next_due_at',
2022-11-18 09:16:30 +00:00
'suspended_at',
2022-08-16 10:44:16 +00:00
];
protected $casts = [
2023-01-14 10:35:13 +00:00
'price' => 'decimal:2',
'managed_price' => 'decimal:2',
2023-02-13 09:05:07 +00:00
'last_paid' => 'decimal:2',
2023-02-12 18:22:12 +00:00
'configuration' => 'array',
'next_due_at' => 'datetime',
'suspended_at' => 'datetime',
2022-08-16 10:44:16 +00:00
];
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-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-20 14:35:53 +00:00
public function scopeActive($query)
{
return $query->whereIn('status', ['running', 'stopped']);
}
2023-02-12 18:22:12 +00:00
public function scopeDraft($query)
{
return $query->where('status', 'draft');
}
public function scopeExpiring($query)
{
return $query->where('status', 'running')->where('next_due_at', '<=', now()->addDays(7));
}
public function scopeSuspended($query)
{
return $query->where('status', 'suspended');
}
2022-11-20 14:35:53 +00:00
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
2023-02-12 18:46:32 +00:00
public function scopeExpiringDays($query, $days)
{
return $query->where('status', 'running')->where('next_due_at', '<=', now()->addDays($days));
}
2023-02-12 18:22:12 +00:00
public function isDraft(): bool
{
return $this->status === 'draft';
}
public function isRunning(): bool
{
return $this->status === 'running';
}
public function isStopped(): bool
{
return $this->status === 'stopped';
}
2023-02-13 07:26:33 +00:00
public function isUnavailable(): bool
{
return $this->status === 'unavailable';
}
2023-02-13 10:18:14 +00:00
public function renew($first = false): bool
2023-02-12 18:22:12 +00:00
{
2023-02-12 19:12:26 +00:00
if (! $this->isCycle()) {
2023-02-12 18:46:32 +00:00
return false;
}
2023-02-12 18:22:12 +00:00
2023-02-12 18:46:32 +00:00
$price = $this->getRenewPrice();
2023-02-13 09:07:55 +00:00
$description = ($first ? '新购' : '续费').' '.$this->name.',价格:'.$price.' 元。';
2023-02-12 18:46:32 +00:00
try {
$this->user->reduce($price, $description, true, [
2023-02-13 09:05:07 +00:00
'module_id' => $this->module_id,
2023-02-12 18:46:32 +00:00
'host_id' => $this->id,
2023-02-13 09:05:07 +00:00
'user_id' => $this->user_id,
]);
$this->module->charge($price, 'balance', '用户'.$description, [
2023-02-12 18:46:32 +00:00
'module_id' => $this->module_id,
2023-02-13 09:05:07 +00:00
'host_id' => $this->id,
2023-02-12 18:46:32 +00:00
]);
} catch (BalanceNotEnoughException) {
return false;
}
$this->addLog($price);
$this->next_due_at = $this->getNewDueDate();
2023-02-13 09:05:07 +00:00
$this->last_paid = $price;
2023-02-12 18:46:32 +00:00
2023-02-12 18:54:50 +00:00
if ($this->isSuspended()) {
$this->run();
}
2023-02-12 18:46:32 +00:00
$this->save();
return true;
}
public function isCycle(): bool
{
return $this->billing_cycle !== null;
2023-02-12 18:22:12 +00:00
}
public function getRenewPrice(): string
{
return match ($this->billing_cycle) {
'monthly' => $this->getPrice(),
'quarterly' => bcmul($this->getPrice(), 3),
'semi-annually' => bcmul($this->getPrice(), 6),
'annually' => bcmul($this->getPrice(), 12),
'biennially' => bcmul($this->getPrice(), 24),
'triennially' => bcmul($this->getPrice(), 36),
2023-02-12 19:05:34 +00:00
default => '0',
2023-02-12 18:22:12 +00:00
};
}
2023-02-12 18:46:32 +00:00
public function getPrice(): float
2023-02-12 18:22:12 +00:00
{
2023-02-12 18:46:32 +00:00
return $this->managed_price ?? $this->price;
}
public function addLog(string $amount = '0'): bool
{
if ($amount === '0') {
2023-02-12 18:37:10 +00:00
return false;
}
2023-02-12 18:46:32 +00:00
/** 统计收益开始 */
$current_month = now()->month;
$current_year = now()->year;
2023-02-12 18:22:12 +00:00
2023-02-12 19:12:26 +00:00
$cache_key = 'module_earning_'.$this->module_id;
2023-02-12 18:22:12 +00:00
2023-02-12 18:46:32 +00:00
// 应支付的提成
$commission = config('settings.billing.commission');
$should_amount = bcmul($amount, $commission, 4);
// 应得的余额
$should_balance = bcsub($amount, $should_amount, 4);
// 如果太小,则重置为 0.0001
if ($should_balance < 0.0001) {
$should_balance = 0.0001;
2023-02-12 18:22:12 +00:00
}
2023-02-12 18:46:32 +00:00
$earnings = Cache::get($cache_key, []);
2023-02-12 18:22:12 +00:00
2023-02-12 19:12:26 +00:00
if (! isset($earnings[$current_year])) {
2023-02-12 18:46:32 +00:00
$earnings[$current_year] = [];
}
2023-02-12 18:22:12 +00:00
2023-02-12 18:46:32 +00:00
if (isset($earnings[$current_year][$current_month])) {
$earnings[$current_year][$current_month]['balance'] = bcadd($earnings[$current_year][$current_month]['balance'], $amount, 4);
$earnings[$current_year][$current_month]['should_balance'] = bcadd($earnings[$current_year][$current_month]['should_balance'], $should_balance, 4);
} else {
$earnings[$current_year][$current_month] = [
'balance' => $amount,
// 应得(交了手续费)
'should_balance' => $should_balance,
];
}
// 删除 前 3 年的数据
if (count($earnings) > 3) {
$earnings = array_slice($earnings, -3, 3, true);
}
$this->module->charge($amount, 'balance', null);
// 保存 1 年
Cache::forever($cache_key, $earnings);
/** 统计收益结束 */
2023-02-12 18:22:12 +00:00
return true;
}
2023-02-12 18:46:32 +00:00
public function getNewDueDate(): string
2023-02-12 18:22:12 +00:00
{
2023-02-12 18:46:32 +00:00
$this->next_due_at = $this->next_due_at ?? now();
return match ($this->billing_cycle) {
'monthly' => $this->next_due_at->addMonth(),
'quarterly' => $this->next_due_at->addMonths(3),
'semi-annually' => $this->next_due_at->addMonths(6),
'annually' => $this->next_due_at->addYear(),
'biennially' => $this->next_due_at->addYears(2),
'triennially' => $this->next_due_at->addYears(3),
default => null,
};
2023-02-12 18:22:12 +00:00
}
2023-02-12 19:12:26 +00:00
public function isSuspended(): bool
{
return $this->status === 'suspended';
}
public function run(): bool
{
$this->update([
'status' => 'running',
]);
return true;
}
public function safeDelete(): bool
{
2023-02-13 09:05:07 +00:00
$is_user = auth()->guard('sanctum')->check() || auth()->guard('web')->check();
if ($this->isCycle() && $is_user) {
// 周期性的,每个月只能删除固定次数
$times = Cache::remember('host_delete_times:'.$this->user_id, 60 * 24 * 30, function () {
return 0;
});
if ($times >= config('settings.billing.cycle_delete_times_every_month')) {
return false;
}
Cache::increment('host_delete_times:'.$this->user_id);
// 根据 next_due_at 来计算退还的金额
if ($this->next_due_at === null) {
$this->next_due_at = now();
}
}
2023-02-13 10:40:16 +00:00
// 如果创建时间大于 1 小时
2023-02-12 19:12:26 +00:00
if (! $this->isCycle() && $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'));
2023-01-30 16:14:07 +00:00
return true;
}
2023-02-13 09:05:07 +00:00
public function cost(
string $amount = null, $auto = true, $description = null
): bool {
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;
2023-02-12 19:12:26 +00:00
if (! $amount) {
2022-11-19 14:42:47 +00:00
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) {
2023-02-12 18:22:12 +00:00
$real_price = $user_group->getCostPrice($real_price);
2023-01-16 20:36:43 +00:00
2023-02-12 19:12:26 +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;
// 本月每天的每小时的价格
2023-01-16 20:36:43 +00:00
// 使用 bcmath 函数,解决浮点数计算精度问题
2023-01-17 16:07:31 +00:00
$real_price = bcdiv($real_price, $days, 4);
$real_price = bcdiv($real_price, 24, 4);
2022-11-20 12:34:13 +00:00
}
if ($real_price == 0) {
2023-02-12 19:12:26 +00:00
echo '价格为 0不扣费'.PHP_EOL;
2023-01-30 16:14:07 +00:00
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
}
2023-01-16 20:36:43 +00:00
$real_price = bcdiv($real_price, 1, 4);
2022-11-19 06:04:42 +00:00
$month = now()->month;
2023-02-12 19:12:26 +00:00
$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
}
2023-01-16 20:36:43 +00:00
$hosts_balances[$this->id] = bcdiv($hosts_balances[$this->id], 1, 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
2023-02-12 19:12:26 +00:00
if (! $description) {
2023-02-10 18:18:34 +00:00
$description = '模块发起的扣费。';
}
2022-11-19 14:42:47 +00:00
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;
}
2023-01-16 20:36:43 +00:00
$data = [
'host_id' => $this->id,
'module_id' => $this->module_id,
];
$left = $user->reduce($real_price, $description, false, $data);
2022-11-19 06:04:42 +00:00
2023-01-17 17:09:15 +00:00
$this->addLog($real_price);
2022-11-19 06:04:42 +00:00
if ($left < 0) {
2023-02-13 09:05:07 +00:00
$this->changeStatus('suspended');
$this->last_paid = $real_price;
$this->save();
2022-11-19 06:04:42 +00:00
}
return true;
}
2022-11-20 12:34:13 +00:00
2023-02-13 09:05:07 +00:00
public function changeStatus(
string $status
): bool {
$user = auth()->guard('sanctum')->user() ?? auth()->guard('web')->user();
2022-11-20 12:34:13 +00:00
2023-02-13 09:05:07 +00:00
if ($user) {
2023-02-13 07:50:19 +00:00
if ($this->isPending() || $this->isOverdue() || $this->status === 'locked' || $this->status === 'unavailable') {
return false;
}
2023-02-13 09:05:07 +00:00
2023-02-13 09:12:11 +00:00
if (! $this->isCycle() && ! $user->hasBalance('0.5')) {
2023-02-13 09:05:07 +00:00
return false;
}
2023-02-13 07:50:19 +00:00
}
if ($status === 'running') {
return $this->run();
2023-02-13 09:12:11 +00:00
} elseif (($status === 'suspended' || $status === 'suspend') && ! $this->isCycle()) {
2023-02-13 07:50:19 +00:00
return $this->suspend();
} elseif ($status === 'stopped') {
return $this->stop();
}
return false;
}
public function isPending(): bool
{
return $this->status === 'pending';
}
public function isOverdue(): bool
{
return now()->gt($this->next_due_at);
2022-11-20 12:34:13 +00:00
}
2023-01-13 14:14:34 +00:00
2023-02-12 18:46:32 +00:00
public function suspend(): bool
2023-01-13 14:14:34 +00:00
{
2023-02-12 18:46:32 +00:00
$this->update([
'status' => 'suspended',
]);
2023-01-13 14:14:34 +00:00
return true;
}
2023-02-13 07:50:19 +00:00
public function stop(): bool
{
$this->update([
'status' => 'stopped',
]);
return true;
}
public function updateOrDelete(): bool
{
dispatch(new UpdateOrDeleteHostJob($this));
return true;
}
2022-08-16 10:44:16 +00:00
}