KnowledgeServiceApi.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. 'search' => $filters['search'] ?? null,
  67. ], fn ($value) => filled($value));
  68. $response = $this->request('GET', '/knowledge-points', $query);
  69. if (is_array($response) && array_key_exists('data', $response)) {
  70. return [
  71. 'data' => $response['data'] ?? [],
  72. 'meta' => $response['meta'] ?? [],
  73. ];
  74. }
  75. return [
  76. 'data' => is_array($response) ? $response : [],
  77. 'meta' => [
  78. 'page' => $page,
  79. 'per_page' => $perPage,
  80. 'total' => is_array($response) ? count($response) : 0,
  81. 'total_pages' => 1,
  82. 'has_next' => false,
  83. 'has_prev' => $page > 1,
  84. ],
  85. ];
  86. }
  87. /**
  88. * @return array<string, mixed>
  89. */
  90. public function getKnowledgePointDetail(string $kpCode): array
  91. {
  92. return Cache::remember(
  93. key: "knowledge-point-detail-{$kpCode}",
  94. ttl: now()->addSeconds($this->cacheTtl),
  95. callback: fn () => $this->request('GET', "/graph/node/{$kpCode}"),
  96. );
  97. }
  98. /**
  99. * @return Collection<int, array<string, mixed>>
  100. */
  101. public function listSkills(?string $kpCode = null, int $limit = 200): Collection
  102. {
  103. return Cache::remember(
  104. key: "knowledge-skills-{$kpCode}-{$limit}",
  105. ttl: now()->addSeconds($this->cacheTtl),
  106. callback: fn () => collect($this->request('GET', '/skills', array_filter([
  107. 'kp_code' => $kpCode,
  108. 'limit' => $limit,
  109. ]))),
  110. );
  111. }
  112. /**
  113. * Search knowledge points by keyword
  114. *
  115. * @return Collection<int, array<string, mixed>>
  116. */
  117. public function searchKnowledgePoints(string $keyword, int $limit = 50): Collection
  118. {
  119. // Ensure keyword is not empty
  120. $keyword = trim($keyword);
  121. if ($keyword === '') {
  122. return collect();
  123. }
  124. // Use query params array for proper encoding (no cache during debugging)
  125. try {
  126. $response = $this->request('GET', 'knowledge-points/search', [
  127. 'keyword' => $keyword,
  128. 'limit' => $limit,
  129. ]);
  130. } catch (\Exception $e) {
  131. // Log error but don't fail completely
  132. \Log::error('Search API error: ' . $e->getMessage());
  133. return collect();
  134. }
  135. return collect($response);
  136. }
  137. /**
  138. * @throws RequestException
  139. * @return mixed
  140. */
  141. protected function request(string $method, string $path, array $params = []): mixed
  142. {
  143. $response = $this->http()->send($method, ltrim($path, '/'), [
  144. 'query' => $params,
  145. ]);
  146. return $response->throw()->json();
  147. }
  148. protected function http(): PendingRequest
  149. {
  150. return Http::baseUrl($this->baseUrl)
  151. ->acceptJson()
  152. ->timeout($this->timeout)
  153. ->retry(2, 200)
  154. ->withHeaders([
  155. 'User-Agent' => 'Laravel/' . (app()->version()),
  156. 'Accept' => 'application/json',
  157. ]);
  158. }
  159. }