KnowledgeServiceApi.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Http\Client\PendingRequest;
  4. use Illuminate\Http\Client\RequestException;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Http;
  8. class KnowledgeServiceApi
  9. {
  10. public function __construct(
  11. protected string $baseUrl = '',
  12. protected int $timeout = 10,
  13. protected int $cacheTtl = 300,
  14. ) {
  15. $this->baseUrl = rtrim($this->baseUrl ?: config('knowledge.base_url'), '/');
  16. $this->timeout = $this->timeout ?: (int) config('knowledge.timeout', 10);
  17. $this->cacheTtl = $this->cacheTtl ?: (int) config('knowledge.cache_ttl', 300);
  18. }
  19. /**
  20. * Fetch and cache all knowledge points by iterating through paginated API responses.
  21. *
  22. * @return Collection<int, array<string, mixed>>
  23. */
  24. public function listKnowledgePoints(int $perPage = 200, array $filters = []): Collection
  25. {
  26. $cacheKey = sprintf(
  27. 'knowledge-points-all-%d-%s',
  28. $perPage,
  29. md5(json_encode($filters))
  30. );
  31. return Cache::remember(
  32. $cacheKey,
  33. now()->addSeconds($this->cacheTtl),
  34. function () use ($perPage, $filters): Collection {
  35. $page = 1;
  36. $results = collect();
  37. while (true) {
  38. $response = $this->paginateKnowledgePoints($page, $perPage, $filters);
  39. $chunk = collect($response['data'] ?? []);
  40. if ($chunk->isEmpty()) {
  41. break;
  42. }
  43. $results = $results->concat($chunk);
  44. $meta = $response['meta'] ?? [];
  45. $hasNext = $meta['has_next'] ?? ($page < ($meta['total_pages'] ?? $page));
  46. if (! $hasNext) {
  47. break;
  48. }
  49. $page++;
  50. }
  51. return $results->values();
  52. },
  53. );
  54. }
  55. /**
  56. * @return array{data: array<int, array<string, mixed>>, meta: array<string, mixed>}
  57. */
  58. public function paginateKnowledgePoints(int $page = 1, int $perPage = 50, array $filters = []): array
  59. {
  60. $query = array_filter([
  61. 'page' => $page,
  62. 'per_page' => $perPage,
  63. 'phase' => $filters['phase'] ?? null,
  64. 'category' => $filters['category'] ?? null,
  65. 'grade' => $filters['grade'] ?? null,
  66. ], fn ($value) => filled($value));
  67. $response = $this->request('GET', '/knowledge-points', $query);
  68. if (is_array($response) && array_key_exists('data', $response)) {
  69. return [
  70. 'data' => $response['data'] ?? [],
  71. 'meta' => $response['meta'] ?? [],
  72. ];
  73. }
  74. return [
  75. 'data' => is_array($response) ? $response : [],
  76. 'meta' => [
  77. 'page' => $page,
  78. 'per_page' => $perPage,
  79. 'total' => is_array($response) ? count($response) : 0,
  80. 'total_pages' => 1,
  81. 'has_next' => false,
  82. 'has_prev' => $page > 1,
  83. ],
  84. ];
  85. }
  86. /**
  87. * @return array<string, mixed>
  88. */
  89. public function getKnowledgePointDetail(string $kpCode): array
  90. {
  91. return Cache::remember(
  92. key: "knowledge-point-detail-{$kpCode}",
  93. ttl: now()->addSeconds($this->cacheTtl),
  94. callback: fn () => $this->request('GET', "/graph/node/{$kpCode}"),
  95. );
  96. }
  97. /**
  98. * @return Collection<int, array<string, mixed>>
  99. */
  100. public function listSkills(?string $kpCode = null, int $limit = 200): Collection
  101. {
  102. return Cache::remember(
  103. key: "knowledge-skills-{$kpCode}-{$limit}",
  104. ttl: now()->addSeconds($this->cacheTtl),
  105. callback: fn () => collect($this->request('GET', '/skills', array_filter([
  106. 'kp_code' => $kpCode,
  107. 'limit' => $limit,
  108. ]))),
  109. );
  110. }
  111. /**
  112. * @throws RequestException
  113. * @return mixed
  114. */
  115. protected function request(string $method, string $path, array $params = []): mixed
  116. {
  117. $response = $this->http()->send($method, ltrim($path, '/'), [
  118. 'query' => $params,
  119. ]);
  120. return $response->throw()->json();
  121. }
  122. protected function http(): PendingRequest
  123. {
  124. return Http::baseUrl($this->baseUrl)
  125. ->acceptJson()
  126. ->timeout($this->timeout)
  127. ->retry(2, 200);
  128. }
  129. }