diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/BadgeOperateListener.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/BadgeOperateListener.java index f667997f..91e16cd9 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/BadgeOperateListener.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/BadgeOperateListener.java @@ -19,7 +19,6 @@ 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 java.sql.Timestamp; import java.util.List; import java.util.Objects; import java.util.Set; @@ -45,8 +44,8 @@ public class BadgeOperateListener implements MessageListener { private final MessageEventProcess messageEventProcess; private final UserRunProfileService userRunProfileService; private final UserBadgeCommon userBadgeCommon; - private final UserProfileGateway userProfileGateway; - private final DistributedLockUtil distributedLockUtil; + private final UserProfileGateway userProfileGateway; + private final DistributedLockUtil distributedLockUtil; @Override public Action consume(ConsumerMessage message) { @@ -107,38 +106,22 @@ public class BadgeOperateListener implements MessageListener { String lockKey = "badge:operate:wear:" + userId + ":" + badgeId; distributedLockUtil.executeWithLock(lockKey, () -> { - // 在锁内查询,避免并发问题 - BadgeBackpack existBadge = badgeBackpackService.getByUserIdByBadgeId(userId, badgeId); + BadgeBackpack existBadge = badgeBackpackService.query() + .eq(BadgeBackpack::getUserId, userId) + .eq(BadgeBackpack::getBadgeId, badgeId) + .getOne(); if (existBadge != null) { - // 已存在徽章,执行更新操作(而不是跳过!) - Timestamp newExpireTime = calculateExpireTime(days); - String expireType = (days != null && days > 0) - ? BadgeBackpackExpireTypeEnum.TEMPORARY.name() - : BadgeBackpackExpireTypeEnum.PERMANENT.name(); - - // 更新:确保useProps=true 且更新过期时间 - boolean updateCount = badgeBackpackService.update() - .set(BadgeBackpack::getUseProps, Boolean.TRUE) - .set(BadgeBackpack::getExpireTime, newExpireTime) - .set(BadgeBackpack::getExpireType, expireType) - .eq(BadgeBackpack::getId, existBadge.getId()) - .execute(); - - if (updateCount) { - log.info("更新已存在的徽章, userId: {}, badgeId: {}, days: {}", userId, badgeId, days); - userProfileGateway.removeCacheAll(userId); - } else { - log.warn("更新徽章失败, userId: {}, badgeId: {}", userId, badgeId); - } - } else { - // 不存在则插入 + log.warn("用户已拥有该徽章, 跳过发放, userId: {}, badgeId: {}", userId, badgeId); + return null; + } + + BadgeBackpack badgeBackpack = badgeBackpackService.getByUserIdByBadgeId(userId, badgeId); + if (Objects.isNull(badgeBackpack)) { badgeBackpackService.save(buildSaveUserBadgeBackpack(badgeId, userId, days)); - log.info("新增徽章, userId: {}, badgeId: {}, days: {}", userId, badgeId, days); userProfileGateway.removeCacheAll(userId); } - - return null; + return null; }); } @@ -161,18 +144,8 @@ public class BadgeOperateListener implements MessageListener { Set saveWear = wearBadge.stream().map(UserUseBadgeDTO::getId) .filter(id -> !badgeId.equals(id)).collect(Collectors.toSet()); userRunProfileService.updateUserWearBadgesOverlay(userId, - userBadgeCommon.listUseBadge(userId, userProfile.getSysOriginChild(), saveWear)); - } - - /** - * 计算过期时间. - */ - private Timestamp calculateExpireTime(Long days) { - if (days != null && days > 0) { - return TimestampUtils.nowPlusDays(days.intValue()); - } else { - return TimestampUtils.now(); - } + userBadgeCommon.listUseBadge(userId, userProfile.getSysOriginChild(), saveWear) + ); } private BadgeBackpack buildSaveUserBadgeBackpack(Long badgeId, Long userId, Long days) {