refactor(authorization): 移除祖先链升级逻辑,只保留团队本人升级

业务规则简化:
- 市/省团队本人伞下认种数达到阈值时,团队本人获得区域授权
- 移除了"伞下成员达到阈值时该成员获得授权"的逻辑
- 两种逻辑是互斥的,只保留团队本人升级的方式

🤖 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-23 07:14:08 -08:00
parent 7c2d0b8b7f
commit 8367530ebe
1 changed files with 5 additions and 201 deletions

View File

@ -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<void> {
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<void> {
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<void> {
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<void> {
this.logger.debug('[TEAM-AUTO-UPGRADE] Starting check for all active team authorizations')