KnowledgePoints.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\KnowledgeServiceApi;
  4. use BackedEnum;
  5. use Filament\Actions\Action;
  6. use Filament\Pages\Page;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Str;
  10. use UnitEnum;
  11. class KnowledgePoints extends Page
  12. {
  13. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-map';
  14. protected static string|UnitEnum|null $navigationGroup = '知识图谱';
  15. protected static ?string $navigationLabel = '知识点总览';
  16. protected string $view = 'filament.pages.knowledge-points';
  17. public ?string $phaseFilter = null;
  18. public ?string $categoryFilter = null;
  19. public ?string $search = null;
  20. public int $page = 1;
  21. public int $perPage = 8;
  22. protected bool $loading = false;
  23. protected ?KnowledgeServiceApi $knowledgeService = null;
  24. protected ?string $selectedKpCode = null;
  25. // Private property to store loaded data
  26. private ?array $selectedPointData = null;
  27. public function mount(Request $request): void
  28. {
  29. // Only set properties that need to be available immediately
  30. // The search and filter properties will be set via getter
  31. $this->selectedKpCode = $request->query('selected');
  32. $this->page = (int) $request->query('page', 1);
  33. }
  34. public function getSelectedPointProperty(): ?array
  35. {
  36. if ($this->selectedPointData) {
  37. return $this->selectedPointData;
  38. }
  39. // If we have a specific kp code to load
  40. if ($this->selectedKpCode) {
  41. $this->selectedPointData = $this->getKnowledgeService()->getFullGraphData($this->selectedKpCode);
  42. return $this->selectedPointData;
  43. }
  44. // Get first point from paginated results
  45. $points = $this->paginatedPoints;
  46. if (!empty($points['data']) && count($points['data']) > 0) {
  47. $kpCode = $points['data'][0]['kp_code'];
  48. $this->selectedPointData = $this->getKnowledgeService()->getFullGraphData($kpCode);
  49. }
  50. return $this->selectedPointData;
  51. }
  52. public function getCurrentPhaseProperty(): ?string
  53. {
  54. return request()->query('phase');
  55. }
  56. protected function getKnowledgeService(): KnowledgeServiceApi
  57. {
  58. if (!$this->knowledgeService) {
  59. $this->knowledgeService = app(KnowledgeServiceApi::class);
  60. }
  61. return $this->knowledgeService;
  62. }
  63. public function selectPoint(string $kpCode): void
  64. {
  65. $this->selectedPoint = $this->getKnowledgeService()->getKnowledgePointDetail($kpCode);
  66. }
  67. public function getStatsProperty(): array
  68. {
  69. $all = $this->getKnowledgeService()->listKnowledgePoints();
  70. $phases = $all->groupBy('phase')->map->count()->sortDesc()->map(fn ($count, $phase) => "{$phase}·{$count}");
  71. $categories = $all->groupBy('category')->map->count()->sortDesc()->take(3)->map(fn ($count, $category) => "{$category}·{$count}");
  72. return [
  73. ['label' => '知识点数量', 'value' => number_format($all->count()), 'hint' => '当前版本节点'],
  74. ['label' => '学段覆盖', 'value' => $phases->implode(' | ') ?: '未配置', 'hint' => 'phase 字段'],
  75. ['label' => 'Top 类别', 'value' => $categories->implode(' | ') ?: '未配置', 'hint' => 'category 字段'],
  76. ['label' => '平均重要度', 'value' => number_format($all->avg('importance') ?? 0, 1), 'hint' => 'importance'],
  77. ];
  78. }
  79. public function getPhaseOptionsProperty(): array
  80. {
  81. return $this->getKnowledgeService()
  82. ->listKnowledgePoints()
  83. ->pluck('phase')
  84. ->filter()
  85. ->unique()
  86. ->sort()
  87. ->mapWithKeys(fn ($phase) => [$phase => $phase])
  88. ->all();
  89. }
  90. public function getCategoryOptionsProperty(): array
  91. {
  92. return $this->getKnowledgeService()
  93. ->listKnowledgePoints()
  94. ->pluck('category')
  95. ->filter()
  96. ->unique()
  97. ->sort()
  98. ->mapWithKeys(fn ($category) => [$category => $category])
  99. ->all();
  100. }
  101. public function getPaginatedPointsProperty(): array
  102. {
  103. // Get filters from URL
  104. $request = request();
  105. $phaseFilter = $request->query('phase');
  106. $categoryFilter = $request->query('category');
  107. $searchTerm = trim((string) $request->query('search'));
  108. // Get all points
  109. $filters = array_filter([
  110. 'phase' => $phaseFilter,
  111. 'category' => $categoryFilter,
  112. ]);
  113. $allPoints = $this->getKnowledgeService()->listKnowledgePoints(
  114. perPage: 200,
  115. filters: $filters
  116. );
  117. // Apply search filter on the client side
  118. if ($searchTerm !== '') {
  119. $allPoints = $allPoints->filter(function (array $record) use ($searchTerm): bool {
  120. return Str::contains(Str::lower($record['cn_name'] ?? ''), Str::lower($searchTerm))
  121. || Str::contains(Str::lower($record['kp_code'] ?? ''), Str::lower($searchTerm))
  122. || Str::contains(Str::lower($record['description'] ?? ''), Str::lower($searchTerm));
  123. });
  124. }
  125. // Paginate the results
  126. $total = $allPoints->count();
  127. $totalPages = (int) ceil($total / $this->perPage);
  128. $offset = ($this->page - 1) * $this->perPage;
  129. $records = $allPoints->slice($offset, $this->perPage)->values();
  130. return [
  131. 'data' => $records,
  132. 'total' => $total,
  133. 'page' => $this->page,
  134. 'total_pages' => $totalPages,
  135. 'has_prev' => $this->page > 1,
  136. 'has_next' => $this->page < $totalPages,
  137. ];
  138. }
  139. public function getSelectedSkillsProperty(): Collection
  140. {
  141. $selectedPoint = $this->selectedPointData;
  142. return collect($selectedPoint['skills'] ?? []);
  143. }
  144. public function getRelatedNodesProperty(): array
  145. {
  146. $point = $this->selectedPointData;
  147. if (! $point) {
  148. return ['parents' => [], 'children' => []];
  149. }
  150. $parents = collect($point['parents'] ?? [])
  151. ->map(fn ($name, $index) => [
  152. 'id' => "parent-{$index}",
  153. 'label' => $name,
  154. 'relation' => 'parent',
  155. ])->values()->all();
  156. $children = collect($point['children'] ?? [])
  157. ->map(fn ($name, $index) => [
  158. 'id' => "child-{$index}",
  159. 'label' => $name,
  160. 'relation' => 'child',
  161. ])->values()->all();
  162. return ['parents' => $parents, 'children' => $children];
  163. }
  164. }