升级处理并优化

This commit is contained in:
tianfeng 2025-12-30 22:45:25 +08:00
parent 79d2132bd4
commit 55d827efd7
2 changed files with 40 additions and 58 deletions

View File

@ -1,5 +1,6 @@
package com.red.circle.other.app.command.family;
import cn.hutool.core.collection.CollectionUtil;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.clientobject.family.FamilyBaseInfoCO;
@ -21,13 +22,9 @@ import com.red.circle.other.infra.enums.family.FamilyStatusEnum;
import com.red.circle.other.infra.enums.family.FamilyBoxConfigEnum;
import com.red.circle.other.inner.model.dto.famliy.FamilyDetailsDTO;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
@ -108,6 +105,8 @@ public class FamilyBaseInfoExe {
FamilyBaseInfoCO.FamilyMemberList memberList,
Boolean joinWaiting) {
Map<Long, BigDecimal> decimalMap = familyLevelExpService.mapTotalExpCountByFamilyIds(new HashSet<>(Collections.singletonList(baseInfo.getId())));
return new FamilyBaseInfoCO()
.setFamilyId(baseInfo.getId())
.setFamilyAdmin(adminInfo)
@ -116,8 +115,7 @@ public class FamilyBaseInfoExe {
.setFamilyName(baseInfo.getFamilyName())
.setFamilyNotice(baseInfo.getFamilyNotice())
.setFamilyIntro(baseInfo.getFamilyIntro())
.setCurrentExp(
familyLevelExpService.getExp(baseInfo.getId(), baseInfo.getFamilyLevelId()).longValue())
.setCurrentExp(decimalMap.get(baseInfo.getId()).longValue())
.setCurrentMember(getFamilyMemberCount(baseInfo.getId()))
.setFamilyLevel(Optional.ofNullable(familyLevel.getLevel())
.orElse(0))

View File

@ -47,11 +47,7 @@ import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
@ -236,60 +232,57 @@ public class FamilyCommon {
long remainingExp = exp;
FamilyDetailsDTO levelInfo = currentLevel;
// 先统计一次总经验只统计一次
expAccumulate(member, exp, regionId);
while (remainingExp > 0) {
FamilyLevelConfig nextLevel = getNewLevel(sysOrigin, levelInfo);
// 已经是满级无法继续升级
if (Objects.isNull(nextLevel)) {
addExpWithoutLevelUp(member, levelInfo, remainingExp, regionId);
// 满级了剩余经验全部加到当前等级
familyLevelExpService.incrExp(member.getFamilyId(),
levelInfo.getFamilyLevelId(), BigDecimal.valueOf(remainingExp));
break;
}
long currentLevelCompleteExp = levelInfo.getLevelExp(); // 当前等级满级所需总经验
long currentFamilyExp = familyLevelExpService.getExp(levelInfo.getFamilyId(),
levelInfo.getFamilyLevelId()).longValue(); // 当前已有经验
long expNeeded = currentLevelCompleteExp - currentFamilyExp; // 还需要多少经验才能升级
long currentLevelCompleteExp = levelInfo.getLevelExp();
long currentFamilyExp = familyLevelExpService.mapTotalExpCountByFamilyIds(new HashSet<>(Collections.singletonList(levelInfo.getFamilyId())))
.getOrDefault(levelInfo.getFamilyId(), BigDecimal.ZERO).longValue();
long expNeeded = currentLevelCompleteExp - currentFamilyExp;
// 经验不足以升级只增加经验不升级
if (remainingExp < expNeeded) {
addExpWithoutLevelUp(member, levelInfo, remainingExp, regionId);
// 经验不足以升级全部加到当前等级
familyLevelExpService.incrExp(member.getFamilyId(),
levelInfo.getFamilyLevelId(), BigDecimal.valueOf(remainingExp));
break;
}
// 经验足够升级先增加升级所需的经验然后执行升级
addExpWithoutLevelUp(member, levelInfo, expNeeded, regionId);
updateFamily(member, nextLevel);
// 经验足够升级
// 1. 把当前等级加满
if (expNeeded > 0) {
familyLevelExpService.incrExp(member.getFamilyId(),
levelInfo.getFamilyLevelId(), BigDecimal.valueOf(expNeeded));
}
// 2. 升级
familyLevelUp(member, nextLevel);
// 3. 剩余经验进入下一轮循环
remainingExp -= expNeeded;
// 重新获取升级后的等级信息
// 4. 初始化新等级经验记录如果有剩余经验在下一轮循环会加上
if (remainingExp > 0) {
familyLevelExpService.incrExp(member.getFamilyId(),
nextLevel.getId(), BigDecimal.ZERO);
}
// 5. 刷新等级信息
levelInfo = getFamilyDetails(sysOrigin, member.getFamilyId());
}
}
/**
* 增加经验值但不升级
*/
private void addExpWithoutLevelUp(FamilyMemberInfo member, FamilyDetailsDTO currentLevel, Long exp, String regionId) {
addFamilyExpIncreaseRecord(exp, member, currentLevel.getFamilyLevelId());
expAccumulate(member, exp, regionId);
}
/**
* 更新家族
*
* @param member 成员
* @param newLevel 新等级
*/
private void updateFamily(FamilyMemberInfo member, FamilyLevelConfig newLevel) {
// 家族升级
familyLevelUp(member, newLevel);
// 更新家族等级缓存数据
// cacheFamilyLevel(member.getSysOrigin() + member.getFamilyId(), newLevel, member.getFamilyId());
}
/**
* 移除家族所有人的头饰与徽章.
@ -429,16 +422,7 @@ public class FamilyCommon {
return Lists.newArrayList(userIds);
}
/**
* 家族成员增加经验值记录
*
* @param exp 经验值
* @param member 成员对象
* @param levelId 等级ID
*/
private void addFamilyExpIncreaseRecord(Long exp, FamilyMemberInfo member, Long levelId) {
familyLevelExpService.incrExp(member.getFamilyId(), levelId, BigDecimal.valueOf(exp));
}
/**
* 累加经验值(家族成员总榜家族周榜家族成员周经验值)