84 lines
2.9 KiB
PHP
84 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Cliente;
|
|
use App\Models\Profesional;
|
|
use App\Models\Profesion;
|
|
use Illuminate\Database\Seeder;
|
|
use App\Models\Servicio;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class BulkDataSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Seed large batches of synthetic data.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$clientes = max(0, (int) config('bulk_seed.clientes', (int) env('SEED_CLIENTES', 1000)));
|
|
$profesionales = max(0, (int) config('bulk_seed.profesionales', (int) env('SEED_PROFESIONALES', 100)));
|
|
$servicios = max(0, (int) config('bulk_seed.servicios', (int) env('SEED_SERVICIOS', 30)));
|
|
$profesiones = max(0, (int) config('bulk_seed.profesiones', (int) env('SEED_PROFESIONES', 4)));
|
|
|
|
// Profesiones nuevas
|
|
if ($profesiones > 0) {
|
|
Profesion::factory()->count($profesiones)->create();
|
|
}
|
|
|
|
$profesionIds = Profesion::query()->pluck('id')->all();
|
|
|
|
// Servicios
|
|
if ($servicios > 0 && count($profesionIds) > 0) {
|
|
$contenidoWebId = DB::table('contenidoswebs')->value('id');
|
|
|
|
Servicio::factory()
|
|
->count($servicios)
|
|
->state(fn () => [
|
|
'profesion_id' => $profesionIds[array_rand($profesionIds)],
|
|
'contenidoweb_id' => $contenidoWebId,
|
|
])
|
|
->create();
|
|
}
|
|
|
|
// Profesionales
|
|
$profesionalesCreados = collect();
|
|
if ($profesionales > 0 && count($profesionIds) > 0) {
|
|
$profesionalesCreados = Profesional::factory()
|
|
->count($profesionales)
|
|
->state(fn () => [
|
|
'profesion_id' => $profesionIds[array_rand($profesionIds)],
|
|
])
|
|
->create();
|
|
}
|
|
|
|
// Clientes
|
|
$clientesCreados = collect();
|
|
if ($clientes > 0) {
|
|
$clientesCreados = Cliente::factory()->count($clientes)->create();
|
|
}
|
|
|
|
// Relacion cliente-profesional: cada cliente queda asociado a 1..3 profesionales
|
|
if ($clientesCreados->isNotEmpty() && $profesionalesCreados->isNotEmpty()) {
|
|
$idsProfesionales = $profesionalesCreados->pluck('id')->values()->all();
|
|
$maxAsignables = min(3, count($idsProfesionales));
|
|
|
|
foreach ($clientesCreados as $cliente) {
|
|
$cantidadAsignaciones = random_int(1, $maxAsignables);
|
|
$idsSeleccionados = collect($idsProfesionales)
|
|
->shuffle()
|
|
->take($cantidadAsignaciones)
|
|
->values()
|
|
->all();
|
|
|
|
$payload = [];
|
|
foreach ($idsSeleccionados as $profesionalId) {
|
|
$payload[$profesionalId] = ['estadorelacion' => 'Activo'];
|
|
}
|
|
|
|
$cliente->profesionales()->syncWithoutDetaching($payload);
|
|
}
|
|
}
|
|
}
|
|
}
|