KnowledgePoints.php 6.6 KB

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