Question.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $normalized = (float) $this->difficulty;
  41. if ($normalized > 1) {
  42. $normalized = $normalized / 5;
  43. }
  44. return match (true) {
  45. $normalized <= 0.4 => '基础',
  46. $normalized <= 0.7 => '中等',
  47. default => '拔高',
  48. };
  49. }
  50. /**
  51. * 获取难度颜色
  52. */
  53. public function getDifficultyColorAttribute(): string
  54. {
  55. $normalized = (float) $this->difficulty;
  56. if ($normalized > 1) {
  57. $normalized = $normalized / 5;
  58. }
  59. return match (true) {
  60. $normalized <= 0.4 => 'success',
  61. $normalized <= 0.7 => 'warning',
  62. default => 'danger',
  63. };
  64. }
  65. /**
  66. * 获取来源标签
  67. */
  68. public function getSourceLabelAttribute(): string
  69. {
  70. if (str_contains($this->source ?? '', 'ai::')) {
  71. return 'AI 生成';
  72. }
  73. if (str_contains($this->source ?? '', 'manual')) {
  74. return '手工录入';
  75. }
  76. return '未知';
  77. }
  78. /**
  79. * 关联的知识点名称
  80. */
  81. public function getKnowledgePointNameAttribute(): string
  82. {
  83. // TODO: 从知识图谱 API 获取知识点名称
  84. // 临时返回 kp_code
  85. return $this->kp_code ?? '未知';
  86. }
  87. public function meta(): HasOne
  88. {
  89. return $this->hasOne(QuestionMeta::class);
  90. }
  91. public function assets(): HasMany
  92. {
  93. return $this->hasMany(QuestionAsset::class);
  94. }
  95. public function knowledgeRelations(): HasMany
  96. {
  97. return $this->hasMany(QuestionKpRelation::class);
  98. }
  99. /**
  100. * 作用域:按知识点过滤
  101. */
  102. public function scopeByKpCode($query, string $kpCode)
  103. {
  104. return $query->where('kp_code', $kpCode);
  105. }
  106. /**
  107. * 作用域:按难度过滤
  108. */
  109. public function scopeByDifficulty($query, float $difficulty)
  110. {
  111. return $query->where('difficulty', $difficulty);
  112. }
  113. /**
  114. * 作用域:按难度范围过滤
  115. */
  116. public function scopeByDifficultyRange($query, float $min, float $max)
  117. {
  118. return $query->whereBetween('difficulty', [$min, $max]);
  119. }
  120. /**
  121. * 作用域:搜索题目
  122. */
  123. public function scopeSearch($query, string $search)
  124. {
  125. return $query->where(function ($q) use ($search) {
  126. $q->where('stem', 'like', "%{$search}%")
  127. ->orWhere('answer', 'like', "%{$search}%")
  128. ->orWhere('solution', 'like', "%{$search}%")
  129. ->orWhere('question_code', 'like', "%{$search}%");
  130. });
  131. }
  132. }