zoujiajian 1 год назад
Родитель
Сommit
30789c4763

+ 2 - 1
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -856,7 +856,8 @@ public class RedisService {
         NETFLIX_CHANGE_TICKET_KEY("netflix_change_ticket_key:", "奈飞更换车票key", 60l),
         NETFLIX_CHANGE_TICKET_KEY("netflix_change_ticket_key:", "奈飞更换车票key", 60l),
         CORP_KF_ACCOUNT_URL("corp_kf_account_url:", "微信客服url:", 60 * 60 * 6),
         CORP_KF_ACCOUNT_URL("corp_kf_account_url:", "微信客服url:", 60 * 60 * 6),
         CORP_WX_YL_KEY("corp_wx_yl_key:", "企业微信引流", 60 * 30),
         CORP_WX_YL_KEY("corp_wx_yl_key:", "企业微信引流", 60 * 30),
-        TG_MINI_USER_SAHRE_COUNT_KEY("mini_popualrize_user_sahre_count_key:", "推广小程序用户分享次数key", 60 * 60 * 24l)
+        TG_MINI_USER_SAHRE_COUNT_KEY("mini_popualrize_user_sahre_count_key:", "推广小程序用户分享次数key", 60 * 60 * 24l),
+        GPT_RECHARGE_KEY("gpt_recharge_key:", "gpt代充", 60),
         ;
         ;
 
 
         private String name;
         private String name;

+ 72 - 58
netflix-service/src/main/java/com/cyksj/server/recharge/impl/GptProxyRechargeServiceImpl.java

@@ -11,6 +11,7 @@ import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
 import com.cyksj.mapper.*;
 import com.cyksj.mapper.*;
 import com.cyksj.model.entity.*;
 import com.cyksj.model.entity.*;
 import com.cyksj.model.request.GroupsRelationRechargeReq;
 import com.cyksj.model.request.GroupsRelationRechargeReq;
+import com.cyksj.redis.RedisService;
 import com.cyksj.server.recharge.GptProxyRechargeService;
 import com.cyksj.server.recharge.GptProxyRechargeService;
 import com.cyksj.server.recharge.dto.CardKeyValidationResult;
 import com.cyksj.server.recharge.dto.CardKeyValidationResult;
 import com.cyksj.server.recharge.dto.TaskResult;
 import com.cyksj.server.recharge.dto.TaskResult;
@@ -50,6 +51,8 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
 
 
 	private final GptUserRechargeRecordService gptUserRechargeRecordService;
 	private final GptUserRechargeRecordService gptUserRechargeRecordService;
 
 
+	private final RedisService redisService;
+
 	private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
 	private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
 
 
 	private HttpRequest createBaseRequest(String uri) {
 	private HttpRequest createBaseRequest(String uri) {
@@ -197,70 +200,81 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
 			throw BusinessRuntimeException.getInstance("剩余可代充次数为0");
 			throw BusinessRuntimeException.getInstance("剩余可代充次数为0");
 		}
 		}
 		if (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharging) {
 		if (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharging) {
-			throw BusinessRuntimeException.getInstance("正在充值中");
+			throw BusinessRuntimeException.getInstance("确认充值中");
 		}
 		}
 		if (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharge_error) {
 		if (relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharge_error) {
 			throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
 			throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
 		}
 		}
-		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 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("确认充值中");
 		}
 		}
-		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("充值失败,请联系客服开通");
+		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("充值失败,请联系客服开通");
+			}
+			relation.setRechargeStatus(GroupsRelation.RechargeStatus.recharging);
+			relation.setCardKey(gptCardKey);
+			String taskId = result.getTaskId();
+			relation.setGptTaskId(taskId);
+			relationMapper.updateById(relation);
+			return taskId;
+		} finally {
+			if (redisService.hasKey(key)) {
+				redisService.del(key);
+			}
 		}
 		}
-		relation.setRechargeStatus(GroupsRelation.RechargeStatus.recharging);
-		relation.setCardKey(gptCardKey);
-		String taskId = result.getTaskId();
-		relation.setGptTaskId(taskId);
-		relationMapper.updateById(relation);
-		return taskId;
 	}
 	}
 
 
 	/**
 	/**