新增徽章切换功能

This commit is contained in:
tianfeng 2025-11-19 18:05:18 +08:00
parent 0be2965c8b
commit 0e668d8dfc
7 changed files with 133 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package com.red.circle.other.adapter.app.user.material;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.cmd.user.material.UserSwitchBadgeCmd;
import com.red.circle.other.app.dto.cmd.user.material.UserToggleBadgeCmd;
import com.red.circle.other.app.service.user.material.UserBadgeService;
import com.red.circle.other.inner.model.dto.material.BadgeConfigDetailsDTO;
import java.util.List;
@ -56,4 +57,17 @@ public class UserBadgeRestController extends BaseController {
return userBadgeService.useWearBadge(cmd);
}
/**
* 切换单个徽章佩戴状态.
*
* @eo.name 切换单个徽章佩戴状态.
* @eo.url /toggle
* @eo.method post
* @eo.request-type json
*/
@PostMapping("/toggle")
public void toggleBadge(@RequestBody @Validated UserToggleBadgeCmd cmd) {
userBadgeService.toggleBadge(cmd);
}
}

View File

@ -74,7 +74,6 @@ public class UserBadgeTableQryExe {
.entrySet().stream().map(entry -> new BadgeGroupTableCO()
.setBadgeKey(entry.getKey())
.setBadgeTables(orderByAes(entry.getValue()))
.putShowBadgeTable()
).sorted(Comparator.comparing(BadgeGroupTableCO::displayBadgeSort))
.collect(Collectors.toList());
}

View File

@ -0,0 +1,82 @@
package com.red.circle.other.app.command.user;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
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.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;
import com.red.circle.other.infra.database.rds.service.badge.BadgeConfigService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.wallet.inner.error.WalletErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 用户切换单个徽章佩戴状态.
*
* @author system
*/
@Component
@RequiredArgsConstructor
public class UserToggleBadgeCmdExe {
private final BadgeBackpackService badgeBackpackService;
private final UserProfileGateway userProfileGateway;
private final BadgeConfigService badgeConfigService;
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());
// 查询当前已佩戴的徽章
LambdaQueryWrapper<BadgeBackpack> eq = Wrappers.lambdaQuery(BadgeBackpack.class)
.eq(BadgeBackpack::getUserId, userId)
.eq(BadgeBackpack::getUseProps, Boolean.TRUE);
List<BadgeBackpack> currentWearBadges = badgeBackpackService.list(eq);
// 检查目标徽章是否已佩戴
boolean isWearing = currentWearBadges.stream()
.anyMatch(badge -> badge.getBadgeId().equals(badgeId));
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);
}
// 更新徽章佩戴状态
if (CollectionUtils.isEmpty(newBadgeIds)) {
userProfileGateway.removeUserAllWearBadge(userId, sysOrigin);
} else {
userProfileGateway.updateUserWearBadge(userId, sysOrigin, newBadgeIds);
}
}
}

View File

@ -3,8 +3,10 @@ package com.red.circle.other.app.service.user.material;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.inner.model.dto.material.BadgeConfigDetailsDTO;
import com.red.circle.other.app.command.user.UserSwitchBadgeCmdExe;
import com.red.circle.other.app.command.user.UserToggleBadgeCmdExe;
import com.red.circle.other.app.command.user.query.UseWearBadgeQryExe;
import com.red.circle.other.app.dto.cmd.user.material.UserSwitchBadgeCmd;
import com.red.circle.other.app.dto.cmd.user.material.UserToggleBadgeCmd;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@ -20,6 +22,7 @@ public class UserBadgeServiceImpl implements UserBadgeService {
private final UseWearBadgeQryExe useWearBadgeQryExe;
private final UserSwitchBadgeCmdExe userSwitchBadgeCmdExe;
private final UserToggleBadgeCmdExe userToggleBadgeCmdExe;
@Override
public void switchBadge(UserSwitchBadgeCmd cmd) {
@ -31,4 +34,9 @@ public class UserBadgeServiceImpl implements UserBadgeService {
return useWearBadgeQryExe.execute(cmd);
}
@Override
public void toggleBadge(UserToggleBadgeCmd cmd) {
userToggleBadgeCmdExe.execute(cmd);
}
}

View File

@ -0,0 +1,23 @@
package com.red.circle.other.app.dto.cmd.user.material;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 用户切换单个徽章佩戴状态.
*
* @author system
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class UserToggleBadgeCmd extends AppExtCommand {
/**
* 徽章ID.
*/
@NotNull(message = "badgeId required.")
private Long badgeId;
}

View File

@ -2,6 +2,7 @@ package com.red.circle.other.app.service.user.material;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.dto.cmd.user.material.UserSwitchBadgeCmd;
import com.red.circle.other.app.dto.cmd.user.material.UserToggleBadgeCmd;
import com.red.circle.other.inner.model.dto.material.BadgeConfigDetailsDTO;
import java.util.List;
@ -16,4 +17,6 @@ public interface UserBadgeService {
List<BadgeConfigDetailsDTO> useWearBadge(AppExtCommand cmd);
void toggleBadge(UserToggleBadgeCmd cmd);
}

View File

@ -9,12 +9,8 @@ import com.red.circle.other.infra.database.rds.service.badge.BadgeBackpackServic
import com.red.circle.other.inner.enums.material.BadgeBackpackExpireType;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.Collection;
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.function.Function;
import java.util.stream.Collectors;
@ -61,7 +57,7 @@ public class BadgeBackpackServiceImpl extends
update()
.set(BadgeBackpack::getUseProps, Boolean.TRUE)
.in(BadgeBackpack::getBadgeId, badgeIds.stream().limit(5).collect(Collectors.toSet()))
.in(BadgeBackpack::getBadgeId, new HashSet<>(badgeIds))
.execute();
}