初版计算

This commit is contained in:
tianfeng 2025-09-12 10:31:16 +08:00
parent 02451e7a86
commit 4e27be35c2

View File

@ -324,43 +324,48 @@ public class DailyTask {
*/
private boolean isUserEligibleForGift(Long userId, LocalDateTime currentTime) {
try {
// 查询用户NOBLE_VIP类型且origin为PURCHASING_GOLD_PROPS的流水记录
List<RunningWaterUserProps> purchasingRecords = runningWaterUserPropsService.query()
// 查询用户NOBLE_VIP类型所有流水记录
List<RunningWaterUserProps> allRecords = runningWaterUserPropsService.query()
.eq(RunningWaterUserProps::getUserId, userId)
.eq(RunningWaterUserProps::getType, "NOBLE_VIP")
.eq(RunningWaterUserProps::getOrigin, "PURCHASING_GOLD_PROPS")
.orderByDesc(RunningWaterUserProps::getCreateTime)
.orderByAsc(RunningWaterUserProps::getCreateTime)
.list();
if (CollectionUtils.isEmpty(allRecords)) {
log.debug("用户 {} 没有NOBLE_VIP类型的流水记录不符合赠送条件", userId);
return false;
}
// 分离购买记录和扣除记录
List<RunningWaterUserProps> purchasingRecords = allRecords.stream()
.filter(record -> "PURCHASING_GOLD_PROPS".equals(record.getOrigin()))
.collect(Collectors.toList());
// 检查非PURCHASING_GOLD_PROPS类型的扣除天数记录
List<RunningWaterUserProps> deductionRecords = allRecords.stream()
.filter(record -> !"PURCHASING_GOLD_PROPS".equals(record.getOrigin()) && record.getDays() != null && record.getDays() < 0)
.collect(Collectors.toList());
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();
// 计算累计有效天数
LocalDateTime earliestValidTime = calculateEffectiveValidTime(purchasingRecords, deductionRecords, currentTime);
// 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;
}
}
if (earliestValidTime == null) {
log.debug("用户 {} 没有有效的购买记录或所有记录都已过期,不符合赠送条件", userId);
return false;
}
log.debug("用户 {} 所有购买记录都已过期,不符合赠送条件", userId);
return false;
// 判断当前时间是否在有效期内
boolean isValid = currentTime.isBefore(earliestValidTime) || currentTime.isEqual(earliestValidTime);
log.debug("用户 {} 赠送条件检查结果: {}, 当前时间: {}, 有效期截止时间: {}",
userId, isValid ? "符合" : "不符合", currentTime, earliestValidTime);
return isValid;
} catch (Exception e) {
log.error("检查用户 {} 赠送资格时发生异常: {}", userId, e.getMessage(), e);
@ -368,6 +373,73 @@ public class DailyTask {
}
}
/**
* 计算用户的有效期截止时间
* 根据购买记录和扣除记录结合时间顺序累计计算有效期
*/
private LocalDateTime calculateEffectiveValidTime(List<RunningWaterUserProps> purchasingRecords,
List<RunningWaterUserProps> deductionRecords,
LocalDateTime currentTime) {
// 所有记录按时间排序
purchasingRecords.sort(Comparator.comparing(RunningWaterUserProps::getCreateTime));
deductionRecords.sort(Comparator.comparing(RunningWaterUserProps::getCreateTime));
LocalDateTime currentValidEndTime = null;
boolean hasValidRecord = false;
// 先处理所有购买记录
for (RunningWaterUserProps record : purchasingRecords) {
LocalDateTime recordCreateTime = record.getCreateTime().toLocalDateTime();
Integer days = record.getDays();
if (days != null && days > 0) {
if (currentValidEndTime == null || recordCreateTime.isAfter(currentValidEndTime)) {
// 如果当前没有有效期或购买时间在当前有效期之后从购买时间开始计算
currentValidEndTime = recordCreateTime.plusDays(days);
} else {
// 如果购买时间在当前有效期内在当前有效期基础上延长
currentValidEndTime = currentValidEndTime.plusDays(days);
}
hasValidRecord = true;
log.debug("用户购买记录增加天数: {}, 购买时间: {}, 累计有效期至: {}", days, recordCreateTime, currentValidEndTime);
}
}
// 再处理所有扣除记录
for (RunningWaterUserProps record : deductionRecords) {
LocalDateTime recordCreateTime = record.getCreateTime().toLocalDateTime();
Integer days = record.getDays();
if (days == null || days >= 0) {
log.warn("扣除天数无效:{}", days);
continue;
}
if (currentValidEndTime == null) {
log.warn("无有效天数:{}", currentValidEndTime);
continue;
}
if (recordCreateTime.isBefore(currentValidEndTime)) {
// 只有在有有效期且扣除时间在有效期内才能扣除
LocalDateTime originalValidEndTime = currentValidEndTime;
currentValidEndTime = currentValidEndTime.plusDays(days); // days是负数相当于减少天数
log.debug("用户扣除记录减少天数: {}, 扣除时间: {}, 原有效期: {}, 调整后有效期: {}",
days, recordCreateTime, originalValidEndTime, currentValidEndTime);
// 如果扣除后有效期已经过了当前时间或扣除时间之前则无效
if (currentValidEndTime.isBefore(currentTime) || currentValidEndTime.isBefore(recordCreateTime)) {
currentValidEndTime = null;
hasValidRecord = false;
log.debug("扣除后有效期已过期或无效,重置有效期");
}
}
}
return hasValidRecord ? currentValidEndTime : null;
}
private void sendFanVotesReward(String code, String sysOrigin) {
List<RoomFanVotesActivityCount> votesCounts = roomFanVotesActivityCountService