diff --git a/backend/services/admin-service/src/infrastructure/persistence/repositories/user-detail-query.repository.impl.ts b/backend/services/admin-service/src/infrastructure/persistence/repositories/user-detail-query.repository.impl.ts index 17e757fd..0bc44d61 100644 --- a/backend/services/admin-service/src/infrastructure/persistence/repositories/user-detail-query.repository.impl.ts +++ b/backend/services/admin-service/src/infrastructure/persistence/repositories/user-detail-query.repository.impl.ts @@ -86,14 +86,16 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository // 注意:优先从 referrals 获取 accountSequences,因为用户可能不存在于 user_query_view const referralAccountSequences = referrals.map(r => r.accountSequence); const [adoptionCounts, directReferralCounts, teamStats] = await Promise.all([ - // 统计每个用户的认种订单数量(状态为 MINING_ENABLED) + // 统计每个用户的认种棵数(状态为 MINING_ENABLED) + // 注意:使用 _sum.treeCount 而非 _count.id,因为一笔订单可以认种多棵, + // 显示的是棵数(认种数量),不是订单条数。 this.prisma.plantingOrderQueryView.groupBy({ by: ['accountSequence'], where: { accountSequence: { in: referralAccountSequences }, status: 'MINING_ENABLED', }, - _count: { id: true }, + _sum: { treeCount: true }, }), // 统计每个用户的直推数量 this.prisma.referralQueryView.groupBy({ @@ -105,7 +107,7 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository this.getBatchUserStats(referralAccountSequences), ]); - const adoptionCountMap = new Map(adoptionCounts.map(a => [a.accountSequence, a._count.id])); + const adoptionCountMap = new Map(adoptionCounts.map(a => [a.accountSequence, a._sum.treeCount ?? 0])); const directCountMap = new Map( directReferralCounts .filter(d => d.referrerId !== null) @@ -169,14 +171,16 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository // 实时统计:获取每个用户的认种数量、团队认种量和直推数量 const userAccountSequences = directReferrals.map(r => r.accountSequence); const [adoptionCounts, directReferralCounts, teamStats] = await Promise.all([ - // 统计每个用户的认种订单数量(状态为 MINING_ENABLED) + // 统计每个用户的认种棵数(状态为 MINING_ENABLED) + // 注意:使用 _sum.treeCount 而非 _count.id,因为一笔订单可以认种多棵, + // 显示的是棵数(认种数量),不是订单条数。 this.prisma.plantingOrderQueryView.groupBy({ by: ['accountSequence'], where: { accountSequence: { in: userAccountSequences }, status: 'MINING_ENABLED', }, - _count: { id: true }, + _sum: { treeCount: true }, }), // 统计每个用户的直推数量 this.prisma.referralQueryView.groupBy({ @@ -188,7 +192,7 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository this.getBatchUserStats(userAccountSequences), ]); - const adoptionCountMap = new Map(adoptionCounts.map(a => [a.accountSequence, a._count.id])); + const adoptionCountMap = new Map(adoptionCounts.map(a => [a.accountSequence, a._sum.treeCount ?? 0])); const directCountMap = new Map( directReferralCounts .filter(d => d.referrerId !== null) @@ -528,15 +532,18 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository } async getPersonalAdoptionCount(accountSequence: string): Promise { - // 统计用户的认种订单数量(状态为 MINING_ENABLED) - const count = await this.prisma.plantingOrderQueryView.count({ + // 统计用户的认种棵数(状态为 MINING_ENABLED) + // 注意:使用 aggregate._sum.treeCount 而非 count(),因为一笔订单可以认种多棵, + // 返回的是总棵数(认种数量),不是订单条数。 + const result = await this.prisma.plantingOrderQueryView.aggregate({ where: { accountSequence, status: 'MINING_ENABLED', }, + _sum: { treeCount: true }, }); - return count; + return result._sum.treeCount ?? 0; } async getDirectReferralCount(accountSequence: string): Promise { @@ -578,17 +585,19 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository const teamAddressCount = teamMembers.length; - // 2. 获取团队认种量:汇总所有团队成员的有效认种订单数 + // 2. 获取团队认种量:汇总所有团队成员的有效认种棵数 + // 注意:使用 aggregate._sum.treeCount 而非 count(),因为一笔订单可以认种多棵。 let teamAdoptionCount = 0; if (teamMembers.length > 0) { - const count = await this.prisma.plantingOrderQueryView.count({ + const result = await this.prisma.plantingOrderQueryView.aggregate({ where: { accountSequence: { in: teamMembers.map((m) => m.accountSequence) }, status: 'MINING_ENABLED', }, + _sum: { treeCount: true }, }); - teamAdoptionCount = count; + teamAdoptionCount = result._sum.treeCount ?? 0; } return { teamAddressCount, teamAdoptionCount }; @@ -611,17 +620,19 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository if (accountSequences.length === 0) return result; - // 1. 批量获取个人认种量 + // 1. 批量获取个人认种棵数 + // 注意:使用 _sum.treeCount 而非 _count.id,因为一笔订单可以认种多棵, + // 显示的是棵数(认种数量),不是订单条数。 const personalAdoptionCounts = await this.prisma.plantingOrderQueryView.groupBy({ by: ['accountSequence'], where: { accountSequence: { in: accountSequences }, status: 'MINING_ENABLED', }, - _count: { id: true }, + _sum: { treeCount: true }, }); const personalAdoptionMap = new Map( - personalAdoptionCounts.map(p => [p.accountSequence, p._count.id]) + personalAdoptionCounts.map(p => [p.accountSequence, p._sum.treeCount ?? 0]) ); // 2. 批量获取用户的省市信息(从认种订单中获取第一个订单的省市) @@ -674,35 +685,41 @@ export class UserDetailQueryRepositoryImpl implements IUserDetailQueryRepository if (teamMembers.length > 0) { const teamAccountSequences = teamMembers.map(m => m.accountSequence); - // 团队总认种量 - teamAdoptionCount = await this.prisma.plantingOrderQueryView.count({ + // 团队总认种棵数(使用 sum treeCount,不是 count orders) + const teamResult = await this.prisma.plantingOrderQueryView.aggregate({ where: { accountSequence: { in: teamAccountSequences }, status: 'MINING_ENABLED', }, + _sum: { treeCount: true }, }); + teamAdoptionCount = teamResult._sum.treeCount ?? 0; - // 如果用户有省市信息,统计同省同市的认种量 + // 如果用户有省市信息,统计同省同市的认种棵数 if (userLocation?.province) { - // 同省认种量 - provinceAdoptionCount = await this.prisma.plantingOrderQueryView.count({ + // 同省认种棵数 + const provinceResult = await this.prisma.plantingOrderQueryView.aggregate({ where: { accountSequence: { in: teamAccountSequences }, status: 'MINING_ENABLED', selectedProvince: userLocation.province, }, + _sum: { treeCount: true }, }); + provinceAdoptionCount = provinceResult._sum.treeCount ?? 0; - // 同市认种量 + // 同市认种棵数 if (userLocation.city) { - cityAdoptionCount = await this.prisma.plantingOrderQueryView.count({ + const cityResult = await this.prisma.plantingOrderQueryView.aggregate({ where: { accountSequence: { in: teamAccountSequences }, status: 'MINING_ENABLED', selectedProvince: userLocation.province, selectedCity: userLocation.city, }, + _sum: { treeCount: true }, }); + cityAdoptionCount = cityResult._sum.treeCount ?? 0; } } }