mivita/database/seeders/IncentiveParticipantSeeder.php
2026-04-10 17:15:27 +02:00

53 lines
1.6 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\IncentiveParticipant;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class IncentiveParticipantSeeder extends Seeder
{
/**
* Spielt Teilnehmer fuer Incentive ID 1 ein.
* Nimmt aktive Berater (mit m_sponsor) sortiert nach Anzahl direkter Partner.
* Bereits bestehende Teilnehmer werden uebersprungen.
*/
public function run(): void
{
$incentive_id = 1;
$limit = 50;
// Bereits teilnehmende User-IDs
$existing_user_ids = IncentiveParticipant::where('incentive_id', $incentive_id)
->pluck('user_id')
->toArray();
// Aktive Berater mit den meisten direkten Partnern holen
$consultants = User::where('active', 1)
->whereNotNull('m_sponsor')
->whereNotIn('id', $existing_user_ids)
->select('users.*')
->selectSub(
DB::table('users as u2')->selectRaw('COUNT(*)')->whereColumn('u2.m_sponsor', 'users.id'),
'direct_partners_count'
)
->orderByDesc('direct_partners_count')
->limit($limit)
->get();
$count = 0;
foreach ($consultants as $consultant) {
IncentiveParticipant::create([
'incentive_id' => $incentive_id,
'user_id' => $consultant->id,
'accepted_terms_at' => Carbon::now(),
]);
$count++;
}
$this->command->info("Incentive #{$incentive_id}: {$count} Teilnehmer hinzugefuegt (bestehende uebersprungen).");
}
}