新增开斋节活动数据处理
This commit is contained in:
parent
0c1ee4b252
commit
9fc19ebe6c
@ -76,6 +76,35 @@ public class UserActivityRechargeClientController implements UserActivityRecharg
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_RECHARGE_1")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_RECHARGE_10")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_RECHARGE_50")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_RECHARGE_100")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
|
||||
log.info("Spins充值任务处理成功, userId={}, totalAmount={}", userId, totalAmount);
|
||||
} catch (Exception e) {
|
||||
log.error("处理Spins充值任务失败, userId={}, amount={}", userId, amount, e);
|
||||
@ -90,7 +119,7 @@ public class UserActivityRechargeClientController implements UserActivityRecharg
|
||||
*/
|
||||
private BigDecimal incrementDailyRechargeAmount(Long userId, BigDecimal amount) {
|
||||
String redisKey = "spins:daily:recharge:amount:" + userId;
|
||||
|
||||
|
||||
// 转换为分(避免浮点数精度问题)
|
||||
long amountInCents = amount.multiply(new BigDecimal("100")).longValue();
|
||||
|
||||
|
||||
@ -1,161 +0,0 @@
|
||||
package com.red.circle.other.app.command;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsTaskConfigDAO;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsUserTaskProgressDAO;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsTaskConfig;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsUserTaskProgress;
|
||||
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 增加任务进度执行器
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-01-21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SpinsTaskProgressIncrementExe {
|
||||
|
||||
private final SpinsTaskConfigDAO spinsTaskConfigDAO;
|
||||
private final SpinsUserTaskProgressDAO spinsUserTaskProgressDAO;
|
||||
|
||||
/**
|
||||
* 执行增加任务进度
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param taskCode 任务编码
|
||||
* @param incrementValue 增加值
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void execute(Long userId, String taskCode, Integer incrementValue) {
|
||||
// 1. 查询任务配置
|
||||
List<SpinsTaskConfig> taskConfigs = spinsTaskConfigDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsTaskConfig>()
|
||||
.eq(SpinsTaskConfig::getTaskCode, taskCode)
|
||||
.eq(SpinsTaskConfig::getStatus, 1)
|
||||
);
|
||||
|
||||
if (taskConfigs.isEmpty()) {
|
||||
log.warn("任务配置不存在或已下线, taskCode={}", taskCode);
|
||||
return;
|
||||
}
|
||||
|
||||
SpinsTaskConfig taskConfig = taskConfigs.get(0);
|
||||
|
||||
// 2. 获取或创建用户任务进度
|
||||
String cycleKey = getCurrentCycleKey(taskConfig.getCycleType());
|
||||
SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey);
|
||||
|
||||
// 3. 校验任务状态,如果已完成或已领取,则不更新
|
||||
if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) {
|
||||
log.info("任务已完成,无需增加进度, userId={}, taskCode={}, taskStatus={}",
|
||||
userId, taskCode, progress.getTaskStatus());
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 增加进度值
|
||||
int newValue = progress.getCurrentValue() + incrementValue;
|
||||
progress.setCurrentValue(newValue);
|
||||
|
||||
// 5. 计算进度百分比
|
||||
BigDecimal progressRate = calculateProgressRate(newValue, taskConfig.getTargetValue());
|
||||
progress.setProgressRate(progressRate);
|
||||
|
||||
// 6. 检查是否完成
|
||||
if (newValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) {
|
||||
progress.setTaskStatus(1);
|
||||
progress.setCompleteTime(new Timestamp(System.currentTimeMillis()));
|
||||
progress.setCompleteTimes(progress.getCompleteTimes() + 1);
|
||||
log.info("任务完成, userId={}, taskCode={}, newValue={}", userId, taskCode, newValue);
|
||||
}
|
||||
|
||||
progress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
|
||||
spinsUserTaskProgressDAO.updateById(progress);
|
||||
|
||||
log.info("任务进度增加成功, userId={}, taskCode={}, incrementValue={}, newValue={}",
|
||||
userId, taskCode, incrementValue, newValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建用户任务进度
|
||||
*/
|
||||
private SpinsUserTaskProgress getOrCreateProgress(Long userId, SpinsTaskConfig taskConfig, String cycleKey) {
|
||||
List<SpinsUserTaskProgress> progressList = spinsUserTaskProgressDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsUserTaskProgress>()
|
||||
.eq(SpinsUserTaskProgress::getUserId, userId)
|
||||
.eq(SpinsUserTaskProgress::getTaskCode, taskConfig.getTaskCode())
|
||||
.eq(SpinsUserTaskProgress::getCycleKey, cycleKey)
|
||||
);
|
||||
SpinsUserTaskProgress progress = null;
|
||||
if (!progressList.isEmpty()) {
|
||||
progress = progressList.get(0);
|
||||
}
|
||||
|
||||
if (progress == null) {
|
||||
progress = new SpinsUserTaskProgress();
|
||||
progress.setUserId(userId);
|
||||
progress.setTaskId(taskConfig.getId());
|
||||
progress.setTaskCode(taskConfig.getTaskCode());
|
||||
progress.setCurrentValue(0);
|
||||
progress.setTargetValue(taskConfig.getTargetValue());
|
||||
progress.setProgressRate(BigDecimal.ZERO);
|
||||
progress.setTaskStatus(0);
|
||||
progress.setCycleType(taskConfig.getCycleType());
|
||||
progress.setCycleDate(LocalDate.now());
|
||||
progress.setCycleKey(cycleKey);
|
||||
progress.setCompleteTimes(0);
|
||||
progress.setRewardType(taskConfig.getRewardType());
|
||||
progress.setRewardValue(taskConfig.getRewardValue());
|
||||
progress.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
||||
progress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
|
||||
spinsUserTaskProgressDAO.insert(progress);
|
||||
}
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算进度百分比
|
||||
*/
|
||||
private BigDecimal calculateProgressRate(Integer currentValue, Integer targetValue) {
|
||||
if (targetValue == null || targetValue == 0) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
return BigDecimal.valueOf(currentValue)
|
||||
.divide(BigDecimal.valueOf(targetValue), 4, RoundingMode.HALF_UP)
|
||||
.multiply(BigDecimal.valueOf(100))
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前周期key
|
||||
*/
|
||||
private String getCurrentCycleKey(Integer cycleType) {
|
||||
LocalDate now = ZonedDateTimeAsiaRiyadhUtils.now().toLocalDate();
|
||||
if (cycleType == 1) {
|
||||
// 每日任务:格式 yyyyMMdd
|
||||
return now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
} else if (cycleType == 2) {
|
||||
// 每周任务:格式 yyyyWw(例如:2025W03)
|
||||
int weekOfYear = now.getDayOfYear() / 7 + 1;
|
||||
return now.getYear() + "W" + String.format("%02d", weekOfYear);
|
||||
} else {
|
||||
// 一次性任务:固定标识
|
||||
return "ONCE";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,111 +0,0 @@
|
||||
package com.red.circle.other.app.command;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsTaskConfigDAO;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsUserTaskProgressDAO;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsTaskConfig;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsUserTaskProgress;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 初始化任务进度执行器
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-01-21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SpinsTaskProgressInitExe {
|
||||
|
||||
private final SpinsTaskConfigDAO spinsTaskConfigDAO;
|
||||
private final SpinsUserTaskProgressDAO spinsUserTaskProgressDAO;
|
||||
|
||||
/**
|
||||
* 执行初始化用户任务进度
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param taskCode 任务编码
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void execute(Long userId, String taskCode) {
|
||||
// 1. 查询任务配置
|
||||
List<SpinsTaskConfig> taskConfigs = spinsTaskConfigDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsTaskConfig>()
|
||||
.eq(SpinsTaskConfig::getTaskCode, taskCode)
|
||||
.eq(SpinsTaskConfig::getStatus, 1)
|
||||
);
|
||||
|
||||
if (taskConfigs.isEmpty()) {
|
||||
log.warn("任务配置不存在或已下线, taskCode={}", taskCode);
|
||||
return;
|
||||
}
|
||||
SpinsTaskConfig taskConfig = taskConfigs.get(0);
|
||||
|
||||
// 2. 获取当前周期key
|
||||
String cycleKey = getCurrentCycleKey(taskConfig.getCycleType());
|
||||
|
||||
// 3. 检查是否已存在
|
||||
List<SpinsUserTaskProgress> existProgressList = spinsUserTaskProgressDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsUserTaskProgress>()
|
||||
.eq(SpinsUserTaskProgress::getUserId, userId)
|
||||
.eq(SpinsUserTaskProgress::getTaskCode, taskCode)
|
||||
.eq(SpinsUserTaskProgress::getCycleKey, cycleKey)
|
||||
);
|
||||
|
||||
if (!existProgressList.isEmpty()) {
|
||||
log.info("任务进度已存在, userId={}, taskCode={}", userId, taskCode);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 创建新的任务进度
|
||||
SpinsUserTaskProgress progress = new SpinsUserTaskProgress();
|
||||
progress.setUserId(userId);
|
||||
progress.setTaskId(taskConfig.getId());
|
||||
progress.setTaskCode(taskConfig.getTaskCode());
|
||||
progress.setCurrentValue(0);
|
||||
progress.setTargetValue(taskConfig.getTargetValue());
|
||||
progress.setProgressRate(BigDecimal.ZERO);
|
||||
progress.setTaskStatus(0);
|
||||
progress.setCycleType(taskConfig.getCycleType());
|
||||
progress.setCycleDate(LocalDate.now());
|
||||
progress.setCycleKey(cycleKey);
|
||||
progress.setCompleteTimes(0);
|
||||
progress.setRewardType(taskConfig.getRewardType());
|
||||
progress.setRewardValue(taskConfig.getRewardValue());
|
||||
progress.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
||||
progress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
spinsUserTaskProgressDAO.insert(progress);
|
||||
|
||||
log.info("初始化用户任务进度成功, userId={}, taskCode={}", userId, taskCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前周期key
|
||||
*/
|
||||
private String getCurrentCycleKey(Integer cycleType) {
|
||||
LocalDate now = LocalDate.now();
|
||||
if (cycleType == 1) {
|
||||
// 每日任务:格式 yyyyMMdd
|
||||
return now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
} else if (cycleType == 2) {
|
||||
// 每周任务:格式 yyyyWw(例如:2025W03)
|
||||
int weekOfYear = now.getDayOfYear() / 7 + 1;
|
||||
return now.getYear() + "W" + String.format("%02d", weekOfYear);
|
||||
} else {
|
||||
// 一次性任务:固定标识
|
||||
return "ONCE";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
package com.red.circle.other.app.command;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsUserTaskProgressDAO;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsUserTaskProgress;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 重置任务进度执行器
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-01-21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SpinsTaskProgressResetExe {
|
||||
|
||||
private final SpinsUserTaskProgressDAO spinsUserTaskProgressDAO;
|
||||
|
||||
/**
|
||||
* 执行重置用户每日任务
|
||||
*
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void execute(Long userId) {
|
||||
// 获取昨天的周期key
|
||||
String yesterdayCycleKey = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
|
||||
// 查询昨天的每日任务进度
|
||||
List<SpinsUserTaskProgress> progressList = spinsUserTaskProgressDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsUserTaskProgress>()
|
||||
.eq(SpinsUserTaskProgress::getUserId, userId)
|
||||
.eq(SpinsUserTaskProgress::getCycleType, 1) // 每日任务
|
||||
.eq(SpinsUserTaskProgress::getCycleKey, yesterdayCycleKey)
|
||||
);
|
||||
|
||||
if (progressList.isEmpty()) {
|
||||
log.info("没有需要重置的每日任务, userId={}", userId);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("重置用户每日任务成功, userId={}, 重置任务数={}", userId, progressList.size());
|
||||
}
|
||||
}
|
||||
@ -1,38 +1,47 @@
|
||||
package com.red.circle.other.app.command;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.red.circle.component.redis.service.RedisService;
|
||||
import com.red.circle.other.app.dto.cmd.SpinsTaskProgressUpdateCmd;
|
||||
import com.red.circle.other.infra.database.cache.key.SpinsTaskKeys;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsTaskConfigDAO;
|
||||
import com.red.circle.other.infra.database.rds.dao.task.SpinsUserTaskProgressDAO;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsTaskConfig;
|
||||
import com.red.circle.other.infra.database.rds.entity.task.SpinsUserTaskProgress;
|
||||
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
|
||||
import com.red.circle.tool.core.json.JacksonUtils;
|
||||
import com.red.circle.tool.core.text.StringUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 更新任务进度执行器
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-01-21
|
||||
* @author tf
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SpinsTaskProgressUpdateExe {
|
||||
|
||||
private static final long TASK_CONFIG_CACHE_SECONDS = 3600L * 24;
|
||||
|
||||
private final SpinsTaskConfigDAO spinsTaskConfigDAO;
|
||||
private final SpinsUserTaskProgressDAO spinsUserTaskProgressDAO;
|
||||
private final RedisService redisService;
|
||||
|
||||
/**
|
||||
* 执行更新任务进度
|
||||
@ -45,95 +54,117 @@ public class SpinsTaskProgressUpdateExe {
|
||||
String taskCode = cmd.getTaskCode();
|
||||
Integer progressValue = cmd.getProgressValue();
|
||||
|
||||
// 1. 查询任务配置
|
||||
List<SpinsTaskConfig> taskConfigs = spinsTaskConfigDAO.selectList(
|
||||
// 1. 查询任务配置(带缓存)
|
||||
SpinsTaskConfig taskConfig = getTaskConfigWithCache(taskCode);
|
||||
if (taskConfig == null) {
|
||||
log.warn("任务配置不存在或已下线, taskCode={}", taskCode);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 获取或创建用户任务进度
|
||||
String cycleKey = getCurrentCycleKey(taskConfig.getCycleType());
|
||||
SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey);
|
||||
|
||||
// 3. 任务已完成/已领取,跳过
|
||||
if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 进度未推进,跳过(防乱序回退)
|
||||
if (progressValue <= progress.getCurrentValue()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 5. 更新进度
|
||||
progress.setCurrentValue(progressValue);
|
||||
progress.setProgressRate(calculateProgressRate(progressValue, taskConfig.getTargetValue()));
|
||||
progress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
if (progressValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) {
|
||||
progress.setTaskStatus(1);
|
||||
progress.setCompleteTime(new Timestamp(System.currentTimeMillis()));
|
||||
progress.setCompleteTimes(progress.getCompleteTimes() + 1);
|
||||
log.info("任务完成, userId={}, taskCode={}", userId, taskCode);
|
||||
}
|
||||
|
||||
spinsUserTaskProgressDAO.updateById(progress);
|
||||
}
|
||||
|
||||
|
||||
// ---- private ----
|
||||
|
||||
private SpinsTaskConfig getTaskConfigWithCache(String taskCode) {
|
||||
String cacheKey = SpinsTaskKeys.SPINS_TASK_CONFIG.getKey(taskCode);
|
||||
String cached = redisService.getString(cacheKey);
|
||||
if (StringUtils.isNotBlank(cached)) {
|
||||
return JacksonUtils.readValue(cached, new TypeReference<>() {});
|
||||
}
|
||||
|
||||
List<SpinsTaskConfig> list = spinsTaskConfigDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsTaskConfig>()
|
||||
.eq(SpinsTaskConfig::getTaskCode, taskCode)
|
||||
.eq(SpinsTaskConfig::getStatus, 1)
|
||||
.last("limit 1")
|
||||
);
|
||||
|
||||
if (taskConfigs.isEmpty()) {
|
||||
log.warn("任务配置不存在或已下线, taskCode={}", taskCode);
|
||||
return;
|
||||
}
|
||||
SpinsTaskConfig taskConfig = taskConfigs.get(0);
|
||||
|
||||
// 2. 获取或创建用户任务进度
|
||||
String cycleKey = getCurrentCycleKey(taskConfig.getCycleType());
|
||||
SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey);
|
||||
|
||||
// 3. 校验任务状态,如果已完成或已领取,则不更新
|
||||
if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) {
|
||||
log.info("任务已完成,无需更新进度, userId={}, taskCode={}, taskStatus={}",
|
||||
userId, taskCode, progress.getTaskStatus());
|
||||
return;
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 4. 更新进度值
|
||||
progress.setCurrentValue(progressValue);
|
||||
|
||||
// 5. 计算进度百分比
|
||||
BigDecimal progressRate = calculateProgressRate(progressValue, taskConfig.getTargetValue());
|
||||
progress.setProgressRate(progressRate);
|
||||
|
||||
// 6. 检查是否完成
|
||||
if (progressValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) {
|
||||
progress.setTaskStatus(1);
|
||||
progress.setCompleteTime(new Timestamp(System.currentTimeMillis()));
|
||||
progress.setCompleteTimes(progress.getCompleteTimes() + 1);
|
||||
log.info("任务完成, userId={}, taskCode={}, progressValue={}", userId, taskCode, progressValue);
|
||||
}
|
||||
|
||||
progress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
|
||||
spinsUserTaskProgressDAO.updateById(progress);
|
||||
|
||||
log.info("任务进度更新成功, userId={}, taskCode={}, progressValue={}, progressRate={}",
|
||||
userId, taskCode, progressValue, progressRate);
|
||||
SpinsTaskConfig config = list.get(0);
|
||||
redisService.setString(cacheKey, JacksonUtils.toJson(config), TASK_CONFIG_CACHE_SECONDS, TimeUnit.SECONDS);
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建用户任务进度
|
||||
*/
|
||||
private SpinsUserTaskProgress getOrCreateProgress(Long userId, SpinsTaskConfig taskConfig, String cycleKey) {
|
||||
List<SpinsUserTaskProgress> progressList = spinsUserTaskProgressDAO.selectList(
|
||||
List<SpinsUserTaskProgress> list = spinsUserTaskProgressDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsUserTaskProgress>()
|
||||
.eq(SpinsUserTaskProgress::getUserId, userId)
|
||||
.eq(SpinsUserTaskProgress::getTaskCode, taskConfig.getTaskCode())
|
||||
.eq(SpinsUserTaskProgress::getCycleKey, cycleKey)
|
||||
);
|
||||
|
||||
SpinsUserTaskProgress progress = null;
|
||||
if (!progressList.isEmpty()) {
|
||||
progress = progressList.get(0);
|
||||
if (!list.isEmpty()) {
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
if (progress == null) {
|
||||
progress = new SpinsUserTaskProgress();
|
||||
progress.setUserId(userId);
|
||||
progress.setTaskId(taskConfig.getId());
|
||||
progress.setTaskCode(taskConfig.getTaskCode());
|
||||
progress.setCurrentValue(0);
|
||||
progress.setTargetValue(taskConfig.getTargetValue());
|
||||
progress.setProgressRate(BigDecimal.ZERO);
|
||||
progress.setTaskStatus(0);
|
||||
progress.setCycleType(taskConfig.getCycleType());
|
||||
progress.setCycleDate(LocalDate.now());
|
||||
progress.setCycleKey(cycleKey);
|
||||
progress.setCompleteTimes(0);
|
||||
progress.setRewardType(taskConfig.getRewardType());
|
||||
progress.setRewardValue(taskConfig.getRewardValue());
|
||||
progress.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
||||
progress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
|
||||
SpinsUserTaskProgress progress = buildNewProgress(userId, taskConfig, cycleKey);
|
||||
try {
|
||||
spinsUserTaskProgressDAO.insert(progress);
|
||||
} catch (DuplicateKeyException e) {
|
||||
// 并发插入,重新查询返回已存在记录
|
||||
List<SpinsUserTaskProgress> retryList = spinsUserTaskProgressDAO.selectList(
|
||||
new LambdaQueryWrapper<SpinsUserTaskProgress>()
|
||||
.eq(SpinsUserTaskProgress::getUserId, userId)
|
||||
.eq(SpinsUserTaskProgress::getTaskCode, taskConfig.getTaskCode())
|
||||
.eq(SpinsUserTaskProgress::getCycleKey, cycleKey)
|
||||
);
|
||||
return retryList.get(0);
|
||||
}
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算进度百分比
|
||||
*/
|
||||
private SpinsUserTaskProgress buildNewProgress(Long userId, SpinsTaskConfig taskConfig, String cycleKey) {
|
||||
SpinsUserTaskProgress progress = new SpinsUserTaskProgress();
|
||||
progress.setUserId(userId);
|
||||
progress.setTaskId(taskConfig.getId());
|
||||
progress.setTaskCode(taskConfig.getTaskCode());
|
||||
progress.setCurrentValue(0);
|
||||
progress.setTargetValue(taskConfig.getTargetValue());
|
||||
progress.setProgressRate(BigDecimal.ZERO);
|
||||
progress.setTaskStatus(0);
|
||||
progress.setCycleType(taskConfig.getCycleType());
|
||||
progress.setCycleDate(LocalDate.now());
|
||||
progress.setCycleKey(cycleKey);
|
||||
progress.setCompleteTimes(0);
|
||||
progress.setRewardType(taskConfig.getRewardType());
|
||||
progress.setRewardValue(taskConfig.getRewardValue());
|
||||
Timestamp now = new Timestamp(System.currentTimeMillis());
|
||||
progress.setCreateTime(now);
|
||||
progress.setUpdateTime(now);
|
||||
return progress;
|
||||
}
|
||||
|
||||
private BigDecimal calculateProgressRate(Integer currentValue, Integer targetValue) {
|
||||
if (targetValue == null || targetValue == 0) {
|
||||
return BigDecimal.ZERO;
|
||||
@ -144,20 +175,14 @@ public class SpinsTaskProgressUpdateExe {
|
||||
.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前周期key
|
||||
*/
|
||||
private String getCurrentCycleKey(Integer cycleType) {
|
||||
LocalDate now = ZonedDateTimeAsiaRiyadhUtils.now().toLocalDate();
|
||||
if (cycleType == 1) {
|
||||
// 每日任务:格式 yyyyMMdd
|
||||
return now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
} else if (cycleType == 2) {
|
||||
// 每周任务:格式 yyyyWw(例如:2025W03)
|
||||
int weekOfYear = now.getDayOfYear() / 7 + 1;
|
||||
return now.getYear() + "W" + String.format("%02d", weekOfYear);
|
||||
int week = now.get(WeekFields.ISO.weekOfWeekBasedYear());
|
||||
return now.getYear() + "W" + String.format("%02d", week);
|
||||
} else {
|
||||
// 一次性任务:固定标识
|
||||
return "ONCE";
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,7 +164,7 @@ public class BindInviteCodeExe {
|
||||
userProfileGateway.removeCacheAll(Collections.singleton(userId));
|
||||
|
||||
// 增加用户每日邀请用户数量
|
||||
int inviteCount = incrementDailyInviteCount(inviterProfile.getId());
|
||||
// int inviteCount = incrementDailyInviteCount(inviterProfile.getId());
|
||||
|
||||
// 处理送礁1个礼物任务
|
||||
/*if (inviteCount >= 1) {
|
||||
|
||||
@ -212,6 +212,23 @@ public class TaskListener implements MessageListener {
|
||||
.setProgressValue(micTime)
|
||||
);
|
||||
|
||||
// 处理上麦30分钟任务
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_MIC_30_MIN")
|
||||
.setProgressValue(micTime)
|
||||
);
|
||||
|
||||
// 处理上麦1小时任务
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_MIC_90_MIN")
|
||||
.setProgressValue(micTime)
|
||||
);
|
||||
|
||||
|
||||
log.info("Spins上麦任务进度更新成功, userId={}, micTime={}", userId, micTime);
|
||||
} catch (Exception e) {
|
||||
log.error("处理Spins上麦任务进度失败, userId={}, micTimeStr={}", userId, micTimeStr, e);
|
||||
|
||||
@ -110,5 +110,27 @@ public class GameActivityService {
|
||||
.setTaskCode("SPINS_SAVE_500000")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_SAVE_50000")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_SAVE_100000")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
spinsUserTaskProgressService.updateTaskProgress(
|
||||
new SpinsTaskProgressUpdateCmd()
|
||||
.setUserId(userId)
|
||||
.setTaskCode("SP_SAVE_500000")
|
||||
.setProgressValue(progressValue)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,5 @@
|
||||
package com.red.circle.other.app.service.task;
|
||||
|
||||
import com.red.circle.other.app.command.SpinsTaskProgressInitExe;
|
||||
import com.red.circle.other.app.command.SpinsTaskProgressIncrementExe;
|
||||
import com.red.circle.other.app.command.SpinsTaskProgressResetExe;
|
||||
import com.red.circle.other.app.command.SpinsTaskProgressUpdateExe;
|
||||
import com.red.circle.other.app.dto.cmd.SpinsTaskProgressUpdateCmd;
|
||||
import com.red.circle.other.app.service.SpinsUserTaskProgressService;
|
||||
@ -10,8 +7,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* Spins用户任务进度服务实现
|
||||
*
|
||||
@ -24,9 +19,6 @@ import javax.annotation.Resource;
|
||||
public class SpinsUserTaskProgressServiceImpl implements SpinsUserTaskProgressService {
|
||||
|
||||
private final SpinsTaskProgressUpdateExe spinsTaskProgressUpdateExe;
|
||||
private final SpinsTaskProgressIncrementExe spinsTaskProgressIncrementExe;
|
||||
private final SpinsTaskProgressResetExe spinsTaskProgressResetExe;
|
||||
private final SpinsTaskProgressInitExe spinsTaskProgressInitExe;
|
||||
|
||||
@Override
|
||||
public void updateTaskProgress(SpinsTaskProgressUpdateCmd cmd) {
|
||||
@ -35,22 +27,4 @@ public class SpinsUserTaskProgressServiceImpl implements SpinsUserTaskProgressSe
|
||||
spinsTaskProgressUpdateExe.execute(cmd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementTaskProgress(Long userId, String taskCode, Integer incrementValue) {
|
||||
log.info("增加用户任务进度, userId={}, taskCode={}, incrementValue={}",
|
||||
userId, taskCode, incrementValue);
|
||||
spinsTaskProgressIncrementExe.execute(userId, taskCode, incrementValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetDailyTasks(Long userId) {
|
||||
log.info("重置用户每日任务, userId={}", userId);
|
||||
spinsTaskProgressResetExe.execute(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initUserTaskProgress(Long userId, String taskCode) {
|
||||
log.info("初始化用户任务进度, userId={}, taskCode={}", userId, taskCode);
|
||||
spinsTaskProgressInitExe.execute(userId, taskCode);
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,27 +17,4 @@ public interface SpinsUserTaskProgressService {
|
||||
*/
|
||||
void updateTaskProgress(SpinsTaskProgressUpdateCmd cmd);
|
||||
|
||||
/**
|
||||
* 增加用户任务进度
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param taskCode 任务编码
|
||||
* @param incrementValue 增加值
|
||||
*/
|
||||
void incrementTaskProgress(Long userId, String taskCode, Integer incrementValue);
|
||||
|
||||
/**
|
||||
* 重置用户每日任务
|
||||
*
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
void resetDailyTasks(Long userId);
|
||||
|
||||
/**
|
||||
* 初始化用户任务进度
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param taskCode 任务编码
|
||||
*/
|
||||
void initUserTaskProgress(Long userId, String taskCode);
|
||||
}
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
package com.red.circle.other.infra.database.cache.key;
|
||||
|
||||
import com.red.circle.component.redis.RedisKeys;
|
||||
|
||||
/**
|
||||
* Spins任务相关Redis Key.
|
||||
*
|
||||
* @author tf
|
||||
*/
|
||||
public enum SpinsTaskKeys implements RedisKeys {
|
||||
|
||||
/**
|
||||
* 任务配置缓存,by taskCode.
|
||||
*/
|
||||
SPINS_TASK_CONFIG;
|
||||
|
||||
@Override
|
||||
public String businessPrefix() {
|
||||
return "spins:task";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String businessKey() {
|
||||
return this.name();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user