|
|
@@ -0,0 +1,130 @@
|
|
|
+package com.cyksj.service.claude.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.common.constant.Constant;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.mapper.GroupsRelationMapper;
|
|
|
+import com.cyksj.model.entity.GoodsDonSku;
|
|
|
+import com.cyksj.model.entity.GroupsRelation;
|
|
|
+import com.cyksj.model.request.ClaudeCodeApiKeysReq;
|
|
|
+import com.cyksj.model.request.ClaudeCodeApiKeysStatusReq;
|
|
|
+import com.cyksj.model.response.claudecode.ClaudeCodeResp;
|
|
|
+import com.cyksj.model.views.ClaudeCodeUserApiKeysView;
|
|
|
+import com.cyksj.service.claude.ClaudeCodeService;
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 项目名: yhlxj11111111
|
|
|
+ * 文件名: ClaudeCodeServiceImpl
|
|
|
+ * 创建者: JavaZou
|
|
|
+ * 创建时间:2025/8/8 10:55
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+ private final GroupsRelationMapper relationMapper;
|
|
|
+
|
|
|
+ private final static String CLAUDE_CODE_API_PREFIX = "http://104.238.220.246:18080/";
|
|
|
+
|
|
|
+ public static final String CLAUDE_CODE_API_ADMIN_KEY = "claudecodeyhlxjclaude";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void generateClaudeCodeUserInfo(Long relationId, Long userId, GoodsDonSku sku) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).eq(GroupsRelation::getId, relationId).in(GroupsRelation::getUserId, userIdList));
|
|
|
+ Date expiryTime = relation.getExpiryTime();
|
|
|
+ Integer codePoints = sku.getCodePoints();
|
|
|
+ Integer codeCreditRecovery = sku.getCodeCreditRecovery();
|
|
|
+ //生成claude code用户套餐
|
|
|
+ try {
|
|
|
+ createOrUpdateClaudeCodeUserPackage(userId, sku.getSpecVal(), codePoints, codeCreditRecovery, expiryTime);
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void createApiKeys(ClaudeCodeApiKeysReq req) {
|
|
|
+ Long userId = req.getUserId();
|
|
|
+ if (checkClaudeCodeUser(userId)) {
|
|
|
+ String apiKeyUrl = String.format(CLAUDE_CODE_API_PREFIX + "api/users/%s/keys", userId);
|
|
|
+ Map<String, Object> parmas = new HashMap<>();
|
|
|
+ parmas.put("name", req.getName());
|
|
|
+ Integer expiresDays = req.getExpiresDays();
|
|
|
+ if (expiresDays != null) {
|
|
|
+ parmas.put("expires_days", expiresDays);
|
|
|
+ }
|
|
|
+ String responseBody = executeClaudeCodePostApi(apiKeyUrl, parmas.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateApiKeysStatus(ClaudeCodeApiKeysStatusReq req) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteApiKeysById(Long userId, Long keyId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ClaudeCodeUserApiKeysView> getUserApiKeys(long userId) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void createOrUpdateClaudeCodeUserPackage(Long userId, String planName, Integer codePoints, Integer codeCreditRecovery, Date expiryTime) throws Exception {
|
|
|
+ Map<String, Object> parmas = new HashMap<>();
|
|
|
+ parmas.put("plan_name", planName.replaceAll(";", ""));
|
|
|
+ parmas.put("credit_limit", codePoints);
|
|
|
+ parmas.put("credit_recovery", codeCreditRecovery);
|
|
|
+ parmas.put("duration_days", expiryTime);
|
|
|
+ String planUrl = String.format(CLAUDE_CODE_API_PREFIX + "api/users/%s/plan", userId);
|
|
|
+ String responseBody = executeClaudeCodePostApi(planUrl, parmas.toString());
|
|
|
+ ClaudeCodeResp claudeCodeResp = Jsons.parseObject(responseBody, ClaudeCodeResp.class);
|
|
|
+ if (claudeCodeResp.getSuccess()) {
|
|
|
+ log.info("用户claude code用户:{}套餐:{}创建或更新成功", userId, planName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验是否是claude code用户
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean checkClaudeCodeUser(Long userId) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ Integer count = relationMapper.selectCountByGoodsId(userIdList, Constant.CLAUDE_CODE_GOODS_ID, DateTime.now());
|
|
|
+ return count > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * claude code Post请求
|
|
|
+ */
|
|
|
+ public String executeClaudeCodePostApi(String url, String body) {
|
|
|
+ HttpResponse execute = HttpUtil.createPost(url)
|
|
|
+ .header("X-Admin-Key", CLAUDE_CODE_API_ADMIN_KEY)
|
|
|
+ .setConnectionTimeout(3000)
|
|
|
+ .setReadTimeout(3000)
|
|
|
+ .body(body)
|
|
|
+ .execute();
|
|
|
+ return execute.body();
|
|
|
+ }
|
|
|
+}
|