From 456e26748d97ee8f7d77bcba7e191fb86c7f922b Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 13 Feb 2023 02:46:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E5=A4=A9=E6=95=B0=E5=86=85=E5=88=B0=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Host.php | 200 ++++++++++++++++++++++++-------------------- 1 file changed, 107 insertions(+), 93 deletions(-) diff --git a/app/Models/Host.php b/app/Models/Host.php index 9e5374e..0a60e38 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -61,11 +61,6 @@ public function module(): BelongsToAlias // return $this->hasMany(WorkOrder::class); // } - public function getPrice(): float - { - return $this->managed_price ?? $this->price; - } - public function scopeActive($query) { return $query->whereIn('status', ['running', 'stopped']); @@ -95,6 +90,11 @@ public function scopeThisUser($query, $module = null) } } + public function scopeExpiringDays($query, $days) + { + return $query->where('status', 'running')->where('next_due_at', '<=', now()->addDays($days)); + } + public function isDraft(): bool { return $this->status === 'draft'; @@ -115,34 +115,6 @@ public function isSuspended(): bool return $this->status === 'suspended'; } - public function getNewDueDate(): string - { - $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, - }; - } - - 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), - default => 0, - }; - } - public function renew(): bool { if (!$this->isCycle()) { @@ -171,16 +143,104 @@ public function renew(): bool return true; } - public function isOverdue(): bool - { - return now()->gt($this->next_due_at); - } - public function isCycle(): bool { return $this->billing_cycle !== null; } + 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), + default => 0, + }; + } + + public function getPrice(): float + { + return $this->managed_price ?? $this->price; + } + + public function addLog(string $amount = '0'): bool + { + if ($amount === '0') { + return false; + } + + /** 统计收益开始 */ + $current_month = now()->month; + $current_year = now()->year; + + $cache_key = 'module_earning_' . $this->module_id; + + // 应支付的提成 + $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; + } + + $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'] = 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); + + /** 统计收益结束 */ + + return true; + } + + public function getNewDueDate(): string + { + $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, + }; + } + + public function isOverdue(): bool + { + return now()->gt($this->next_due_at); + } + public function safeDelete(): bool { // 如果创建时间大于大于 1 小时 @@ -294,65 +354,19 @@ public function cost(string $amount = null, $auto = true, $description = null): return true; } - public function addLog(string $amount = '0'): bool - { - if ($amount === '0') { - return false; - } - - /** 统计收益开始 */ - $current_month = now()->month; - $current_year = now()->year; - - $cache_key = 'module_earning_' . $this->module_id; - - // 应支付的提成 - $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; - } - - $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'] = 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); - - /** 统计收益结束 */ - - return true; - } - public function updateOrDelete(): bool { dispatch(new UpdateOrDeleteHostJob($this)); return true; } + + public function suspend(): bool + { + $this->update([ + 'status' => 'suspended', + ]); + + return true; + } }