|
|
@@ -0,0 +1,294 @@
|
|
|
+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.http.HttpStatus;
|
|
|
+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.common.util.StringUtil;
|
|
|
+import com.cyksj.mapper.*;
|
|
|
+import com.cyksj.model.dto.GptCardTokenDto;
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
+import com.cyksj.model.response.CardVerificationResponse;
|
|
|
+import com.cyksj.model.response.SancRechargeResultResponse;
|
|
|
+import com.cyksj.model.response.ScRechargeResponse;
|
|
|
+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.SancRechargeService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class SancRechargeServiceImpl implements SancRechargeService {
|
|
|
+
|
|
|
+ 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 String SAN_CHUAN_LIMIT_KEY = "SAN_CHUAN_LIMIT_KEY";
|
|
|
+
|
|
|
+ private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CardVerificationResponse validateCardKey(String cardKey) throws Exception {
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ String url = BASE_URL + "/cards/verify";
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.putOpt("cardInfo", cardKey);
|
|
|
+
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
+ .body(params.toString())
|
|
|
+ .execute();
|
|
|
+ log.info("三川验证卡密结果:{}", response.body());
|
|
|
+ CardVerificationResponse resp = Jsons.parseObject(response.body(), CardVerificationResponse.class);
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String confirmRecharge(GroupsRelation relation, String gptCardKey, CmsUser cmsUser) {
|
|
|
+ Long relationId = relation.getId();
|
|
|
+ if (redisService.hasKey(SAN_CHUAN_LIMIT_KEY)) {
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请稍后重试");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance("三川库存不足");
|
|
|
+ }
|
|
|
+ //校验卡密是否可用
|
|
|
+ CardVerificationResponse cardVerificationResponse = null;
|
|
|
+ try {
|
|
|
+ cardVerificationResponse = validateCardKey(gptCardKey);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance("卡密:{0}错误", gptCardKey);
|
|
|
+ }
|
|
|
+ if (!cardVerificationResponse.getData().isSuccess()) {
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance("卡密:{0}不存在", gptCardKey);
|
|
|
+ }
|
|
|
+ String accessToken = relation.getGptToken();
|
|
|
+ String rechargeAccount = relation.getAccount();
|
|
|
+ //提交任务
|
|
|
+ String taskId = null;
|
|
|
+ try {
|
|
|
+ taskId = submitTask(gptCardKey, accessToken);
|
|
|
+ } catch (Exception e) {
|
|
|
+ setRechargeRelationStatusAutoError(relation);
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance("充值失败,{0}", e.getMessage());
|
|
|
+ }
|
|
|
+ String operator = null;
|
|
|
+ if (cmsUser != null) {
|
|
|
+ operator = cmsUser.getNickname();
|
|
|
+ }
|
|
|
+ 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, accessToken)
|
|
|
+ .set(GroupsRelation::getGptTaskId, taskId)
|
|
|
+ .eq(GroupsRelation::getId, relationId));
|
|
|
+ return taskId;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TaskResult getTaskResult(GroupsRelation relation) {
|
|
|
+ try {
|
|
|
+ String taskId = relation.getGptTaskId();
|
|
|
+ String cardKey = relation.getCardKey();
|
|
|
+ SancRechargeResultResponse respData = null;
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ String url = BASE_URL + "/recharge/query-task-status";
|
|
|
+ HttpResponse response = null;
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.putOpt("cardInfo", cardKey);
|
|
|
+ params.putOpt("productId", 3);
|
|
|
+ params.putOpt("taskId", taskId);
|
|
|
+ try {
|
|
|
+ response = HttpRequest.post(url).body(params.toString()).setConnectionTimeout(10000).execute();
|
|
|
+ String body = response.body();
|
|
|
+ log.info("三川任务查询响应: {}", body);
|
|
|
+ respData = Jsons.parseObject(body, SancRechargeResultResponse.class);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("三川查询充值任务失败,{}", StringUtil.getErrorMsg(e));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String taskStatus = respData.getData().getStatus();
|
|
|
+ //充值中
|
|
|
+ TaskResult taskResult = new TaskResult();
|
|
|
+ if ("processing".equals(taskStatus)) {
|
|
|
+ taskResult.setStatus("recharging");
|
|
|
+ return taskResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ String resultResult = respData.getData().getMessage();
|
|
|
+ //更新代充状态
|
|
|
+ resetGroupsRelationRechargeStatus(relation, taskStatus, resultResult);
|
|
|
+
|
|
|
+ if ("completed".equals(taskStatus)) {
|
|
|
+ taskResult.setStatus("completed");
|
|
|
+ } else {
|
|
|
+ taskResult.setStatus("failed");
|
|
|
+ }
|
|
|
+ return taskResult;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取任务结果失败: {}", e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新代充状态
|
|
|
+ */
|
|
|
+ private void resetGroupsRelationRechargeStatus(GroupsRelation relation, String taskStatus, String result) {
|
|
|
+ GroupsRelation.RechargeStatus rechargeStatus;
|
|
|
+ if ("completed".equals(taskStatus)) {
|
|
|
+ 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) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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 = 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(), -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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String submitTask(String cardKey, String accessToken) {
|
|
|
+ log.info("开始提交三川任务 - 卡密: {}", cardKey);
|
|
|
+ try {
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ String url = BASE_URL + "/cards/verify-gpt";
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("cardInfo", cardKey);
|
|
|
+ requestBody.put("fullAuthData", accessToken);
|
|
|
+
|
|
|
+ GptCardTokenDto gptCardTokenDto = Jsons.parseObject(accessToken, GptCardTokenDto.class);
|
|
|
+
|
|
|
+ requestBody.put("userEmail", gptCardTokenDto.getUser().getEmail());
|
|
|
+ requestBody.put("userGptToken", gptCardTokenDto.getAccessToken());
|
|
|
+
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
+ .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0")
|
|
|
+ .setConnectionTimeout(60000)
|
|
|
+ .body(requestBody.toString())
|
|
|
+ .execute();
|
|
|
+ String body = response.body();
|
|
|
+ log.info("三川任务提交响应: {}", body);
|
|
|
+
|
|
|
+ ScRechargeResponse rechargeResponse = Jsons.parseObject(body, ScRechargeResponse.class);
|
|
|
+ if (rechargeResponse.getCode() != HttpStatus.HTTP_OK) {
|
|
|
+ throw BusinessRuntimeException.getInstance(rechargeResponse.getMessage());
|
|
|
+ }
|
|
|
+ return rechargeResponse.getData().getThirdPartyResult();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("提交任务失败: {}", e.getMessage(), e);
|
|
|
+ if (StringUtil.getErrorMsg(e).contains("库存不足")) {
|
|
|
+ if (!redisService.hasKey(SAN_CHUAN_LIMIT_KEY)) {
|
|
|
+ //没库存 缓存5分钟
|
|
|
+ redisService.set(SAN_CHUAN_LIMIT_KEY, "1", 60 * 5L);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("提交任务失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置代充自动充值异常
|
|
|
+ */
|
|
|
+ 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()));
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|