| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace App\Filament\Pages;
- use App\Services\KnowledgeServiceApi;
- use BackedEnum;
- use Filament\Pages\Page;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Str;
- use UnitEnum;
- class KnowledgePoints extends Page
- {
- protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-map';
- protected static string|UnitEnum|null $navigationGroup = '知识图谱';
- protected static ?string $navigationLabel = '知识点总览';
- protected string $view = 'filament.pages.knowledge-points';
- public ?string $phaseFilter = null;
- public ?string $categoryFilter = null;
- public ?string $search = null;
- public int $page = 1;
- public int $perPage = 8;
- protected ?array $selectedPoint = null;
- protected bool $loading = false;
- protected ?KnowledgeServiceApi $knowledgeService = null;
- public function mount(): void
- {
- $this->hydratePoints();
- }
- protected function getKnowledgeService(): KnowledgeServiceApi
- {
- if (!$this->knowledgeService) {
- $this->knowledgeService = app(KnowledgeServiceApi::class);
- }
- return $this->knowledgeService;
- }
- public function updatedPhaseFilter(): void
- {
- $this->page = 1;
- $this->hydratePoints();
- }
- public function updatedCategoryFilter(): void
- {
- $this->page = 1;
- $this->hydratePoints();
- }
- public function updatedSearch(): void
- {
- $this->page = 1;
- $this->hydratePoints();
- }
- public function changePage(int $page): void
- {
- $this->page = max($page, 1);
- $this->hydratePoints();
- }
- public function selectPoint(string $kpCode): void
- {
- $this->selectedPoint = $this->getKnowledgeService()->getKnowledgePointDetail($kpCode);
- }
- public function getStatsProperty(): array
- {
- $all = $this->getKnowledgeService()->listKnowledgePoints();
- $phases = $all->groupBy('phase')->map->count()->sortDesc()->map(fn ($count, $phase) => "{$phase}·{$count}");
- $categories = $all->groupBy('category')->map->count()->sortDesc()->take(3)->map(fn ($count, $category) => "{$category}·{$count}");
- return [
- ['label' => '知识点数量', 'value' => number_format($all->count()), 'hint' => '当前版本节点'],
- ['label' => '学段覆盖', 'value' => $phases->implode(' | ') ?: '未配置', 'hint' => 'phase 字段'],
- ['label' => 'Top 类别', 'value' => $categories->implode(' | ') ?: '未配置', 'hint' => 'category 字段'],
- ['label' => '平均重要度', 'value' => number_format($all->avg('importance') ?? 0, 1), 'hint' => 'importance'],
- ];
- }
- public function getPhaseOptionsProperty(): array
- {
- return $this->getKnowledgeService()
- ->listKnowledgePoints()
- ->pluck('phase')
- ->filter()
- ->unique()
- ->sort()
- ->mapWithKeys(fn ($phase) => [$phase => $phase])
- ->all();
- }
- public function getCategoryOptionsProperty(): array
- {
- return $this->getKnowledgeService()
- ->listKnowledgePoints()
- ->pluck('category')
- ->filter()
- ->unique()
- ->sort()
- ->mapWithKeys(fn ($category) => [$category => $category])
- ->all();
- }
- public function getPaginatedPointsProperty(): array
- {
- $filters = array_filter([
- 'phase' => $this->phaseFilter,
- 'category' => $this->categoryFilter,
- ]);
- $response = $this->getKnowledgeService()->paginateKnowledgePoints(
- page: $this->page,
- perPage: $this->perPage,
- filters: $filters
- );
- $records = collect($response['data'] ?? []);
- if (filled($this->search)) {
- $search = Str::lower($this->search);
- $records = $records->filter(function (array $record) use ($search): bool {
- return Str::contains(Str::lower($record['cn_name'] ?? ''), $search)
- || Str::contains(Str::lower($record['kp_code'] ?? ''), $search);
- });
- }
- $meta = $response['meta'] ?? [];
- return [
- 'data' => $records->values(),
- 'total' => $meta['total'] ?? $records->count(),
- 'page' => $meta['page'] ?? $this->page,
- 'total_pages' => $meta['total_pages'] ?? 1,
- 'has_prev' => $meta['has_prev'] ?? ($this->page > 1),
- 'has_next' => $meta['has_next'] ?? false,
- ];
- }
- public function getSelectedSkillsProperty(): Collection
- {
- return collect($this->selectedPoint['skills'] ?? []);
- }
- public function getSelectedPointProperty(): ?array
- {
- return $this->selectedPoint;
- }
- public function getRelatedNodesProperty(): array
- {
- $point = $this->selectedPoint;
- if (! $point) {
- return ['parents' => [], 'children' => []];
- }
- $parents = collect($point['parents'] ?? [])
- ->map(fn ($name, $index) => [
- 'id' => "parent-{$index}",
- 'label' => $name,
- 'relation' => 'parent',
- ])->values()->all();
- $children = collect($point['children'] ?? [])
- ->map(fn ($name, $index) => [
- 'id' => "child-{$index}",
- 'label' => $name,
- 'relation' => 'child',
- ])->values()->all();
- return ['parents' => $parents, 'children' => $children];
- }
- protected function hydratePoints(): void
- {
- $this->loading = true;
- $this->selectedPoint = null;
- $points = $this->paginatedPoints['data'];
- if ($points->isNotEmpty()) {
- $this->selectPoint($points->first()['kp_code']);
- }
- $this->loading = false;
- }
- }
|