diff --git a/backend/services/planting-service/src/pre-planting/application/services/pre-planting-reward.service.ts b/backend/services/planting-service/src/pre-planting/application/services/pre-planting-reward.service.ts index b2c92ba9..188f71eb 100644 --- a/backend/services/planting-service/src/pre-planting/application/services/pre-planting-reward.service.ts +++ b/backend/services/planting-service/src/pre-planting/application/services/pre-planting-reward.service.ts @@ -196,9 +196,9 @@ export class PrePlantingRewardService { ] = await Promise.all([ this.referralClient.getReferralChain(accountSequence), this.authorizationClient.getCommunityDistribution(accountSequence), - this.authorizationClient.getProvinceAreaDistribution(provinceCode), + this.authorizationClient.getProvinceAreaDistribution(provinceCode, portionCount), this.authorizationClient.getProvinceTeamDistribution(accountSequence), - this.authorizationClient.getCityAreaDistribution(cityCode), + this.authorizationClient.getCityAreaDistribution(cityCode, portionCount), this.authorizationClient.getCityTeamDistribution(accountSequence), ]); diff --git a/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-authorization.client.ts b/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-authorization.client.ts index cf072015..926aadae 100644 --- a/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-authorization.client.ts +++ b/backend/services/planting-service/src/pre-planting/infrastructure/external/pre-planting-authorization.client.ts @@ -9,6 +9,16 @@ export interface RewardDistributionResult { isFallback: boolean; } +/** + * authorization-service 省/市区域 API 返回的 distributions 数组项 + */ +interface AreaDistributionItem { + accountSequence: string; + treeCount: number; + reason: string; + isSystemAccount: boolean; +} + @Injectable() export class PrePlantingAuthorizationClient { private readonly logger = new Logger(PrePlantingAuthorizationClient.name); @@ -53,19 +63,39 @@ export class PrePlantingAuthorizationClient { /** * 省区域权益分配对象 + * + * [2026-02-28] 修复: + * 1. 传 treeCount 参数(authorization-service 必需,用于省公司月度考核累计) + * 2. 正确解析 { distributions: [{accountSequence, ...}] } 返回格式(之前错误地期望 { accountSequence }) + * 3. fallback 路径添加 padStart(6,'0'),确保生成 7 位标准格式(如 9440000 而非 944) */ async getProvinceAreaDistribution( provinceCode: string, + portionCount: number = 1, ): Promise { try { const response = await firstValueFrom( - this.httpService.get<{ accountSequence: string }>( + this.httpService.get<{ distributions: AreaDistributionItem[] }>( `${this.baseUrl}/internal/authorization/province-area-reward-distribution`, - { params: { provinceCode } }, + { params: { provinceCode, treeCount: portionCount } }, ), ); + + const distributions = response.data.distributions; + if (!distributions || distributions.length === 0) { + throw new Error('Empty distributions returned'); + } + + // 预种不拆分金额,取第一个分配对象的 accountSequence + // 如果跨考核达标点有多个 distribution,优先取非系统账户(即省公司) + const target = distributions.find(d => !d.isSystemAccount) || distributions[0]; + + this.logger.debug( + `Province area distribution for ${provinceCode}: ${target.accountSequence} (${target.reason})`, + ); + return { - recipientAccountSequence: response.data.accountSequence, + recipientAccountSequence: target.accountSequence, isFallback: false, }; } catch (error) { @@ -73,7 +103,7 @@ export class PrePlantingAuthorizationClient { `Failed to get province area distribution for ${provinceCode}, fallback to system province account`, ); return { - recipientAccountSequence: `9${provinceCode}`, + recipientAccountSequence: `9${provinceCode.padStart(6, '0')}`, isFallback: true, }; } @@ -109,19 +139,38 @@ export class PrePlantingAuthorizationClient { /** * 市区域权益分配对象 + * + * [2026-02-28] 修复:与省区域同样的三个问题 + * 1. 传 treeCount 参数 + * 2. 正确解析 { distributions: [...] } 返回格式 + * 3. fallback 路径添加 padStart(6,'0'),确保生成 7 位标准格式(如 8440100 而非 84401) */ async getCityAreaDistribution( cityCode: string, + portionCount: number = 1, ): Promise { try { const response = await firstValueFrom( - this.httpService.get<{ accountSequence: string }>( + this.httpService.get<{ distributions: AreaDistributionItem[] }>( `${this.baseUrl}/internal/authorization/city-area-reward-distribution`, - { params: { cityCode } }, + { params: { cityCode, treeCount: portionCount } }, ), ); + + const distributions = response.data.distributions; + if (!distributions || distributions.length === 0) { + throw new Error('Empty distributions returned'); + } + + // 预种不拆分金额,取第一个分配对象的 accountSequence + const target = distributions.find(d => !d.isSystemAccount) || distributions[0]; + + this.logger.debug( + `City area distribution for ${cityCode}: ${target.accountSequence} (${target.reason})`, + ); + return { - recipientAccountSequence: response.data.accountSequence, + recipientAccountSequence: target.accountSequence, isFallback: false, }; } catch (error) { @@ -129,7 +178,7 @@ export class PrePlantingAuthorizationClient { `Failed to get city area distribution for ${cityCode}, fallback to system city account`, ); return { - recipientAccountSequence: `8${cityCode}`, + recipientAccountSequence: `8${cityCode.padStart(6, '0')}`, isFallback: true, }; }