Files
blazor-cms/Infrastructure/Database/Demo/MedicalRecordSeeder.cs
T
2026-07-21 13:52:43 +07:00

116 lines
5.1 KiB
C#

using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Data.Entities;
using Indotalent.Data.Enums;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Infrastructure.Database.Demo;
public static class MedicalRecordSeeder
{
public static async Task GenerateDataAsync(AppDbContext context)
{
if (await context.MedicalRecord.AnyAsync()) return;
var random = new Random();
var entityName = nameof(MedicalRecord);
var patients = await context.Patient.ToListAsync();
var doctors = await context.Employee
.Where(x => x.Name!.StartsWith("Dr.") || x.JobTitle!.Contains("Practitioner") || x.JobTitle!.Contains("Surgeon"))
.ToListAsync();
var groups = await context.MedicalRecordGroup.ToListAsync();
var categories = await context.MedicalRecordCategory.ToListAsync();
if (!patients.Any() || !doctors.Any() || !groups.Any() || !categories.Any()) return;
var soapSamples = new List<(string S, string O, string A, string P)>
{
(
"Patient reports persistent lower back pain for 2 weeks, exacerbated by morning activity.",
"Tenderness noted in the lumbar spine region, decreased range of motion during trunk flexion.",
"Lumbago with sciatica (ICD-10 M54.5).",
"Start NSAID regimen, referral to Physical Therapy twice weekly, and activity modification."
),
(
"Dry cough and low-grade fever for 72 hours. Patient denies dyspnea or chest pain.",
"Lungs clear to auscultation bilaterally. Pharynx mildly erythematous. Temp 100.8 F.",
"Acute Upper Respiratory Infection (ICD-10 J06.9).",
"Rest, increased fluid intake, and over-the-counter antipyretics as needed."
),
(
"Routine dental evaluation. Patient reports sensitivity to cold stimuli on lower left quadrant.",
"Localized enamel wear on tooth #19. Gingival tissue appears within normal limits.",
"Dentin hypersensitivity (ICD-10 K03.8).",
"Applied fluoride varnish. Recommended use of potassium nitrate toothpaste."
),
(
"Follow-up for essential hypertension. Patient reports compliance with medication, no headaches.",
"S1, S2 audible, regular rate and rhythm. No peripheral edema noted.",
"Essential Hypertension (ICD-10 I10).",
"Continue current antihypertensive therapy. Reinforce DASH diet and low sodium intake."
),
(
"Urgent care visit: inverted right ankle while running this morning.",
"Edema and ecchymosis over the lateral malleolus. Guarded weight-bearing due to pain.",
"Ankle sprain, grade 1 (ICD-10 S93.4).",
"Follow RICE protocol (Rest, Ice, Compression, Elevation). Applied ACE bandage."
)
};
var dateFinish = DateTime.Now;
var dateStart = dateFinish.AddMonths(-12);
foreach (var patient in patients)
{
int recordsPerPatient = random.Next(1, 4);
for (int i = 0; i < recordsPerPatient; i++)
{
var randomDays = random.Next(0, (dateFinish - dateStart).Days);
var recordDate = dateStart.AddDays(randomDays);
var doctor = doctors[random.Next(doctors.Count)];
var soap = soapSamples[random.Next(soapSamples.Count)];
var autoNo = await context.GenerateAutoNumberAsync(
entityName: entityName,
prefixTemplate: $"MR/{{Year}}/{{Month}}/"
);
var tempF = Math.Round(97.0 + (random.NextDouble() * 3.5), 1);
var heightInches = random.Next(60, 76);
var feet = heightInches / 12;
var inches = heightInches % 12;
var medicalRecord = new MedicalRecord
{
AutoNumber = autoNo,
RecordDate = recordDate,
PatientId = patient.Id,
EmployeeId = doctor.Id,
MedicalRecordGroupId = groups[random.Next(groups.Count)].Id,
MedicalRecordCategoryId = categories[random.Next(categories.Count)].Id,
Subjective = soap.S,
Objective = soap.O,
Assessment = soap.A,
Planning = soap.P,
BloodPressure = $"{random.Next(110, 135)}/{random.Next(70, 88)} mmHg",
Temperature = $"{tempF} F",
HeartRate = $"{random.Next(60, 100)} bpm",
Weight = $"{random.Next(120, 230)} lbs",
Height = $"{feet}'{inches}\"",
Description = $"Clinical encounter documentation for {patient.Name}",
Status = random.Next(1, 10) > 2 ? MedicalRecordStatus.Completed : MedicalRecordStatus.Examining
};
await context.MedicalRecord.AddAsync(medicalRecord);
}
}
await context.SaveChangesAsync();
}
}