|
|
@@ -2862,6 +2862,88 @@ public class GroupRelationFrontServiceImpl implements GroupRelationFrontService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public ReceiveGptActivityTicketResp receiveStudentCertGptApiQuotaTicket(long userId) throws Exception {
|
|
|
+ List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ userIds = List.of(userId);
|
|
|
+ }
|
|
|
+ String lockKey = String.format("%s:%s", RedisKey.STUDENT_CERT_GPT_API_QUOTA_KEY, Collections.min(userIds));
|
|
|
+ if (!redisService.setNx(lockKey, userId, 60L)) {
|
|
|
+ throw BusinessRuntimeException.getInstance("领取中,请稍后再试");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ TicketRedemptionCodeRecord existRecord = ticketRedemptionCodeRecordMapper.selectOne(Wrappers.lambdaQuery(TicketRedemptionCodeRecord.class)
|
|
|
+ .in(TicketRedemptionCodeRecord::getUserId, userIds)
|
|
|
+ .eq(TicketRedemptionCodeRecord::getSkuId, Constant.GPT_ACTIVITY_GIFT_SKU_ID)
|
|
|
+ .eq(TicketRedemptionCodeRecord::getSourceType, RedisKey.STUDENT_CERT_GPT_API_QUOTA_KEY)
|
|
|
+ .eq(TicketRedemptionCodeRecord::getStatus, "success")
|
|
|
+ .orderByDesc(TicketRedemptionCodeRecord::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (existRecord != null) {
|
|
|
+ ReceiveGptActivityTicketResp resp = new ReceiveGptActivityTicketResp();
|
|
|
+ resp.setRelationId(existRecord.getRelationId());
|
|
|
+ resp.setRedeemCode(existRecord.getRedeemCode());
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ TicketRedemptionCodeRecord record = new TicketRedemptionCodeRecord();
|
|
|
+ record.setUserId(userId);
|
|
|
+ record.setSkuId(Constant.GPT_ACTIVITY_GIFT_SKU_ID);
|
|
|
+ record.setSourceType(RedisKey.STUDENT_CERT_GPT_API_QUOTA_KEY);
|
|
|
+ record.setRedeemName("大学生认证API站额度券");
|
|
|
+ record.setStatus("processing");
|
|
|
+ ticketRedemptionCodeRecordMapper.insert(record);
|
|
|
+
|
|
|
+ GoodsDonSku giftSku = goodsDonSkuMapper.selectById(Constant.GPT_ACTIVITY_GIFT_SKU_ID);
|
|
|
+ GroupsRelation giftRelation = null;
|
|
|
+ try {
|
|
|
+ if (giftSku == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重新领取");
|
|
|
+ }
|
|
|
+ Long giftRelationId = getTicket(userId, giftSku.getId(), Boolean.FALSE);
|
|
|
+ if (giftRelationId == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重新领取");
|
|
|
+ }
|
|
|
+ giftRelation = groupsRelationMapper.selectById(giftRelationId);
|
|
|
+ if (giftRelation == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重新领取");
|
|
|
+ }
|
|
|
+
|
|
|
+ GptActivityRedeemResult redeemResult = requestGptActivityRedeemCode(Constant.AI_API_SVC_DOMAIN, giftRelation, giftSku);
|
|
|
+ record.setRelationId(giftRelation.getId());
|
|
|
+ record.setGoodsId(giftSku.getGoodsId());
|
|
|
+ record.setRedeemCode(redeemResult.getRedeemCode());
|
|
|
+ record.setQuota(redeemResult.getQuota());
|
|
|
+ record.setRedeemCount(0);
|
|
|
+ record.setExpiredTime(redeemResult.getExpiredTime());
|
|
|
+ record.setStatus("success");
|
|
|
+ record.setRelationExpiryTime(giftRelation.getExpiryTime());
|
|
|
+ record.setApiResponse(redeemResult.getApiResponse());
|
|
|
+ ticketRedemptionCodeRecordMapper.updateById(record);
|
|
|
+
|
|
|
+ ReceiveGptActivityTicketResp resp = new ReceiveGptActivityTicketResp();
|
|
|
+ resp.setRelationId(giftRelation.getId());
|
|
|
+ resp.setRedeemCode(redeemResult.getRedeemCode());
|
|
|
+ return resp;
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (giftSku != null) {
|
|
|
+ record.setGoodsId(giftSku.getGoodsId());
|
|
|
+ }
|
|
|
+ record.setStatus("error");
|
|
|
+ record.setErrorMsg(StringUtil.getErrorText(e));
|
|
|
+ if (giftRelation != null) {
|
|
|
+ record.setRelationId(giftRelation.getId());
|
|
|
+ record.setRelationExpiryTime(giftRelation.getExpiryTime());
|
|
|
+ }
|
|
|
+ ticketRedemptionCodeRecordMapper.updateById(record);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ redisService.del(lockKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private boolean isGptApiQuotaSourceTicket(OrderDon orderDon, Long goodsId, Integer skuNum) {
|
|
|
if (orderDon == null) {
|
|
|
return false;
|