From 217be89c4364fdcc636b6c0cb1471c346c7e7c7b Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 8 Jan 2026 11:16:04 -0800 Subject: [PATCH] =?UTF-8?q?fix(wallet-service):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=B5=81=E6=B0=B4=E6=9F=A5=E8=AF=A2=E4=BD=BF=E7=94=A8=20userId?= =?UTF-8?q?=20=E5=AF=BC=E8=87=B4=E8=AE=B0=E5=BD=95=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将所有流水查询从 userId 改为使用 accountSequence: - getMyLedger: 使用 findByAccountSequence 替代 findByUserId - getLedgerStatistics: 查询改为按 accountSequence - getLedgerTrend: 查询改为按 accountSequence - findByAccountSequence: 添加 HIDDEN_ENTRY_TYPES 过滤 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../src/api/controllers/ledger.controller.ts | 6 ++--- .../queries/get-my-ledger.query.ts | 2 +- .../wallet-application.service.spec.ts | 9 +++++--- .../services/wallet-application.service.ts | 23 ++++++++----------- .../wallet-account.aggregate.spec.ts | 2 +- .../ledger-entry.repository.impl.ts | 7 +++++- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/backend/services/wallet-service/src/api/controllers/ledger.controller.ts b/backend/services/wallet-service/src/api/controllers/ledger.controller.ts index 659657a9..79105899 100644 --- a/backend/services/wallet-service/src/api/controllers/ledger.controller.ts +++ b/backend/services/wallet-service/src/api/controllers/ledger.controller.ts @@ -22,7 +22,7 @@ export class LedgerController { @Query() queryDto: GetMyLedgerQueryDTO, ): Promise { const query = new GetMyLedgerQuery( - user.userId, + user.accountSequence, // 使用 accountSequence 替代 userId queryDto.page, queryDto.pageSize, queryDto.entryType, @@ -39,7 +39,7 @@ export class LedgerController { async getStatistics( @CurrentUser() user: CurrentUserPayload, ): Promise { - return this.walletService.getLedgerStatistics(user.userId); + return this.walletService.getLedgerStatistics(user.accountSequence); } @Get('trend') @@ -51,6 +51,6 @@ export class LedgerController { @Query('days') days?: string, ): Promise { const numDays = days ? parseInt(days, 10) : 30; - return this.walletService.getLedgerTrend(user.userId, numDays); + return this.walletService.getLedgerTrend(user.accountSequence, numDays); } } diff --git a/backend/services/wallet-service/src/application/queries/get-my-ledger.query.ts b/backend/services/wallet-service/src/application/queries/get-my-ledger.query.ts index 00e7d2a9..71fe68d6 100644 --- a/backend/services/wallet-service/src/application/queries/get-my-ledger.query.ts +++ b/backend/services/wallet-service/src/application/queries/get-my-ledger.query.ts @@ -2,7 +2,7 @@ import { LedgerEntryType, AssetType } from '@/domain/value-objects'; export class GetMyLedgerQuery { constructor( - public readonly userId: string, + public readonly accountSequence: string, // 使用 accountSequence 替代 userId public readonly page?: number, public readonly pageSize?: number, public readonly entryType?: LedgerEntryType, diff --git a/backend/services/wallet-service/src/application/services/wallet-application.service.spec.ts b/backend/services/wallet-service/src/application/services/wallet-application.service.spec.ts index bb4afb59..ade5114e 100644 --- a/backend/services/wallet-service/src/application/services/wallet-application.service.spec.ts +++ b/backend/services/wallet-service/src/application/services/wallet-application.service.spec.ts @@ -28,7 +28,7 @@ describe('WalletApplicationService', () => { const createMockWallet = (userId: bigint, usdtBalance = 0) => { return WalletAccount.reconstruct({ walletId: BigInt(1), - accountSequence: userId, // 使用 userId 作为 accountSequence + accountSequence: `D${userId.toString().padStart(11, '0')}`, // 生成 accountSequence userId, usdtAvailable: new Decimal(usdtBalance), usdtFrozen: new Decimal(0), @@ -51,6 +51,8 @@ describe('WalletApplicationService', () => { expiredTotalUsdt: new Decimal(0), expiredTotalHashpower: new Decimal(0), status: 'ACTIVE', + hasPlanted: false, + version: 0, createdAt: new Date(), updatedAt: new Date(), }); @@ -69,6 +71,7 @@ describe('WalletApplicationService', () => { save: jest.fn(), saveAll: jest.fn(), findByUserId: jest.fn(), + findByAccountSequence: jest.fn(), findByRefOrderId: jest.fn(), findByRefTxHash: jest.fn(), }; @@ -187,9 +190,9 @@ describe('WalletApplicationService', () => { describe('getMyLedger', () => { it('should return paginated ledger entries', async () => { - const query = new GetMyLedgerQuery('1', 1, 10); + const query = new GetMyLedgerQuery('D00000000001', 1, 10); - mockLedgerRepo.findByUserId.mockResolvedValue({ + mockLedgerRepo.findByAccountSequence.mockResolvedValue({ data: [], total: 0, page: 1, 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 2fd50f81..058da79f 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 @@ -1916,10 +1916,9 @@ export class WalletApplicationService { } async getMyLedger(query: GetMyLedgerQuery): Promise { - const userId = BigInt(query.userId); - - const result = await this.ledgerRepo.findByUserId( - userId, + // 使用 accountSequence 查询流水(废弃 userId 查询) + const result = await this.ledgerRepo.findByAccountSequence( + query.accountSequence, { entryType: query.entryType, assetType: query.assetType, @@ -1934,7 +1933,7 @@ export class WalletApplicationService { // 调试日志:打印流水数据(只打印前5条) this.logger.debug(`[getMyLedger] ======== 流水数据调试 ========`); - this.logger.debug(`[getMyLedger] userId: ${userId}, 共 ${result.data.length} 条, 总计 ${result.total} 条`); + this.logger.debug(`[getMyLedger] accountSequence: ${query.accountSequence}, 共 ${result.data.length} 条, 总计 ${result.total} 条`); for (let i = 0; i < result.data.length && i < 5; i++) { const entry = result.data[i]; const allocationType = (entry.payloadJson as Record)?.allocationType; @@ -2458,7 +2457,7 @@ export class WalletApplicationService { /** * 获取流水统计信息 */ - async getLedgerStatistics(userId: string): Promise<{ + async getLedgerStatistics(accountSequence: string): Promise<{ totalIncome: number; totalExpense: number; netAmount: number; @@ -2472,11 +2471,9 @@ export class WalletApplicationService { startDate: string; endDate: string; }> { - const userIdBigInt = BigInt(userId); - - // 获取所有流水 + // 使用 accountSequence 查询流水(废弃 userId 查询) const entries = await this.prisma.ledgerEntry.findMany({ - where: { userId: userIdBigInt, assetType: 'USDT' }, + where: { accountSequence, assetType: 'USDT' }, orderBy: { createdAt: 'asc' }, }); @@ -2547,7 +2544,7 @@ export class WalletApplicationService { /** * 获取流水趋势 */ - async getLedgerTrend(userId: string, days: number = 30): Promise<{ + async getLedgerTrend(accountSequence: string, days: number = 30): Promise<{ dailyTrend: Array<{ date: string; income: number; @@ -2560,14 +2557,14 @@ export class WalletApplicationService { periodExpense: number; periodNet: number; }> { - const userIdBigInt = BigInt(userId); + // 使用 accountSequence 查询流水(废弃 userId 查询) const startDate = new Date(); startDate.setDate(startDate.getDate() - days); startDate.setHours(0, 0, 0, 0); const entries = await this.prisma.ledgerEntry.findMany({ where: { - userId: userIdBigInt, + accountSequence, assetType: 'USDT', createdAt: { gte: startDate }, }, diff --git a/backend/services/wallet-service/src/domain/aggregates/wallet-account.aggregate.spec.ts b/backend/services/wallet-service/src/domain/aggregates/wallet-account.aggregate.spec.ts index 92f1d845..5256e909 100644 --- a/backend/services/wallet-service/src/domain/aggregates/wallet-account.aggregate.spec.ts +++ b/backend/services/wallet-service/src/domain/aggregates/wallet-account.aggregate.spec.ts @@ -27,7 +27,7 @@ describe('WalletAccount Aggregate', () => { let wallet: WalletAccount; beforeEach(() => { - wallet = WalletAccount.createNew(BigInt(1), UserId.create(1)); + wallet = WalletAccount.createNew('D00000000001', UserId.create(1)); }); describe('createNew', () => { diff --git a/backend/services/wallet-service/src/infrastructure/persistence/repositories/ledger-entry.repository.impl.ts b/backend/services/wallet-service/src/infrastructure/persistence/repositories/ledger-entry.repository.impl.ts index a73d7de0..1f5bd811 100644 --- a/backend/services/wallet-service/src/infrastructure/persistence/repositories/ledger-entry.repository.impl.ts +++ b/backend/services/wallet-service/src/infrastructure/persistence/repositories/ledger-entry.repository.impl.ts @@ -133,9 +133,14 @@ export class LedgerEntryRepositoryImpl implements ILedgerEntryRepository { filters?: LedgerFilters, pagination?: Pagination, ): Promise> { - const where: Record = { accountSequence }; + const where: Record = { + accountSequence, + // 排除临时性流水类型 + entryType: { notIn: LedgerEntryRepositoryImpl.HIDDEN_ENTRY_TYPES }, + }; if (filters?.entryType) { + // 如果用户指定了类型筛选,则使用用户指定的类型 where.entryType = filters.entryType; } if (filters?.assetType) {