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 86be384d..57ae2961 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 @@ -7,7 +7,7 @@ import { IWithdrawalOrderRepository, WITHDRAWAL_ORDER_REPOSITORY, IPendingRewardRepository, PENDING_REWARD_REPOSITORY, } from '@/domain/repositories'; -import { LedgerEntry, DepositOrder, SettlementOrder, WithdrawalOrder, PendingRewardStatus } from '@/domain/aggregates'; +import { LedgerEntry, DepositOrder, SettlementOrder, WithdrawalOrder, PendingReward, PendingRewardStatus } from '@/domain/aggregates'; import { UserId, Money, Hashpower, LedgerEntryType, AssetType, ChainType, SettleCurrency, } from '@/domain/value-objects'; @@ -617,7 +617,9 @@ export class WalletApplicationService { /** * 分配资金到用户钱包 - * 支持用户账户 (D+日期+序号) 和系统账户 (S+序号, 9+省代码, 8+市代码) + * 支持用户账户 (D+日期+序号) 和系统账户 (S+序号, 7+省代码, 6+市代码, 9+省代码, 8+市代码) + * + * 新方案:使用 pending_rewards 表逐笔追踪待领取奖励 */ private async allocateToUserWallet( allocation: FundAllocationItem, @@ -626,17 +628,16 @@ export class WalletApplicationService { // targetId 是 accountSequence // - 用户账户: D2512120001 // - 固定系统账户: S0000000001 (userId: -1 to -100, 由migration seed创建) + // - 省团队账户: 7440000 (7 + provinceCode, userId = 7440000) + // - 市团队账户: 6440100 (6 + cityCode, userId = 6440100) // - 省区域账户: 9440000 (9 + provinceCode, userId = 9440000) // - 市区域账户: 8440100 (8 + cityCode, userId = 8440100) // 为系统账户生成 userId (用于数据库约束) let systemUserId = BigInt(0); const targetId = allocation.targetId; - if (targetId.startsWith('9')) { - // 省区域账户: 9440000 -> userId = 9440000 (动态创建) - systemUserId = BigInt(targetId); - } else if (targetId.startsWith('8')) { - // 市区域账户: 8440100 -> userId = 8440100 (动态创建) + if (targetId.startsWith('9') || targetId.startsWith('8') || targetId.startsWith('7') || targetId.startsWith('6')) { + // 区域/团队账户: 9440000, 8440100, 7440000, 6440100 -> userId = accountSequence systemUserId = BigInt(targetId); } else if (targetId.startsWith('S')) { // 固定系统账户: S0000000001 -> userId = -1 (负数,由seed创建,直接查找) @@ -644,7 +645,7 @@ export class WalletApplicationService { systemUserId = BigInt(-parseInt(numPart, 10)); } - // 使用 getOrCreate 自动创建不存在的账户(仅用于省/市区域账户的动态创建) + // 使用 getOrCreate 自动创建不存在的账户(仅用于区域/团队账户的动态创建) // 固定系统账户(S开头)应该已由migration seed创建 const wallet = await this.walletRepo.getOrCreate(targetId, systemUserId); if (!wallet) { @@ -654,21 +655,24 @@ export class WalletApplicationService { const amount = Money.USDT(allocation.amount); - // 添加待领取奖励(24小时后过期) + // 添加待领取奖励(24小时后过期)- 写入 pending_rewards 表 const expireAt = new Date(Date.now() + 24 * 60 * 60 * 1000); - wallet.addPendingReward( - amount, - Hashpower.create(0), + const pendingReward = PendingReward.create({ + accountSequence: wallet.accountSequence, + userId: wallet.userId, + usdtAmount: amount, + hashpowerAmount: Hashpower.create(0), + sourceOrderId: orderId, + allocationType: allocation.allocationType, expireAt, - orderId, - ); - await this.walletRepo.save(wallet); + }); + await this.pendingRewardRepo.save(pendingReward); // 记录流水 const ledgerEntry = LedgerEntry.create({ accountSequence: wallet.accountSequence, - userId: wallet.userId, // 从钱包获取 userId + userId: wallet.userId, entryType: LedgerEntryType.REWARD_PENDING, amount, refOrderId: orderId,