From 4d0cb9aea3290fd3f00793f6c7b02cad7a4357db Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 5 Dec 2025 14:05:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B4=BB=E5=8A=A8=E6=A0=A1=E9=AA=8C=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=20=E6=96=B0=E5=A2=9E=E5=B9=B8=E8=BF=90?= =?UTF-8?q?=E7=A4=BC=E7=89=A9=E6=89=93=E6=A6=9C=E6=B4=BB=E5=8A=A8=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/strategy/RankCountStrategy.java | 38 +++++++++++-------- .../domain/ranking/RankingActivityType.java | 16 ++++++++ .../activity/RankingActivityRecord.java | 2 +- .../gateway/RankingActivityGatewayImpl.java | 26 +++++++++---- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java index 82fae59b..07bf74d6 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/gift/strategy/RankCountStrategy.java @@ -9,7 +9,6 @@ import com.red.circle.other.app.dto.cmd.activity.RankingDataUpdateCmd; import com.red.circle.other.app.service.activity.RankingActivityService; import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway; -import com.red.circle.other.domain.model.user.UserProfile; import com.red.circle.other.domain.ranking.RankingActivityType; import com.red.circle.other.domain.ranking.RankingCycleType; import com.red.circle.other.domain.ranking.RankingDimension; @@ -389,12 +388,6 @@ public class RankCountStrategy implements GiftStrategy { */ private void accumulateNewRankingData(GiftGiveRunningWater runningWater, boolean isLuckyGift, BigDecimal luckyGiftRatio) { try { - // 获取送礼用户信息 - UserProfile senderProfile = userProfileGateway.getByUserId(runningWater.getUserId()); - if (Objects.isNull(senderProfile)) { - log.warn("累计排行榜数据失败:未找到送礼用户信息, userId={}", runningWater.getUserId()); - return; - } // 先查询上架且进行中的活动 List ongoingActivities = activityConfigService.listOngoingActivities(runningWater.getSysOrigin()); @@ -411,18 +404,15 @@ public class RankCountStrategy implements GiftStrategy { // 遍历活动,找到对应的type再累计对应活动类型的数据 for (ActivityConfig activity : ongoingActivities) { - if (Objects.isNull(activity.getActivityType())) { - continue; - } - if (!activity.getGiftIds().contains(String.valueOf(runningWater.getGiftId()))) { - continue; - } + if (!matchActivityRule(runningWater, activity)) { + continue; + } // 将活动类型转换为RankingActivityType枚举 try { RankingActivityType activityType = RankingActivityType.valueOf(activity.getActivityType()); // 累计对应活动类型的数据 - getDataUpdateCmd(runningWater, senderProfile.getUserSex(), cycleKey, actualAmount, activityType); + getDataUpdateCmd(runningWater, cycleKey, actualAmount, activityType); } catch (IllegalArgumentException e) { log.warn("未识别的活动类型: {}, activityId={}", activity.getActivityType(), activity.getId()); } @@ -433,10 +423,26 @@ public class RankCountStrategy implements GiftStrategy { } } - private void getDataUpdateCmd(GiftGiveRunningWater runningWater, Integer userSex, String cycleKey, Long actualAmount, RankingActivityType activityType) { + private static boolean matchActivityRule(GiftGiveRunningWater runningWater, ActivityConfig activity) { + RankingActivityType activityType = RankingActivityType.getByConstantName(activity.getActivityType()); + + // 活动类型无效,不符合条件 + if (activityType == null) { + return false; + } + + if (RankingActivityType.LUCK_GIFT == activityType) { + return Boolean.TRUE.equals(runningWater.getLuckyGift()); + } + + // 默认规则 + return activity.getGiftIds().contains(String.valueOf(runningWater.getGiftId())); + } + + private void getDataUpdateCmd(GiftGiveRunningWater runningWater, String cycleKey, Long actualAmount, RankingActivityType activityType) { RankingDataUpdateCmd receiverCmd = new RankingDataUpdateCmd(); receiverCmd.setUserId(runningWater.getUserId()); - receiverCmd.setUserSex(userSex); + receiverCmd.setUserSex(1); receiverCmd.setActivityType(activityType.getCode()); receiverCmd.setCycleType(RankingCycleType.WEEKLY.getCode()); receiverCmd.setCycleKey(cycleKey); diff --git a/rc-service/rc-service-other/other-domain/src/main/java/com/red/circle/other/domain/ranking/RankingActivityType.java b/rc-service/rc-service-other/other-domain/src/main/java/com/red/circle/other/domain/ranking/RankingActivityType.java index f62db26e..48d759df 100644 --- a/rc-service/rc-service-other/other-domain/src/main/java/com/red/circle/other/domain/ranking/RankingActivityType.java +++ b/rc-service/rc-service-other/other-domain/src/main/java/com/red/circle/other/domain/ranking/RankingActivityType.java @@ -39,6 +39,8 @@ public enum RankingActivityType { ALIANQIU_DAY(7, "阿联酋国庆日", "统计送礼物消费排行"), GREEDY_GAME(1068, "GreedyLion", "游戏中奖排行"), + + LUCK_GIFT(9999, "LuckGift", "统计送幸运礼物排行"), ; /** @@ -71,6 +73,20 @@ public enum RankingActivityType { return null; } + /** + * 根据枚举常量名获取枚举 + */ + public static RankingActivityType getByConstantName(String constantName) { + if (constantName == null || constantName.isEmpty()) { + return null; + } + try { + return RankingActivityType.valueOf(constantName); + } catch (IllegalArgumentException e) { + return null; + } + } + /** * 判断是否是有效的活动类型 */ diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/RankingActivityRecord.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/RankingActivityRecord.java index 3037349c..77f97be8 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/RankingActivityRecord.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/entity/activity/RankingActivityRecord.java @@ -39,7 +39,7 @@ import java.sql.Timestamp; @FieldDefaults(level = AccessLevel.PRIVATE) @CompoundIndexes({ @CompoundIndex(name = "idx_ranking_query", - def = "{'sysOrigin': 1, 'activityType': 1, 'cycleType': 1, 'cycleKey': 1, 'userSex': 1, 'quantity': -1}"), + def = "{'sysOrigin': 1, 'userId': 1, 'activityType': 1, 'cycleKey': 1, 'deleted': 1}"), @CompoundIndex(name = "idx_user_ranking", def = "{'userId': 1, 'activityType': 1, 'cycleKey': 1}"), @CompoundIndex(name = "idx_expired", diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/RankingActivityGatewayImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/RankingActivityGatewayImpl.java index ace5e06f..8999bc41 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/RankingActivityGatewayImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/RankingActivityGatewayImpl.java @@ -7,7 +7,12 @@ import com.red.circle.other.infra.database.mongo.service.activity.RankingActivit import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.PageRequest; +import org.springframework.data.mongodb.core.FindAndModifyOptions; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.stereotype.Component; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; import java.sql.Timestamp; import java.util.List; @@ -22,27 +27,32 @@ import java.util.Optional; public class RankingActivityGatewayImpl implements RankingActivityGateway { private final RankingActivityRecordRepository repository; + private final MongoTemplate mongoTemplate; private static final Integer NOT_DELETED = 0; @Override public RankingActivityRecord accumulateRankingData(RankingActivityRecord record, Long incrementQuantity) { Timestamp now = new Timestamp(System.currentTimeMillis()); - + if (record.getId() == null) { - // 新记录 + // 新记录 - 直接插入,无并发问题 record.setQuantity(incrementQuantity); record.setCreateTime(now); record.setUpdateTime(now); record.setDeleted(NOT_DELETED); + return repository.save(record); } else { - // 累计更新 - Long currentQuantity = record.getQuantity() == null ? 0L : record.getQuantity(); - record.setQuantity(currentQuantity + incrementQuantity); - record.setUpdateTime(now); + // 已存在记录 - 使用原子更新 + Query query = new Query(Criteria.where("_id").is(record.getId())); + Update update = new Update() + .inc("quantity", incrementQuantity) + .set("updateTime", now); + + FindAndModifyOptions options = new FindAndModifyOptions() + .returnNew(true); // 返回更新后的文档 + return mongoTemplate.findAndModify(query, update, options, RankingActivityRecord.class); } - - return repository.save(record); } @Override