6
0
Fork 0

优化可提和余额变更

release
李静 2021-12-28 12:02:26 +08:00
parent f60c6f2ea0
commit 99c9b1f88e
2 changed files with 20 additions and 10 deletions

View File

@ -20,16 +20,21 @@ class BalanceService
*/ */
public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null) public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null)
{ {
if ($changeBalance === 0) {
return;
}
$balance = $user->balance()->lockForUpdate()->first(); $balance = $user->balance()->lockForUpdate()->first();
if (is_null($balance)) { if ($balance === null) {
if ($changeBalance < 0) { if ($changeBalance < 0) {
throw new BalanceNotEnoughException(); throw new BalanceNotEnoughException();
} else {
$user->balance()->create();
$balance = $user->balance()->lockForUpdate()->first();
} }
// 创建余额表
$user->balance()->create();
// 锁定余额表
$balance = $user->balance()->lockForUpdate()->first();
} }
// 变更前余额 // 变更前余额

View File

@ -20,16 +20,21 @@ class WalletService
*/ */
public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null) public function changeBalance(User $user, int $changeBalance, int $action, ?string $remarks = null, $loggable = null)
{ {
if ($changeBalance === 0) {
return;
}
$wallet = $user->wallet()->lockForUpdate()->first(); $wallet = $user->wallet()->lockForUpdate()->first();
if (is_null($wallet)) { if ($wallet === null) {
if ($changeBalance < 0) { if ($changeBalance < 0) {
throw new WalletNotEnoughException(); throw new WalletNotEnoughException();
} else {
$user->wallet()->create();
$wallet = $user->wallet()->lockForUpdate()->first();
} }
// 创建可提表
$user->wallet()->create();
// 锁定可提表
$wallet = $user->wallet()->lockForUpdate()->first();
} }
// 变更前余额 // 变更前余额