|
|
@@ -6,6 +6,7 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpException;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.http.Method;
|
|
|
@@ -44,10 +45,7 @@ import com.ejlchina.searcher.BeanSearcher;
|
|
|
import com.ejlchina.searcher.param.Operator;
|
|
|
import com.ejlchina.searcher.util.MapBuilder;
|
|
|
import com.ejlchina.searcher.util.MapUtils;
|
|
|
-import com.github.rholder.retry.Retryer;
|
|
|
-import com.github.rholder.retry.RetryerBuilder;
|
|
|
-import com.github.rholder.retry.StopStrategies;
|
|
|
-import com.github.rholder.retry.WaitStrategies;
|
|
|
+import com.github.rholder.retry.*;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
@@ -357,6 +355,110 @@ public class CodexServiceImpl implements CodexService {
|
|
|
return orderDon;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Boolean getUserSubs(Long userId) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ //是否有该车票
|
|
|
+ GroupsRelationView groupsRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
|
|
|
+ .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
|
|
|
+ .field(GroupsRelationView::getGoodsId, Constant.CODEX_GOODS_ID)
|
|
|
+ .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
|
|
|
+ .build());
|
|
|
+ CodexUser codexUser = codexUserMapper.selectOne(Wrappers.lambdaQuery(CodexUser.class).in(CodexUser::getUserId, userIdList).last("limit 1"));
|
|
|
+ //车票不存在
|
|
|
+ if (groupsRelationView == null) {
|
|
|
+ //套餐是否存在
|
|
|
+ if (codexUser != null) {
|
|
|
+ //清除用户套餐
|
|
|
+ if (delUserPackages(codexUser.getUserId())) {
|
|
|
+ codexUserMapper.deleteById(codexUser.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CodexUserInfoView getUserSubsInfo(long userId) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ //是否有该车票
|
|
|
+ GroupsRelationView groupsRelationView = beanSearcher.searchFirst(GroupsRelationView.class, MapUtils.builder()
|
|
|
+ .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
|
|
|
+ .field(GroupsRelationView::getGoodsId, Constant.CODEX_GOODS_ID)
|
|
|
+ .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
|
|
|
+ .build());
|
|
|
+ CodexUser codexUser = codexUserMapper.selectOne(Wrappers.lambdaQuery(CodexUser.class).in(CodexUser::getUserId, userIdList).last("limit 1"));
|
|
|
+ //车票不存在
|
|
|
+ if (groupsRelationView == null) {
|
|
|
+ //套餐是否存在
|
|
|
+ if (codexUser != null) {
|
|
|
+ //清除用户套餐
|
|
|
+ if (delUserPackages(codexUser.getUserId())) {
|
|
|
+ codexUserMapper.deleteById(codexUser.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ GoodsDonSku sku = null;
|
|
|
+ if (codexUser == null) {
|
|
|
+ OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
+ .eq(OrderDon::getGoodsId, Constant.CODEX_GOODS_ID)
|
|
|
+ .eq(OrderDon::getRelationId, groupsRelationView.getId())
|
|
|
+ .in(OrderDon::getUserId, userIdList)
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
|
|
|
+ .orderByDesc(OrderDon::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (orderDon != null) {
|
|
|
+ sku = skuMapper.selectById(orderDon.getSkuId());
|
|
|
+ createOrUpdateCodexUserInfo(orderDon.getId(), orderDon.getRelationId(), orderDon.getUserId(), sku, orderDon.getOrderType());
|
|
|
+ } else {
|
|
|
+ //试用车票
|
|
|
+ Long skuId = groupsRelationView.getSkuId();
|
|
|
+ sku = skuMapper.selectById(skuId);
|
|
|
+ createOrUpdateCodexUserInfo(null, groupsRelationView.getId(), groupsRelationView.getUserId(), sku, null);
|
|
|
+ }
|
|
|
+ codexUser = codexUserMapper.selectOne(Wrappers.lambdaQuery(CodexUser.class).in(CodexUser::getUserId, userIdList).last("limit 1"));
|
|
|
+ } else {
|
|
|
+ codexUser.setExpiryTime(groupsRelationView.getExpiryTime());
|
|
|
+ codexUserMapper.updateById(codexUser);
|
|
|
+ }
|
|
|
+ //续费升级
|
|
|
+ if (codexUser.getRenewSkuId() != null) {
|
|
|
+ sku = skuMapper.selectById(codexUser.getRenewSkuId());
|
|
|
+ }
|
|
|
+ if (sku == null) {
|
|
|
+ sku = skuMapper.selectById(groupsRelationView.getSkuId());
|
|
|
+ }
|
|
|
+ //是否需要重试更新 cladueCode userInfo信息
|
|
|
+ if (codexUser.getIsRetry()) {
|
|
|
+ try {
|
|
|
+ ClaudeCodeResp codeUserPackage = createOrUpdateCodexUserPackageApi(codexUser.getUserId(), codexUser.getPlanName(), codexUser.getOpenaiDailyLimit(), codexUser.getOpenaiQuota(), codexUser.getExpiryTime());
|
|
|
+ if (!Constant.SUCCESS.equals(codeUserPackage.getMessage())) {
|
|
|
+ codexUser.setIsRetry(Boolean.TRUE);
|
|
|
+ codexUserMapper.updateById(codexUser);
|
|
|
+ } else {
|
|
|
+ codexUser.setIsRetry(Boolean.FALSE);
|
|
|
+ codexUserMapper.updateById(codexUser);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return CodexUserInfoView.builder()
|
|
|
+ .planName(codexUser.getPlanName())
|
|
|
+ .relationId(codexUser.getRelationId())
|
|
|
+ .openaiDailyLimit(codexUser.getOpenaiDailyLimit())
|
|
|
+ .openaiQuota(codexUser.getOpenaiQuota())
|
|
|
+ .expiryTime(codexUser.getExpiryTime())
|
|
|
+ .renewSkuId(codexUser.getRenewSkuId())
|
|
|
+ .renewOrderId(codexUser.getRenewOrderId())
|
|
|
+ .renewExpiryTime(codexUser.getRenewExpiryTime())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
private GroupsRelation getClaudeCodeRelation(Long userId, GoodsDonSku sku) {
|
|
|
//获取车票车位
|
|
|
GroupsRelation relation = getRelation(sku);
|
|
|
@@ -1167,4 +1269,43 @@ public class CodexServiceImpl implements CodexService {
|
|
|
orderDon.setCouponId(couponUser.getCouponId());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private boolean delUserPackages(Long userId) {
|
|
|
+ String url = String.format(Constant.CLAUDE_CODE_API_PREFIX + "api/users/%s/plan?service_type=openai", userId);
|
|
|
+ try {
|
|
|
+ ClaudeCodeResp claudeCodeResp = executeClaudeCodeDeletedApi(url);
|
|
|
+ if (!Constant.SUCCESS.equals(claudeCodeResp.getMessage())) {
|
|
|
+ log.error("删除用户:{} claude code套餐失败:{}", userId, claudeCodeResp.getMessage());
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+ return Boolean.TRUE;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("删除用户:{} claude code套餐报错:{}", userId, StringUtil.getErrorText(e));
|
|
|
+ }
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * claude code DELETE请求
|
|
|
+ */
|
|
|
+ public ClaudeCodeResp executeClaudeCodeDeletedApi(String url) {
|
|
|
+ Retryer<ClaudeCodeResp> build = getApiRetryer(1, 3);
|
|
|
+ try {
|
|
|
+ return build.call(() -> {
|
|
|
+ try {
|
|
|
+ HttpResponse execute = HttpUtil.createRequest(Method.DELETE, url).header(Constant.CLAUDE_CODE_HEARD_KEY, Constant.CLAUDE_CODE_API_ADMIN_KEY).setConnectionTimeout(Constant.CONNECT_MILLISECONDS).execute();
|
|
|
+ ClaudeCodeResp resp = Jsons.parseObject(execute.body(), ClaudeCodeResp.class);
|
|
|
+ if (!Constant.SUCCESS.equals(resp.getMessage())) {
|
|
|
+ log.error("claude code DELETE URL:{}接口返回msg:{}", url, resp.getMessage());
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ } catch (HttpException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (ExecutionException | RetryException e) {
|
|
|
+ log.error("重试调用claude code DELETE请求url->{}失败,msg->{}", url, StringUtil.getErrorText(e));
|
|
|
+ throw BusinessRuntimeException.getInstance("接口请求失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|