using Health.Domain.Entities; using Health.Domain.Enums; using Microsoft.Extensions.Configuration; namespace Health.Infrastructure.Data; /// /// 开发环境测试数据填充。生产环境不要调用! /// 开关:DEVDATA_ENABLED=true 才会执行 /// public static class DevDataSeeder { public static async Task SeedIfEnabled(AppDbContext db, IConfiguration config) { // 通过环境变量控制:DEVDATA_ENABLED=true 才填充测试数据 var enabled = config["DEVDATA_ENABLED"]?.ToLowerInvariant(); if (enabled != "true") return; // 已有任何真实用户时跳过(避免污染真实数据) if (db.Users.Any(u => u.Phone != "13800000001")) return; // 检查是否已有测试用户(避免重复填充) if (db.Users.Any(u => u.Phone == "13800000001")) return; // ---- 种子医生数据 ---- if (!db.Doctors.Any()) { var doctor1 = new Doctor { Id = Guid.NewGuid(), Name = "张明", Title = "主任医师", Department = "心脏康复科", Phone = "13800000002", ProfessionalDirection = "冠心病康复、术后管理", IsActive = true, CreatedAt = DateTime.UtcNow, }; var doctor2 = new Doctor { Id = Guid.NewGuid(), Name = "李芳", Title = "副主任医师", Department = "营养科", Phone = "13800000003", ProfessionalDirection = "糖尿病管理、饮食指导", IsActive = true, CreatedAt = DateTime.UtcNow, }; var doctor3 = new Doctor { Id = Guid.NewGuid(), Name = "王建国", Title = "主任医师", Department = "心血管内科", Phone = "13800000004", ProfessionalDirection = "高血压管理、PCI术后随访", IsActive = true, CreatedAt = DateTime.UtcNow, }; db.Doctors.AddRange(doctor1, doctor2, doctor3); await db.SaveChangesAsync(); } // ---- 创建测试患者 ---- var doctorWang = db.Doctors.FirstOrDefault(d => d.Name == "王建国"); var user = new User { Id = Guid.NewGuid(), Phone = "13800000001", Name = "张三", Gender = "男", BirthDate = new DateOnly(1970, 3, 15), DoctorId = doctorWang?.Id, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, }; db.Users.Add(user); await db.SaveChangesAsync(); // ---- 健康档案 ---- db.HealthArchives.Add(new HealthArchive { Id = Guid.NewGuid(), UserId = user.Id, Diagnosis = "冠心病", SurgeryType = "PCI支架植入术", SurgeryDate = new DateOnly(2026, 3, 15), Allergies = ["青霉素"], DietRestrictions = ["低盐", "低脂"], ChronicDiseases = ["高血压", "高血脂"], FamilyHistory = "父亲冠心病", UpdatedAt = DateTime.UtcNow, }); // ---- 健康数据(过去 7 天)---- var random = new Random(42); for (int i = 7; i >= 0; i--) { var date = DateTime.UtcNow.AddDays(-i); // 血压 db.HealthRecords.Add(new HealthRecord { Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.BloodPressure, Systolic = 120 + random.Next(-5, 15), Diastolic = 75 + random.Next(-5, 10), Unit = "mmHg", Source = HealthRecordSource.AiEntry, IsAbnormal = false, RecordedAt = date, }); // 心率 db.HealthRecords.Add(new HealthRecord { Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.HeartRate, Value = 68 + random.Next(-5, 10), Unit = "次/分", Source = HealthRecordSource.AiEntry, IsAbnormal = false, RecordedAt = date, }); // 血糖 db.HealthRecords.Add(new HealthRecord { Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.Glucose, Value = 5.0m + (decimal)(random.NextDouble() * 1.5), Unit = "mmol/L", Source = HealthRecordSource.AiEntry, IsAbnormal = false, RecordedAt = date, }); // 血氧 db.HealthRecords.Add(new HealthRecord { Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.SpO2, Value = 96 + random.Next(0, 3), Unit = "%", Source = HealthRecordSource.AiEntry, IsAbnormal = false, RecordedAt = date, }); } // 一条异常血压 db.HealthRecords.Add(new HealthRecord { Id = Guid.NewGuid(), UserId = user.Id, MetricType = HealthMetricType.BloodPressure, Systolic = 148, Diastolic = 92, Unit = "mmHg", Source = HealthRecordSource.AiEntry, IsAbnormal = true, RecordedAt = DateTime.UtcNow.AddDays(-1), }); // ---- 用药计划 ---- db.Medications.Add(new Medication { Id = Guid.NewGuid(), UserId = user.Id, Name = "阿司匹林", Dosage = "100mg", Frequency = MedicationFrequency.Daily, TimeOfDay = [new TimeOnly(8, 0)], Source = MedicationSource.Prescription, IsActive = true, StartDate = new DateOnly(2026, 4, 1), }); db.Medications.Add(new Medication { Id = Guid.NewGuid(), UserId = user.Id, Name = "阿托伐他汀", Dosage = "20mg", Frequency = MedicationFrequency.Daily, TimeOfDay = [new TimeOnly(20, 0)], Source = MedicationSource.Prescription, IsActive = true, StartDate = new DateOnly(2026, 4, 1), }); // ---- 运动计划 ---- var monday = DateOnly.FromDateTime(DateTime.UtcNow.AddHours(8).AddDays(-(int)DateTime.UtcNow.AddHours(8).DayOfWeek + 1)); var plan = new ExercisePlan { Id = Guid.NewGuid(), UserId = user.Id, WeekStartDate = monday }; plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 0, ExerciseType = "散步", DurationMinutes = 30, IsCompleted = true, CompletedAt = DateTime.UtcNow.AddDays(-1) }); plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 1, ExerciseType = "慢跑", DurationMinutes = 20 }); plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 2, ExerciseType = "散步", DurationMinutes = 30 }); plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 3, IsRestDay = true }); plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 4, ExerciseType = "太极", DurationMinutes = 40 }); plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 5, IsRestDay = true }); plan.Items.Add(new ExercisePlanItem { Id = Guid.NewGuid(), DayOfWeek = 6, ExerciseType = "散步", DurationMinutes = 30 }); db.ExercisePlans.Add(plan); // ---- 饮食记录 ---- var lunch = new DietRecord { Id = Guid.NewGuid(), UserId = user.Id, MealType = MealType.Lunch, TotalCalories = 644, HealthScore = 3, RecordedAt = DateOnly.FromDateTime(DateTime.UtcNow.AddHours(8)), }; lunch.FoodItems.Add(new DietFoodItem { Id = Guid.NewGuid(), Name = "米饭", Portion = "约1碗", Calories = 174, SortOrder = 1 }); lunch.FoodItems.Add(new DietFoodItem { Id = Guid.NewGuid(), Name = "红烧肉", Portion = "约5块", Calories = 470, Warning = "脂肪含量偏高", SortOrder = 2 }); db.DietRecords.Add(lunch); // ---- 复查计划 ---- db.FollowUps.Add(new FollowUp { Id = Guid.NewGuid(), UserId = user.Id, Title = "心内科复查", DoctorName = "王建国", Department = "心血管内科", ScheduledAt = DateTime.UtcNow.AddDays(3), Status = FollowUpStatus.Upcoming, }); db.FollowUps.Add(new FollowUp { Id = Guid.NewGuid(), UserId = user.Id, Title = "术后3周复查", DoctorName = "王建国", Department = "心血管内科", ScheduledAt = DateTime.UtcNow.AddDays(-14), Status = FollowUpStatus.Completed, }); await db.SaveChangesAsync(); Console.WriteLine($"[DEV] 测试数据已填充:用户 {user.Phone}"); } }