chore(reward-service): add detailed debug logs to reward calculation

Add comprehensive logging to RewardCalculationService for easier debugging:
- Log start/end of calculateRewards with order info
- Log each benefit calculation with input parameters
- Log distribution results from authorization-service calls
- Log share right decisions (settleable vs pending)

🤖 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-11 07:59:01 -08:00
parent 16d95999de
commit 4d01bc5faa
1 changed files with 61 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { Injectable, Inject } from '@nestjs/common';
import { Injectable, Inject, Logger } from '@nestjs/common';
import { RewardLedgerEntry } from '../aggregates/reward-ledger-entry/reward-ledger-entry.aggregate';
import { RewardSource } from '../value-objects/reward-source.vo';
import { RightType, RIGHT_AMOUNTS } from '../value-objects/right-type.enum';
@ -61,6 +61,8 @@ const RWAD_POOL_ACCOUNT_ID = BigInt(4);
@Injectable()
export class RewardCalculationService {
private readonly logger = new Logger(RewardCalculationService.name);
constructor(
@Inject(REFERRAL_SERVICE_CLIENT)
private readonly referralService: IReferralServiceClient,
@ -79,6 +81,11 @@ export class RewardCalculationService {
provinceCode: string;
cityCode: string;
}): Promise<RewardLedgerEntry[]> {
this.logger.log(
`[calculateRewards] START orderNo=${params.sourceOrderNo}, userId=${params.sourceUserId}, ` +
`treeCount=${params.treeCount}, province=${params.provinceCode}, city=${params.cityCode}`,
);
const rewards: RewardLedgerEntry[] = [];
// ============================================
@ -173,6 +180,10 @@ export class RewardCalculationService {
);
rewards.push(...communityRewards);
this.logger.log(
`[calculateRewards] DONE orderNo=${params.sourceOrderNo}, totalRewards=${rewards.length}`,
);
return rewards;
}
@ -308,6 +319,8 @@ export class RewardCalculationService {
sourceUserId: bigint,
treeCount: number,
): Promise<RewardLedgerEntry[]> {
this.logger.debug(`[calculateShareRights] userId=${sourceUserId}, treeCount=${treeCount}`);
const { usdt, hashpowerPercent } = RIGHT_AMOUNTS[RightType.SHARE_RIGHT];
const usdtAmount = Money.USDT(usdt * treeCount);
const hashpower = Hashpower.fromTreeCount(treeCount, hashpowerPercent);
@ -326,6 +339,9 @@ export class RewardCalculationService {
if (directReferrer.hasPlanted) {
// 推荐人已认种,直接可结算
this.logger.debug(
`[calculateShareRights] referrer=${directReferrer.userId} hasPlanted=true -> SETTLEABLE`,
);
return [RewardLedgerEntry.createSettleable({
userId: directReferrer.userId,
accountSequence: directReferrer.userId,
@ -336,6 +352,9 @@ export class RewardCalculationService {
})];
} else {
// 推荐人未认种进入待领取24h倒计时
this.logger.debug(
`[calculateShareRights] referrer=${directReferrer.userId} hasPlanted=false -> PENDING (24h)`,
);
return [RewardLedgerEntry.createPending({
userId: directReferrer.userId,
accountSequence: directReferrer.userId,
@ -347,6 +366,7 @@ export class RewardCalculationService {
}
} else {
// 无推荐人,进总部社区
this.logger.debug(`[calculateShareRights] no referrer -> HEADQUARTERS`);
return [RewardLedgerEntry.createSettleable({
userId: HEADQUARTERS_COMMUNITY_USER_ID,
accountSequence: HEADQUARTERS_COMMUNITY_USER_ID,
@ -368,6 +388,10 @@ export class RewardCalculationService {
provinceCode: string,
treeCount: number,
): Promise<RewardLedgerEntry[]> {
this.logger.debug(
`[calculateProvinceTeamRight] userId=${sourceUserId}, province=${provinceCode}, treeCount=${treeCount}`,
);
const { usdt, hashpowerPercent } = RIGHT_AMOUNTS[RightType.PROVINCE_TEAM_RIGHT];
// 调用 authorization-service 获取省团队权益分配方案
@ -377,6 +401,10 @@ export class RewardCalculationService {
treeCount,
);
this.logger.debug(
`[calculateProvinceTeamRight] distribution: ${JSON.stringify(distribution.distributions)}`,
);
const rewards: RewardLedgerEntry[] = [];
// 根据分配方案创建奖励记录
@ -417,6 +445,10 @@ export class RewardCalculationService {
provinceCode: string,
treeCount: number,
): Promise<RewardLedgerEntry[]> {
this.logger.debug(
`[calculateProvinceAreaRight] province=${provinceCode}, treeCount=${treeCount}`,
);
const { usdt, hashpowerPercent } = RIGHT_AMOUNTS[RightType.PROVINCE_AREA_RIGHT];
// 调用 authorization-service 获取省区域权益分配方案
@ -425,6 +457,10 @@ export class RewardCalculationService {
treeCount,
);
this.logger.debug(
`[calculateProvinceAreaRight] distribution: ${JSON.stringify(distribution.distributions)}`,
);
const rewards: RewardLedgerEntry[] = [];
// 根据分配方案创建奖励记录
@ -463,6 +499,10 @@ export class RewardCalculationService {
cityCode: string,
treeCount: number,
): Promise<RewardLedgerEntry[]> {
this.logger.debug(
`[calculateCityTeamRight] userId=${sourceUserId}, city=${cityCode}, treeCount=${treeCount}`,
);
const { usdt, hashpowerPercent } = RIGHT_AMOUNTS[RightType.CITY_TEAM_RIGHT];
// 调用 authorization-service 获取市团队权益分配方案
@ -472,6 +512,10 @@ export class RewardCalculationService {
treeCount,
);
this.logger.debug(
`[calculateCityTeamRight] distribution: ${JSON.stringify(distribution.distributions)}`,
);
const rewards: RewardLedgerEntry[] = [];
// 根据分配方案创建奖励记录
@ -512,6 +556,10 @@ export class RewardCalculationService {
cityCode: string,
treeCount: number,
): Promise<RewardLedgerEntry[]> {
this.logger.debug(
`[calculateCityAreaRight] city=${cityCode}, treeCount=${treeCount}`,
);
const { usdt, hashpowerPercent } = RIGHT_AMOUNTS[RightType.CITY_AREA_RIGHT];
// 调用 authorization-service 获取市区域权益分配方案
@ -520,6 +568,10 @@ export class RewardCalculationService {
treeCount,
);
this.logger.debug(
`[calculateCityAreaRight] distribution: ${JSON.stringify(distribution.distributions)}`,
);
const rewards: RewardLedgerEntry[] = [];
// 根据分配方案创建奖励记录
@ -559,6 +611,10 @@ export class RewardCalculationService {
sourceUserId: bigint,
treeCount: number,
): Promise<RewardLedgerEntry[]> {
this.logger.debug(
`[calculateCommunityRight] userId=${sourceUserId}, treeCount=${treeCount}`,
);
const { usdt, hashpowerPercent } = RIGHT_AMOUNTS[RightType.COMMUNITY_RIGHT];
// 调用 authorization-service 获取社区权益分配方案
@ -567,6 +623,10 @@ export class RewardCalculationService {
treeCount,
);
this.logger.debug(
`[calculateCommunityRight] distribution: ${JSON.stringify(distribution.distributions)}`,
);
const rewards: RewardLedgerEntry[] = [];
// 根据分配方案创建奖励记录