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(); } // 变更前余额