数据库种子初始化搭建

This commit is contained in:
陈淳
2022-09-19 17:25:43 +08:00
parent e8bb256a8d
commit 5785f5beea
8 changed files with 265 additions and 30 deletions

View File

@@ -13,6 +13,7 @@ using Yi.Framework.WebCore.SignalRHub;
using Hei.Captcha;
using Yi.Framework.WebCore;
using Microsoft.Extensions.DependencyInjection;
using Yi.Framework.WebCore.DbExtend;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddCommandLine(args);
@@ -186,6 +187,11 @@ app.UseAuthorization();
//Consul<75><6C><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
#endregion
app.UseConsulService();
#region
//<2F><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
#endregion
app.UseDbSeedInitService();
#region
//redis<69><73><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
#endregion

View File

@@ -20,16 +20,6 @@
<None Remove="wwwrooot\**" />
</ItemGroup>
<ItemGroup>
<None Remove="yi-sqlsugar-dev.db" />
</ItemGroup>
<ItemGroup>
<Content Include="yi-sqlsugar-dev.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.WebCore\Yi.Framework.WebCore.csproj" />
</ItemGroup>
@@ -39,4 +29,10 @@
<Folder Include="wwwroot\image\" />
</ItemGroup>
<ItemGroup>
<None Update="yi-sqlsugar-dev.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,22 +1,125 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.Enum;
using Yi.Framework.Model.Models;
namespace Yi.Framework.Model.SeedData
{
public class MenuSeed: AbstractSeed<MenuEntity>
public class MenuSeed : AbstractSeed<MenuEntity>
{
public override List<MenuEntity> GetSeed()
{
MenuEntity menu = new MenuEntity()
//系统管理
MenuEntity system = new MenuEntity()
{
MenuName="首页",
PermissionCode="*:*:*"
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "系统管理",
PermissionCode = "*:*:*",
MenuType = MenuTypeEnum.Catalogue.GetHashCode(),
Router = "/system",
IsShow = true,
IsLink = false,
MenuIcon = "system",
OrderNum = 100,
ParentId = 0,
IsDeleted = false
};
Entitys.Add(menu);
Entitys.Add(system);
//用户管理
MenuEntity user = new MenuEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "用户管理",
PermissionCode = "system:user:list",
MenuType = MenuTypeEnum.Menu.GetHashCode(),
Router = "user",
IsShow = true,
IsLink = false,
IsCache = true,
Component = "system/user/index",
MenuIcon = "user",
OrderNum = 100,
ParentId = system.Id,
IsDeleted = false
};
Entitys.Add(user);
MenuEntity userQuery = new MenuEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "用户查询",
PermissionCode = "system:user:query",
MenuType = MenuTypeEnum.Component.GetHashCode(),
OrderNum = 100,
ParentId = user.Id,
IsDeleted = false
};
Entitys.Add(userQuery);
MenuEntity userAdd = new MenuEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "用户新增",
PermissionCode = "system:user:add",
MenuType = MenuTypeEnum.Component.GetHashCode(),
OrderNum = 100,
ParentId = user.Id,
IsDeleted = false
};
Entitys.Add(userAdd);
MenuEntity userEdit = new MenuEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "用户修改",
PermissionCode = "system:user:edit",
MenuType = MenuTypeEnum.Component.GetHashCode(),
OrderNum = 100,
ParentId = user.Id,
IsDeleted = false
};
Entitys.Add(userEdit);
MenuEntity userRemove = new MenuEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "用户删除",
PermissionCode = "system:user:remove",
MenuType = MenuTypeEnum.Component.GetHashCode(),
OrderNum = 100,
ParentId = user.Id,
IsDeleted = false
};
Entitys.Add(userRemove);
//角色管理
MenuEntity role = new MenuEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
MenuName = "角色管理",
PermissionCode = "system:role:list",
MenuType = MenuTypeEnum.Menu.GetHashCode(),
Router = "role",
IsShow = true,
IsLink = false,
IsCache = true,
Component = "system/role/index",
MenuIcon = "peoples",
OrderNum = 100,
ParentId = system.Id,
IsDeleted = false
};
Entitys.Add(role);
return Entitys;
}
}

View File

@@ -1,8 +1,10 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.Enum;
using Yi.Framework.Model.Models;
namespace Yi.Framework.Model.SeedData
@@ -11,12 +13,30 @@ namespace Yi.Framework.Model.SeedData
{
public override List<RoleEntity> GetSeed()
{
RoleEntity role = new RoleEntity()
RoleEntity role1 = new RoleEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
RoleName = "管理员",
RoleCode = "admin",
DataScope = DataScopeEnum.ALL.GetHashCode(),
OrderNum = 999,
Remark ="管理员",
IsDeleted = false
};
Entitys.Add(role);
Entitys.Add(role1);
RoleEntity role2 = new RoleEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
RoleName = "测试角色",
RoleCode = "test",
DataScope = DataScopeEnum.ALL.GetHashCode(),
OrderNum = 1,
Remark = "测试用的角色",
IsDeleted = false
};
Entitys.Add(role2);
return Entitys;
}
}

View File

@@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,10 +8,10 @@ using Yi.Framework.Model.Models;
namespace Yi.Framework.Model.SeedData
{
public class SeedFactory
public class SeedFactory
{
public static List<UserEntity> GetUserSeed()
{
{
return new UserSeed().GetSeed();
}
public static List<RoleEntity> GetRoleSeed()
@@ -21,9 +22,30 @@ namespace Yi.Framework.Model.SeedData
{
return new MenuSeed().GetSeed();
}
public static List<UserRoleEntity> GetUserRoleSeed(List<UserEntity> users,List<RoleEntity> roles)
public static List<UserRoleEntity> GetUserRoleSeed(List<UserEntity> users, List<RoleEntity> roles)
{
return new List<UserRoleEntity>();
List<UserRoleEntity> userRoleEntities = new();
foreach (var u in users)
{
foreach (var r in roles)
{
userRoleEntities.Add(new UserRoleEntity() {Id= SnowFlakeSingle.Instance.NextId(),UserId = u.Id, RoleId = r.Id, IsDeleted = false });
}
}
return userRoleEntities;
}
public static List<RoleMenuEntity> GetRoleMenuSeed(List<RoleEntity> roles, List<MenuEntity> menus)
{
List<RoleMenuEntity> roleMenuEntities = new();
foreach (var r in roles)
{
foreach (var m in menus)
{
roleMenuEntities.Add(new RoleMenuEntity() { Id = SnowFlakeSingle.Instance.NextId(), RoleId = r.Id, MenuId = m.Id, IsDeleted = false });
}
}
return roleMenuEntities;
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -11,15 +12,46 @@ namespace Yi.Framework.Model.SeedData
{
public override List<UserEntity> GetSeed()
{
UserEntity user = new UserEntity()
UserEntity user1 = new UserEntity()
{
Id =SnowFlakeSingle.Instance.NextId(),
Name = "大橙子",
UserName = "cc",
Nick = "橙子",
Password = "123456"
Password = "123456",
Email="454313500@qq.com",
Phone="13800000000",
Sex=0,
Address="深圳",
Age=20,
Introduction="还有谁?",
OrderNum=999,
Remark="描述是什么呢?",
IsDeleted=false
};
user.BuildPassword();
Entitys.Add(user);
user1.BuildPassword();
Entitys.Add(user1);
UserEntity user2 = new UserEntity()
{
Id = SnowFlakeSingle.Instance.NextId(),
Name = "大测试",
UserName = "test",
Nick = "测试",
Password = "123456",
Email = "454313500@qq.com",
Phone = "15900000000",
Sex = 0,
Address = "深圳",
Age = 18,
Introduction = "还有我!",
OrderNum = 1,
Remark = "我没有描述!",
IsDeleted = false
};
user2.BuildPassword();
Entitys.Add(user2);
return Entitys;
}
}

View File

@@ -0,0 +1,56 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
using Yi.Framework.Model.SeedData;
namespace Yi.Framework.WebCore.DbExtend
{
public static class DbSeedExtend
{
public static void UseDbSeedInitService(this IApplicationBuilder app)
{
if (Appsettings.appBool("DbSeed_Enabled"))
{
var _Db = app.ApplicationServices.GetService<ISqlSugarClient>();
var users = SeedFactory.GetUserSeed();
var roles = SeedFactory.GetRoleSeed();
var menus = SeedFactory.GetMenuSeed();
if (!_Db.Queryable<UserEntity>().Any())
{
_Db.Insertable(users).ExecuteCommand();
}
if (!_Db.Queryable<RoleEntity>().Any())
{
_Db.Insertable(roles).ExecuteCommand();
}
if (!_Db.Queryable<MenuEntity>().Any())
{
_Db.Insertable(menus).ExecuteCommand();
}
if (!_Db.Queryable<UserRoleEntity>().Any())
{
_Db.Insertable(SeedFactory.GetUserRoleSeed(users, roles)).ExecuteCommand();
}
if (!_Db.Queryable<RoleMenuEntity>().Any())
{
_Db.Insertable(SeedFactory.GetRoleMenuSeed(roles, menus)).ExecuteCommand();
}
}
}
}
}

View File

@@ -20,7 +20,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
if (Appsettings.appBool("RedisSeed_Enabled"))
{
var _cacheClientDB = ServiceLocator.Instance.GetService<CacheClientDB>();
var _cacheClientDB = app.ApplicationServices.GetService<CacheClientDB>();
try
{