改进 模块统计

This commit is contained in:
iVampireSP.com 2022-10-29 11:44:52 +08:00
parent e2d0271519
commit 92e740294b
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 108 additions and 26 deletions

View File

@ -11,6 +11,7 @@
use App\Console\Commands\UnbanUser;
use App\Console\Commands\UserAddBalance;
use App\Jobs\AutoCloseWorkOrder;
use App\Jobs\CalcModule as JobsCalcModule;
use App\Jobs\CheckAndChargeBalance;
use App\Jobs\HostCost;
use App\Jobs\ClearTasks;
@ -64,5 +65,7 @@ protected function schedule(Schedule $schedule)
$schedule->job(new CheckAndChargeBalance())->everyThirtyMinutes();
$schedule->job(new AutoCloseWorkOrder())->everyFiveMinutes();
$schedule->job(new JobsCalcModule())->everyFiveMinutes();
}
}

View File

@ -93,36 +93,17 @@ public function exportCall(Request $request, Module $module)
public function calcModule(Module $module)
{
// begin of this month
$beginOfMonth = now()->startOfMonth();
// end of this month
$endOfMonth = now()->endOfMonth();
$default = [
'balance' => 0,
'drops' => 0,
];
$this_month_balance_and_drops = Cache::remember($module->id . '_this_month_balance_and_drops', 3600, function () use ($module, $beginOfMonth, $endOfMonth) {
$this_month = Transaction::where('module_id', $module->id)->where('type', 'payout')->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($module->id . '_last_month_balance_and_drops', 3600, function () use ($module, $beginOfMonth, $endOfMonth) {
// last month transactions
$last_moth = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
return [
'balance' => $last_moth->sum('outcome'),
'drops' => $last_moth->sum('outcome_drops')
];
});
$data = [
'transactions' => [
'this_month' => $this_month_balance_and_drops,
'last_month' => $last_month_balance_and_drops,
'this_month' => Cache::get('this_month_balance_and_drops_' . $module->id, $default),
'last_month' => Cache::get('last_month_balance_and_drops_' . $module->id, $default),
]
];

64
app/Jobs/CalcModule.php Normal file
View File

@ -0,0 +1,64 @@
<?php
namespace App\Jobs;
use App\Models\Transaction;
use App\Models\Module\Module;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Remote\ModuleController;
class CalcModule extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// begin of this month
$beginOfMonth = now()->startOfMonth();
// end of this month
$endOfMonth = now()->endOfMonth();
$moduleController = new ModuleController();
Module::chunk(100, function ($modules) use ($moduleController, $beginOfMonth, $endOfMonth) {
foreach ($modules as $module) {
$this_month = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
// this month transactions
$this_month = [
'balance' => $this_month->sum('outcome'),
'drops' => $this_month->sum('outcome_drops')
];
Cache::put('this_month_balance_and_drops_' . $module->id, $this_month, 60 * 24 * 30);
// last month transactions
$last_moth = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
$last_moth = [
'balance' => $last_moth->sum('outcome'),
'drops' => $last_moth->sum('outcome_drops')
];
Cache::put('last_month_balance_and_drops_' . $module->id, $last_moth, 60 * 24 * 30);
}
});
return 0;
}
}

View File

@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Jenssegers\Mongodb\Eloquent\Model;
use App\Exceptions\User\BalanceNotEnoughException;
use Illuminate\Contracts\Cache\LockTimeoutException;
class Transaction extends Model
@ -193,7 +194,7 @@ public function addIncomeBalance($user_id, $payment, $amount, $description)
return $this->addLog($user_id, $data);
}
public function addPayoutBalance($user_id, $amount, $description)
public function addPayoutBalance($user_id, $amount, $description, $module_id = null)
{
$data = [
'type' => 'payout',
@ -205,6 +206,10 @@ public function addPayoutBalance($user_id, $amount, $description)
'outcome_drops' => 0
];
if ($module_id) {
$data['module_id'] = $module_id;
}
return $this->addLog($user_id, $data);
}
@ -250,6 +255,35 @@ public function reduceAmount($user_id, $amount = 0, $description = '扣除费用
return false;
}
public function reduceAmountModuleFail($user_id, $module_id, $amount = 0, $description = '扣除费用请求。')
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
try {
$lock->block(5);
$user = User::findOrFail($user_id);
$user->balance -= $amount;
// if balance < 0
if ($user->balance < 0) {
throw new BalanceNotEnoughException('余额不足。');
}
$user->save();
$this->addPayoutBalance($user_id, $amount, $description, $module_id);
return $user->balance;
} finally {
optional($lock)->release();
}
return false;
}
public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $description = '扣除费用请求。')
{