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 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 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 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 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 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 getUserApiKeys(Long userId) { try { String url = "/api/users/" + userId + "/keys"; AdminApiResponse> 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 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 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 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 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 response = adminApiClient.get(url, CacheStatsResponse.class); return response.getData(); } catch (Exception e) { log.error("获取缓存统计信息失败", e); throw new AdminApiException("获取缓存统计信息失败: " + e.getMessage()); } } // ========== 统计和历史 ========== /** * 查询积分变化明细 */ public PageResponse 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 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 response = adminApiClient.get(url, UsageStatsResponse.class); return response.getData(); } catch (Exception e) { log.error("获取用户使用统计失败, userId: {}", userId, e); throw new AdminApiException("获取用户使用统计失败: " + e.getMessage()); } } // ========== 批量操作 ========== /** * 批量查询用户状态 */ public List getBatchUserStatus(List 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> 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 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 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 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 response = adminApiClient.get(url, CreditsAnalyticsResponse.class); return response.getData(); } catch (Exception e) { log.error("获取积分图表分析失败, userId: {}", userId, e); throw new AdminApiException("获取积分图表分析失败: " + e.getMessage()); } } }