|
|
@@ -0,0 +1,244 @@
|
|
|
+package com.cyksj.service.recharge.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+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.*;
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
+import com.cyksj.model.response.recharge.YearAfDianCardKeyResp;
|
|
|
+import com.cyksj.model.response.recharge.YearAfDianTaskResp;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
+import com.cyksj.service.chatgpt.GptUserRechargeRecordService;
|
|
|
+import com.cyksj.service.recharge.GptRechargeCardKeyChannelService;
|
|
|
+import com.cyksj.service.recharge.YearAfDianRechargeService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class YearAfDianRechargeServiceImpl implements YearAfDianRechargeService {
|
|
|
+ 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();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public YearAfDianCardKeyResp validateCardKey(String cardKey, String accessToken) throws Exception {
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ String url = BASE_URL + "/verify";
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.putOpt("cdk", cardKey);
|
|
|
+ JSONObject platformCredential = new JSONObject();
|
|
|
+ platformCredential.putOpt("platform", "chatgpt");
|
|
|
+ platformCredential.putOpt("data", Jsons.parseObject(accessToken, JSONObject.class));
|
|
|
+ params.putOpt("platformCredential", platformCredential);
|
|
|
+
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
+ .body(params.toString())
|
|
|
+ .execute();
|
|
|
+ log.info("year.afdian验证卡密结果:{}", response.body());
|
|
|
+ YearAfDianCardKeyResp data = Jsons.parseObject(response.body(), YearAfDianCardKeyResp.class);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String confirmRecharge(GroupsRelation relation, String gptCardKey, CmsUser cmsUser) {
|
|
|
+ Long relationId = relation.getId();
|
|
|
+ //校验卡密是否可用
|
|
|
+ YearAfDianCardKeyResp yearAfDianCardKeyResp = null;
|
|
|
+ try {
|
|
|
+ yearAfDianCardKeyResp = validateCardKey(gptCardKey, relation.getGptToken());
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance("卡密:{0}错误", gptCardKey);
|
|
|
+ }
|
|
|
+ if (!yearAfDianCardKeyResp.getSuccess()) {
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance(yearAfDianCardKeyResp.getError().getMessage());
|
|
|
+ }
|
|
|
+ if (!"unused".equals(yearAfDianCardKeyResp.getData().getCdk().getStatus())) {
|
|
|
+ log.error("卡密:{}已被使用", gptCardKey);
|
|
|
+ if (cmsUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重试");
|
|
|
+ }
|
|
|
+ throw BusinessRuntimeException.getInstance("卡密:{0}已被使用", gptCardKey);
|
|
|
+ }
|
|
|
+ String accessToken = relation.getGptToken();
|
|
|
+ String rechargeAccount = relation.getAccount();
|
|
|
+ //提交任务返回状态
|
|
|
+ Boolean success = false;
|
|
|
+ try {
|
|
|
+ success = 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();
|
|
|
+ String taskId = "yhrgzc:" + RandomUtil.randomNumbers(20);
|
|
|
+ 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));
|
|
|
+ relation.setRechargeStatus(GroupsRelation.RechargeStatus.recharging);
|
|
|
+ resetGroupsRelationRechargeStatus(relation, success, rechargeAccount);
|
|
|
+ return taskId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Boolean submitTask(String cardKey, String accessToken) {
|
|
|
+ log.info("开始提交year.afdian任务 - 卡密: {}", cardKey);
|
|
|
+ try {
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ String url = BASE_URL + "/redeem";
|
|
|
+
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.putOpt("cdk", cardKey);
|
|
|
+ requestBody.putOpt("confirm", true);
|
|
|
+ JSONObject platformCredential = new JSONObject();
|
|
|
+ platformCredential.putOpt("platform", "chatgpt");
|
|
|
+ platformCredential.putOpt("data", Jsons.parseObject(accessToken, JSONObject.class));
|
|
|
+ requestBody.putOpt("platformCredential", platformCredential);
|
|
|
+
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
+ .setConnectionTimeout(10000)
|
|
|
+ .body(requestBody.toString())
|
|
|
+ .execute();
|
|
|
+ String body = response.body();
|
|
|
+ log.info("year.afdian任务提交响应: {}", body);
|
|
|
+
|
|
|
+ YearAfDianTaskResp yearAfDianTaskResp = Jsons.parseObject(body, YearAfDianTaskResp.class);
|
|
|
+ Boolean success = yearAfDianTaskResp.getSuccess();
|
|
|
+ return success;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("提交任务失败: {}", e.getMessage(), e);
|
|
|
+ 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()));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新代充状态
|
|
|
+ */
|
|
|
+ private void resetGroupsRelationRechargeStatus(GroupsRelation relation, Boolean taskStatus, String result) {
|
|
|
+ GroupsRelation.RechargeStatus rechargeStatus;
|
|
|
+ if (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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Integer rechargeNum = 12;
|
|
|
+ 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 + 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));
|
|
|
+ //记录代充记录
|
|
|
+ if (rechargeStatus == GroupsRelation.RechargeStatus.complete) {
|
|
|
+ String remark = "自动代充";
|
|
|
+ if (StrUtil.isNotBlank(relation.getOperator())) {
|
|
|
+ remark = "后台自动代充";
|
|
|
+ }
|
|
|
+ gptUserRechargeRecordService.recordGptUserRechargeNum(relation.getUserId(), relation.getId(), relation.getAccount(), -rechargeNum, 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|