Falcon.SugarApi/Falcon.SugarApi/FalconClaim/Tables/SugarDbContextExtend.cs
2023-03-02 17:17:16 +08:00

69 lines
2.7 KiB
C#

using Falcon.SugarApi.DatabaseDefinitions;
using System;
namespace Falcon.SugarApi.FalconClaim.Tables
{
/// <summary>
/// 数据库扩展方法
/// </summary>
public static class SugarDbContextExtend
{
/// <summary>
/// 初始化FalconClaim角色相关表
/// </summary>
/// <param name="dbContext">数据库上下文</param>
/// <param name="InitDataFunc">默认超管数据</param>
/// <returns>数据库上下文</returns>
public static SugarDbContext InitFalconClaimRoleDbTables(this SugarDbContext dbContext,Func<InitAdministratorData>? InitDataFunc) {
dbContext.CodeFirst.InitTables<FalconClaim_Roles>();
dbContext.CodeFirst.InitTables<FalconClaim_UserInRoles>();
dbContext.CodeFirst.InitTables<FalconClaim_Right>();
dbContext.CodeFirst.InitTables<FalconClaim_RightInRoles>();
if(InitDataFunc!=null) {
var initData = InitDataFunc();
var role = initData?.Role;
var roleName = role?.RoleName;
var right = initData?.Right;
var rightName = right?.RightName;
if(roleName.IsNotNullOrEmpty()) {
var roleQu = dbContext.Queryable<FalconClaim_Roles>().Where(m => m.RoleName==roleName);
if(!roleQu.Any()) {
dbContext.Insertable(role).ExecuteCommand();
}
}
if(rightName.IsNotNullOrEmpty()) {
var rightQu = dbContext.Queryable<FalconClaim_Right>().Where(m => m.RightName==rightName);
if(!rightQu.Any()) {
dbContext.Insertable(right).ExecuteCommand();
}
}
if(roleName.IsNotNullOrEmpty()&&rightName.IsNotNullOrEmpty()) {
var map = dbContext.Queryable<FalconClaim_RightInRoles>().Where(m => m.RoleName==roleName&&m.RightName==rightName);
if(!map.Any()) {
dbContext.Insertable(new FalconClaim_RightInRoles {
RoleName=roleName,RightName=rightName,
}).ExecuteCommand();
}
}
}
return dbContext;
}
}
/// <summary>
/// 默认初始化超管数据
/// </summary>
public class InitAdministratorData
{
/// <summary>
/// 默认超管角色
/// </summary>
public FalconClaim_Roles Role { get; set; } = new FalconClaim_Roles();
/// <summary>
/// 默认超管权利
/// </summary>
public FalconClaim_Right Right { get; set; } = new FalconClaim_Right();
}
}