fix(wallet-service): allocateToUserWallet 改用 pending_rewards 表

- 移除 wallet.addPendingReward() 调用(旧方案,更新 wallet_accounts.pending_usdt)
- 改用 PendingReward.create() + pendingRewardRepo.save() 写入 pending_rewards 表
- 支持 7+省代码(省团队)和 6+市代码(市团队)账户格式

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-13 03:53:23 -08:00
parent 8f81c46d75
commit 737fc3bb3c
1 changed files with 20 additions and 16 deletions

View File

@ -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,