From 4c6e64a60415191523385ef2c9d0648933f464bf Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 6 Jan 2026 06:56:50 -0800 Subject: [PATCH] =?UTF-8?q?fix(wallet-service):=20settleToBalance=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B9=90=E8=A7=82=E9=94=81=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/wallet-application.service.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/services/wallet-service/src/application/services/wallet-application.service.ts b/backend/services/wallet-service/src/application/services/wallet-application.service.ts index b70c6e32..8d68de25 100644 --- a/backend/services/wallet-service/src/application/services/wallet-application.service.ts +++ b/backend/services/wallet-service/src/application/services/wallet-application.service.ts @@ -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: {