AdminApiService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.cyksj.service.claudecode;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.log.Log;
  5. import cn.hutool.log.LogFactory;
  6. import com.cyksj.service.claudecode.client.AdminApiClient;
  7. import com.cyksj.service.claudecode.dto.request.*;
  8. import com.cyksj.service.claudecode.dto.response.*;
  9. import com.cyksj.service.claudecode.exception.AdminApiException;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.util.List;
  13. @Service
  14. public class AdminApiService {
  15. private static final Log log = LogFactory.get();
  16. @Autowired
  17. private AdminApiClient adminApiClient;
  18. // ========== 用户套餐管理 ==========
  19. /**
  20. * 创建或更新用户套餐
  21. */
  22. public UserPlanResponse createOrUpdateUserPlan(Long userId, CreateUserPlanRequest request) {
  23. try {
  24. String url = "/api/users/" + userId + "/plan";
  25. AdminApiResponse<UserPlanResponse> response = adminApiClient.put(url, request, UserPlanResponse.class);
  26. return response.getData();
  27. } catch (Exception e) {
  28. log.error("创建或更新用户套餐失败, userId: {}", userId, e);
  29. throw new AdminApiException("创建或更新用户套餐失败: " + e.getMessage());
  30. }
  31. }
  32. /**
  33. * 查询用户套餐
  34. */
  35. public UserPlanResponse getUserPlan(Long userId) {
  36. try {
  37. String url = "/api/users/" + userId + "/plan";
  38. AdminApiResponse<UserPlanResponse> response = adminApiClient.get(url, UserPlanResponse.class);
  39. return response.getData();
  40. } catch (Exception e) {
  41. log.error("查询用户套餐失败, userId: {}", userId, e);
  42. throw new AdminApiException("查询用户套餐失败: " + e.getMessage());
  43. }
  44. }
  45. /**
  46. * 调整用户积分
  47. */
  48. public CreditAdjustResponse adjustUserCredits(Long userId, CreditAdjustRequest request) {
  49. try {
  50. String url = "/api/users/" + userId + "/credits/adjust";
  51. AdminApiResponse<CreditAdjustResponse> response = adminApiClient.post(url, request, CreditAdjustResponse.class);
  52. return response.getData();
  53. } catch (Exception e) {
  54. log.error("调整用户积分失败, userId: {}", userId, e);
  55. throw new AdminApiException("调整用户积分失败: " + e.getMessage());
  56. }
  57. }
  58. /**
  59. * 查询用户余额
  60. */
  61. public UserBalanceResponse getUserBalance(Long userId) {
  62. try {
  63. String url = "/api/users/" + userId + "/balance";
  64. AdminApiResponse<UserBalanceResponse> response = adminApiClient.get(url, UserBalanceResponse.class);
  65. return response.getData();
  66. } catch (Exception e) {
  67. log.error("查询用户余额失败, userId: {}", userId, e);
  68. throw new AdminApiException("查询用户余额失败: " + e.getMessage());
  69. }
  70. }
  71. // ========== API Key管理 ==========
  72. /**
  73. * 创建API Key
  74. */
  75. public ApiKeyResponse createApiKey(Long userId, CreateApiKeyRequest request) {
  76. try {
  77. String url = "/api/users/" + userId + "/keys";
  78. AdminApiResponse<ApiKeyResponse> response = adminApiClient.post(url, request, ApiKeyResponse.class);
  79. return response.getData();
  80. } catch (Exception e) {
  81. log.error("创建API Key失败, userId: {}", userId, e);
  82. throw new AdminApiException("创建API Key失败: " + e.getMessage());
  83. }
  84. }
  85. /**
  86. * 查询用户API Keys
  87. */
  88. public List<ApiKeyInfo> getUserApiKeys(Long userId) {
  89. try {
  90. String url = "/api/users/" + userId + "/keys";
  91. AdminApiResponse<List<ApiKeyInfo>> response = adminApiClient.getList(url, ApiKeyInfo.class);
  92. return response.getData();
  93. } catch (Exception e) {
  94. log.error("查询用户API Keys失败, userId: {}", userId, e);
  95. throw new AdminApiException("查询用户API Keys失败: " + e.getMessage());
  96. }
  97. }
  98. /**
  99. * 更新API Key状态
  100. */
  101. public ApiKeyStatusResponse updateApiKeyStatus(Long userId, Long keyId, UpdateApiKeyStatusRequest request) {
  102. try {
  103. String url = "/api/keys/" + userId + "/status?keyId=" + keyId;
  104. AdminApiResponse<ApiKeyStatusResponse> response = adminApiClient.put(url, request, ApiKeyStatusResponse.class);
  105. return response.getData();
  106. } catch (Exception e) {
  107. log.error("更新API Key状态失败, userId: {}, keyId: {}", userId, keyId, e);
  108. throw new AdminApiException("更新API Key状态失败: " + e.getMessage());
  109. }
  110. }
  111. /**
  112. * 删除API Key
  113. */
  114. public ApiKeyDeleteResponse deleteApiKey(Long userId, Long keyId) {
  115. try {
  116. String url = "/api/keys/" + userId + "?keyId=" + keyId;
  117. AdminApiResponse<ApiKeyDeleteResponse> response = adminApiClient.delete(url, ApiKeyDeleteResponse.class);
  118. return response.getData();
  119. } catch (Exception e) {
  120. log.error("删除API Key失败, userId: {}, keyId: {}", userId, keyId, e);
  121. throw new AdminApiException("删除API Key失败: " + e.getMessage());
  122. }
  123. }
  124. // ========== 缓存管理 ==========
  125. /**
  126. * 清理用户缓存
  127. */
  128. public CacheInvalidateResponse invalidateUserCache(Long userId) {
  129. try {
  130. String url = "/api/cache/invalidate/user/" + userId;
  131. AdminApiResponse<CacheInvalidateResponse> response = adminApiClient.post(url, null, CacheInvalidateResponse.class);
  132. return response.getData();
  133. } catch (Exception e) {
  134. log.error("清理用户缓存失败, userId: {}", userId, e);
  135. throw new AdminApiException("清理用户缓存失败: " + e.getMessage());
  136. }
  137. }
  138. /**
  139. * 清理API Key缓存
  140. */
  141. public ApiKeyCacheResponse invalidateApiKeyCache(String apiKey) {
  142. try {
  143. String url = "/api/cache/invalidate/apikey";
  144. InvalidateApiKeyCacheRequest request = new InvalidateApiKeyCacheRequest();
  145. request.setApiKey(apiKey);
  146. AdminApiResponse<ApiKeyCacheResponse> response = adminApiClient.post(url, request, ApiKeyCacheResponse.class);
  147. return response.getData();
  148. } catch (Exception e) {
  149. log.error("清理API Key缓存失败, apiKey: {}", apiKey, e);
  150. throw new AdminApiException("清理API Key缓存失败: " + e.getMessage());
  151. }
  152. }
  153. /**
  154. * 获取缓存统计信息
  155. */
  156. public CacheStatsResponse getCacheStats() {
  157. try {
  158. String url = "/api/cache/stats";
  159. AdminApiResponse<CacheStatsResponse> response = adminApiClient.get(url, CacheStatsResponse.class);
  160. return response.getData();
  161. } catch (Exception e) {
  162. log.error("获取缓存统计信息失败", e);
  163. throw new AdminApiException("获取缓存统计信息失败: " + e.getMessage());
  164. }
  165. }
  166. // ========== 统计和历史 ==========
  167. /**
  168. * 查询积分变化明细
  169. */
  170. public PageResponse<CreditHistoryItem> getCreditHistory(Long userId, Integer page, Integer limit, String type) {
  171. try {
  172. String url = "/api/users/" + userId + "/credits/history";
  173. if (page != null || limit != null || StrUtil.isNotBlank(type)) {
  174. url += "?";
  175. if (page != null) url += "page=" + page + "&";
  176. if (limit != null) url += "limit=" + limit + "&";
  177. if (StrUtil.isNotBlank(type)) url += "type=" + type + "&";
  178. url = url.substring(0, url.length() - 1);
  179. }
  180. AdminApiResponse<PageResponse> response = adminApiClient.get(url, PageResponse.class);
  181. return response.getData();
  182. } catch (Exception e) {
  183. log.error("查询积分变化明细失败, userId: {}", userId, e);
  184. throw new AdminApiException("查询积分变化明细失败: " + e.getMessage());
  185. }
  186. }
  187. /**
  188. * 获取用户使用统计
  189. */
  190. public UsageStatsResponse getUserUsageStats(Long userId, Integer days) {
  191. try {
  192. String url = "/api/users/" + userId + "/usage/stats";
  193. if (days != null) {
  194. url += "?days=" + days;
  195. }
  196. AdminApiResponse<UsageStatsResponse> response = adminApiClient.get(url, UsageStatsResponse.class);
  197. return response.getData();
  198. } catch (Exception e) {
  199. log.error("获取用户使用统计失败, userId: {}", userId, e);
  200. throw new AdminApiException("获取用户使用统计失败: " + e.getMessage());
  201. }
  202. }
  203. // ========== 批量操作 ==========
  204. /**
  205. * 批量查询用户状态
  206. */
  207. public List<BatchUserStatusItem> getBatchUserStatus(List<Long> userIds) {
  208. try {
  209. if (CollUtil.isEmpty(userIds) || userIds.size() > 100) {
  210. throw new AdminApiException("用户ID列表为空或超过100个");
  211. }
  212. String url = "/api/users/batch-status";
  213. BatchUserStatusRequest request = new BatchUserStatusRequest();
  214. request.setUserIds(userIds);
  215. AdminApiResponse<List<BatchUserStatusItem>> response = adminApiClient.postList(url, request, BatchUserStatusItem.class);
  216. return response.getData();
  217. } catch (Exception e) {
  218. log.error("批量查询用户状态失败, userIds: {}", userIds, e);
  219. throw new AdminApiException("批量查询用户状态失败: " + e.getMessage());
  220. }
  221. }
  222. // ========== 系统管理 ==========
  223. /**
  224. * 查询积分恢复状态
  225. */
  226. public RecoveryStatusResponse getRecoveryStatus() {
  227. try {
  228. String url = "/api/system/recovery/status";
  229. AdminApiResponse<RecoveryStatusResponse> response = adminApiClient.get(url, RecoveryStatusResponse.class);
  230. return response.getData();
  231. } catch (Exception e) {
  232. log.error("查询积分恢复状态失败", e);
  233. throw new AdminApiException("查询积分恢复状态失败: " + e.getMessage());
  234. }
  235. }
  236. /**
  237. * 手动触发积分恢复
  238. */
  239. public RecoveryTriggerResponse triggerRecovery() {
  240. try {
  241. String url = "/api/system/recovery/trigger";
  242. AdminApiResponse<RecoveryTriggerResponse> response = adminApiClient.post(url, null, RecoveryTriggerResponse.class);
  243. return response.getData();
  244. } catch (Exception e) {
  245. log.error("手动触发积分恢复失败", e);
  246. throw new AdminApiException("手动触发积分恢复失败: " + e.getMessage());
  247. }
  248. }
  249. // ========== 高级统计接口 ==========
  250. /**
  251. * 获取用户仪表盘数据
  252. */
  253. public DashboardResponse getUserDashboard(Long userId) {
  254. try {
  255. String url = "/api/users/" + userId + "/dashboard";
  256. AdminApiResponse<DashboardResponse> response = adminApiClient.get(url, DashboardResponse.class);
  257. return response.getData();
  258. } catch (Exception e) {
  259. log.error("获取用户仪表盘数据失败, userId: {}", userId, e);
  260. throw new AdminApiException("获取用户仪表盘数据失败: " + e.getMessage());
  261. }
  262. }
  263. /**
  264. * 获取积分图表分析
  265. */
  266. public CreditsAnalyticsResponse getCreditsAnalytics(Long userId, String start, String end,
  267. String tz, Integer page, Integer limit,
  268. String order, String type) {
  269. try {
  270. String url = "/api/users/" + userId + "/credits/analytics";
  271. url += "?start=" + start + "&end=" + end;
  272. if (StrUtil.isNotBlank(tz)) url += "&tz=" + tz;
  273. if (page != null) url += "&page=" + page;
  274. if (limit != null) url += "&limit=" + limit;
  275. if (StrUtil.isNotBlank(order)) url += "&order=" + order;
  276. if (StrUtil.isNotBlank(type)) url += "&type=" + type;
  277. AdminApiResponse<CreditsAnalyticsResponse> response = adminApiClient.get(url, CreditsAnalyticsResponse.class);
  278. return response.getData();
  279. } catch (Exception e) {
  280. log.error("获取积分图表分析失败, userId: {}", userId, e);
  281. throw new AdminApiException("获取积分图表分析失败: " + e.getMessage());
  282. }
  283. }
  284. }