From a1d2767d4b950888af0f9b276522800f8bc08b2a Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 16 Nov 2022 20:35:09 +0800 Subject: [PATCH] optimize --- app/Console/Commands/UserAddDrops.php | 6 +- .../Modules/WorkOrderController.php | 1 - app/Models/Transaction.php | 138 +++++++++--------- resources/views/balances/index.blade.php | 2 +- routes/web.php | 1 - 5 files changed, 73 insertions(+), 75 deletions(-) diff --git a/app/Console/Commands/UserAddDrops.php b/app/Console/Commands/UserAddDrops.php index 49f8d9e..2a3e13b 100644 --- a/app/Console/Commands/UserAddDrops.php +++ b/app/Console/Commands/UserAddDrops.php @@ -28,7 +28,7 @@ class UserAddDrops extends Command * * @return int */ - public function handle() + public function handle(): int { $user_id = $this->argument('user_id'); @@ -40,7 +40,7 @@ public function handle() $current_drops = $transaction->getDrops($user->id); - $this->info($user->name . ', 当前 ' . $current_drops. ' Drops'); + $this->info($user->name . ', 当前 ' . $current_drops . ' Drops'); $this->info($user->name . ', 当前余额: ' . $user->balance . ' 元'); @@ -48,7 +48,7 @@ public function handle() if (!$this->confirm('确认添加 ' . $amount . ' Drops?')) { $this->info('已取消。'); - return; + return 0; } $transaction->increaseDrops($user->id, $amount, '管理员添加 Drops', 'console'); diff --git a/app/Http/Controllers/Modules/WorkOrderController.php b/app/Http/Controllers/Modules/WorkOrderController.php index 3840f18..ce23088 100644 --- a/app/Http/Controllers/Modules/WorkOrderController.php +++ b/app/Http/Controllers/Modules/WorkOrderController.php @@ -6,7 +6,6 @@ use App\Http\Requests\Remote\WorkOrderRequest; use App\Models\WorkOrder\WorkOrder; use Illuminate\Http\JsonResponse; -use Illuminate\Http\Request; class WorkOrderController extends Controller { diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index d96f058..dd0da7c 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -91,71 +91,18 @@ public function increaseDrops($user_id, $amount, $description = null, $payment = return $current_drops['drops']; } - public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = 0) - { - - $cache_key = 'user_drops_' . $user_id; - - $current_drops = Cache::get($cache_key, [ - 'drops' => 0, - ]); - - $current_drops['drops'] = $current_drops['drops'] - $amount; - - $current_drops['drops'] = round($current_drops['drops'], 5); - - Cache::forever($cache_key, $current_drops); - - if ($auto) { - $description = '平台按时间自动扣费。'; - } else { - $description = '集成模块发起的扣费。'; - } - - // $this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id); - } - - public function reduceDropsWithoutHost($user_id, $amount = 0, $description = null) - { - - $cache_key = 'user_drops_' . $user_id; - - $current_drops = Cache::get($cache_key, [ - 'drops' => 0, - ]); - - $current_drops['drops'] = $current_drops['drops'] - $amount; - - $current_drops['drops'] = round($current_drops['drops'], 5); - - Cache::forever($cache_key, $current_drops); - - $this->addPayoutDrops($user_id, $amount, $description, null, null); - } - - public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id) + public function addIncomeDrops($user_id, $amount, $description, $payment = 'balance') { $data = [ - 'type' => 'payout', - 'payment' => 'drops', + 'type' => 'income', + 'payment' => $payment, 'description' => $description, 'income' => 0, - 'income_drops' => 0, + 'income_drops' => (float)$amount, 'outcome' => 0, - 'outcome_drops' => (float)$amount, - 'host_id' => $host_id, - 'module_id' => $module_id, + 'outcome_drops' => 0, ]; - - // $amount = (double) $amount; - - // Log::debug($amount); - - // $month = now()->month; - - // Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount); - return $this->addLog($user_id, $data); } @@ -198,19 +145,28 @@ public function getDrops($user_id = null): float return $drops['drops']; } - public function addIncomeDrops($user_id, $amount, $description, $payment = 'balance') + public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = 0) { - $data = [ - 'type' => 'income', - 'payment' => $payment, - 'description' => $description, - 'income' => 0, - 'income_drops' => (float)$amount, - 'outcome' => 0, - 'outcome_drops' => 0, - ]; - return $this->addLog($user_id, $data); + $cache_key = 'user_drops_' . $user_id; + + $current_drops = Cache::get($cache_key, [ + 'drops' => 0, + ]); + + $current_drops['drops'] = $current_drops['drops'] - $amount; + + $current_drops['drops'] = round($current_drops['drops'], 5); + + Cache::forever($cache_key, $current_drops); + + if ($auto) { + $description = '平台按时间自动扣费。'; + } else { + $description = '集成模块发起的扣费。'; + } + + // $this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id); } public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。') @@ -420,4 +376,48 @@ public function transferDrops(User $user, User $to, float $amount, string|null $ } + + public function reduceDropsWithoutHost($user_id, $amount = 0, $description = null) + { + + $cache_key = 'user_drops_' . $user_id; + + $current_drops = Cache::get($cache_key, [ + 'drops' => 0, + ]); + + $current_drops['drops'] = $current_drops['drops'] - $amount; + + $current_drops['drops'] = round($current_drops['drops'], 5); + + Cache::forever($cache_key, $current_drops); + + $this->addPayoutDrops($user_id, $amount, $description, null, null); + } + + public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id) + { + $data = [ + 'type' => 'payout', + 'payment' => 'drops', + 'description' => $description, + 'income' => 0, + 'income_drops' => 0, + 'outcome' => 0, + 'outcome_drops' => (float)$amount, + 'host_id' => $host_id, + 'module_id' => $module_id, + ]; + + + // $amount = (double) $amount; + + // Log::debug($amount); + + // $month = now()->month; + + // Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount); + + return $this->addLog($user_id, $data); + } } diff --git a/resources/views/balances/index.blade.php b/resources/views/balances/index.blade.php index fed1534..09edd8b 100644 --- a/resources/views/balances/index.blade.php +++ b/resources/views/balances/index.blade.php @@ -13,7 +13,7 @@
@csrf - 元 +
Drops diff --git a/routes/web.php b/routes/web.php index 1158987..edaba6a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -13,7 +13,6 @@ Route::delete('deleteAll', [AuthController::class, 'deleteAll'])->name('deleteAll'); - Route::get('transactions', [BalanceController::class, 'transactions'])->name('transactions'); Route::resource('balances', BalanceController::class);