|
|
@@ -8,7 +8,6 @@ import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
-import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.mapper.GoodsDonSkuMapper;
|
|
|
import com.cyksj.mapper.GptRechargeCardKeyMapper;
|
|
|
@@ -24,6 +23,7 @@ import com.cyksj.model.entity.OrderDon;
|
|
|
import com.cyksj.model.response.recharge.CdkAicnmCardKeyLookupResp;
|
|
|
import com.cyksj.model.response.recharge.CdkAicnmRedeemStartResp;
|
|
|
import com.cyksj.redis.RedisService;
|
|
|
+import com.cyksj.server.recharge.dto.TaskResult;
|
|
|
import com.cyksj.service.chatgpt.GptUserRechargeRecordService;
|
|
|
import com.cyksj.service.recharge.CdkAicnmRechargeService;
|
|
|
import com.cyksj.service.recharge.GptRechargeCardKeyChannelService;
|
|
|
@@ -39,8 +39,6 @@ import java.util.Date;
|
|
|
public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
|
|
|
private static final String BASE_URL = "https://cdk.aicnm.cc";
|
|
|
- private static final int MAX_POLL_TIMES = 20;
|
|
|
- private static final long POLL_INTERVAL_MILLIS = 1000L;
|
|
|
|
|
|
private final GroupsRelationMapper relationMapper;
|
|
|
|
|
|
@@ -57,9 +55,6 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
private final GptUserRechargeRecordService gptUserRechargeRecordService;
|
|
|
private final GptRechargeCardKeyChannelService gptRechargeCardKeyChannelService;
|
|
|
|
|
|
-
|
|
|
- private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
-
|
|
|
@Override
|
|
|
public String confirmRecharge(GroupsRelation relation, String gptCardKey, CmsUser cmsUser) {
|
|
|
Long relationId = relation.getId();
|
|
|
@@ -96,7 +91,7 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
String rechargeAccount = relation.getAccount();
|
|
|
CdkAicnmRedeemStartResp startResp;
|
|
|
try {
|
|
|
- startResp = submitTask(gptCardKey, accessToken);
|
|
|
+ startResp = submitTask(baseUrl,gptCardKey, accessToken);
|
|
|
if (startResp == null || !Boolean.TRUE.equals(startResp.getOk())) {
|
|
|
throw BusinessRuntimeException.getInstance("cdk.aicnm激活任务提交失败");
|
|
|
}
|
|
|
@@ -125,36 +120,39 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
.set(GroupsRelation::getGptToken, accessToken)
|
|
|
.set(GroupsRelation::getGptTaskId, taskId)
|
|
|
.eq(GroupsRelation::getId, relationId));
|
|
|
- relation.setRechargeStatus(GroupsRelation.RechargeStatus.recharging);
|
|
|
- relation.setCardKey(gptCardKey);
|
|
|
- relation.setGptTaskId(taskId);
|
|
|
- if (cmsUser != null) {
|
|
|
- relation.setOperator(operator);
|
|
|
- }
|
|
|
+
|
|
|
+ return taskId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TaskResult getTaskResult(GroupsRelation relation) {
|
|
|
try {
|
|
|
- CdkAicnmRedeemStartResp taskStatusResp = waitRechargeResult(baseUrl, taskId);
|
|
|
- if (taskStatusResp == null || !Boolean.TRUE.equals(taskStatusResp.getDone())) {
|
|
|
- setRechargeRelationStatusAutoError(relation);
|
|
|
- throw BusinessRuntimeException.getInstance("充值失败,查询激活结果超时");
|
|
|
- }
|
|
|
- boolean success = Boolean.TRUE.equals(taskStatusResp.getSuccess());
|
|
|
- String resultMessage = StrUtil.blankToDefault(taskStatusResp.getMessage(), success ? rechargeAccount : "激活失败");
|
|
|
- resetGroupsRelationRechargeStatus(relation, success, resultMessage);
|
|
|
- if (!success) {
|
|
|
- if (cmsUser == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
- }
|
|
|
- throw BusinessRuntimeException.getInstance("充值失败,{0}", resultMessage);
|
|
|
+ String taskId = relation.getGptTaskId();
|
|
|
+ String cardKey = relation.getCardKey();
|
|
|
+
|
|
|
+ String baseUrl = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ CdkAicnmRedeemStartResp taskStatusResp = queryTaskResult(baseUrl, taskId);
|
|
|
+
|
|
|
+ Boolean pending = !taskStatusResp.getDone();
|
|
|
+ Boolean status = taskStatusResp.getSuccess();
|
|
|
+
|
|
|
+ //充值中
|
|
|
+ TaskResult taskResult = new TaskResult();
|
|
|
+ if (pending) {
|
|
|
+ taskResult.setStatus("recharging");
|
|
|
+ return taskResult;
|
|
|
}
|
|
|
- return taskId;
|
|
|
- } catch (BusinessRuntimeException e) {
|
|
|
- throw e;
|
|
|
+
|
|
|
+ String resultResult = taskStatusResp.getMessage();
|
|
|
+ //更新代充状态
|
|
|
+ resetGroupsRelationRechargeStatus(relation, pending, status, resultResult);
|
|
|
+ if (status && !pending) {
|
|
|
+ taskResult.setStatus("completed");
|
|
|
+ } else taskResult.setStatus("failed");
|
|
|
+ return taskResult;
|
|
|
} catch (Exception e) {
|
|
|
- setRechargeRelationStatusAutoError(relation);
|
|
|
- if (cmsUser == null) {
|
|
|
- throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
- }
|
|
|
- throw BusinessRuntimeException.getInstance("充值失败,{0}", e.getMessage());
|
|
|
+ log.error("获取任务结果失败: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -173,8 +171,7 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
return Jsons.parseObject(body, CdkAicnmCardKeyLookupResp.class);
|
|
|
}
|
|
|
|
|
|
- public CdkAicnmRedeemStartResp submitTask(String cardKey, String accessToken) throws Exception {
|
|
|
- String baseUrl = getBaseUrl(cardKey);
|
|
|
+ public CdkAicnmRedeemStartResp submitTask(String baseUrl,String cardKey, String accessToken) throws Exception {
|
|
|
String url = baseUrl + "/api/public/code/redeem/start";
|
|
|
JSONObject params = new JSONObject();
|
|
|
params.putOpt("code", cardKey);
|
|
|
@@ -213,25 +210,6 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
return Jsons.parseObject(body, CdkAicnmRedeemStartResp.class);
|
|
|
}
|
|
|
|
|
|
- private CdkAicnmRedeemStartResp waitRechargeResult(String baseUrl, String jobId) throws Exception {
|
|
|
- CdkAicnmRedeemStartResp taskStatusResp = null;
|
|
|
- for (int i = 0; i < MAX_POLL_TIMES; i++) {
|
|
|
- taskStatusResp = queryTaskResult(baseUrl, jobId);
|
|
|
- if (taskStatusResp != null && Boolean.TRUE.equals(taskStatusResp.getDone())) {
|
|
|
- return taskStatusResp;
|
|
|
- }
|
|
|
- if (i == MAX_POLL_TIMES - 1) {
|
|
|
- break;
|
|
|
- }
|
|
|
- try {
|
|
|
- Thread.sleep(POLL_INTERVAL_MILLIS);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- Thread.currentThread().interrupt();
|
|
|
- throw new RuntimeException("查询激活结果被中断", e);
|
|
|
- }
|
|
|
- }
|
|
|
- return taskStatusResp;
|
|
|
- }
|
|
|
|
|
|
boolean isCardKeyAvailable(CdkAicnmCardKeyLookupResp lookupResp) {
|
|
|
return lookupResp != null
|
|
|
@@ -239,37 +217,30 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
&& "unused".equalsIgnoreCase(lookupResp.getStatus());
|
|
|
}
|
|
|
|
|
|
- public void setRechargeRelationStatusAutoError(GroupsRelation relation) {
|
|
|
- TASK_EXECUTOR.execute(() -> relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
- .set(GroupsRelation::getAccount, relation.getAccount())
|
|
|
- .set(GroupsRelation::getGptToken, relation.getGptToken())
|
|
|
- .set(GroupsRelation::getRechargeStatus, GroupsRelation.RechargeStatus.recharge_error)
|
|
|
- .eq(GroupsRelation::getId, relation.getId())));
|
|
|
- }
|
|
|
|
|
|
- private void resetGroupsRelationRechargeStatus(GroupsRelation relation, Boolean taskStatus, String result) {
|
|
|
- GroupsRelation.RechargeStatus rechargeStatus = Boolean.TRUE.equals(taskStatus)
|
|
|
- ? GroupsRelation.RechargeStatus.complete
|
|
|
- : GroupsRelation.RechargeStatus.recharge_error;
|
|
|
+ private void resetGroupsRelationRechargeStatus(GroupsRelation relation, Boolean pending, Boolean status, String result) {
|
|
|
+ GroupsRelation.RechargeStatus rechargeStatus;
|
|
|
+ if (status && !pending) {
|
|
|
+ rechargeStatus = GroupsRelation.RechargeStatus.complete;
|
|
|
+ } else {
|
|
|
+ rechargeStatus = GroupsRelation.RechargeStatus.recharge_error;
|
|
|
+ }
|
|
|
String taskId = relation.getGptTaskId();
|
|
|
if (relation != null && relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharging) {
|
|
|
Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
String cardKey = relation.getCardKey();
|
|
|
- GptRechargeCardKey gptRechargeCardKey = gptRechargeCardKeyMapper.selectOne(
|
|
|
- Wrappers.lambdaQuery(GptRechargeCardKey.class)
|
|
|
- .eq(GptRechargeCardKey::getCardKey, cardKey)
|
|
|
- .last("limit 1"));
|
|
|
- if (rechargeStatus == GroupsRelation.RechargeStatus.recharge_error && gptRechargeCardKey != null) {
|
|
|
- gptRechargeCardKeyMapper.update(null, Wrappers.lambdaUpdate(GptRechargeCardKey.class)
|
|
|
- .set(GptRechargeCardKey::getStatus, Boolean.FALSE)
|
|
|
- .set(GptRechargeCardKey::getOrderId, null)
|
|
|
- .set(GptRechargeCardKey::getOrderNo, null)
|
|
|
- .eq(GptRechargeCardKey::getId, gptRechargeCardKey.getId())
|
|
|
- .eq(GptRechargeCardKey::getStatus, Boolean.TRUE));
|
|
|
+ GptRechargeCardKey gptRechargeCardKey = gptRechargeCardKeyMapper.selectOne(Wrappers.lambdaQuery(GptRechargeCardKey.class).eq(GptRechargeCardKey::getCardKey, cardKey).last("limit 1"));
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.recharge_error) {
|
|
|
+ if (gptRechargeCardKey != null) {
|
|
|
+ if ("stock not found.".equals(result)) {
|
|
|
+ gptRechargeCardKeyMapper.update(null, Wrappers.lambdaUpdate(GptRechargeCardKey.class).set(GptRechargeCardKey::getOrderNo, "卡密异常").eq(GptRechargeCardKey::getId, gptRechargeCardKey.getId()).eq(GptRechargeCardKey::getStatus, Boolean.TRUE));
|
|
|
+ } else {
|
|
|
+ gptRechargeCardKeyMapper.update(null, Wrappers.lambdaUpdate(GptRechargeCardKey.class).set(GptRechargeCardKey::getStatus, Boolean.FALSE).set(GptRechargeCardKey::getOrderId, null).set(GptRechargeCardKey::getOrderNo, null).eq(GptRechargeCardKey::getId, gptRechargeCardKey.getId()).eq(GptRechargeCardKey::getStatus, Boolean.TRUE));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
if (rechargeStatus == GroupsRelation.RechargeStatus.recharge_error) {
|
|
|
- if (!redisService.setNx(RedisService.key.GPT_RECHARGE_RECOVER_KEY.getName() + taskId,
|
|
|
- taskId, RedisService.key.GPT_RECHARGE_RECOVER_KEY.getTimeout())) {
|
|
|
+ if (!redisService.setNx(RedisService.key.GPT_RECHARGE_RECOVER_KEY.getName() + taskId, taskId, RedisService.key.GPT_RECHARGE_RECOVER_KEY.getTimeout())) {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
@@ -287,31 +258,19 @@ public class CdkAicnmRechargeServiceImpl implements CdkAicnmRechargeService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- Integer rechargeNum = 1;
|
|
|
- String rechargeErrorInfo = rechargeStatus == GroupsRelation.RechargeStatus.recharge_error ? result : null;
|
|
|
- relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
- .set(rechargeStatus == GroupsRelation.RechargeStatus.recharge_error,
|
|
|
- GroupsRelation::getRechargeRemainNum, rechargeRemainNum + rechargeNum)
|
|
|
- .set(GroupsRelation::getRechargeStatus, rechargeStatus)
|
|
|
- .set(expiryTime != null, GroupsRelation::getStartTime, startTime)
|
|
|
- .set(expiryTime != null, GroupsRelation::getExpiryTime, expiryTime)
|
|
|
- .set(StrUtil.isNotEmpty(rechargeErrorInfo), GroupsRelation::getRechargeErrorInfo, rechargeErrorInfo)
|
|
|
- .eq(GroupsRelation::getId, relation.getId())
|
|
|
- .le(GroupsRelation::getRechargeRemainNum, relation.getRechargeNum())
|
|
|
- .eq(GroupsRelation::getRechargeStatus, GroupsRelation.RechargeStatus.recharging));
|
|
|
+ String rechargeErrorInfo = null;
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.recharge_error) {
|
|
|
+ rechargeErrorInfo = result;
|
|
|
+ }
|
|
|
+ relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class).set(rechargeStatus == GroupsRelation.RechargeStatus.recharge_error, GroupsRelation::getRechargeRemainNum, rechargeRemainNum + 1).set(GroupsRelation::getRechargeStatus, rechargeStatus).set(expiryTime != null, GroupsRelation::getStartTime, startTime).set(expiryTime != null, GroupsRelation::getExpiryTime, expiryTime).set(StrUtil.isNotEmpty(rechargeErrorInfo), GroupsRelation::getRechargeErrorInfo, rechargeErrorInfo).eq(GroupsRelation::getId, relation.getId()).le(GroupsRelation::getRechargeRemainNum, relation.getRechargeNum()).eq(GroupsRelation::getRechargeStatus, GroupsRelation.RechargeStatus.recharging));
|
|
|
+ //记录代充记录
|
|
|
if (rechargeStatus == GroupsRelation.RechargeStatus.complete) {
|
|
|
String remark = "自动代充";
|
|
|
if (StrUtil.isNotBlank(relation.getOperator())) {
|
|
|
remark = "后台自动代充";
|
|
|
}
|
|
|
- gptUserRechargeRecordService.recordGptUserRechargeNum(
|
|
|
- relation.getCardKey(),
|
|
|
- relation.getUserId(),
|
|
|
- relation.getId(),
|
|
|
- relation.getAccount(),
|
|
|
- -rechargeNum,
|
|
|
- relation.getOperator(),
|
|
|
- remark);
|
|
|
+ gptUserRechargeRecordService.recordGptUserRechargeNum(relation.getCardKey(), relation.getUserId(), relation.getId(), relation.getAccount(), -1, relation.getOperator(), remark);
|
|
|
+ //修改对应订单状态
|
|
|
if (gptRechargeCardKey != null) {
|
|
|
Long orderId = gptRechargeCardKey.getOrderId();
|
|
|
OrderDon orderDon = orderDonMapper.selectById(orderId);
|