钱包余额增加悲观锁

This commit is contained in:
cano
2024-03-29 11:10:00 +08:00
parent 4b47fe7250
commit 81a95f47a0
3 changed files with 63 additions and 2 deletions

View File

@ -106,6 +106,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
@ -144,7 +146,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resBalanceModel->total_amount, $bean->getAmount()));
@ -182,6 +185,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
@ -220,6 +225,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
@ -275,6 +282,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
$bean->setBalanceId($resBalanceModel->id);
$bean->setBeforeTotalAmount($resBalanceModel->total_amount);
$bean->setAfterTotalAmount(Math::bcAdd($resBalanceModel->total_amount, $bean->getAmount()));
@ -315,6 +324,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findPlatformBalanceOrCreate($platform_id, $currency_code);
if (!$resBalanceModel) throw new ModelException('findPlatformBalanceOrCreate error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
//检查余额是否足够
if (Math::bcComp(Math::bcSub($resBalanceModel->available_amount, $withdraw_fee), abs($bean->getAmount())) == -1) throw new ModelException('available_amount not enough');
$bean->setBalanceId($resBalanceModel->id);
@ -372,6 +383,8 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel
$oWalletPlatformBalanceModel = new WalletPlatformBalanceModel();
$resBalanceModel = $oWalletPlatformBalanceModel->findItem($resModel->balance_id);
if (!$resBalanceModel) throw new ModelException('WalletPlatformBalanceModel findItem error');
//加悲观锁
$resBalanceModel = $oWalletPlatformBalanceModel->findByWhereWithLock($resBalanceModel->id);
if ($status == self::STATUS_SUCCESS) {
//变更余额