feat(任务): 每日赠送金币定时任务完善

This commit is contained in:
tianfeng 2025-09-11 21:07:36 +08:00
parent 900998ed36
commit 3a7031f1e8

View File

@ -12,6 +12,7 @@ import com.red.circle.other.infra.common.activity.PropsActivitySendCommon;
import com.red.circle.other.infra.common.activity.send.SendRewardGroup;
import com.red.circle.other.infra.database.mongo.entity.activity.RoomFanVotesActivityCount;
import com.red.circle.other.infra.database.mongo.service.activity.RoomFanVotesActivityCountService;
import com.red.circle.other.infra.database.rds.dao.props.RunningWaterUserPropsDAO;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRuleConfig;
import com.red.circle.other.infra.database.rds.entity.props.PropsBackpack;
import com.red.circle.other.infra.database.rds.entity.props.PropsNobleVipAbility;
@ -28,9 +29,11 @@ import com.red.circle.other.inner.model.dto.material.props.PropsStoreCommodity;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.google.common.collect.Lists;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.red.circle.tool.core.date.LocalDateTimeUtils;
import com.red.circle.tool.core.tuple.ImmutableKeyValuePair;
@ -61,6 +64,7 @@ public class DailyTask {
private final FamilyDailyTaskTriggerRecordService familyDailyTaskTriggerRecordService;
private final PropsStoreGateway propsStoreGateway;
private final RunningWaterUserPropsService runningWaterUserPropsService;
private final RunningWaterUserPropsDAO runningWaterUserPropsDAO;
private final PropsBackpackService propsBackpackService;
private final WalletGoldClient walletGoldClient;
private final PropsPurchasingCmdExe propsPurchasingCmdExe;
@ -214,7 +218,7 @@ public class DailyTask {
/**
* 贵族每日金币奖励发放 - 沙特时间每日0点执行.
*/
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Riyadh")
@Scheduled(cron = "0 * * * * *", zone = "Asia/Riyadh")
@TaskCacheLock(key = "NOBLE_DAILY_GOLD_REWARD_TASK", expireSecond = 86000 )
public void nobleDailyGoldRewardTask() {
long startTime = System.currentTimeMillis();
@ -238,17 +242,19 @@ public class DailyTask {
commodityList.forEach(propsStoreCommodity -> {
List<PropsBackpack> propsBackpackList = propsBackpackService.listUsersByNobleVipAndPropsId(PropsCommodityType.NOBLE_VIP, propsStoreCommodity.getPropsResources().getId());
log.info("VIP用户: {}", JSON.toJSONString(propsStoreCommodity));
log.info("VIP用户: {}", JSON.toJSONString(propsBackpackList));
List<Long> userIds = propsBackpackList.stream().map(e -> {
RunningWaterUserProps runningWaterUserProps = runningWaterUserPropsService.selectRunningWaterUserProps(e.getType(), e.getUserId(), e.getPropsId(),
LocalDateTimeUtils.format(e.getCreateTime().toLocalDateTime(), "yyyy-MM-dd HH:mm"));
return runningWaterUserProps == null ? e.getUserId() : null;
}).filter(Objects::nonNull).toList();
log.info("userIds: {}", JSON.toJSONString(userIds));
// 提取用户ID列表
List<Long> allUsers = propsBackpackList.stream()
.map(PropsBackpack::getUserId)
.distinct()
.collect(Collectors.toList());
// 查询用户是否处于可赠送金币区间
List<Long> eligibleUsers = getEligibleGiftUsers(allUsers);
// 给每个用户发送奖励排除已处理的用户
for (Long userId : userIds) {
for (Long userId : eligibleUsers) {
if (!processedUsers.contains(userId)) {
ResultResponse<WalletReceiptResDTO> res = walletGoldClient.changeBalance(GoldReceiptCmd.builder()
.appIncome()
@ -288,6 +294,80 @@ public class DailyTask {
log.info("exec noble_daily_gold_reward_task end with {}", System.currentTimeMillis() - startTime);
}
/**
* 查询用户是否处于可赠送金币区间
* 规则
* 1. 查询NOBLE_VIP的origin为PURCHASING_GOLD_PROPS类型的按照创建时间加天数计算
* 2. 如果当前时间在范围内则为有效赠送用户
* 3. 如果没有PURCHASING_GOLD_PROPS类型则其余不赠送
*/
private List<Long> getEligibleGiftUsers(List<Long> allUsers) {
if (CollectionUtils.isEmpty(allUsers)) {
return Lists.newArrayList();
}
List<Long> eligibleUsers = Lists.newArrayList();
LocalDateTime now = LocalDateTime.now();
for (Long userId : allUsers) {
if (isUserEligibleForGift(userId, now)) {
eligibleUsers.add(userId);
}
}
log.info("所有贵族用户数: {}, 符合赠送条件的用户数: {}", allUsers.size(), eligibleUsers.size());
return eligibleUsers;
}
/**
* 判断单个用户是否符合赠送条件
*/
private boolean isUserEligibleForGift(Long userId, LocalDateTime currentTime) {
try {
// 查询用户NOBLE_VIP类型且origin为PURCHASING_GOLD_PROPS的流水记录
List<RunningWaterUserProps> purchasingRecords = runningWaterUserPropsService.query()
.eq(RunningWaterUserProps::getUserId, userId)
.eq(RunningWaterUserProps::getType, "NOBLE_VIP")
.eq(RunningWaterUserProps::getOrigin, "PURCHASING_GOLD_PROPS")
.orderByDesc(RunningWaterUserProps::getCreateTime)
.list();
if (CollectionUtils.isEmpty(purchasingRecords)) {
log.debug("用户 {} 没有PURCHASING_GOLD_PROPS类型的流水记录不符合赠送条件", userId);
return false;
}
// 检查是否有有效的购买记录在有效期内
for (RunningWaterUserProps record : purchasingRecords) {
LocalDateTime createTime = record.getCreateTime().toLocalDateTime();
Integer days = record.getDays();
// days为-1表示永久有效
if (days != null && days == -1) {
log.debug("用户 {} 有永久有效的购买记录,符合赠送条件", userId);
return true;
}
// 计算有效期
if (days != null && days > 0) {
LocalDateTime expireTime = createTime.plusDays(days);
if (currentTime.isBefore(expireTime) || currentTime.isEqual(expireTime)) {
log.debug("用户 {} 购买记录在有效期内,符合赠送条件。购买时间: {}, 有效天数: {}, 过期时间: {}",
userId, createTime, days, expireTime);
return true;
}
}
}
log.debug("用户 {} 所有购买记录都已过期,不符合赠送条件", userId);
return false;
} catch (Exception e) {
log.error("检查用户 {} 赠送资格时发生异常: {}", userId, e.getMessage(), e);
return false;
}
}
private void sendFanVotesReward(String code, String sysOrigin) {
List<RoomFanVotesActivityCount> votesCounts = roomFanVotesActivityCountService