diff --git a/Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs b/Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs index cb3b88b7d3559bc9297a33447858cf70323463ac..c4ac82b28d56d73e33d111a201c270b407324eb4 100644 --- a/Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs +++ b/Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs @@ -7,6 +7,7 @@ using CodeSpirit.Core; using CodeSpirit.ExamApi.Data.Seeds; using CodeSpirit.Core.IdGenerator; using Microsoft.Extensions.DependencyInjection; +using System; namespace CodeSpirit.ExamApi.Data; @@ -398,6 +399,7 @@ public class ExamDbContext : AuditableDbContext public async Task InitializeDatabaseAsync() { using var scope = _serviceProvider.CreateScope(); - await ExamDbContextSeed.SeedAsync(this); + var hostEnvironment = scope.ServiceProvider.GetRequiredService(); + await ExamDbContextSeed.SeedAsync(this, hostEnvironment.IsDevelopment()); } } \ No newline at end of file diff --git a/Src/CodeSpirit.ExamApi/Data/Seeds/ExamDbContextSeed.cs b/Src/CodeSpirit.ExamApi/Data/Seeds/ExamDbContextSeed.cs index e796dec7552c1308bfad0abd3527966fb4caa06d..ed4a7ca0b99f490c203cd46b7da4ed1399b958b2 100644 --- a/Src/CodeSpirit.ExamApi/Data/Seeds/ExamDbContextSeed.cs +++ b/Src/CodeSpirit.ExamApi/Data/Seeds/ExamDbContextSeed.cs @@ -15,28 +15,31 @@ public static class ExamDbContextSeed /// 初始化数据库 /// /// 数据库上下文 - /// ID生成器 - public static async Task SeedAsync(ExamDbContext context) + /// 是否是开发环境 + public static async Task SeedAsync(ExamDbContext context, bool isDevelopment) { ArgumentNullException.ThrowIfNull(context); context.UserId = -1; // 确保数据库已创建 await context.Database.MigrateAsync(); - - // 初始化基础数据 - await SeedQuestionCategoriesAsync(context); - await SeedStudentGroupsAsync(context); - await SeedQuestionsAsync(context); - await SeedStudentsAsync(context); - await SeedExamPapersAsync(context); - await SeedExamSettingsAsync(context); - await SeedWrongQuestionsAsync(context); - //await SeedExamRecordsAsync(context); - //await SeedPracticeRecordsAsync(context); - // 为ID为-1的用户初始化考试数据 - //await SeedTestUserDataAsync(context); + if (isDevelopment) + { + // 初始化基础数据 + await SeedQuestionCategoriesAsync(context); + await SeedStudentGroupsAsync(context); + await SeedQuestionsAsync(context); + await SeedStudentsAsync(context); + await SeedExamPapersAsync(context); + await SeedExamSettingsAsync(context); + await SeedWrongQuestionsAsync(context); + //await SeedExamRecordsAsync(context); + //await SeedPracticeRecordsAsync(context); + + // 为ID为-1的用户初始化考试数据 + //await SeedTestUserDataAsync(context); + } // 保存所有更改 await context.SaveChangesAsync(); diff --git a/Src/CodeSpirit.IdentityApi/Data/Seeders/RoleSeeder.cs b/Src/CodeSpirit.IdentityApi/Data/Seeders/RoleSeeder.cs index ebfd4d5684e496c9250ed20c6a3dde3174a5303f..d5f1ed7c82c98a9077025bebcd9104b63b4acfe4 100644 --- a/Src/CodeSpirit.IdentityApi/Data/Seeders/RoleSeeder.cs +++ b/Src/CodeSpirit.IdentityApi/Data/Seeders/RoleSeeder.cs @@ -9,15 +9,18 @@ namespace CodeSpirit.IdentityApi.Data.Seeders private readonly RoleManager _roleManager; private readonly ILogger _logger; private readonly IIdGenerator _idGenerator; + private readonly IHostEnvironment _hostEnvironment; public RoleSeeder( RoleManager roleManager, ILogger logger, - IIdGenerator idGenerator) + IIdGenerator idGenerator, + IHostEnvironment hostEnvironment) { _roleManager = roleManager; _logger = logger; _idGenerator = idGenerator; + _hostEnvironment = hostEnvironment; } public async Task SeedRolesAsync(List roles) @@ -50,19 +53,24 @@ namespace CodeSpirit.IdentityApi.Data.Seeders public List GetRoles() { - return - [ - new ApplicationRole { Id = _idGenerator.NewId(), Name = "Admin", Description = "系统管理员,拥有所有权限。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "项目经理", Description = "项目经理,负责项目管理和团队协调。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "开发人员", Description = "开发人员,负责编码和实现功能。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "测试人员", Description = "测试人员,负责软件测试和质量保证。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "技术支持", Description = "支持人员,提供技术支持和客户服务。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "人力资源", Description = "人力资源,管理员工信息和招聘流程。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "财务人员", Description = "财务人员,负责财务管理和预算控制。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "销售人员", Description = "销售人员,负责销售和市场推广。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "市场营销", Description = "市场营销,负责市场分析和营销策略。" }, - new ApplicationRole { Id = _idGenerator.NewId(), Name = "访客", Description = "访客,具有最低权限的用户。" } - ]; + var roles = new List() + { + new() { Id = _idGenerator.NewId(), Name = "Admin", Description = "系统管理员,拥有所有权限。" } + }; + + if (_hostEnvironment.IsDevelopment()) + { + roles.Add(new() { Id = _idGenerator.NewId(), Name = "项目经理", Description = "项目经理,负责项目管理和团队协调。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "开发人员", Description = "开发人员,负责编码和实现功能。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "测试人员", Description = "测试人员,负责软件测试和质量保证。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "技术支持", Description = "支持人员,提供技术支持和客户服务。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "人力资源", Description = "人力资源,管理员工信息和招聘流程。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "财务人员", Description = "财务人员,负责财务管理和预算控制。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "销售人员", Description = "销售人员,负责销售和市场推广。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "市场营销", Description = "市场营销,负责市场分析和营销策略。" }); + roles.Add(new() { Id = _idGenerator.NewId(), Name = "访客", Description = "访客,具有最低权限的用户。" }); + } + return roles; } } } diff --git a/Src/CodeSpirit.IdentityApi/Data/Seeders/UserSeeder.cs b/Src/CodeSpirit.IdentityApi/Data/Seeders/UserSeeder.cs index 2459ddb23adfa58f1c652fc80e001a3495c041cb..a5886dd866448d0e8618a0a39dd5d072608d1908 100644 --- a/Src/CodeSpirit.IdentityApi/Data/Seeders/UserSeeder.cs +++ b/Src/CodeSpirit.IdentityApi/Data/Seeders/UserSeeder.cs @@ -10,19 +10,21 @@ public class UserSeeder : IScopedDependency private readonly RoleManager _roleManager; private readonly UserManager _userManager; private readonly IIdGenerator _idGenerator; - + private readonly IHostEnvironment _hostEnvironment; public UserSeeder( IServiceProvider serviceProvider, ILogger logger, RoleManager roleManager, UserManager userManager, - IIdGenerator idGenerator) + IIdGenerator idGenerator, + IHostEnvironment hostEnvironment) { _serviceProvider = serviceProvider; _logger = logger; _roleManager = roleManager; _userManager = userManager; _idGenerator = idGenerator; + _hostEnvironment = hostEnvironment; } public async Task SeedAdminUserAsync() @@ -108,6 +110,11 @@ public class UserSeeder : IScopedDependency public async Task SeedRandomUsersAsync(int userCount, RoleManager roleManager) { + if (!_hostEnvironment.IsDevelopment()) + { + return; + } + if (await _userManager.Users.AnyAsync()) { return; diff --git a/Src/CodeSpirit.Messaging/Data/Seeders/ConversationSeeder.cs b/Src/CodeSpirit.Messaging/Data/Seeders/ConversationSeeder.cs index ddef420a22962a1b9d1bb5e55eccad774da8f91a..1ba8c8a0d25f95c903c57add164cca612ad8c2e2 100644 --- a/Src/CodeSpirit.Messaging/Data/Seeders/ConversationSeeder.cs +++ b/Src/CodeSpirit.Messaging/Data/Seeders/ConversationSeeder.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq; +using Microsoft.Extensions.Hosting; namespace CodeSpirit.Messaging.Data.Seeders { @@ -14,7 +15,7 @@ namespace CodeSpirit.Messaging.Data.Seeders { private readonly ILogger _logger; private readonly MessagingDbContext _dbContext; - + public readonly IHostEnvironment _hostEnvironment; /// /// 构造函数 /// @@ -22,10 +23,12 @@ namespace CodeSpirit.Messaging.Data.Seeders /// 数据库上下文 public ConversationSeeder( ILogger logger, - MessagingDbContext dbContext) + MessagingDbContext dbContext, + IHostEnvironment hostEnvironment) { _logger = logger; _dbContext = dbContext; + _hostEnvironment = hostEnvironment; } /// @@ -35,6 +38,12 @@ namespace CodeSpirit.Messaging.Data.Seeders { try { + // 检查 非开发环境不创建种子数据 + if (!_hostEnvironment.IsDevelopment()) + { + return; + } + // 检查是否已经有数据 if (_dbContext.Conversations.Any()) {