|
@@ -1,19 +1,27 @@
|
|
package com.xs.core.service.Impl.task;
|
|
package com.xs.core.service.Impl.task;
|
|
|
|
|
|
import cn.hutool.core.util.ObjUtil;
|
|
import cn.hutool.core.util.ObjUtil;
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
import com.xs.core.common.content.UserContext;
|
|
import com.xs.core.common.content.UserContext;
|
|
import com.xs.core.common.content.UserContextHolder;
|
|
import com.xs.core.common.content.UserContextHolder;
|
|
import com.xs.core.common.enums.CoinTransactionCategoryEnum;
|
|
import com.xs.core.common.enums.CoinTransactionCategoryEnum;
|
|
import com.xs.core.common.enums.CoinTransactionTypeEnum;
|
|
import com.xs.core.common.enums.CoinTransactionTypeEnum;
|
|
|
|
+import com.xs.core.common.enums.TaskCallbackLogTypeEnum;
|
|
import com.xs.core.common.validation.CheckUtils;
|
|
import com.xs.core.common.validation.CheckUtils;
|
|
import com.xs.core.model.task.entity.TaskInfo;
|
|
import com.xs.core.model.task.entity.TaskInfo;
|
|
import com.xs.core.model.task.entity.UserTaskRecord;
|
|
import com.xs.core.model.task.entity.UserTaskRecord;
|
|
import com.xs.core.model.task.req.TaskInfoReq;
|
|
import com.xs.core.model.task.req.TaskInfoReq;
|
|
import com.xs.core.model.task.resp.TaskInfoResp;
|
|
import com.xs.core.model.task.resp.TaskInfoResp;
|
|
|
|
+import com.xs.core.service.task.ITaskCallbackLogService;
|
|
import com.xs.core.service.task.ITaskInfoService;
|
|
import com.xs.core.service.task.ITaskInfoService;
|
|
import com.xs.core.service.task.IUserTaskRecordService;
|
|
import com.xs.core.service.task.IUserTaskRecordService;
|
|
import com.xs.core.service.task.TaskService;
|
|
import com.xs.core.service.task.TaskService;
|
|
import com.xs.core.service.wallet.IUserWalletService;
|
|
import com.xs.core.service.wallet.IUserWalletService;
|
|
|
|
+import com.xs.core.utils.HttpClientService;
|
|
|
|
+import com.xs.core.utils.OpenApiEncryptionUtils;
|
|
|
|
+import com.xs.core.utils.SecurityUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -21,8 +29,10 @@ import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
|
+@Slf4j
|
|
public class TaskServiceImpl implements TaskService {
|
|
public class TaskServiceImpl implements TaskService {
|
|
@Autowired
|
|
@Autowired
|
|
private ITaskInfoService taskInfoService;
|
|
private ITaskInfoService taskInfoService;
|
|
@@ -33,6 +43,12 @@ public class TaskServiceImpl implements TaskService {
|
|
@Autowired
|
|
@Autowired
|
|
private IUserWalletService walletService;
|
|
private IUserWalletService walletService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private HttpClientService httpClientService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ITaskCallbackLogService taskCallbackLogService;
|
|
|
|
+
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public List<TaskInfoResp> getTaskInfoList(TaskInfoReq req) {
|
|
public List<TaskInfoResp> getTaskInfoList(TaskInfoReq req) {
|
|
@@ -44,32 +60,73 @@ public class TaskServiceImpl implements TaskService {
|
|
@Override
|
|
@Override
|
|
@Transactional
|
|
@Transactional
|
|
public void handleTask(TaskInfoReq req, Long userId) {
|
|
public void handleTask(TaskInfoReq req, Long userId) {
|
|
- CheckUtils.throwIfNull(req, "error.task.handle.id.is.null");
|
|
|
|
|
|
+ CheckUtils.throwIfBlank(req.getTaskId(), "error.task.handle.id.is.null");
|
|
TaskInfo taskInfo = taskInfoService.getById(req.getTaskId());
|
|
TaskInfo taskInfo = taskInfoService.getById(req.getTaskId());
|
|
CheckUtils.throwIfNull(taskInfo, "error.task.handle.not.exist");
|
|
CheckUtils.throwIfNull(taskInfo, "error.task.handle.not.exist");
|
|
- //查询处理记录
|
|
|
|
|
|
+ String categoryCode = taskInfo.getCategoryCode();
|
|
|
|
+ //查询任务完成情况
|
|
UserTaskRecord record = taskRecordService.getByTaskId(req.getTaskId(), userId);
|
|
UserTaskRecord record = taskRecordService.getByTaskId(req.getTaskId(), userId);
|
|
- if (ObjUtil.isNull(record)) {
|
|
|
|
- //说明任务还没开始 添加任务记录
|
|
|
|
- UserTaskRecord newRecord = new UserTaskRecord();
|
|
|
|
- newRecord.setTaskId(req.getTaskId());
|
|
|
|
- newRecord.setUserId(userId);
|
|
|
|
- newRecord.setStatus(1);
|
|
|
|
- newRecord.setCreateTime(LocalDateTime.now());
|
|
|
|
- newRecord.setStartTime(LocalDateTime.now());
|
|
|
|
- newRecord.setRewardAmount(taskInfo.getGoldCoin());
|
|
|
|
- taskRecordService.save(newRecord);
|
|
|
|
|
|
+ if (categoryCode.equals("2")) {
|
|
|
|
+ //说明是回调任务
|
|
|
|
+ if (ObjUtil.isNull(record)) {
|
|
|
|
+ //说明任务还没开始 添加任务记录
|
|
|
|
+ UserTaskRecord newRecord = new UserTaskRecord();
|
|
|
|
+ newRecord.setTaskId(req.getTaskId());
|
|
|
|
+ newRecord.setUserId(userId);
|
|
|
|
+ newRecord.setStatus(1);
|
|
|
|
+ newRecord.setCreateTime(LocalDateTime.now());
|
|
|
|
+ newRecord.setStartTime(LocalDateTime.now());
|
|
|
|
+ newRecord.setRewardAmount(taskInfo.getGoldCoin());
|
|
|
|
+ taskRecordService.save(newRecord);
|
|
|
|
+ // 通知任务开始
|
|
|
|
+ startTaskNotification(taskInfo, userId);
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- //说明任务已经开始 根据任务状态进行处理
|
|
|
|
- Integer status = record.getStatus();
|
|
|
|
- if (status == 1) {
|
|
|
|
- //说明任务已经已经开始 进入下一步
|
|
|
|
- record.setStatus(2);
|
|
|
|
- record.setUpdateTime(LocalDateTime.now());
|
|
|
|
- taskRecordService.updateById(record);
|
|
|
|
- //领取奖励
|
|
|
|
- walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.TASK_REWARDS, "任务奖励", "任务奖励", new BigDecimal(record.getRewardAmount()), userId);
|
|
|
|
|
|
+ //说明是普通任务
|
|
|
|
+ if (ObjUtil.isNull(record)) {
|
|
|
|
+ //说明任务还没开始 添加任务记录
|
|
|
|
+ UserTaskRecord newRecord = new UserTaskRecord();
|
|
|
|
+ newRecord.setTaskId(req.getTaskId());
|
|
|
|
+ newRecord.setUserId(userId);
|
|
|
|
+ newRecord.setStatus(1);
|
|
|
|
+ newRecord.setCreateTime(LocalDateTime.now());
|
|
|
|
+ newRecord.setStartTime(LocalDateTime.now());
|
|
|
|
+ newRecord.setRewardAmount(taskInfo.getGoldCoin());
|
|
|
|
+ taskRecordService.save(newRecord);
|
|
|
|
+ } else {
|
|
|
|
+ //说明任务已经开始 根据任务状态进行处理
|
|
|
|
+ Integer status = record.getStatus();
|
|
|
|
+ if (status == 1) {
|
|
|
|
+ //说明任务已经已经开始 进入下一步
|
|
|
|
+ record.setStatus(2);
|
|
|
|
+ record.setUpdateTime(LocalDateTime.now());
|
|
|
|
+ record.setCompleteTime(LocalDateTime.now());
|
|
|
|
+ taskRecordService.updateById(record);
|
|
|
|
+ //领取奖励
|
|
|
|
+ walletService.coinTransaction(CoinTransactionTypeEnum.ADD, CoinTransactionCategoryEnum.TASK_REWARDS, "任务奖励", "任务奖励", new BigDecimal(record.getRewardAmount()), userId);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void startTaskNotification(TaskInfo taskInfo, Long userId) {
|
|
|
|
+ // 通知任务开始
|
|
|
|
+ // 调用第三方接口通知第三方任务开始
|
|
|
|
+ String taskAcceptApiLink = taskInfo.getTaskAcceptApiLink();
|
|
|
|
+ CheckUtils.throwIfBlank(taskAcceptApiLink, "error.task.accept.api.link.is.null");
|
|
|
|
+ String taskId = taskInfo.getId();
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("taskId", taskId);
|
|
|
|
+ jsonObject.put("userId", userId + "");
|
|
|
|
+ JSONObject entry = new JSONObject();
|
|
|
|
+ String jsonString = JSON.toJSONString(jsonObject);
|
|
|
|
+ String aesEncrypt = OpenApiEncryptionUtils.aesEncrypt(jsonString);
|
|
|
|
+ entry.put("encryptData", aesEncrypt);
|
|
|
|
+ entry.put("identifying", SecurityUtil.getSign(aesEncrypt));
|
|
|
|
+ String postJson = httpClientService.postJson(taskAcceptApiLink, JSON.toJSONString(entry), null);
|
|
|
|
+ //记录回调日志
|
|
|
|
+ CompletableFuture.runAsync(() -> taskCallbackLogService.saveCallbackLog(taskId, userId, taskAcceptApiLink, jsonString, postJson, TaskCallbackLogTypeEnum.START));
|
|
|
|
+ }
|
|
}
|
|
}
|