活动校验处理, 新增幸运礼物打榜活动类型
This commit is contained in:
parent
afc1b6a168
commit
4d0cb9aea3
@ -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<ActivityConfig> 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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是有效的活动类型
|
||||
*/
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user