From 8367530ebe4fa07ce753c6258e013e16a3160735 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 23 Dec 2025 07:14:08 -0800 Subject: [PATCH] =?UTF-8?q?refactor(authorization):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E7=A5=96=E5=85=88=E9=93=BE=E5=8D=87=E7=BA=A7=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E4=BF=9D=E7=95=99=E5=9B=A2=E9=98=9F=E6=9C=AC?= =?UTF-8?q?=E4=BA=BA=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 业务规则简化: - 市/省团队本人伞下认种数达到阈值时,团队本人获得区域授权 - 移除了"伞下成员达到阈值时该成员获得授权"的逻辑 - 两种逻辑是互斥的,只保留团队本人升级的方式 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../kafka/event-consumer.controller.ts | 206 +----------------- 1 file changed, 5 insertions(+), 201 deletions(-) diff --git a/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts b/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts index 457f61be..836b18b8 100644 --- a/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts +++ b/backend/services/authorization-service/src/infrastructure/kafka/event-consumer.controller.ts @@ -183,11 +183,8 @@ export class EventConsumerController { } } - // 4. 检查自动升级条件(省区域/市区域)- 仅针对当前用户的祖先链 - await this.checkAutoUpgrade(teamStats) - - // 5. 检查所有已激活市/省团队的自动升级条件 - // 每次认种都可能导致某个市/省团队达到升级阈值 + // 4. 检查所有已激活市/省团队的自动升级条件 + // 业务规则:市/省团队本人伞下认种数达到阈值时,团队本人获得区域授权 await this.checkAllTeamAutoUpgrade() this.logger.log(`[PLANTING] Completed processing tree planted event for user ${userId}`) @@ -297,205 +294,12 @@ export class EventConsumerController { private static readonly PROVINCE_UPGRADE_THRESHOLD = 50000 // 5万棵 private static readonly CITY_UPGRADE_THRESHOLD = 10000 // 1万棵 - /** - * 检查并执行自动升级 - * 业务规则: - * - 省团队账号伞下第一个累计达到5万棵的账户自动获得该省区域授权 - * - 市团队账号伞下第一个累计达到1万棵的账户自动获得该市区域授权 - */ - private async checkAutoUpgrade(teamStats: TeamStatistics): Promise { - const userId = teamStats.userId - const accountSequence = teamStats.accountSequence - const totalTeamCount = teamStats.totalTeamPlantingCount - - this.logger.debug(`[AUTO-UPGRADE] Checking auto upgrade for user ${userId}, total team count: ${totalTeamCount}`) - - // 检查省区域升级(5万棵) - if (totalTeamCount >= EventConsumerController.PROVINCE_UPGRADE_THRESHOLD) { - await this.checkProvinceAutoUpgrade(teamStats) - } - - // 检查市区域升级(1万棵) - if (totalTeamCount >= EventConsumerController.CITY_UPGRADE_THRESHOLD) { - await this.checkCityAutoUpgrade(teamStats) - } - } - - /** - * 检查省区域自动升级 - * 条件:用户是某省团队的伞下成员,且累计达到5万棵,且该省尚无省区域授权 - */ - private async checkProvinceAutoUpgrade(teamStats: TeamStatistics): Promise { - const userId = UserId.create(teamStats.userId, teamStats.accountSequence) - const accountSequence = teamStats.accountSequence - - this.logger.debug(`[AUTO-UPGRADE] Checking province auto upgrade for ${accountSequence}`) - - // 1. 检查用户是否已有省区域授权 - const existingProvince = await this.authorizationRepository.findByAccountSequenceAndRoleType( - accountSequence, - RoleType.PROVINCE_COMPANY, - ) - if (existingProvince && existingProvince.status !== AuthorizationStatus.REVOKED) { - this.logger.debug(`[AUTO-UPGRADE] User ${accountSequence} already has province company authorization`) - return - } - - // 2. 检查用户是否已有市区域授权(互斥) - const existingCity = await this.authorizationRepository.findByAccountSequenceAndRoleType( - accountSequence, - RoleType.CITY_COMPANY, - ) - if (existingCity && existingCity.status !== AuthorizationStatus.REVOKED) { - this.logger.debug(`[AUTO-UPGRADE] User ${accountSequence} already has city company authorization, cannot auto upgrade to province`) - return - } - - // 3. 获取用户的祖先链,查找上级省团队 - const ancestorSeqs = await this.statsRepository.getReferralChain(accountSequence) as unknown as string[] - if (ancestorSeqs.length === 0) { - this.logger.debug(`[AUTO-UPGRADE] No ancestors found for ${accountSequence}`) - return - } - - // 4. 查找祖先链中的省团队授权 - const ancestorAuthProvinces = await this.authorizationRepository.findAuthProvinceByAccountSequences(ancestorSeqs) - if (ancestorAuthProvinces.length === 0) { - this.logger.debug(`[AUTO-UPGRADE] No auth province found in ancestor chain for ${accountSequence}`) - return - } - - // 5. 取最近的上级省团队 - let nearestAuthProvince: AuthorizationRole | null = null - for (const ancestorSeq of ancestorSeqs) { - const found = ancestorAuthProvinces.find(auth => auth.userId.accountSequence === ancestorSeq) - if (found && found.benefitActive) { - nearestAuthProvince = found - break - } - } - - if (!nearestAuthProvince) { - this.logger.debug(`[AUTO-UPGRADE] No active auth province found in ancestor chain for ${accountSequence}`) - return - } - - const provinceCode = nearestAuthProvince.regionCode.value - const provinceName = nearestAuthProvince.regionName - - // 6. 检查该省是否已有省区域授权 - const existingProvinceRegion = await this.authorizationRepository.findProvinceCompanyByRegion(provinceCode) - if (existingProvinceRegion) { - this.logger.debug(`[AUTO-UPGRADE] Province ${provinceName} already has province company authorization`) - return - } - - // 7. 执行自动升级 - this.logger.log(`[AUTO-UPGRADE] Auto upgrading user ${accountSequence} to province company: ${provinceName}`) - - const authorization = AuthorizationRole.createAutoUpgradedProvinceCompany({ - userId, - provinceCode, - provinceName, - }) - - await this.authorizationRepository.save(authorization) - await this.eventPublisher.publishAll(authorization.domainEvents) - authorization.clearDomainEvents() - - this.logger.log(`[AUTO-UPGRADE] Successfully auto upgraded user ${accountSequence} to province company: ${provinceName}`) - } - - /** - * 检查市区域自动升级 - * 条件:用户是某市团队的伞下成员,且累计达到1万棵,且该市尚无市区域授权 - */ - private async checkCityAutoUpgrade(teamStats: TeamStatistics): Promise { - const userId = UserId.create(teamStats.userId, teamStats.accountSequence) - const accountSequence = teamStats.accountSequence - - this.logger.debug(`[AUTO-UPGRADE] Checking city auto upgrade for ${accountSequence}`) - - // 1. 检查用户是否已有市区域授权 - const existingCity = await this.authorizationRepository.findByAccountSequenceAndRoleType( - accountSequence, - RoleType.CITY_COMPANY, - ) - if (existingCity && existingCity.status !== AuthorizationStatus.REVOKED) { - this.logger.debug(`[AUTO-UPGRADE] User ${accountSequence} already has city company authorization`) - return - } - - // 2. 检查用户是否已有省区域授权(互斥) - const existingProvince = await this.authorizationRepository.findByAccountSequenceAndRoleType( - accountSequence, - RoleType.PROVINCE_COMPANY, - ) - if (existingProvince && existingProvince.status !== AuthorizationStatus.REVOKED) { - this.logger.debug(`[AUTO-UPGRADE] User ${accountSequence} already has province company authorization, cannot auto upgrade to city`) - return - } - - // 3. 获取用户的祖先链,查找上级市团队 - const ancestorSeqs = await this.statsRepository.getReferralChain(accountSequence) as unknown as string[] - if (ancestorSeqs.length === 0) { - this.logger.debug(`[AUTO-UPGRADE] No ancestors found for ${accountSequence}`) - return - } - - // 4. 查找祖先链中的市团队授权 - const ancestorAuthCities = await this.authorizationRepository.findAuthCityByAccountSequences(ancestorSeqs) - if (ancestorAuthCities.length === 0) { - this.logger.debug(`[AUTO-UPGRADE] No auth city found in ancestor chain for ${accountSequence}`) - return - } - - // 5. 取最近的上级市团队 - let nearestAuthCity: AuthorizationRole | null = null - for (const ancestorSeq of ancestorSeqs) { - const found = ancestorAuthCities.find(auth => auth.userId.accountSequence === ancestorSeq) - if (found && found.benefitActive) { - nearestAuthCity = found - break - } - } - - if (!nearestAuthCity) { - this.logger.debug(`[AUTO-UPGRADE] No active auth city found in ancestor chain for ${accountSequence}`) - return - } - - const cityCode = nearestAuthCity.regionCode.value - const cityName = nearestAuthCity.regionName - - // 6. 检查该市是否已有市区域授权 - const existingCityRegion = await this.authorizationRepository.findCityCompanyByRegion(cityCode) - if (existingCityRegion) { - this.logger.debug(`[AUTO-UPGRADE] City ${cityName} already has city company authorization`) - return - } - - // 7. 执行自动升级 - this.logger.log(`[AUTO-UPGRADE] Auto upgrading user ${accountSequence} to city company: ${cityName}`) - - const authorization = AuthorizationRole.createAutoUpgradedCityCompany({ - userId, - cityCode, - cityName, - }) - - await this.authorizationRepository.save(authorization) - await this.eventPublisher.publishAll(authorization.domainEvents) - authorization.clearDomainEvents() - - this.logger.log(`[AUTO-UPGRADE] Successfully auto upgraded user ${accountSequence} to city company: ${cityName}`) - } - /** * 检查所有已激活市/省团队的自动升级条件 * 业务规则: - * - 已激活权益的省团队(AUTH_PROVINCE_COMPANY)用户,如果团队认种数达到5万棵,自动升级为省区域(PROVINCE_COMPANY) - * - 已激活权益的市团队(AUTH_CITY_COMPANY)用户,如果团队认种数达到1万棵,自动升级为市区域(CITY_COMPANY) + * - 已激活权益的省团队(AUTH_PROVINCE_COMPANY)用户,如果伞下认种数达到5万棵,自动升级为省区域(PROVINCE_COMPANY) + * - 已激活权益的市团队(AUTH_CITY_COMPANY)用户,如果伞下认种数达到1万棵,自动升级为市区域(CITY_COMPANY) + * 注意:伞下认种数不包括团队本人的认种 */ private async checkAllTeamAutoUpgrade(): Promise { this.logger.debug('[TEAM-AUTO-UPGRADE] Starting check for all active team authorizations')