|
|
@@ -4,10 +4,27 @@ import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.common.constant.Constant;
|
|
|
+import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
+import com.cyksj.mapper.*;
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
+import com.cyksj.model.request.GroupsRelationRechargeReq;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.server.recharge.GptProxyRechargeService;
|
|
|
-import com.cyksj.server.recharge.dto.*;
|
|
|
+import com.cyksj.server.recharge.dto.CardKeyValidationResult;
|
|
|
+import com.cyksj.server.recharge.dto.TaskResult;
|
|
|
+import com.cyksj.server.recharge.dto.TaskSubmitResult;
|
|
|
+import com.cyksj.server.recharge.dto.TokenParseResult;
|
|
|
+import com.cyksj.service.chatgpt.GptUserRechargeRecordService;
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author chan
|
|
|
@@ -15,11 +32,30 @@ import org.springframework.stereotype.Service;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
|
|
|
private static final String BASE_URL = "https://api.ow520.com/api";
|
|
|
|
|
|
- private HttpRequest createBaseRequest(String uri) {
|
|
|
+ private final GptRechargeCardKeyMapper gptRechargeCardKeyMapper;
|
|
|
+
|
|
|
+ private final GroupsRelationMapper relationMapper;
|
|
|
+
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+ private final GroupsMapper groupsMapper;
|
|
|
+
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
+
|
|
|
+ private final OrderDonMapper orderDonMapper;
|
|
|
+
|
|
|
+ private final GptUserRechargeRecordService gptUserRechargeRecordService;
|
|
|
+
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
+
|
|
|
+ private HttpRequest createBaseRequest(String uri) {
|
|
|
return HttpRequest.get(BASE_URL + uri)
|
|
|
.header("accept", "application/json, text/plain, */*")
|
|
|
.header("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
|
|
|
@@ -123,17 +159,178 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
.execute();
|
|
|
String body = response.body();
|
|
|
log.info("任务查询响应: {}", body);
|
|
|
-
|
|
|
+
|
|
|
JSONObject jsonResponse = JSONUtil.parseObj(body);
|
|
|
TaskResult result = new TaskResult(
|
|
|
jsonResponse.getStr("status"),
|
|
|
jsonResponse.getStr("result")
|
|
|
);
|
|
|
log.info("任务查询结果: {}", result);
|
|
|
- return result;
|
|
|
+ String status = result.getStatus();
|
|
|
+ //更新代充状态
|
|
|
+ resetGroupsRelationRechargeStatus(taskId, status);
|
|
|
+ return result;
|
|
|
} catch (Exception e) {
|
|
|
log.error("获取任务结果失败: {}", e.getMessage(), e);
|
|
|
throw new RuntimeException("获取任务结果失败: " + e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Throwable.class)
|
|
|
+ public String confirmRecharge(GroupsRelationRechargeReq req) {
|
|
|
+ Long relationId = req.getRelationId();
|
|
|
+ Long userId = req.getUserId();
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
+ .in(GroupsRelation::getUserId, userIdList)
|
|
|
+ .eq(GroupsRelation::getId, relationId)
|
|
|
+ .eq(GroupsRelation::getStatus, GroupsRelation.Status.validity));
|
|
|
+ if (relation == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("车票不存在");
|
|
|
+ }
|
|
|
+ GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
|
|
|
+ GoodsDonSku sku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
+ //GPT 代充
|
|
|
+ if (sku.getGoodsId() != Constant.GPT_RECHARGE_GOODS_ID) {
|
|
|
+ throw BusinessRuntimeException.getInstance("车票类型错误");
|
|
|
+ }
|
|
|
+ Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
+ if (rechargeRemainNum == null || rechargeRemainNum <= 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("剩余可代充次数为0");
|
|
|
+ }
|
|
|
+ if (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharging) {
|
|
|
+ throw BusinessRuntimeException.getInstance("确认充值中");
|
|
|
+ }
|
|
|
+ if (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharge_error) {
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
+ }
|
|
|
+ String key = RedisService.key.GPT_RECHARGE_KEY.getName() + relationId;
|
|
|
+ boolean b = redisService.setNx(key, relationId, RedisService.key.GPT_RECHARGE_KEY.getTimeout());
|
|
|
+ if (!b) {
|
|
|
+ throw BusinessRuntimeException.getInstance("确认充值中");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int updateRemainNum = relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
+ .set(GroupsRelation::getRechargeRemainNum, rechargeRemainNum - 1)
|
|
|
+ .eq(GroupsRelation::getId, relationId)
|
|
|
+ .eq(GroupsRelation::getUserId, relation.getUserId())
|
|
|
+ .eq(GroupsRelation::getRechargeRemainNum, rechargeRemainNum));
|
|
|
+ if (updateRemainNum == 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("代充异常,请重试");
|
|
|
+ }
|
|
|
+ OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
+ .in(OrderDon::getUserId, userIdList)
|
|
|
+ .eq(OrderDon::getRelationId, relationId)
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
|
|
|
+ .orderByDesc(OrderDon::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (orderDon == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("代充订单不存在或已退款");
|
|
|
+ }
|
|
|
+ String accessToken = req.getAccessToken();
|
|
|
+ //验证用户token是否正确
|
|
|
+ TokenParseResult tokenParseResult = parseToken(accessToken);
|
|
|
+ if (!tokenParseResult.getSuccess()) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您输入的凭证有误,请重新输入");
|
|
|
+ }
|
|
|
+ String rechargeAccount = tokenParseResult.getMessage();
|
|
|
+ //记录代充账号 token
|
|
|
+ relation.setAccount(rechargeAccount);
|
|
|
+ //获取GPT代充key
|
|
|
+ GptRechargeCardKey gptRechargeCardKey = gptRechargeCardKeyMapper.selectOne(Wrappers.lambdaQuery(GptRechargeCardKey.class)
|
|
|
+ .eq(GptRechargeCardKey::getStatus, Boolean.FALSE)
|
|
|
+ .last("order by rand() limit 1"));
|
|
|
+ if (gptRechargeCardKey == null) {
|
|
|
+ setRechargeRelationStatusAutoError(relation);
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
+ }
|
|
|
+ int update = gptRechargeCardKeyMapper.update(null, Wrappers.lambdaUpdate(GptRechargeCardKey.class)
|
|
|
+ .set(GptRechargeCardKey::getStatus, Boolean.TRUE)
|
|
|
+ .set(GptRechargeCardKey::getOrderId, orderDon.getId())
|
|
|
+ .set(GptRechargeCardKey::getOrderNo, orderDon.getOrderNo())
|
|
|
+ .eq(GptRechargeCardKey::getId, gptRechargeCardKey.getId())
|
|
|
+ .eq(GptRechargeCardKey::getStatus, Boolean.FALSE));
|
|
|
+ if (update == 0) {
|
|
|
+ setRechargeRelationStatusAutoError(relation);
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
+ }
|
|
|
+ String gptCardKey = gptRechargeCardKey.getCardKey();
|
|
|
+ //校验卡密是否可用
|
|
|
+ validateCardKey(gptCardKey);
|
|
|
+ //提交任务
|
|
|
+ TaskSubmitResult result = submitTask(gptCardKey, accessToken, "auth0");
|
|
|
+ if (!result.getSuccess()) {
|
|
|
+ setRechargeRelationStatusAutoError(relation);
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
+ }
|
|
|
+ String taskId = result.getTaskId();
|
|
|
+ relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
+ //记录代充账号
|
|
|
+ .set(GroupsRelation::getAccount, rechargeAccount)
|
|
|
+ .set(GroupsRelation::getRechargeStatus, GroupsRelation.RechargeStatus.recharging)
|
|
|
+ .set(GroupsRelation::getCardKey, gptCardKey)
|
|
|
+ .set(GroupsRelation::getGptTaskId, taskId)
|
|
|
+ .eq(GroupsRelation::getId, relationId));
|
|
|
+ return taskId;
|
|
|
+ } finally {
|
|
|
+ if (redisService.hasKey(key)) {
|
|
|
+ redisService.del(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置代充自动充值异常
|
|
|
+ */
|
|
|
+ public void setRechargeRelationStatusAutoError(GroupsRelation relation) {
|
|
|
+ TASK_EXECUTOR.execute(()->{
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.recharge_error);
|
|
|
+ relationMapper.updateById(relation);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新代充状态
|
|
|
+ */
|
|
|
+ private void resetGroupsRelationRechargeStatus(String taskId, String status) {
|
|
|
+ if ("completed".equals(status) || "failed".equals(status)) {
|
|
|
+ GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
+ .eq(GroupsRelation::getGptTaskId, taskId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (relation != null && relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharging) {
|
|
|
+ Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
+ GroupsRelation.RechargeStatus rechargeStatus = "completed".equals(status) ? GroupsRelation.RechargeStatus.complete : GroupsRelation.RechargeStatus.recharge_error;
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.recharge_error) {
|
|
|
+ String cardKey = relation.getCardKey();
|
|
|
+ GptRechargeCardKey gptRechargeCardKey = gptRechargeCardKeyMapper.selectOne(Wrappers.lambdaQuery(GptRechargeCardKey.class)
|
|
|
+ .eq(GptRechargeCardKey::getCardKey, cardKey)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (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) {
|
|
|
+ if (!redisService.setNx(RedisService.key.GPT_RECHARGE_RECOVER_KEY.getName() + taskId, taskId, RedisService.key.GPT_RECHARGE_RECOVER_KEY.getTimeout())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ relationMapper.update(null, Wrappers.lambdaUpdate(GroupsRelation.class)
|
|
|
+ .set(rechargeStatus == GroupsRelation.RechargeStatus.recharge_error, GroupsRelation::getRechargeRemainNum, rechargeRemainNum + 1)
|
|
|
+ .set(GroupsRelation::getRechargeStatus, rechargeStatus)
|
|
|
+ .eq(GroupsRelation::getId, relation.getId())
|
|
|
+ .le(GroupsRelation::getRechargeRemainNum, relation.getRechargeNum())
|
|
|
+ .eq(GroupsRelation::getRechargeStatus, GroupsRelation.RechargeStatus.recharging));
|
|
|
+ //记录代充记录
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.complete) {
|
|
|
+ gptUserRechargeRecordService.recordGptUserRechargeNum(relation.getUserId(), relation.getId(), relation.getAccount(), 1, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|