fix(wallet-service): settleToBalance 添加乐观锁防止并发冲突
This commit is contained in:
parent
65cb574f59
commit
4c6e64a604
|
|
@ -949,17 +949,26 @@ export class WalletApplicationService {
|
|||
const newSettledTotal = currentSettledTotal.plus(usdtAmountDecimal);
|
||||
balanceAfter = newAvailable.toNumber();
|
||||
|
||||
// 5. 更新钱包账户(在事务内)
|
||||
await tx.walletAccount.update({
|
||||
where: { accountSequence: params.accountSequence },
|
||||
// 5. 更新钱包账户(在事务内,使用乐观锁)
|
||||
const currentVersion = walletRecord.version;
|
||||
const updateResult = await tx.walletAccount.updateMany({
|
||||
where: {
|
||||
accountSequence: params.accountSequence,
|
||||
version: currentVersion, // 乐观锁:只有版本匹配才更新
|
||||
},
|
||||
data: {
|
||||
settleableUsdt: newSettleable.toFixed(8),
|
||||
usdtAvailable: newAvailable.toFixed(8),
|
||||
settledTotalUsdt: newSettledTotal.toFixed(8),
|
||||
version: currentVersion + 1,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
if (updateResult.count === 0) {
|
||||
throw new Error(`Optimistic lock conflict for wallet ${params.accountSequence}`);
|
||||
}
|
||||
|
||||
// 6. 创建流水记录(在事务内)
|
||||
await tx.ledgerEntry.create({
|
||||
data: {
|
||||
|
|
|
|||
Loading…
Reference in New Issue