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: {