切换徽章逻辑重构

This commit is contained in:
tianfeng 2026-02-04 21:43:04 +08:00
parent 59280d2b06
commit 0ae11c76e6

View File

@ -7,6 +7,8 @@ import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.cmd.user.material.UserToggleBadgeCmd;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.common.props.UserBadgeCommon;
import com.red.circle.other.infra.database.mongo.service.user.profile.UserRunProfileService;
import com.red.circle.other.infra.database.rds.entity.badge.BadgeBackpack;
import com.red.circle.other.infra.database.rds.entity.badge.BadgeConfig;
import com.red.circle.other.infra.database.rds.service.badge.BadgeBackpackService;
@ -32,45 +34,47 @@ public class UserToggleBadgeCmdExe {
private final BadgeBackpackService badgeBackpackService;
private final UserProfileGateway userProfileGateway;
private final BadgeConfigService badgeConfigService;
private final UserRunProfileService userRunProfileService;
private final UserBadgeCommon userBadgeCommon;
public void execute(UserToggleBadgeCmd cmd) {
Long userId = cmd.getReqUserId();
Long badgeId = cmd.getBadgeId();
String sysOrigin = cmd.getReqSysOrigin().getOrigin();
List<BadgeConfig> badgeConfigs = badgeConfigService.listByIds(Collections.singletonList(badgeId));
ResponseAssert.isFalse(CommonErrorCode.OPERATING_FAILURE,
badgeConfigs.isEmpty());
// 验证徽章配置存在
List<BadgeConfig> badgeConfigs = badgeConfigService.listByIds(Collections.singletonList(badgeId));
ResponseAssert.isFalse(CommonErrorCode.OPERATING_FAILURE, badgeConfigs.isEmpty());
// 查询当前已佩戴的徽章
List<BadgeBackpack> currentWearBadges = badgeBackpackService.listNotExpireTimeByUserIds(new HashSet<>(Collections.singletonList(userId)));
// 查询当前背包中的徽章
List<BadgeBackpack> backpackList = badgeBackpackService.listNotExpireTimeByUserIds(
new HashSet<>(Collections.singletonList(userId))
);
ResponseAssert.isFalse(CommonErrorCode.OPERATING_FAILURE, CollectionUtils.isEmpty(backpackList));
// 检查目标徽章是否已佩戴
boolean isWearing = currentWearBadges.stream()
.anyMatch(badge -> badge.getBadgeId().equals(badgeId) && Boolean.TRUE.equals(badge.getUseProps()));
// 检查目标徽章是否在背包中
Optional<BadgeBackpack> targetBadgeOpt = backpackList.stream()
.filter(badge -> badge.getBadgeId().equals(badgeId))
.findFirst();
ResponseAssert.isTrue(CommonErrorCode.NOT_FOUND_MAPPING_INFO, targetBadgeOpt.isPresent());
List<Long> newBadgeIds;
if (isWearing) {
// 已佩戴 卸下该徽章
newBadgeIds = currentWearBadges.stream()
.map(BadgeBackpack::getBadgeId)
.filter(id -> !id.equals(badgeId))
.collect(Collectors.toList());
} else {
// 未佩戴 佩戴该徽章
newBadgeIds = currentWearBadges.stream()
.map(BadgeBackpack::getBadgeId)
.collect(Collectors.toList());
newBadgeIds.add(badgeId);
}
// 切换徽章佩戴状态
BadgeBackpack targetBadge = targetBadgeOpt.get();
boolean currentUseStatus = targetBadge.getUseProps();
badgeBackpackService.updateSelectiveById(
new BadgeBackpack()
.setId(targetBadge.getId())
.setUseProps(!currentUseStatus)
);
// 更新徽章佩戴状态
if (CollectionUtils.isEmpty(newBadgeIds)) {
userProfileGateway.removeUserAllWearBadge(userId, sysOrigin);
} else {
userProfileGateway.updateUserWearBadge(userId, sysOrigin, newBadgeIds);
}
// 更新用户佩戴徽章覆盖层
Set<Long> backpackBadgeSet = backpackList.stream()
.map(BadgeBackpack::getBadgeId)
.collect(Collectors.toSet());
userRunProfileService.updateUserWearBadgesOverlay(userId,
userBadgeCommon.listUseBadge(userId, sysOrigin, backpackBadgeSet)
);
}
}