添加 模块收益

This commit is contained in:
iVampireSP.com 2022-09-16 19:56:58 +08:00
parent baf08ebd22
commit 2dfb3ca1c8
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 124 additions and 39 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace App\Console\Commands;
use App\Http\Controllers\Remote\ModuleController;
use App\Models\Module\Module;
use Illuminate\Console\Command;
class CalcModule extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'modules:calc';
/**
* The console command description.
*
* @var string
*/
protected $description = '计算模块的本月收益。';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
$moduleController = new ModuleController();
$modules = Module::get();
// 当前时间
$now = now();
$this->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);
}
}
}

View File

@ -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,
];
/**

View File

@ -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;
}
}