|
@@ -0,0 +1,487 @@
|
|
|
|
|
+package com.cyksj.service.recharge.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
|
+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.*;
|
|
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
|
|
+import com.cyksj.model.response.recharge.RedeemCloudCdkQueryResponse;
|
|
|
|
|
+import com.cyksj.model.response.recharge.RedeemCloudTaskResultResponse;
|
|
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
|
|
+import com.cyksj.server.recharge.dto.TaskResult;
|
|
|
|
|
+import com.cyksj.service.chatgpt.GptUserRechargeRecordService;
|
|
|
|
|
+import com.cyksj.service.recharge.GptRechargeCardKeyChannelService;
|
|
|
|
|
+import com.cyksj.service.recharge.RedeemCloudRechargeService;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class RedeemCloudRechargeServiceImpl implements RedeemCloudRechargeService {
|
|
|
|
|
+
|
|
|
|
|
+ private final GptRechargeCardKeyChannelService gptRechargeCardKeyChannelService;
|
|
|
|
|
+
|
|
|
|
|
+ private final GroupsRelationMapper relationMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final GroupsMapper groupsMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final RedisService redisService;
|
|
|
|
|
+
|
|
|
|
|
+ private final OrderDonMapper orderDonMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final GptRechargeCardKeyMapper gptRechargeCardKeyMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final GptUserRechargeRecordService gptUserRechargeRecordService;
|
|
|
|
|
+
|
|
|
|
|
+ private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
+
|
|
|
|
|
+ private static final int SUCCESS_CODE = 200;
|
|
|
|
|
+
|
|
|
|
|
+ private static final int LEGACY_SUCCESS_CODE = 10000;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public RedeemCloudCdkQueryResponse validateCardKey(String cardKey) throws Exception {
|
|
|
|
|
+ String baseUrl = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
|
|
+ String url = baseUrl + "/sys-cdk/query?cdkCode=" + encode(cardKey);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.get(url)
|
|
|
|
|
+ .setConnectionTimeout(10000)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ String body = response.body();
|
|
|
|
|
+ RedeemCloudCdkQueryResponse result = Jsons.parseObject(body, RedeemCloudCdkQueryResponse.class);
|
|
|
|
|
+ log.info("redeemcloud validate card key response - code: {}, useStatus: {}, channelEnabled: {}",
|
|
|
|
|
+ result != null ? result.getCode() : null,
|
|
|
|
|
+ result != null && result.getData() != null ? result.getData().getUseStatus() : null,
|
|
|
|
|
+ result != null && result.getData() != null ? result.getData().getChannelEnabled() : null);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String confirmRecharge(GroupsRelation relation, String gptCardKey, CmsUser cmsUser) {
|
|
|
|
|
+ Long relationId = relation.getId();
|
|
|
|
|
+ String rechargePayload = relation.getGptToken();
|
|
|
|
|
+ String rechargeAccount = relation.getAccount();
|
|
|
|
|
+ String taskId;
|
|
|
|
|
+ try {
|
|
|
|
|
+ taskId = submitTask(gptCardKey, rechargePayload);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ setRechargeRelationStatusAutoError(relation);
|
|
|
|
|
+ if (cmsUser == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
|
|
+ }
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,{0}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String operator = cmsUser != null ? cmsUser.getNickname() : null;
|
|
|
|
|
+ String password = relation.getPassword();
|
|
|
|
|
+ relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
|
|
+ .set(GroupsRelation::getAccount, rechargeAccount)
|
|
|
|
|
+ .set(GroupsRelation::getOperator, operator)
|
|
|
|
|
+ .set(StrUtil.isNotBlank(password), GroupsRelation::getPassword, password)
|
|
|
|
|
+ .set(GroupsRelation::getRechargeStatus, GroupsRelation.RechargeStatus.recharging)
|
|
|
|
|
+ .set(GroupsRelation::getCardKey, gptCardKey)
|
|
|
|
|
+ .set(GroupsRelation::getGptToken, rechargePayload)
|
|
|
|
|
+ .set(GroupsRelation::getGptTaskId, taskId)
|
|
|
|
|
+ .eq(GroupsRelation::getId, relationId));
|
|
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.recharging);
|
|
|
|
|
+ relation.setCardKey(gptCardKey);
|
|
|
|
|
+ relation.setOperator(operator);
|
|
|
|
|
+ return taskId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String submitTask(String cardKey, String rechargePayload) {
|
|
|
|
|
+ log.info("start submit redeemcloud task - cardKey: {}", cardKey);
|
|
|
|
|
+ try {
|
|
|
|
|
+ String baseUrl = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
|
|
+ String requestBody = buildExchangeBody(baseUrl, cardKey, rechargePayload);
|
|
|
|
|
+ String url = baseUrl + "/sys-cdk/exchange?cdkCode=" + encode(cardKey);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = addBrowserHeaders(HttpRequest.post(url)
|
|
|
|
|
+ .setConnectionTimeout(60000)
|
|
|
|
|
+ .header("content-type", "application/json"))
|
|
|
|
|
+ .body(requestBody)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ String body = response.body();
|
|
|
|
|
+ log.info("redeemcloud submit response: {}", body);
|
|
|
|
|
+
|
|
|
|
|
+ String directTaskId = parseDirectTaskId(body);
|
|
|
|
|
+ if (StrUtil.isNotBlank(directTaskId)) {
|
|
|
|
|
+ return directTaskId;
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject jsonResponse = parseResponseObject(body, "exchange");
|
|
|
|
|
+ Object data = jsonResponse.get("data");
|
|
|
|
|
+ if (isSuccessResponse(jsonResponse) && data instanceof CharSequence && StrUtil.isNotBlank(data.toString())) {
|
|
|
|
|
+ return data.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (data instanceof JSONObject) {
|
|
|
|
|
+ JSONObject dataObj = (JSONObject) data;
|
|
|
|
|
+ String status = dataObj.getStr("status");
|
|
|
|
|
+ if ("FAILED".equalsIgnoreCase(status)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance(getErrorMessage(dataObj, jsonResponse));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance(getErrorMessage(null, jsonResponse));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("submit redeemcloud task failed: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new RuntimeException("提交任务失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String buildExchangeBody(String baseUrl, String cardKey, String rechargePayload) throws Exception {
|
|
|
|
|
+ rechargePayload = unwrapJsonString(rechargePayload);
|
|
|
|
|
+ if (StrUtil.isBlank(rechargePayload)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("redeemcloud提交任务需要session");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StrUtil.startWith(rechargePayload.trim(), "[")) {
|
|
|
|
|
+ JSONArray array = JSONUtil.parseArray(rechargePayload);
|
|
|
|
|
+ if (array.isEmpty()) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("redeemcloud session is empty");
|
|
|
|
|
+ }
|
|
|
|
|
+ Object first = array.get(0);
|
|
|
|
|
+ rechargePayload = unwrapJsonString(first instanceof JSONObject ? first.toString() : String.valueOf(first));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StrUtil.startWith(rechargePayload.trim(), "{")) {
|
|
|
|
|
+ JSONObject session = new JSONObject();
|
|
|
|
|
+ session.putOpt("accessToken", rechargePayload);
|
|
|
|
|
+ return verifyAccount(baseUrl, cardKey, session.toString()).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject payload = JSONUtil.parseObj(rechargePayload);
|
|
|
|
|
+ if (isExchangeBody(payload)) {
|
|
|
|
|
+ return payload.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isSessionPayload(payload)) {
|
|
|
|
|
+ return verifyAccount(baseUrl, cardKey, payload.toString()).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ String sessionContent = getSessionContent(payload);
|
|
|
|
|
+ return verifyAccount(baseUrl, cardKey, StrUtil.isNotBlank(sessionContent) ? sessionContent : rechargePayload).toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isExchangeBody(JSONObject payload) {
|
|
|
|
|
+ return payload.containsKey("cdkPackageName") && payload.containsKey("payload") && payload.containsKey("idp");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isSessionPayload(JSONObject payload) {
|
|
|
|
|
+ return payload.containsKey("accessToken") && (payload.containsKey("user") || payload.containsKey("account") || payload.containsKey("expires"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getSessionContent(JSONObject payload) {
|
|
|
|
|
+ String accessToken = payload.getStr("accessToken");
|
|
|
|
|
+ if (StrUtil.isBlank(accessToken)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ accessToken = unwrapJsonString(accessToken);
|
|
|
|
|
+ if (StrUtil.startWith(accessToken.trim(), "{")) {
|
|
|
|
|
+ return accessToken;
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject session = new JSONObject();
|
|
|
|
|
+ session.putOpt("accessToken", accessToken);
|
|
|
|
|
+ return session.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSONObject verifyAccount(String baseUrl, String cardKey, String content) {
|
|
|
|
|
+ String verifyContent = buildChatGptVerifyContent(content);
|
|
|
|
|
+ String url = baseUrl + "/account/verify";
|
|
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
|
|
+ requestBody.putOpt("cdkCode", cardKey);
|
|
|
|
|
+ requestBody.putOpt("content", verifyContent);
|
|
|
|
|
+ log.info("redeemcloud account verify request - contentLength: {}", verifyContent.length());
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
|
|
+ .header("Accept-Language", "zh-CN")
|
|
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
|
|
+ .header("X-Product-Code", "GPT")
|
|
|
|
|
+ .setConnectionTimeout(60000)
|
|
|
|
|
+ .body(requestBody.toString())
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ String body = response.body();
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject jsonResponse = parseResponseObject(body, "account verify");
|
|
|
|
|
+ Object data = jsonResponse.get("data");
|
|
|
|
|
+ log.info("redeemcloud account verify response - code: {}, message: {}, hasData: {}",
|
|
|
|
|
+ jsonResponse.getInt("code"), jsonResponse.getStr("message"), data instanceof JSONObject);
|
|
|
|
|
+ if (isSuccessResponse(jsonResponse) && data instanceof JSONObject) {
|
|
|
|
|
+ return (JSONObject) data;
|
|
|
|
|
+ }
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance(getErrorMessage(data instanceof JSONObject ? (JSONObject) data : null, jsonResponse));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String buildChatGptVerifyContent(String content) {
|
|
|
|
|
+ String verifyContent = unwrapJsonString(content);
|
|
|
|
|
+ if (StrUtil.isBlank(verifyContent)) {
|
|
|
|
|
+ return verifyContent;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StrUtil.startWith(verifyContent.trim(), "{")) {
|
|
|
|
|
+ JSONObject session = new JSONObject();
|
|
|
|
|
+ session.putOpt("accessToken", verifyContent);
|
|
|
|
|
+ return session.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ JSONObject source = JSONUtil.parseObj(verifyContent);
|
|
|
|
|
+ String accessToken = source.getStr("accessToken");
|
|
|
|
|
+ if (StrUtil.isBlank(accessToken)) {
|
|
|
|
|
+ return verifyContent;
|
|
|
|
|
+ }
|
|
|
|
|
+ accessToken = unwrapJsonString(accessToken);
|
|
|
|
|
+ if (StrUtil.startWith(accessToken.trim(), "{")) {
|
|
|
|
|
+ return buildChatGptVerifyContent(accessToken);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isCompleteChatGptSession(source)) {
|
|
|
|
|
+ return verifyContent;
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject session = new JSONObject();
|
|
|
|
|
+ session.putOpt("accessToken", accessToken);
|
|
|
|
|
+ return session.toString();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return verifyContent;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isCompleteChatGptSession(JSONObject source) {
|
|
|
|
|
+ return source.get("user") instanceof JSONObject
|
|
|
|
|
+ || source.get("account") instanceof JSONObject
|
|
|
|
|
+ || source.containsKey("expires")
|
|
|
|
|
+ || source.containsKey("authProvider")
|
|
|
|
|
+ || source.containsKey("sessionToken");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private HttpRequest addBrowserHeaders(HttpRequest request) {
|
|
|
|
|
+ return request
|
|
|
|
|
+ .header("accept", "*/*")
|
|
|
|
|
+ .header("accept-language", "zh-CN")
|
|
|
|
|
+ .header("origin", "https://redeemcloud.com")
|
|
|
|
|
+ .header("referer", "https://redeemcloud.com/zh")
|
|
|
|
|
+ .header("sec-ch-ua", "\"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"150\", \"Microsoft Edge\";v=\"150\"")
|
|
|
|
|
+ .header("sec-ch-ua-mobile", "?0")
|
|
|
|
|
+ .header("sec-ch-ua-platform", "\"Windows\"")
|
|
|
|
|
+ .header("sec-fetch-dest", "empty")
|
|
|
|
|
+ .header("sec-fetch-mode", "cors")
|
|
|
|
|
+ .header("sec-fetch-site", "same-origin")
|
|
|
|
|
+ .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0")
|
|
|
|
|
+ .header("x-product-code", "GPT");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isSuccessResponse(JSONObject jsonResponse) {
|
|
|
|
|
+ Integer code = jsonResponse.getInt("code");
|
|
|
|
|
+ return code != null && (code == SUCCESS_CODE || code == LEGACY_SUCCESS_CODE) && !Boolean.FALSE.equals(jsonResponse.getBool("flag"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String parseDirectTaskId(String body) {
|
|
|
|
|
+ String responseBody = unwrapJsonString(body);
|
|
|
|
|
+ if (StrUtil.isBlank(responseBody) || StrUtil.startWith(responseBody.trim(), "{") || StrUtil.startWith(responseBody.trim(), "[")) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return responseBody;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JSONObject parseResponseObject(String body, String apiName) {
|
|
|
|
|
+ String responseBody = unwrapJsonString(body);
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (StrUtil.startWith(responseBody.trim(), "[")) {
|
|
|
|
|
+ JSONArray array = JSONUtil.parseArray(responseBody);
|
|
|
|
|
+ if (!array.isEmpty() && array.get(0) instanceof JSONObject) {
|
|
|
|
|
+ return (JSONObject) array.get(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return JSONUtil.parseObj(responseBody);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("redeemcloud {0} response is not json object: {1}", apiName, shortBody(responseBody));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String unwrapJsonString(String value) {
|
|
|
|
|
+ String result = StrUtil.trim(value);
|
|
|
|
|
+ for (int i = 0; i < 3 && StrUtil.startWith(result, "\""); i++) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ result = StrUtil.trim(Jsons.parseObject(result, String.class));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String shortBody(String body) {
|
|
|
|
|
+ if (StrUtil.isBlank(body)) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ String cleanBody = body.replaceAll("\\s+", " ");
|
|
|
|
|
+ return StrUtil.sub(cleanBody, 0, Math.min(cleanBody.length(), 200));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String encode(String value) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
|
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public TaskResult getTaskResult(GroupsRelation relation) {
|
|
|
|
|
+ String cardKey = relation.getCardKey();
|
|
|
|
|
+ String taskId = relation.getGptTaskId();
|
|
|
|
|
+ try {
|
|
|
|
|
+ RedeemCloudTaskResultResponse taskResultResponse = queryTaskResult(cardKey, taskId);
|
|
|
|
|
+ TaskResult taskResult = convertTaskResult(taskResultResponse);
|
|
|
|
|
+ resetGroupsRelationRechargeStatus(relation, taskResult.getStatus(), taskResult.getError());
|
|
|
|
|
+ return taskResult;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("query redeemcloud task result failed: {}", e.getMessage(), e);
|
|
|
|
|
+ throw new RuntimeException("获取任务结果失败: " + e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private RedeemCloudTaskResultResponse queryTaskResult(String cardKey, String taskId) throws Exception {
|
|
|
|
|
+ String baseUrl = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
|
|
+ String url = baseUrl + "/sys-cdk/exchange/result?taskId=" + encode(taskId);
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.get(url)
|
|
|
|
|
+ .setConnectionTimeout(10000)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ String body = response.body();
|
|
|
|
|
+ log.info("redeemcloud task result response: {}", body);
|
|
|
|
|
+ return Jsons.parseObject(body, RedeemCloudTaskResultResponse.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private TaskResult convertTaskResult(RedeemCloudTaskResultResponse taskResultResponse) {
|
|
|
|
|
+ TaskResult result = new TaskResult();
|
|
|
|
|
+ if (taskResultResponse == null || taskResultResponse.getCode() == null || taskResultResponse.getCode() != SUCCESS_CODE) {
|
|
|
|
|
+ result.setStatus("failed");
|
|
|
|
|
+ result.setError(taskResultResponse != null ? taskResultResponse.getMessage() : "任务查询失败");
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (taskResultResponse.getData() == null) {
|
|
|
|
|
+ result.setStatus("recharging");
|
|
|
|
|
+ result.setResult(taskResultResponse.getMessage());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ String status = taskResultResponse.getData().getStatus();
|
|
|
|
|
+ if ("COMPLETED".equalsIgnoreCase(status)) {
|
|
|
|
|
+ result.setStatus("completed");
|
|
|
|
|
+ } else if ("FAILED".equalsIgnoreCase(status) || "ERROR".equalsIgnoreCase(status)) {
|
|
|
|
|
+ result.setStatus("failed");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result.setStatus("recharging");
|
|
|
|
|
+ }
|
|
|
|
|
+ result.setResult(status);
|
|
|
|
|
+ result.setError(taskResultResponse.getData().getErrorMessage());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getErrorMessage(JSONObject data, JSONObject response) {
|
|
|
|
|
+ if (data != null) {
|
|
|
|
|
+ String error = data.getStr("error");
|
|
|
|
|
+ if (StrUtil.isNotBlank(error)) {
|
|
|
|
|
+ return error;
|
|
|
|
|
+ }
|
|
|
|
|
+ String errorMessage = data.getStr("errorMessage");
|
|
|
|
|
+ if (StrUtil.isNotBlank(errorMessage)) {
|
|
|
|
|
+ return errorMessage;
|
|
|
|
|
+ }
|
|
|
|
|
+ String result = data.getStr("result");
|
|
|
|
|
+ if (StrUtil.isNotBlank(result)) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ String message = response.getStr("message");
|
|
|
|
|
+ if (StrUtil.isBlank(message)) {
|
|
|
|
|
+ message = response.getStr("msg");
|
|
|
|
|
+ }
|
|
|
|
|
+ return StrUtil.isNotBlank(message) ? message : "提交任务失败";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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, String status, String result) {
|
|
|
|
|
+ if (!"completed".equals(status) && !"failed".equals(status) && !"unknown".equals(status)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ relation = relationMapper.selectById(relation.getId());
|
|
|
|
|
+ if (relation == null || relation.getRechargeStatus() != GroupsRelation.RechargeStatus.recharging) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String taskId = relation.getGptTaskId();
|
|
|
|
|
+ Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
|
|
+ GroupsRelation.RechargeStatus rechargeStatus = "completed".equals(status) ? GroupsRelation.RechargeStatus.complete : GroupsRelation.RechargeStatus.recharge_error;
|
|
|
|
|
+ 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));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.recharge_error
|
|
|
|
|
+ && !redisService.setNx(RedisService.key.GPT_RECHARGE_RECOVER_KEY.getName() + taskId, taskId, RedisService.key.GPT_RECHARGE_RECOVER_KEY.getTimeout())) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Date startTime = relation.getStartTime();
|
|
|
|
|
+ DateTime expiryTime = null;
|
|
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.complete && startTime == null) {
|
|
|
|
|
+ GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
|
|
|
|
|
+ if (groupsTrips != null) {
|
|
|
|
|
+ startTime = DateTime.now();
|
|
|
|
|
+ GoodsDonSku sku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
|
|
+ if (sku.getDays() != null && sku.getDays() > 0) {
|
|
|
|
|
+ expiryTime = DateUtil.offsetDay(startTime, sku.getDays());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ expiryTime = DateUtil.offsetMonth(startTime, sku.getMonths());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ String rechargeErrorInfo = rechargeStatus == GroupsRelation.RechargeStatus.recharge_error ? result : null;
|
|
|
|
|
+ int update = 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 (update == 0) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.complete) {
|
|
|
|
|
+ String remark = StrUtil.isNotBlank(relation.getOperator()) ? "后台自动代充" : "自动代充";
|
|
|
|
|
+ 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);
|
|
|
|
|
+ if (orderDon != null && orderDon.getStatus() == OrderDon.Status.hasPayment) {
|
|
|
|
|
+ orderDon.setStatus(OrderDon.Status.complete);
|
|
|
|
|
+ orderDonMapper.updateById(orderDon);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|