|
|
@@ -4,15 +4,25 @@ import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
-import com.cyksj.mapper.GptRechargeCardKeyMapper;
|
|
|
+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.server.recharge.GptProxyRechargeService;
|
|
|
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.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
|
|
|
@@ -27,7 +37,19 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
|
|
|
private final GptRechargeCardKeyMapper gptRechargeCardKeyMapper;
|
|
|
|
|
|
- private HttpRequest createBaseRequest(String uri) {
|
|
|
+ private final GroupsRelationMapper relationMapper;
|
|
|
+
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+ private GroupsMapper groupsMapper;
|
|
|
+
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
+
|
|
|
+ private final OrderDonMapper orderDonMapper;
|
|
|
+
|
|
|
+ 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")
|
|
|
@@ -138,10 +160,113 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
jsonResponse.getStr("result")
|
|
|
);
|
|
|
log.info("任务查询结果: {}", result);
|
|
|
- return result;
|
|
|
+ String status = result.getStatus();
|
|
|
+ if ("success".equals(status)) {
|
|
|
+ GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
+ .eq(GroupsRelation::getGptTaskId, taskId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (relation != null) {
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.complete);
|
|
|
+ relationMapper.updateById(relation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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");
|
|
|
+ }
|
|
|
+ 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, relation)
|
|
|
+ .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();
|
|
|
+ //获取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("充值失败,请联系客服开通");
|
|
|
+ }
|
|
|
+ relation.setAccount(rechargeAccount);
|
|
|
+ relation.setGptToken(accessToken);
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.auto_recharge);
|
|
|
+ String taskId = result.getTaskId();
|
|
|
+ relation.setGptTaskId(taskId);
|
|
|
+ relationMapper.updateById(relation);
|
|
|
+ return taskId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置代充自动充值异常
|
|
|
+ */
|
|
|
+ public void setRechargeRelationStatusAutoError(GroupsRelation relation) {
|
|
|
+ TASK_EXECUTOR.execute(()->{
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.auto_error);
|
|
|
+ relationMapper.updateById(relation);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|