| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- package com.cyksj.service.claudecode;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.log.Log;
- import cn.hutool.log.LogFactory;
- import com.cyksj.service.claudecode.client.AdminApiClient;
- import com.cyksj.service.claudecode.dto.request.*;
- import com.cyksj.service.claudecode.dto.response.*;
- import com.cyksj.service.claudecode.exception.AdminApiException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.List;
- @Service
- public class AdminApiService {
-
- private static final Log log = LogFactory.get();
-
- @Autowired
- private AdminApiClient adminApiClient;
-
- // ========== 用户套餐管理 ==========
-
- /**
- * 创建或更新用户套餐
- */
- public UserPlanResponse createOrUpdateUserPlan(Long userId, CreateUserPlanRequest request) {
- try {
- String url = "/api/users/" + userId + "/plan";
- AdminApiResponse<UserPlanResponse> response = adminApiClient.put(url, request, UserPlanResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("创建或更新用户套餐失败, userId: {}", userId, e);
- throw new AdminApiException("创建或更新用户套餐失败: " + e.getMessage());
- }
- }
-
- /**
- * 查询用户套餐
- */
- public UserPlanResponse getUserPlan(Long userId) {
- try {
- String url = "/api/users/" + userId + "/plan";
- AdminApiResponse<UserPlanResponse> response = adminApiClient.get(url, UserPlanResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("查询用户套餐失败, userId: {}", userId, e);
- throw new AdminApiException("查询用户套餐失败: " + e.getMessage());
- }
- }
-
- /**
- * 调整用户积分
- */
- public CreditAdjustResponse adjustUserCredits(Long userId, CreditAdjustRequest request) {
- try {
- String url = "/api/users/" + userId + "/credits/adjust";
- AdminApiResponse<CreditAdjustResponse> response = adminApiClient.post(url, request, CreditAdjustResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("调整用户积分失败, userId: {}", userId, e);
- throw new AdminApiException("调整用户积分失败: " + e.getMessage());
- }
- }
-
- /**
- * 查询用户余额
- */
- public UserBalanceResponse getUserBalance(Long userId) {
- try {
- String url = "/api/users/" + userId + "/balance";
- AdminApiResponse<UserBalanceResponse> response = adminApiClient.get(url, UserBalanceResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("查询用户余额失败, userId: {}", userId, e);
- throw new AdminApiException("查询用户余额失败: " + e.getMessage());
- }
- }
-
- // ========== API Key管理 ==========
-
- /**
- * 创建API Key
- */
- public ApiKeyResponse createApiKey(Long userId, CreateApiKeyRequest request) {
- try {
- String url = "/api/users/" + userId + "/keys";
- AdminApiResponse<ApiKeyResponse> response = adminApiClient.post(url, request, ApiKeyResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("创建API Key失败, userId: {}", userId, e);
- throw new AdminApiException("创建API Key失败: " + e.getMessage());
- }
- }
-
- /**
- * 查询用户API Keys
- */
- public List<ApiKeyInfo> getUserApiKeys(Long userId) {
- try {
- String url = "/api/users/" + userId + "/keys";
- AdminApiResponse<List<ApiKeyInfo>> response = adminApiClient.getList(url, ApiKeyInfo.class);
- return response.getData();
- } catch (Exception e) {
- log.error("查询用户API Keys失败, userId: {}", userId, e);
- throw new AdminApiException("查询用户API Keys失败: " + e.getMessage());
- }
- }
-
- /**
- * 更新API Key状态
- */
- public ApiKeyStatusResponse updateApiKeyStatus(Long userId, Long keyId, UpdateApiKeyStatusRequest request) {
- try {
- String url = "/api/keys/" + userId + "/status?keyId=" + keyId;
- AdminApiResponse<ApiKeyStatusResponse> response = adminApiClient.put(url, request, ApiKeyStatusResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("更新API Key状态失败, userId: {}, keyId: {}", userId, keyId, e);
- throw new AdminApiException("更新API Key状态失败: " + e.getMessage());
- }
- }
-
- /**
- * 删除API Key
- */
- public ApiKeyDeleteResponse deleteApiKey(Long userId, Long keyId) {
- try {
- String url = "/api/keys/" + userId + "?keyId=" + keyId;
- AdminApiResponse<ApiKeyDeleteResponse> response = adminApiClient.delete(url, ApiKeyDeleteResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("删除API Key失败, userId: {}, keyId: {}", userId, keyId, e);
- throw new AdminApiException("删除API Key失败: " + e.getMessage());
- }
- }
-
- // ========== 缓存管理 ==========
-
- /**
- * 清理用户缓存
- */
- public CacheInvalidateResponse invalidateUserCache(Long userId) {
- try {
- String url = "/api/cache/invalidate/user/" + userId;
- AdminApiResponse<CacheInvalidateResponse> response = adminApiClient.post(url, null, CacheInvalidateResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("清理用户缓存失败, userId: {}", userId, e);
- throw new AdminApiException("清理用户缓存失败: " + e.getMessage());
- }
- }
-
- /**
- * 清理API Key缓存
- */
- public ApiKeyCacheResponse invalidateApiKeyCache(String apiKey) {
- try {
- String url = "/api/cache/invalidate/apikey";
- InvalidateApiKeyCacheRequest request = new InvalidateApiKeyCacheRequest();
- request.setApiKey(apiKey);
- AdminApiResponse<ApiKeyCacheResponse> response = adminApiClient.post(url, request, ApiKeyCacheResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("清理API Key缓存失败, apiKey: {}", apiKey, e);
- throw new AdminApiException("清理API Key缓存失败: " + e.getMessage());
- }
- }
-
- /**
- * 获取缓存统计信息
- */
- public CacheStatsResponse getCacheStats() {
- try {
- String url = "/api/cache/stats";
- AdminApiResponse<CacheStatsResponse> response = adminApiClient.get(url, CacheStatsResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("获取缓存统计信息失败", e);
- throw new AdminApiException("获取缓存统计信息失败: " + e.getMessage());
- }
- }
-
- // ========== 统计和历史 ==========
-
- /**
- * 查询积分变化明细
- */
- public PageResponse<CreditHistoryItem> getCreditHistory(Long userId, Integer page, Integer limit, String type) {
- try {
- String url = "/api/users/" + userId + "/credits/history";
- if (page != null || limit != null || StrUtil.isNotBlank(type)) {
- url += "?";
- if (page != null) url += "page=" + page + "&";
- if (limit != null) url += "limit=" + limit + "&";
- if (StrUtil.isNotBlank(type)) url += "type=" + type + "&";
- url = url.substring(0, url.length() - 1);
- }
- AdminApiResponse<PageResponse> response = adminApiClient.get(url, PageResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("查询积分变化明细失败, userId: {}", userId, e);
- throw new AdminApiException("查询积分变化明细失败: " + e.getMessage());
- }
- }
-
- /**
- * 获取用户使用统计
- */
- public UsageStatsResponse getUserUsageStats(Long userId, Integer days) {
- try {
- String url = "/api/users/" + userId + "/usage/stats";
- if (days != null) {
- url += "?days=" + days;
- }
- AdminApiResponse<UsageStatsResponse> response = adminApiClient.get(url, UsageStatsResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("获取用户使用统计失败, userId: {}", userId, e);
- throw new AdminApiException("获取用户使用统计失败: " + e.getMessage());
- }
- }
-
- // ========== 批量操作 ==========
-
- /**
- * 批量查询用户状态
- */
- public List<BatchUserStatusItem> getBatchUserStatus(List<Long> userIds) {
- try {
- if (CollUtil.isEmpty(userIds) || userIds.size() > 100) {
- throw new AdminApiException("用户ID列表为空或超过100个");
- }
-
- String url = "/api/users/batch-status";
- BatchUserStatusRequest request = new BatchUserStatusRequest();
- request.setUserIds(userIds);
- AdminApiResponse<List<BatchUserStatusItem>> response = adminApiClient.postList(url, request, BatchUserStatusItem.class);
- return response.getData();
- } catch (Exception e) {
- log.error("批量查询用户状态失败, userIds: {}", userIds, e);
- throw new AdminApiException("批量查询用户状态失败: " + e.getMessage());
- }
- }
-
- // ========== 系统管理 ==========
-
- /**
- * 查询积分恢复状态
- */
- public RecoveryStatusResponse getRecoveryStatus() {
- try {
- String url = "/api/system/recovery/status";
- AdminApiResponse<RecoveryStatusResponse> response = adminApiClient.get(url, RecoveryStatusResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("查询积分恢复状态失败", e);
- throw new AdminApiException("查询积分恢复状态失败: " + e.getMessage());
- }
- }
-
- /**
- * 手动触发积分恢复
- */
- public RecoveryTriggerResponse triggerRecovery() {
- try {
- String url = "/api/system/recovery/trigger";
- AdminApiResponse<RecoveryTriggerResponse> response = adminApiClient.post(url, null, RecoveryTriggerResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("手动触发积分恢复失败", e);
- throw new AdminApiException("手动触发积分恢复失败: " + e.getMessage());
- }
- }
-
- // ========== 高级统计接口 ==========
-
- /**
- * 获取用户仪表盘数据
- */
- public DashboardResponse getUserDashboard(Long userId) {
- try {
- String url = "/api/users/" + userId + "/dashboard";
- AdminApiResponse<DashboardResponse> response = adminApiClient.get(url, DashboardResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("获取用户仪表盘数据失败, userId: {}", userId, e);
- throw new AdminApiException("获取用户仪表盘数据失败: " + e.getMessage());
- }
- }
-
- /**
- * 获取积分图表分析
- */
- public CreditsAnalyticsResponse getCreditsAnalytics(Long userId, String start, String end,
- String tz, Integer page, Integer limit,
- String order, String type) {
- try {
- String url = "/api/users/" + userId + "/credits/analytics";
- url += "?start=" + start + "&end=" + end;
-
- if (StrUtil.isNotBlank(tz)) url += "&tz=" + tz;
- if (page != null) url += "&page=" + page;
- if (limit != null) url += "&limit=" + limit;
- if (StrUtil.isNotBlank(order)) url += "&order=" + order;
- if (StrUtil.isNotBlank(type)) url += "&type=" + type;
-
- AdminApiResponse<CreditsAnalyticsResponse> response = adminApiClient.get(url, CreditsAnalyticsResponse.class);
- return response.getData();
- } catch (Exception e) {
- log.error("获取积分图表分析失败, userId: {}", userId, e);
- throw new AdminApiException("获取积分图表分析失败: " + e.getMessage());
- }
- }
- }
|