|
@@ -2,6 +2,7 @@ package com.xs.core.service.Impl.task;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.xs.core.common.constant.GoldCoinConstant;
|
|
|
import com.xs.core.common.enums.CoinTransactionCategoryEnum;
|
|
|
import com.xs.core.common.enums.CoinTransactionTypeEnum;
|
|
|
import com.xs.core.common.enums.TaskCallbackLogTypeEnum;
|
|
@@ -15,6 +16,7 @@ import com.xs.core.service.task.IUserTaskRecordService;
|
|
|
import com.xs.core.service.task.TaskCallbackService;
|
|
|
import com.xs.core.service.user.IUserService;
|
|
|
import com.xs.core.service.wallet.IUserWalletService;
|
|
|
+import com.xs.core.utils.redis.RedissonLockUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -50,25 +52,42 @@ public class TaskCallbackServiceImpl implements TaskCallbackService {
|
|
|
CheckUtils.throwIfNull(taskInfo, "error.task.handle.not.exist");
|
|
|
boolean exist = userService.checkUserExist(Long.valueOf(userId));
|
|
|
CheckUtils.throwIf(!exist, "error.user.not.exist");
|
|
|
- //查询任务完成情况
|
|
|
- UserTaskRecord record = taskRecordService.getByTaskId(taskId, Long.valueOf(userId));
|
|
|
- if (record != null) {
|
|
|
- //说明任务已经开始 根据任务状态进行处理
|
|
|
- Integer status = record.getStatus();
|
|
|
- if (status == 1) {
|
|
|
- //说明任务已经已经开始 进入下一步
|
|
|
- record.setStatus(2);
|
|
|
- record.setCallbackNum(1);
|
|
|
- record.setUpdateTime(LocalDateTime.now());
|
|
|
- record.setCompleteTime(LocalDateTime.now());
|
|
|
- taskRecordService.updateById(record);
|
|
|
- //领取奖励
|
|
|
- walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.TASK_REWARDS, "任务奖励", "任务奖励", new BigDecimal(record.getRewardAmount()), Long.valueOf(userId));
|
|
|
- CompletableFuture.runAsync(() -> {
|
|
|
- // 保存回调日志
|
|
|
- taskCallbackLogService.saveCallbackLog(taskId, Long.valueOf(userId), taskInfo.getTaskAcceptApiLink(), jsonObject.toJSONString(), JSON.toJSONString(ResponseResult.success()), TaskCallbackLogTypeEnum.CALLBACK);
|
|
|
- });
|
|
|
+ String key = GoldCoinConstant.TASK_CALLBACK_LOCK_KEY + taskId + "_" + userId;
|
|
|
+ try {
|
|
|
+ RedissonLockUtil.tryLock(key);
|
|
|
+ //查询任务完成情况
|
|
|
+ UserTaskRecord record = taskRecordService.getByTaskId(taskId, Long.valueOf(userId));
|
|
|
+ if (record != null) {
|
|
|
+ //说明任务已经开始 根据任务状态进行处理
|
|
|
+ Integer status = record.getStatus();
|
|
|
+ if (status == 1) {
|
|
|
+ //说明任务已经已经开始 进入下一步
|
|
|
+ record.setStatus(2);
|
|
|
+ record.setCallbackNum(1);
|
|
|
+ record.setUpdateTime(LocalDateTime.now());
|
|
|
+ record.setCompleteTime(LocalDateTime.now());
|
|
|
+ taskRecordService.updateById(record);
|
|
|
+ //领取奖励
|
|
|
+ walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.TASK_REWARDS, "任务奖励", "任务奖励", new BigDecimal(record.getRewardAmount()), Long.valueOf(userId));
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ // 保存回调日志
|
|
|
+ taskCallbackLogService.saveCallbackLog(taskId, Long.valueOf(userId), taskInfo.getTaskAcceptApiLink(), jsonObject.toJSONString(), JSON.toJSONString(ResponseResult.success()), TaskCallbackLogTypeEnum.CALLBACK);
|
|
|
+ });
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ //判断任务完成量是否已经达到阈值
|
|
|
+ long count = taskRecordService.statisticsTaskDoneCount(taskId);
|
|
|
+ if (count >= taskInfo.getAutoCloseThreshold()) {
|
|
|
+ //修改任务状态
|
|
|
+ taskInfo.setStatus(2);
|
|
|
+ taskInfoService.updateById(taskInfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ RedissonLockUtil.unLock(key);
|
|
|
}
|
|
|
}
|
|
|
}
|