purityselect/app/Console/Commands/InsertDataForPatientBulk.php
2024-10-25 01:05:27 +05:00

101 lines
3.8 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Patient;
use App\Models\PatientRegActivity;
use App\Models\PatientPlan;
use App\Models\PlanV1;
use Illuminate\Support\Facades\DB;
use Faker\Factory as Faker;
class InsertDataForPatientBulk extends Command
{
protected $signature = 'insert:data-patient-bulk';
protected $description = 'Insert patient data into the database using bulk insert';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$faker = Faker::create();
$batchSize = 500; // Number of records to insert in each batch
$totalRecords = 20000; // Total number of records to insert
$plans = PlanV1::all();
for ($j = 0; $j < $totalRecords; $j += $batchSize) {
$patientData = [];
$patientRegActivityData = [];
$patientPlanData = [];
for ($i = 0; $i < $batchSize; $i++) {
$patientId = $j + $i + 37000; // Starting from 10001 as per your original code
$email = $faker->unique()->safeEmail;
$emailParts = explode('@', $email);
$emailParts[0] .= rand(1000, 9999); // Append a random 4-digit number
$uniqueEmail = implode('@', $emailParts);
$patientData[] = [
'id' => $patientId,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $uniqueEmail,
//'phone' => $faker->phoneNumber,
'password' => bcrypt("12345"),
'address' => $faker->address,
'city' => $faker->city,
'state' => $faker->state,
'zip_code' => $faker->postcode,
'lat' => $faker->latitude,
'long' => $faker->longitude,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'recording_switch' => $faker->boolean,
'country' => $faker->country,
'phone_no' => $faker->phoneNumber,
'shipping_address' => $faker->address,
'shipping_city' => $faker->city,
'shipping_state' => $faker->state,
'shipping_country' => $faker->country,
'shipping_zipcode' => $faker->postcode,
'timezone' => 'UTC',
'gender' => $faker->randomElement(['male', 'female']),
'marital_status' => $faker->randomElement(['single', 'married']),
'height' => $faker->numberBetween(150, 200),
'weight' => $faker->numberBetween(50, 100),
'created_at' => now(),
'updated_at' => now(),
];
$patientRegActivityData[] = [
'patient_id' => $patientId,
'activity' => 'patient_registered',
'created_at' => now(),
'updated_at' => now(),
];
$patientPlanData[] = [
'patient_id' => $patientId,
'plan_id' => $plans->random()->id,
'created_at' => now(),
'updated_at' => now(),
];
}
// Bulk insert data
DB::table('patients')->insert($patientData);
DB::table('patient_reg_activity')->insert($patientRegActivityData);
DB::table('patient_plan')->insert($patientPlanData);
$this->info("Inserted batch of $batchSize records. Total progress: " . ($j + $batchSize) . " / $totalRecords");
}
$this->info('Data insertion completed successfully.');
}
}