diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/RankingRewardGrantCmdExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/RankingRewardGrantCmdExe.java index da3ba62e..eb3c769a 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/RankingRewardGrantCmdExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/activity/RankingRewardGrantCmdExe.java @@ -90,22 +90,54 @@ public class RankingRewardGrantCmdExe { Integer currentRank = currentUser.getRank(); RankingRewardSnapshot.RankingUserSnapshot lastUser = lastUserMap.get(userId); - + List currentRewards = getRewardsForRank(currentRank, activityConfig); if (lastUser == null) { log.info("用户新获得排名,发放奖励, userId: {}, rank: {}", userId, currentRank); - List rewards = getRewardsForRank(currentRank, activityConfig); - grantRewards(userId, rewards); - + grantRewards(userId, currentRewards); } else if (!Objects.equals(lastUser.getRank(), currentRank)) { log.info("用户排名变化, userId: {}, 旧排名: {}, 新排名: {}", userId, lastUser.getRank(), currentRank); - revokeRewards(userId, lastUser.getRewards()); - List rewards = getRewardsForRank(currentRank, activityConfig); - grantRewards(userId, rewards); + processRewardDiff(userId, lastUser.getRewards(), currentRewards); } } } + /** + * 处理奖励差异 + */ + private void processRewardDiff(Long userId, + List oldRewards, + List newRewards) { + + Set oldRewardKeys = oldRewards.stream() + .map(r -> r.getType() + ":" + r.getContent()) + .collect(Collectors.toSet()); + + Set newRewardKeys = newRewards.stream() + .map(r -> r.getType() + ":" + r.getContent()) + .collect(Collectors.toSet()); + + List toRevoke = oldRewards.stream() + .filter(r -> !newRewardKeys.contains(r.getType() + ":" + r.getContent())) + .collect(Collectors.toList()); + + List toGrant = newRewards.stream() + .filter(r -> !oldRewardKeys.contains(r.getType() + ":" + r.getContent())) + .collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(toGrant)) { + grantRewards(userId, toGrant); + } + + if (CollectionUtils.isNotEmpty(toRevoke)) { + revokeRewards(userId, toRevoke); + } + + if (CollectionUtils.isEmpty(toGrant) && CollectionUtils.isEmpty(toRevoke)) { + log.info("奖励未发生变化, userId: {}", userId); + } + } + /** * 发放奖励. */