| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use App\Models\QuestionMeta;
- use App\Models\QuestionAsset;
- use App\Models\QuestionKpRelation;
- class Question extends Model
- {
- use HasFactory;
- protected $fillable = [
- 'question_code',
- 'question_type',
- 'kp_code',
- 'stem',
- 'options',
- 'answer',
- 'solution',
- 'difficulty',
- 'source_file_id',
- 'source_paper_id',
- 'paper_part_id',
- 'textbook_id',
- 'source',
- 'tags',
- 'meta',
- ];
- protected $casts = [
- 'difficulty' => 'decimal:2',
- 'options' => 'array',
- 'meta' => 'array',
- ];
- /**
- * 获取难度标签
- */
- public function getDifficultyLabelAttribute(): string
- {
- $normalized = (float) $this->difficulty;
- if ($normalized > 1) {
- $normalized = $normalized / 5;
- }
- return match (true) {
- $normalized <= 0.4 => '基础',
- $normalized <= 0.7 => '中等',
- default => '拔高',
- };
- }
- /**
- * 获取难度颜色
- */
- public function getDifficultyColorAttribute(): string
- {
- $normalized = (float) $this->difficulty;
- if ($normalized > 1) {
- $normalized = $normalized / 5;
- }
- return match (true) {
- $normalized <= 0.4 => 'success',
- $normalized <= 0.7 => 'warning',
- default => 'danger',
- };
- }
- /**
- * 获取来源标签
- */
- public function getSourceLabelAttribute(): string
- {
- if (str_contains($this->source ?? '', 'ai::')) {
- return 'AI 生成';
- }
- if (str_contains($this->source ?? '', 'manual')) {
- return '手工录入';
- }
- return '未知';
- }
- /**
- * 关联的知识点名称
- */
- public function getKnowledgePointNameAttribute(): string
- {
- // TODO: 从知识图谱 API 获取知识点名称
- // 临时返回 kp_code
- return $this->kp_code ?? '未知';
- }
- public function meta(): HasOne
- {
- return $this->hasOne(QuestionMeta::class);
- }
- public function assets(): HasMany
- {
- return $this->hasMany(QuestionAsset::class);
- }
- public function knowledgeRelations(): HasMany
- {
- return $this->hasMany(QuestionKpRelation::class);
- }
- /**
- * 作用域:按知识点过滤
- */
- public function scopeByKpCode($query, string $kpCode)
- {
- return $query->where('kp_code', $kpCode);
- }
- /**
- * 作用域:按难度过滤
- */
- public function scopeByDifficulty($query, float $difficulty)
- {
- return $query->where('difficulty', $difficulty);
- }
- /**
- * 作用域:按难度范围过滤
- */
- public function scopeByDifficultyRange($query, float $min, float $max)
- {
- return $query->whereBetween('difficulty', [$min, $max]);
- }
- /**
- * 作用域:搜索题目
- */
- public function scopeSearch($query, string $search)
- {
- return $query->where(function ($q) use ($search) {
- $q->where('stem', 'like', "%{$search}%")
- ->orWhere('answer', 'like', "%{$search}%")
- ->orWhere('solution', 'like', "%{$search}%")
- ->orWhere('question_code', 'like', "%{$search}%");
- });
- }
- }
|