更新进度新增redis
This commit is contained in:
parent
de802fb8eb
commit
4d725763a6
@ -1,5 +1,7 @@
|
||||
package com.red.circle.other.app.command.task;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.red.circle.component.redis.service.RedisService;
|
||||
import com.red.circle.other.app.dto.cmd.task.RoomDailyTaskProgressUpdateCmd;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.RoomDailyTaskConfig;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.RoomDailyTaskProgress;
|
||||
@ -15,6 +17,7 @@ import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 更新每日任务进度
|
||||
@ -24,7 +27,12 @@ import java.util.List;
|
||||
@RequiredArgsConstructor
|
||||
public class RoomDailyTaskProgressUpdateExe {
|
||||
|
||||
private static final String CACHE_KEY_CONFIG = "room:daily:task:config";
|
||||
private static final String CACHE_KEY_TIERS = "room:daily:task:tiers";
|
||||
private static final long CACHE_TTL_HOURS = 24;
|
||||
|
||||
private final RoomDailyTaskDatabaseService taskDatabaseService;
|
||||
private final RedisService redisService;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void execute(RoomDailyTaskProgressUpdateCmd cmd) {
|
||||
@ -32,13 +40,13 @@ public class RoomDailyTaskProgressUpdateExe {
|
||||
String taskCode = cmd.getTaskCode();
|
||||
Integer progressValue = cmd.getProgressValue();
|
||||
|
||||
RoomDailyTaskConfig config = taskDatabaseService.getConfigByCode(taskCode);
|
||||
RoomDailyTaskConfig config = getConfigFromCache(taskCode);
|
||||
if (config == null) {
|
||||
log.warn("任务配置不存在或已下线, taskCode={}", taskCode);
|
||||
return;
|
||||
}
|
||||
|
||||
List<RoomDailyTaskTier> tiers = taskDatabaseService.listTiersByTaskCodes(Collections.singletonList(taskCode));
|
||||
List<RoomDailyTaskTier> tiers = getTiersFromCache(taskCode);
|
||||
if (tiers.isEmpty()) {
|
||||
log.warn("任务档位未配置, taskCode={}", taskCode);
|
||||
return;
|
||||
@ -60,7 +68,6 @@ public class RoomDailyTaskProgressUpdateExe {
|
||||
progress.setCreateTime(LocalDateTime.now());
|
||||
}
|
||||
|
||||
// 所有档位均已领取,不再更新
|
||||
if (progress.getMaxTierClaimed() >= tiers.size()) {
|
||||
log.info("所有档位已领取,无需更新进度, userId={}, taskCode={}", userId, taskCode);
|
||||
return;
|
||||
@ -68,7 +75,6 @@ public class RoomDailyTaskProgressUpdateExe {
|
||||
|
||||
progress.setCurrentValue(progressValue);
|
||||
|
||||
// 计算当前达到的最高档位
|
||||
int maxTierCompleted = 0;
|
||||
for (RoomDailyTaskTier tier : tiers) {
|
||||
if (progressValue >= tier.getTargetValue()) {
|
||||
@ -82,4 +88,28 @@ public class RoomDailyTaskProgressUpdateExe {
|
||||
|
||||
log.info("任务进度更新成功, userId={}, taskCode={}, progressValue={}, maxTierCompleted={}", userId, taskCode, progressValue, maxTierCompleted);
|
||||
}
|
||||
|
||||
private RoomDailyTaskConfig getConfigFromCache(String taskCode) {
|
||||
RoomDailyTaskConfig config = redisService.hashGet(CACHE_KEY_CONFIG, taskCode, RoomDailyTaskConfig.class);
|
||||
if (config != null) {
|
||||
return config;
|
||||
}
|
||||
config = taskDatabaseService.getConfigByCode(taskCode);
|
||||
if (config != null) {
|
||||
redisService.hashPut(CACHE_KEY_CONFIG, taskCode, config, CACHE_TTL_HOURS, TimeUnit.HOURS);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private List<RoomDailyTaskTier> getTiersFromCache(String taskCode) {
|
||||
List<RoomDailyTaskTier> tiers = redisService.hashGet(CACHE_KEY_TIERS, taskCode, new TypeReference<>() {});
|
||||
if (tiers != null) {
|
||||
return tiers;
|
||||
}
|
||||
tiers = taskDatabaseService.listTiersByTaskCodes(Collections.singletonList(taskCode));
|
||||
if (!tiers.isEmpty()) {
|
||||
redisService.hashPut(CACHE_KEY_TIERS, taskCode, tiers, CACHE_TTL_HOURS, TimeUnit.HOURS);
|
||||
}
|
||||
return tiers;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user