From 92e740294bad5c16793377fcfcd440d934a477da Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Sat, 29 Oct 2022 11:44:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Kernel.php | 3 + .../Controllers/Remote/ModuleController.php | 31 ++------- app/Jobs/CalcModule.php | 64 +++++++++++++++++++ app/Models/Transaction.php | 36 ++++++++++- 4 files changed, 108 insertions(+), 26 deletions(-) create mode 100644 app/Jobs/CalcModule.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index bfba2ab..a6bfba3 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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(); } } diff --git a/app/Http/Controllers/Remote/ModuleController.php b/app/Http/Controllers/Remote/ModuleController.php index 265159f..de414ae 100644 --- a/app/Http/Controllers/Remote/ModuleController.php +++ b/app/Http/Controllers/Remote/ModuleController.php @@ -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), ] ]; diff --git a/app/Jobs/CalcModule.php b/app/Jobs/CalcModule.php new file mode 100644 index 0000000..55d575d --- /dev/null +++ b/app/Jobs/CalcModule.php @@ -0,0 +1,64 @@ +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; + } +} diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 8c9d96f..2d3d14d 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -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 = '扣除费用请求。') {