This commit is contained in:
2026-07-24 12:42:51 +08:00 Unverified
commit 67905dfa16
56 changed files with 7630 additions and 0 deletions
@@ -0,0 +1,190 @@
using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Domain.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace Jiaowu.Api.Infrastructure.Persistence;
public sealed class DatabaseInitializer(
AppDbContext db,
RoleManager<ApplicationRole> roleManager,
UserManager<ApplicationUser> userManager,
IConfiguration configuration,
IHostEnvironment environment,
ILogger<DatabaseInitializer> logger)
{
public async Task InitializeAsync()
{
if (environment.IsDevelopment())
{
await db.Database.EnsureCreatedAsync();
}
else
{
await db.Database.MigrateAsync();
}
await SeedRolesAsync();
await SeedAdministratorAsync();
if (environment.IsDevelopment())
{
await SeedDevelopmentDataAsync();
}
}
private async Task SeedRolesAsync()
{
var roleDefinitions = new Dictionary<string, (string Description, DataScope Scope)>
{
[SystemRoles.SuperAdmin] = ("系统配置与全部数据管理", DataScope.All),
[SystemRoles.AcademicAdmin] = ("校级教务管理", DataScope.All),
[SystemRoles.CollegeAdmin] = ("院系教务管理", DataScope.College),
[SystemRoles.Teacher] = ("教师教学工作台", DataScope.Self),
[SystemRoles.Counselor] = ("辅导员与班级管理", DataScope.Class),
[SystemRoles.Student] = ("学生自助服务", DataScope.Self),
[SystemRoles.Leader] = ("校级统计查看", DataScope.All)
};
foreach (var (name, definition) in roleDefinitions)
{
if (await roleManager.RoleExistsAsync(name))
{
continue;
}
var result = await roleManager.CreateAsync(new ApplicationRole
{
Name = name,
Description = definition.Description,
DataScope = definition.Scope
});
EnsureSucceeded(result, $"创建角色 {name}");
}
}
private async Task SeedAdministratorAsync()
{
var userName = configuration["SeedAdmin:UserName"];
var password = configuration["SeedAdmin:Password"];
if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
{
if (!environment.IsDevelopment())
{
logger.LogWarning("未配置 SeedAdmin,生产环境不会创建默认管理员。");
}
return;
}
var user = await userManager.FindByNameAsync(userName);
if (user is null)
{
user = new ApplicationUser
{
UserName = userName,
DisplayName = configuration["SeedAdmin:DisplayName"] ?? "系统管理员",
LockoutEnabled = true,
IsEnabled = true
};
EnsureSucceeded(await userManager.CreateAsync(user, password), "创建初始管理员");
}
if (!user.LockoutEnabled)
{
user.LockoutEnabled = true;
EnsureSucceeded(await userManager.UpdateAsync(user), "启用管理员登录保护");
}
if (!await userManager.IsInRoleAsync(user, SystemRoles.SuperAdmin))
{
EnsureSucceeded(
await userManager.AddToRoleAsync(user, SystemRoles.SuperAdmin),
"授予超级管理员角色");
}
}
private async Task SeedDevelopmentDataAsync()
{
if (await db.Campuses.AnyAsync())
{
return;
}
var campus = new Campus
{
Code = "MAIN",
Name = "主校区",
Address = "大学路 1 号"
};
var college = new College
{
Code = "CS",
Name = "计算机学院",
ShortName = "计算机学院",
CampusId = campus.Id
};
var major = new Major
{
Code = "080901",
Name = "计算机科学与技术",
CollegeId = college.Id,
DegreeType = "工学学士",
SchoolingYears = 4
};
var building = new Building
{
Code = "J1",
Name = "第一教学楼",
CampusId = campus.Id
};
db.AddRange(
campus,
college,
major,
new AdministrativeClass
{
Code = "CS2026-01",
Name = "计科 2026-1 班",
MajorId = major.Id,
Grade = 2026,
CounselorName = "陈老师"
},
building,
new Classroom
{
Code = "J1-201",
Name = "J1-201",
BuildingId = building.Id,
Capacity = 60,
RoomType = "多媒体教室",
Equipment = "投影、扩声、录播"
},
new AcademicTerm
{
Code = "2026-2027-1",
Name = "2026—2027 学年第一学期",
AcademicYear = "2026-2027",
Season = TermSeason.Autumn,
StartDate = new DateOnly(2026, 9, 7),
EndDate = new DateOnly(2027, 1, 17),
IsCurrent = true
});
await db.SaveChangesAsync();
}
private static void EnsureSucceeded(IdentityResult result, string action)
{
if (result.Succeeded)
{
return;
}
throw new InvalidOperationException(
$"{action}失败:{string.Join("", result.Errors.Select(x => x.Description))}");
}
}