diff --git a/backend/services/wallet-service/src/api/controllers/internal-wallet.controller.ts b/backend/services/wallet-service/src/api/controllers/internal-wallet.controller.ts index ac980698..e248e7e7 100644 --- a/backend/services/wallet-service/src/api/controllers/internal-wallet.controller.ts +++ b/backend/services/wallet-service/src/api/controllers/internal-wallet.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Param } from '@nestjs/common'; +import { Controller, Get, Post, Body, Param, Logger } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger'; import { WalletApplicationService } from '@/application/services'; import { GetMyWalletQuery } from '@/application/queries'; @@ -19,6 +19,8 @@ import { Public } from '@/shared/decorators'; @ApiTags('Internal Wallet API') @Controller('wallets') export class InternalWalletController { + private readonly logger = new Logger(InternalWalletController.name); + constructor(private readonly walletService: WalletApplicationService) {} @Get(':userId/balance') @@ -60,13 +62,26 @@ export class InternalWalletController { async freezeForPlanting( @Body() dto: { userId: string; amount: number; orderId: string }, ) { - const command = new FreezeForPlantingCommand( - dto.userId, - dto.amount, - dto.orderId, - ); - const result = await this.walletService.freezeForPlanting(command); - return result; + this.logger.log(`========== freeze-for-planting 请求 ==========`); + this.logger.log(`请求参数: ${JSON.stringify(dto)}`); + this.logger.log(` userId: ${dto.userId}`); + this.logger.log(` amount: ${dto.amount}`); + this.logger.log(` orderId: ${dto.orderId}`); + + try { + const command = new FreezeForPlantingCommand( + dto.userId, + dto.amount, + dto.orderId, + ); + const result = await this.walletService.freezeForPlanting(command); + this.logger.log(`冻结成功: ${JSON.stringify(result)}`); + return result; + } catch (error) { + this.logger.error(`冻结失败: ${error.message}`); + this.logger.error(`错误堆栈: ${error.stack}`); + throw error; + } } @Post('confirm-planting-deduction') 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 2b5b9141..10400971 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 @@ -187,11 +187,18 @@ export class WalletApplicationService { success: boolean; frozenAmount: number; }> { + this.logger.log(`[freezeForPlanting] ========== 开始处理 ==========`); + this.logger.log(`[freezeForPlanting] userId: ${command.userId}`); + this.logger.log(`[freezeForPlanting] amount: ${command.amount}`); + this.logger.log(`[freezeForPlanting] orderId: ${command.orderId}`); + const userId = BigInt(command.userId); const amount = Money.USDT(command.amount); + this.logger.log(`[freezeForPlanting] 解析后 amount.value: ${amount.value}`); // 幂等性检查:通过 orderId 检查是否已经冻结 const existingEntries = await this.ledgerRepo.findByRefOrderId(command.orderId); + this.logger.log(`[freezeForPlanting] 已存在的流水条数: ${existingEntries.length}`); const alreadyFrozen = existingEntries.some( (entry) => entry.entryType === LedgerEntryType.PLANT_FREEZE, ); @@ -204,19 +211,32 @@ export class WalletApplicationService { const wallet = await this.walletRepo.findByUserId(userId); if (!wallet) { + this.logger.error(`[freezeForPlanting] 钱包不存在: userId=${command.userId}`); throw new WalletNotFoundError(`userId: ${command.userId}`); } + this.logger.log(`[freezeForPlanting] 钱包信息:`); + this.logger.log(`[freezeForPlanting] walletId: ${wallet.walletId}`); + this.logger.log(`[freezeForPlanting] accountSequence: ${wallet.accountSequence}`); + this.logger.log(`[freezeForPlanting] USDT可用余额: ${wallet.balances.usdt.available.value}`); + this.logger.log(`[freezeForPlanting] USDT冻结余额: ${wallet.balances.usdt.frozen.value}`); + // 检查余额是否足够 if (wallet.balances.usdt.available.lessThan(amount)) { + this.logger.error(`[freezeForPlanting] 余额不足!`); + this.logger.error(`[freezeForPlanting] 需要: ${command.amount} USDT`); + this.logger.error(`[freezeForPlanting] 当前可用: ${wallet.balances.usdt.available.value} USDT`); throw new BadRequestException( `余额不足: 需要 ${command.amount} USDT, 当前可用 ${wallet.balances.usdt.available.value} USDT`, ); } + this.logger.log(`[freezeForPlanting] 余额检查通过,开始冻结...`); + // 冻结资金 wallet.freeze(amount); await this.walletRepo.save(wallet); + this.logger.log(`[freezeForPlanting] 钱包已保存`); // 记录冻结流水 const ledgerEntry = LedgerEntry.create({ @@ -229,10 +249,11 @@ export class WalletApplicationService { memo: 'Plant freeze', }); await this.ledgerRepo.save(ledgerEntry); + this.logger.log(`[freezeForPlanting] 流水已记录`); await this.walletCacheService.invalidateWallet(userId); - this.logger.log(`Frozen ${command.amount} USDT for order ${command.orderId}`); + this.logger.log(`[freezeForPlanting] 成功冻结 ${command.amount} USDT for order ${command.orderId}`); return { success: true, frozenAmount: command.amount }; }