| 1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- namespace App\Console\Commands;
- use App\Models\KnowledgePoint;
- use App\Models\QuestionKpRelation;
- use Illuminate\Console\Command;
- class RebuildKnowledgeStatsCommand extends Command
- {
- protected $signature = 'knowledge:rebuild-stats';
- protected $description = 'Recalculate knowledge stats from questions';
- public function handle(): int
- {
- $stats = QuestionKpRelation::query()
- ->selectRaw('kp_code, COUNT(*) as question_count')
- ->groupBy('kp_code')
- ->get();
- foreach ($stats as $row) {
- KnowledgePoint::query()
- ->where('kp_code', $row->kp_code)
- ->update([
- 'stats' => [
- 'question_count' => (int) $row->question_count,
- ],
- ]);
- }
- $this->info('Knowledge stats rebuilt.');
- return self::SUCCESS;
- }
- }
|