You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.7 KiB
105 lines
4.7 KiB
using Microsoft.EntityFrameworkCore.Infrastructure; |
|
using Microsoft.EntityFrameworkCore.Storage; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Threading.Tasks; |
|
using AX.FireTrainingSys.Models; |
|
using static BCrypt.Net.BCrypt; |
|
using AX.FireTrainingSys.Services; |
|
|
|
namespace AX.FireTrainingSys |
|
{ |
|
/// <summary> |
|
/// DbContext 扩展类。 |
|
/// </summary> |
|
public static class DbContextExtensions |
|
{ |
|
private const string DefaultPassword = "12345678"; |
|
|
|
/// <summary> |
|
/// 向数据库内置一些数据。 |
|
/// </summary> |
|
/// <param name="context"></param> |
|
public static void SeedDatabase(this DriveDbContext context) |
|
{ |
|
if (!(context.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists()) |
|
throw new InvalidOperationException("请先创建数据库!"); |
|
|
|
context.SetupAccount(); |
|
} |
|
|
|
private static void SetupAccount(this DriveDbContext dbContext) |
|
{ |
|
//内置管理员帐号 |
|
if (!dbContext.Users.Any()) |
|
{ |
|
var admin = new User |
|
{ |
|
Name = "admin", |
|
RealName = "管理员", |
|
Password = HashPassword(DefaultPassword), |
|
RoleType = RoleType.Admin, |
|
}; |
|
|
|
dbContext.Users.Add(admin); |
|
dbContext.SaveChanges(); |
|
} |
|
|
|
//内置职务 |
|
if (!dbContext.Posts.Any()) |
|
{ |
|
var list = new List<Post> |
|
{ |
|
//支队级 |
|
new Post { Id = 1000, Name = "支队级/主官" }, |
|
new Post { Id = 1001, Name = "支队级/副官" }, |
|
new Post { Id = 1002, Name = "支队级/灭火救援指挥岗位" }, |
|
new Post { Id = 1003, Name = "支队级/政工岗位" }, |
|
new Post { Id = 1004, Name = "支队级/后勤与保障岗位" }, |
|
new Post { Id = 1005, Name = "支队级/防火监督岗位" }, |
|
new Post { Id = 1006, Name = "支队级/指挥中心" }, |
|
new Post { Id = 1007, Name = "支队级/安全员" }, |
|
//大队级 |
|
new Post { Id = 2000, Name = "大队级/主官" }, |
|
new Post { Id = 2001, Name = "大队级/副官" }, |
|
new Post { Id = 2002, Name = "大队级/灭火救援指挥岗位" }, |
|
new Post { Id = 2003, Name = "大队级/政工岗位" }, |
|
new Post { Id = 2004, Name = "大队级/后勤与保障岗位" }, |
|
new Post { Id = 2005, Name = "大队级/防火监督岗位" }, |
|
new Post { Id = 2006, Name = "大队级/安全员" }, |
|
//消防救援站 |
|
new Post { Id = 3000, Name = "消防救援站/消防站指挥员" }, |
|
new Post { Id = 3001, Name = "消防救援站/站长助理" }, |
|
new Post { Id = 3002, Name = "消防救援站/战斗员" }, |
|
new Post { Id = 3003, Name = "消防救援站/供水员" }, |
|
new Post { Id = 3004, Name = "消防救援站/训导员" }, |
|
new Post { Id = 3005, Name = "消防救援站/通信员" }, |
|
new Post { Id = 3006, Name = "消防救援站/无人机飞手" }, |
|
new Post { Id = 3007, Name = "消防救援站/消防车驾驶员" }, |
|
new Post { Id = 3008, Name = "消防救援站/装备技师" }, |
|
new Post { Id = 3009, Name = "消防救援站/安全员" } |
|
}; |
|
|
|
dbContext.Posts.AddRange(list); |
|
dbContext.SaveChanges(); |
|
} |
|
|
|
//内置单位类型 |
|
if (!dbContext.BuildingTypes.Any()) |
|
{ |
|
var type0 = new BuildingType() { Id = 1, Name = "高层" }; |
|
var type1 = new BuildingType() { Id = 2, Name = "大型城市综合体" }; |
|
var type2 = new BuildingType() { Id = 3, Name = "地下建筑" }; |
|
var type3 = new BuildingType() { Id = 4, Name = "石油化工" }; |
|
var type4 = new BuildingType() { Id = 5, Name = "地铁" }; |
|
var type5 = new BuildingType() { Id = 6, Name = "堆场、仓库、厂房" }; |
|
var type6 = new BuildingType() { Id = 7, Name = "隧道" }; |
|
var type7 = new BuildingType() { Id = 8, Name = "人员密集" }; |
|
|
|
dbContext.BuildingTypes.AddRange(type0, type1, type2, type3, type4, type5, type6, type7); |
|
dbContext.SaveChanges(); |
|
} |
|
} |
|
} |
|
}
|
|
|