Question.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use App\Models\QuestionMeta;
  8. use App\Models\QuestionAsset;
  9. use App\Models\QuestionKpRelation;
  10. class Question extends Model
  11. {
  12. use HasFactory;
  13. protected $fillable = [
  14. 'question_code',
  15. 'question_type',
  16. 'kp_code',
  17. 'stem',
  18. 'options',
  19. 'answer',
  20. 'solution',
  21. 'difficulty',
  22. 'source_file_id',
  23. 'source_paper_id',
  24. 'paper_part_id',
  25. 'textbook_id',
  26. 'source',
  27. 'tags',
  28. 'meta',
  29. ];
  30. protected $casts = [
  31. 'difficulty' => 'decimal:2',
  32. 'options' => 'array',
  33. 'meta' => 'array',
  34. ];
  35. /**
  36. * 获取难度标签
  37. */
  38. public function getDifficultyLabelAttribute(): string
  39. {
  40. if ($this->difficulty === null) {
  41. return '未知';
  42. }
  43. $normalized = (float) $this->difficulty;
  44. if ($normalized > 1) {
  45. $normalized = $normalized / 5;
  46. }
  47. return match (true) {
  48. $normalized <= 0.4 => '基础',
  49. $normalized <= 0.7 => '中等',
  50. default => '拔高',
  51. };
  52. }
  53. /**
  54. * 获取难度颜色
  55. */
  56. public function getDifficultyColorAttribute(): string
  57. {
  58. if ($this->difficulty === null) {
  59. return 'gray'; // 未知难度用灰色
  60. }
  61. $normalized = (float) $this->difficulty;
  62. if ($normalized > 1) {
  63. $normalized = $normalized / 5;
  64. }
  65. return match (true) {
  66. $normalized <= 0.4 => 'success',
  67. $normalized <= 0.7 => 'warning',
  68. default => 'danger',
  69. };
  70. }
  71. /**
  72. * 获取来源标签
  73. */
  74. public function getSourceLabelAttribute(): string
  75. {
  76. if (str_contains($this->source ?? '', 'ai::')) {
  77. return 'AI 生成';
  78. }
  79. if (str_contains($this->source ?? '', 'manual')) {
  80. return '手工录入';
  81. }
  82. return '未知';
  83. }
  84. /**
  85. * 关联的知识点名称
  86. */
  87. public function getKnowledgePointNameAttribute(): string
  88. {
  89. // TODO: 从知识图谱 API 获取知识点名称
  90. // 临时返回 kp_code
  91. return $this->kp_code ?? '未知';
  92. }
  93. public function meta(): HasOne
  94. {
  95. return $this->hasOne(QuestionMeta::class);
  96. }
  97. public function assets(): HasMany
  98. {
  99. return $this->hasMany(QuestionAsset::class);
  100. }
  101. public function knowledgeRelations(): HasMany
  102. {
  103. return $this->hasMany(QuestionKpRelation::class);
  104. }
  105. /**
  106. * 作用域:按知识点过滤
  107. */
  108. public function scopeByKpCode($query, string $kpCode)
  109. {
  110. return $query->where('kp_code', $kpCode);
  111. }
  112. /**
  113. * 作用域:按难度过滤
  114. */
  115. public function scopeByDifficulty($query, float $difficulty)
  116. {
  117. return $query->where('difficulty', $difficulty);
  118. }
  119. /**
  120. * 作用域:按难度范围过滤
  121. */
  122. public function scopeByDifficultyRange($query, float $min, float $max)
  123. {
  124. return $query->whereBetween('difficulty', [$min, $max]);
  125. }
  126. /**
  127. * 作用域:搜索题目
  128. */
  129. public function scopeSearch($query, string $search)
  130. {
  131. return $query->where(function ($q) use ($search) {
  132. $q->where('stem', 'like', "%{$search}%")
  133. ->orWhere('answer', 'like', "%{$search}%")
  134. ->orWhere('solution', 'like', "%{$search}%")
  135. ->orWhere('question_code', 'like', "%{$search}%");
  136. });
  137. }
  138. }