using Microsoft.EntityFrameworkCore; using HardwarePerformance.Core.Entities; namespace HardwarePerformance.Infrastructure.Data { public class DataSeeder { public static void SeedData(AppDbContext context) { // 确保数据库已创建 context.Database.EnsureCreated(); // 检查是否已有数据 if (context.Categories.Any()) { return; // 数据库已有数据,不需要种子数据 } // 添加初始产品类别 var categories = new List { new Category { Name = "手机CPU", Description = "移动设备处理器性能排名" }, new Category { Name = "手机GPU", Description = "移动设备图形处理器性能排名" }, new Category { Name = "电脑CPU", Description = "桌面和笔记本处理器性能排名" }, new Category { Name = "电脑GPU", Description = "桌面和笔记本图形处理器性能排名" } }; context.Categories.AddRange(categories); context.SaveChanges(); // 添加初始数据源 var dataSources = new List { new DataSource { Name = "Geekbench", Description = "跨平台处理器和内存性能测试工具", Url = "https://browser.geekbench.com/" }, new DataSource { Name = "3DMark", Description = "游戏和图形性能基准测试工具", Url = "https://www.3dmark.com/" }, new DataSource { Name = "AnTuTu", Description = "移动设备综合性能测试平台", Url = "https://www.antutu.com/" } }; context.DataSources.AddRange(dataSources); context.SaveChanges(); // 添加一些示例产品数据 var mobileCpuCategory = context.Categories.FirstOrDefault(c => c.Name == "手机CPU"); var mobileGpuCategory = context.Categories.FirstOrDefault(c => c.Name == "手机GPU"); var desktopCpuCategory = context.Categories.FirstOrDefault(c => c.Name == "电脑CPU"); var desktopGpuCategory = context.Categories.FirstOrDefault(c => c.Name == "电脑GPU"); var geekbenchSource = context.DataSources.FirstOrDefault(s => s.Name == "Geekbench"); var threeDMarkSource = context.DataSources.FirstOrDefault(s => s.Name == "3DMark"); var antutuSource = context.DataSources.FirstOrDefault(s => s.Name == "AnTuTu"); if (mobileCpuCategory != null && geekbenchSource != null) { var mobileCpus = new List { new Product { Name = "Apple A17 Pro", Model = "A17 Pro", Manufacturer = "Apple", ReleaseDate = new DateTime(2023, 9, 1), ImageUrl = "/images/apple-a17-pro.jpg", CategoryId = mobileCpuCategory.Id, CurrentRank = 1 }, new Product { Name = "Snapdragon 8 Gen 3", Model = "SM8650-AB", Manufacturer = "Qualcomm", ReleaseDate = new DateTime(2023, 10, 1), ImageUrl = "/images/snapdragon-8-gen-3.jpg", CategoryId = mobileCpuCategory.Id, CurrentRank = 2 }, new Product { Name = "MediaTek Dimensity 9300", Model = "MT6989", Manufacturer = "MediaTek", ReleaseDate = new DateTime(2023, 11, 1), ImageUrl = "/images/dimensity-9300.jpg", CategoryId = mobileCpuCategory.Id, CurrentRank = 3 } }; context.Products.AddRange(mobileCpus); context.SaveChanges(); // 添加性能分数 var performanceScores = new List(); foreach (var cpu in mobileCpus) { performanceScores.Add(new PerformanceScore { ProductId = cpu.Id, TestName = "Single-Core", Score = cpu.Name.Contains("A17") ? 2950 : cpu.Name.Contains("Snapdragon") ? 2300 : 2200, TestDate = DateTime.Now, DataSourceId = geekbenchSource.Id }); performanceScores.Add(new PerformanceScore { ProductId = cpu.Id, TestName = "Multi-Core", Score = cpu.Name.Contains("A17") ? 7200 : cpu.Name.Contains("Snapdragon") ? 7400 : 7500, TestDate = DateTime.Now, DataSourceId = geekbenchSource.Id }); } context.PerformanceScores.AddRange(performanceScores); context.SaveChanges(); // 添加规格参数 var specifications = new List(); foreach (var cpu in mobileCpus) { if (cpu.Name.Contains("A17")) { specifications.Add(new Specification { ProductId = cpu.Id, Name = "制程工艺", Value = "3nm", Unit = "nm" }); specifications.Add(new Specification { ProductId = cpu.Id, Name = "核心数", Value = "6", Unit = "核" }); } else if (cpu.Name.Contains("Snapdragon")) { specifications.Add(new Specification { ProductId = cpu.Id, Name = "制程工艺", Value = "4nm", Unit = "nm" }); specifications.Add(new Specification { ProductId = cpu.Id, Name = "核心数", Value = "8", Unit = "核" }); } else if (cpu.Name.Contains("Dimensity")) { specifications.Add(new Specification { ProductId = cpu.Id, Name = "制程工艺", Value = "4nm", Unit = "nm" }); specifications.Add(new Specification { ProductId = cpu.Id, Name = "核心数", Value = "8", Unit = "核" }); } } context.Specifications.AddRange(specifications); context.SaveChanges(); } if (desktopCpuCategory != null && geekbenchSource != null) { var desktopCpus = new List { new Product { Name = "Intel Core i9-14900K", Model = "i9-14900K", Manufacturer = "Intel", ReleaseDate = new DateTime(2023, 10, 1), ImageUrl = "/images/intel-i9-14900k.jpg", CategoryId = desktopCpuCategory.Id, CurrentRank = 1 }, new Product { Name = "AMD Ryzen 9 7950X", Model = "Ryzen 9 7950X", Manufacturer = "AMD", ReleaseDate = new DateTime(2023, 9, 1), ImageUrl = "/images/amd-ryzen9-7950x.jpg", CategoryId = desktopCpuCategory.Id, CurrentRank = 2 } }; context.Products.AddRange(desktopCpus); context.SaveChanges(); // 添加性能分数 var performanceScores = new List(); foreach (var cpu in desktopCpus) { performanceScores.Add(new PerformanceScore { ProductId = cpu.Id, TestName = "Single-Core", Score = cpu.Name.Contains("Intel") ? 3200 : 2300, TestDate = DateTime.Now, DataSourceId = geekbenchSource.Id }); performanceScores.Add(new PerformanceScore { ProductId = cpu.Id, TestName = "Multi-Core", Score = cpu.Name.Contains("Intel") ? 22000 : 30000, TestDate = DateTime.Now, DataSourceId = geekbenchSource.Id }); } context.PerformanceScores.AddRange(performanceScores); context.SaveChanges(); } } } }