From 99c9b1f88ef410fbdb0874b6997acc31ac3962d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Tue, 28 Dec 2021 12:02:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=AF=E6=8F=90=E5=92=8C?= =?UTF-8?q?=E4=BD=99=E9=A2=9D=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/BalanceService.php | 15 ++++++++++----- app/Services/WalletService.php | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/Services/BalanceService.php b/app/Services/BalanceService.php index 06c9427b..88ce7051 100644 --- a/app/Services/BalanceService.php +++ b/app/Services/BalanceService.php @@ -20,16 +20,21 @@ class BalanceService */ public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null) { + if ($changeBalance === 0) { + return; + } + $balance = $user->balance()->lockForUpdate()->first(); - if (is_null($balance)) { + if ($balance === null) { if ($changeBalance < 0) { throw new BalanceNotEnoughException(); - } else { - $user->balance()->create(); - - $balance = $user->balance()->lockForUpdate()->first(); } + + // 创建余额表 + $user->balance()->create(); + // 锁定余额表 + $balance = $user->balance()->lockForUpdate()->first(); } // 变更前余额 diff --git a/app/Services/WalletService.php b/app/Services/WalletService.php index 1810c4de..6ee7b090 100644 --- a/app/Services/WalletService.php +++ b/app/Services/WalletService.php @@ -20,16 +20,21 @@ class WalletService */ public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null) { + if ($changeBalance === 0) { + return; + } + $wallet = $user->wallet()->lockForUpdate()->first(); - if (is_null($wallet)) { + if ($wallet === null) { if ($changeBalance < 0) { throw new WalletNotEnoughException(); - } else { - $user->wallet()->create(); - - $wallet = $user->wallet()->lockForUpdate()->first(); } + + // 创建可提表 + $user->wallet()->create(); + // 锁定可提表 + $wallet = $user->wallet()->lockForUpdate()->first(); } // 变更前余额