From cc86ffe760ba4b4bd086c788ea4e7f33029db7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8D=E6=BD=9C?= <1620556043@qq.com> Date: Tue, 25 Mar 2025 18:48:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=A7=8D=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs | 4 ++- .../Data/Seeds/ExamDbContextSeed.cs | 33 +++++++++-------- .../Data/Seeders/RoleSeeder.cs | 36 +++++++++++-------- .../Data/Seeders/UserSeeder.cs | 11 ++++-- .../Data/Seeders/ConversationSeeder.cs | 13 +++++-- 5 files changed, 63 insertions(+), 34 deletions(-) diff --git a/Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs b/Src/CodeSpirit.ExamApi/Data/ExamDbContext.cs index cb3b88b..c4ac82b 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 e796dec..ed4a7ca 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 ebfd4d5..d5f1ed7 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 2459ddb..a5886dd 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 ddef420..1ba8c8a 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()) { -- Gitee