diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java index 37cef53b..b32fc673 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/listener/task/TaskListener.java @@ -266,21 +266,25 @@ public class TaskListener implements MessageListener { private void handleTask8(TaskApprovalEvent eventBody) { log.warn("处理任务-8 发送10个幸运礼物: {}", eventBody); - // 增加幸运礼物发送次数(不设置过期时间) - String redisKey = "task:lucky-gift:count:" + eventBody.getUserId(); - String countStr = redisService.getString(redisKey); + String redisKey = "task:lucky-gift:count:" + eventBody.getUserId(); int num = eventBody.getQuantity() == null ? 1 : eventBody.getQuantity(); - if (StringUtils.isEmpty(countStr)) { + + // 获取当前计数 + String countStr = redisService.getString(redisKey); + int currentCount = StringUtils.isEmpty(countStr) ? 0 : Integer.parseInt(countStr); + + // 计算新的计数值 + int newCount = currentCount + num; + + // 更新任务进度 + eventBody.setTargetValue(10); + eventBody.setCompleteValue(Math.min(newCount, 10)); + boolean updated = updateTaskStatus(eventBody); + + // 只有任务状态更新成功才更新 Redis + if (updated) { redisService.increment(redisKey, num); - } else { - int result = Integer.parseInt(countStr) + num; - eventBody.setTargetValue(10); - eventBody.setCompleteValue(Math.min(result, 10)); - boolean updated = updateTaskStatus(eventBody); - if (updated) { - redisService.increment(redisKey, 1); - } } }