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 d94adf60..bd4b8399 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 @@ -980,7 +980,7 @@ export class WalletApplicationService { * 分配规则: * - SHARE_RIGHT (分享权益): 写入 pending_rewards 表,24小时待领取 * - PROVINCE_TEAM_RIGHT, PROVINCE_AREA_RIGHT, CITY_TEAM_RIGHT, CITY_AREA_RIGHT: 直接到账 - * - COMMUNITY_RIGHT (社区权益): 进入总部社区账户 S0000000001,直接到账 + * - COMMUNITY_RIGHT (社区权益): 根据 targetId 分配(用户账户或总部) */ private async allocateToUserWallet( allocation: FundAllocationItem, @@ -989,9 +989,17 @@ export class WalletApplicationService { const amount = Money.USDT(allocation.amount); const allocationType = allocation.allocationType; - // 社区权益直接进入总部社区账户 S0000000001 + // 社区权益:根据 targetId 判断分配目标 + // - targetId 为 '1' 或 'S0000000001' 时进入总部社区账户 + // - targetId 为用户账户(D开头)时进入该用户账户 if (allocationType === 'COMMUNITY_RIGHT') { - await this.allocateToHeadquartersCommunity(allocation, orderId); + const targetId = allocation.targetId; + if (targetId === '1' || targetId === 'S0000000001') { + await this.allocateToHeadquartersCommunity(allocation, orderId); + } else { + // 分配到具体用户的社区权益 + await this.allocateCommunityRightToUser(allocation, orderId); + } return; } @@ -1081,6 +1089,49 @@ export class WalletApplicationService { } } + /** + * 社区权益分配给具体用户(社区授权达标后) + * 直接进入用户的可结算余额 + */ + private async allocateCommunityRightToUser( + allocation: FundAllocationItem, + orderId: string, + ): Promise { + const targetId = allocation.targetId; + const wallet = await this.walletRepo.findByAccountSequence(targetId); + if (!wallet) { + this.logger.warn(`Failed to find wallet for community right allocation: ${targetId}`); + return; + } + + const amount = Money.USDT(allocation.amount); + + // 社区权益直接进入可结算余额(与分享权益的处理类似但不进 pending) + wallet.addSettleableReward(amount, Hashpower.create(0)); + await this.walletRepo.save(wallet); + + // 记录流水 + const ledgerEntry = LedgerEntry.create({ + accountSequence: wallet.accountSequence, + userId: wallet.userId, + entryType: LedgerEntryType.REWARD_TO_SETTLEABLE, + amount, + refOrderId: orderId, + memo: `${allocation.allocationType} allocation (direct settleable)`, + payloadJson: { + allocationType: allocation.allocationType, + metadata: allocation.metadata, + }, + }); + await this.ledgerRepo.save(ledgerEntry); + + await this.walletCacheService.invalidateWallet(wallet.userId.value); + + this.logger.debug( + `Allocated ${allocation.amount} USDT to user ${targetId} for ${allocation.allocationType} (community right)`, + ); + } + /** * 社区权益直接进入总部社区账户 S0000000001 */