From 2dfb3ca1c8bc0499d36b126910262b1d9edb26b5 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Fri, 16 Sep 2022 19:56:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=94=B6=E7=9B=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/CalcModule.php | 70 +++++++++++++++ app/Console/Kernel.php | 4 +- .../Controllers/Remote/ModuleController.php | 89 +++++++++++-------- 3 files changed, 124 insertions(+), 39 deletions(-) create mode 100644 app/Console/Commands/CalcModule.php diff --git a/app/Console/Commands/CalcModule.php b/app/Console/Commands/CalcModule.php new file mode 100644 index 0000000..66aa5e8 --- /dev/null +++ b/app/Console/Commands/CalcModule.php @@ -0,0 +1,70 @@ +warn('开始计算集成模块收益。'); + $this->warn('当前时间: ' . $now); + + foreach ($modules as $module) { + $report = $moduleController->calcModule($module); + + + $income = $report['transactions']['this_month']['drops'] / $report['balance']['rate']; + + if ($income < 0) { + $income = 0; + } + + // 取 2 位 + $income = round($income, 2); + + $text = $module->name . " 收益 {$income} 元 "; + $this->info($text); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index acb4154..12b1ac4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -3,6 +3,7 @@ namespace App\Console; use App\Console\Commands\BanUser; +use App\Console\Commands\CalcModule; use App\Console\Commands\SuspendUserAllHosts; use App\Console\Commands\UnbanUser; use App\Jobs\HostCost; @@ -23,7 +24,8 @@ class Kernel extends ConsoleKernel // BanUser::class, UnbanUser::class, - SuspendUserAllHosts::class + SuspendUserAllHosts::class, + CalcModule::class, ]; /** diff --git a/app/Http/Controllers/Remote/ModuleController.php b/app/Http/Controllers/Remote/ModuleController.php index 9a247d7..6736a3f 100644 --- a/app/Http/Controllers/Remote/ModuleController.php +++ b/app/Http/Controllers/Remote/ModuleController.php @@ -15,48 +15,15 @@ public function index() { $module = auth('remote')->user(); - $transactions = new Transaction(); - - // begin of this month - $beginOfMonth = now()->startOfMonth(); - - // end of this month - $endOfMonth = now()->endOfMonth(); - - $this_month_balance_and_drops = Cache::remember('this_month_balance_and_drops_' . $module->id, 3600, function () use ($transactions, $module, $beginOfMonth, $endOfMonth) { - $this_month = $transactions->where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]); - - // this month transactions - return [ - 'balance' => $this_month->sum('outcome'), - 'drops' => $this_month->sum('outcome_drops') - ]; - }); - - $last_month_balance_and_drops = Cache::remember('last_month_balance_and_drops_' . $module->id, 3600, function () use ($transactions, $module, $beginOfMonth, $endOfMonth) { - // last month transactions - $last_moth = $transactions->where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]); - - return [ - 'balance' => $last_moth->sum('outcome'), - 'drops' => $last_moth->sum('outcome_drops') - ]; - }); - - - $rate = (int)config('drops.rate') - 10; + $calc = $this->calcModule($module); $data = [ - 'module' => $module, - 'transactions' => [ - 'this_month' => $this_month_balance_and_drops, - 'last_month' => $last_month_balance_and_drops, - ], - 'balance' => [ - 'rate' => $rate, - ] + 'module' => $module ]; + // merge + $data = array_merge($data, $calc); + return $this->success($data); } @@ -102,4 +69,50 @@ public function call(Request $request, Module $module) return $this->remoteResponse($response['json'], $response['status']); } + + + public function calcModule(Module $module) + { + // begin of this month + $beginOfMonth = now()->startOfMonth(); + + // end of this month + $endOfMonth = now()->endOfMonth(); + + $this_month_balance_and_drops = Cache::remember('this_month_balance_and_drops_' . $module->id, 3600, function () use ($module, $beginOfMonth, $endOfMonth) { + $this_month = Transaction::where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]); + + // this month transactions + return [ + 'balance' => $this_month->sum('outcome'), + 'drops' => $this_month->sum('outcome_drops') + ]; + }); + + $last_month_balance_and_drops = Cache::remember('last_month_balance_and_drops_' . $module->id, 3600, function () use ($module, $beginOfMonth, $endOfMonth) { + // last month transactions + $last_moth = Transaction::where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]); + + return [ + 'balance' => $last_moth->sum('outcome'), + 'drops' => $last_moth->sum('outcome_drops') + ]; + }); + + + $rate = (int)config('drops.rate') - 10; + + $data = [ + 'transactions' => [ + 'this_month' => $this_month_balance_and_drops, + 'last_month' => $last_month_balance_and_drops, + ], + 'balance' => [ + 'rate' => $rate, + ] + ]; + + + return $data; + } }