KnowledgePointStatsComponent.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Livewire\Integrations;
  3. use Livewire\Component;
  4. use App\Services\QuestionBankService;
  5. class KnowledgePointStatsComponent extends Component
  6. {
  7. public $selectedKpCode = null;
  8. public $statsData = [];
  9. public $isLoading = false;
  10. public $showDetails = false;
  11. protected $listeners = [
  12. 'kpSelected' => 'handleKpSelected',
  13. 'clearSelection' => 'clearSelection',
  14. 'refreshStats' => 'loadStatsData',
  15. ];
  16. public function mount($selectedKpCode = null)
  17. {
  18. $this->selectedKpCode = $selectedKpCode;
  19. $this->loadStatsData();
  20. }
  21. public function loadStatsData()
  22. {
  23. $this->isLoading = true;
  24. try {
  25. $service = app(QuestionBankService::class);
  26. $this->statsData = $service->getKnowledgePointStatistics($this->selectedKpCode);
  27. } catch (\Exception $e) {
  28. \Log::error('加载知识点统计失败', [
  29. 'kp_code' => $this->selectedKpCode,
  30. 'error' => $e->getMessage()
  31. ]);
  32. $this->dispatch('error', message: '加载统计数据失败');
  33. }
  34. $this->isLoading = false;
  35. }
  36. public function handleKpSelected($kpCode)
  37. {
  38. $this->selectedKpCode = $kpCode;
  39. $this->loadStatsData();
  40. }
  41. public function clearSelection()
  42. {
  43. $this->selectedKpCode = null;
  44. $this->loadStatsData();
  45. }
  46. public function toggleDetails()
  47. {
  48. $this->showDetails = !$this->showDetails;
  49. }
  50. public function getTotalQuestions($stats)
  51. {
  52. return $stats['total_questions'] ?? 0;
  53. }
  54. public function getDirectQuestions($stats)
  55. {
  56. return $stats['direct_questions'] ?? 0;
  57. }
  58. public function getChildrenQuestions($stats)
  59. {
  60. return $stats['children_questions'] ?? 0;
  61. }
  62. public function getSkillQuestions($stats)
  63. {
  64. return $stats['skills_total_questions'] ?? 0;
  65. }
  66. public function render()
  67. {
  68. return view('livewire.integrations.knowledge-point-stats-component', [
  69. 'statsData' => $this->statsData,
  70. ]);
  71. }
  72. }