diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/entity/user/user/CpLevelConfig.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/entity/user/user/CpLevelConfig.java index fa968fee..c33095bb 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/entity/user/user/CpLevelConfig.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/entity/user/user/CpLevelConfig.java @@ -11,13 +11,14 @@ import java.util.List; * CP等级配置. *

* 字段说明: - * - expThreshold : 达到该等级所需的累计CP值(对应 user_cp_value.cp_val) - * - loveHeartLevel : 爱心样式等级(0=无, 1-3=对应样式,展示在资料卡和用户头像旁) - * - micEffectLevel : 邻麦动效等级(0=无, 1-3,仅在左右相邻麦位时触发) - * - profileCardLevel: 资料卡动态背景等级(0=无, 1-3,展示在CP资料卡内层元素) - * - giftWindowLevel : 赠礼飘窗等级(0=无, 1-3,双方赠送CP礼物时触发) - * - hasNonMicEffect : 是否解锁非邻麦动效(true=解锁,非相邻时也可展示动效) - * - hasCpHeadwear : 是否解锁CP头饰(true=解锁,展示在双方头像周期性飞出效果) + * - expThreshold : 升入下一等级所需超过的累计CP值门槛 + * 例:超过500000升LV2,超过1000000升LV3,以此类推 + * - loveHeartLevel : 爱心样式等级(1-3) + * - micEffectLevel : 邻麦动效等级(0=无, 1-3) + * - profileCardLevel: 资料卡动态背景等级(0=无, 1-3) + * - giftWindowLevel : 赠礼飘窗等级(0=无, 1-3) + * - hasNonMicEffect : 是否解锁非邻麦动效 + * - hasCpHeadwear : 是否解锁CP头饰 * * @author tf */ @@ -36,6 +37,7 @@ public enum CpLevelConfig { LV10(10, 200000000L, 3, 3, 3, 3, true, true ); private final int level; + /** 升入下一等级所需超过的门槛值,LV10为满级门槛. */ private final long expThreshold; private final int loveHeartLevel; private final int micEffectLevel; @@ -58,32 +60,50 @@ public enum CpLevelConfig { this.hasCpHeadwear = hasCpHeadwear; } - /** 按 expThreshold 降序,用于 of() 从高到低匹配. */ - private static final List DESC = Arrays.stream(values()) - .sorted(Comparator.comparingLong(CpLevelConfig::getExpThreshold).reversed()) + /** 按 expThreshold 升序,用于 of() 逐级匹配. */ + private static final List ASC = Arrays.stream(values()) + .sorted(Comparator.comparingLong(CpLevelConfig::getExpThreshold)) .toList(); /** - * 根据累计CP值推算当前等级,未达到Lv1门槛返回 null. + * 根据累计CP值推算当前等级. + * 超过LV(n-1).expThreshold即为LVn,不足LV1.expThreshold默认LV1,超过LV10.expThreshold返回LV10. + * 例:cpVal=500001 超过LV1门槛500000,返回LV2. */ public static CpLevelConfig of(BigDecimal cpVal) { - if (cpVal == null) { - return null; + if (cpVal == null || cpVal.compareTo(BigDecimal.ZERO) <= 0) { + return LV1; } - long val = cpVal.longValue(); - return DESC.stream() - .filter(lv -> val >= lv.expThreshold) - .findFirst() - .orElse(null); + // 找第一个未超过其门槛的等级,返回上一级;全部超过则返回LV10 + for (int i = 0; i < ASC.size(); i++) { + if (cpVal.compareTo(BigDecimal.valueOf(ASC.get(i).expThreshold)) <= 0) { + return i == 0 ? LV1 : ASC.get(i); + } + } + return LV10; } /** - * 获取下一等级经验门槛,满级返回 null. + * 是否已超过LV1门槛(即真正升过LV1). + */ + public static boolean hasReachedLevel(BigDecimal cpVal) { + return cpVal != null + && cpVal.compareTo(BigDecimal.valueOf(LV1.expThreshold)) > 0; + } + + /** + * 升入下一级需要超过的门槛值,即当前等级的expThreshold,满级返回自身门槛. + * 例:当前LV2,需超过1000000升LV3,返回1000000. */ public Long nextExpThreshold() { - int nextOrdinal = this.ordinal() + 1; - CpLevelConfig[] all = values(); - return nextOrdinal < all.length ? all[nextOrdinal].expThreshold : null; + return this.expThreshold; + } + + /** + * 根据累计CP值获取升入下一级需要超过的门槛值,满级返回自身门槛. + */ + public static Long nextExpThreshold(BigDecimal cpVal) { + return of(cpVal).nextExpThreshold(); } }