Merge pull request '✨ feat(SeedData): 拆分种子数据类型获取逻辑到帮助类' (#407) from jasondom/Admin.NET.Pro:v2-1 into v2
Reviewed-on: https://code.adminnet.top/Admin.NET/Admin.NET.Pro/pulls/407
This commit is contained in:
commit
5a9eb4264b
@ -497,7 +497,7 @@ public class SysDatabaseService : IDynamicApiController, ITransient
|
||||
var pkInfo = entityTypes[0].GetProperties().FirstOrDefault(u => u.GetCustomAttribute<SugarColumn>()?.IsPrimaryKey == true);
|
||||
if (pkInfo != null)
|
||||
{
|
||||
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>)) && i.GenericTypeArguments[0] == entityTypes[0])).ToList();
|
||||
var seedDataTypes = SeedDataHelper.GetSeedDataTypeList(entityTypes[0]);
|
||||
// 可能会重名的种子数据不作为过滤项
|
||||
string doNotFilterFullName1 = $"{input.Position}.SeedData.{input.SeedDataName}";
|
||||
string doNotFilterFullName2 = $"{input.Position}.{input.SeedDataName}"; // Core中的命名空间没有SeedData
|
||||
@ -639,10 +639,7 @@ public class SysDatabaseService : IDynamicApiController, ITransient
|
||||
[DisplayName("获取种子数据列表")]
|
||||
public List<DataInitItemOutput> GetSeedDataList([FromQuery] string configId)
|
||||
{
|
||||
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
||||
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
||||
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList();
|
||||
|
||||
var seedDataTypes = SeedDataHelper.GetInitSeedDataTypeList();
|
||||
var outputList = new List<DataInitItemOutput>();
|
||||
foreach (var seedDataType in seedDataTypes)
|
||||
{
|
||||
|
||||
@ -440,10 +440,7 @@ public static class SqlSugarSetup
|
||||
|
||||
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||
Log.Information($"初始化种子数据 {config.DbType} - {config.ConfigId}");
|
||||
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
||||
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
||||
.WhereIF(enableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
|
||||
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList();
|
||||
var seedDataTypes = SeedDataHelper.GetInitSeedDataTypeList(enableIncreSeed);
|
||||
|
||||
// 过滤指定程序集种子
|
||||
if (seedTypes != null && seedTypes.Count > 0)
|
||||
@ -574,9 +571,7 @@ public static class SqlSugarSetup
|
||||
/// <param name="tenantId">租户Id</param>
|
||||
public static void InitTenantData(ITenant iTenant, long dbConfigId, long tenantId)
|
||||
{
|
||||
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
||||
.Where(u => u.IsDefined(typeof(TenantSeedAttribute), false))
|
||||
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList();
|
||||
var seedDataTypes = SeedDataHelper.GetTenantSeedDataTypeList();
|
||||
if (seedDataTypes.Count < 1) return;
|
||||
|
||||
var db = iTenant.GetConnectionScope(dbConfigId);
|
||||
|
||||
86
Admin.NET/Admin.NET.Core/Utils/System/SeedDataHelper.cs
Normal file
86
Admin.NET/Admin.NET.Core/Utils/System/SeedDataHelper.cs
Normal file
@ -0,0 +1,86 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 种子数据帮助类
|
||||
/// </summary>
|
||||
public static class SeedDataHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取初始化种子数据类型
|
||||
/// </summary>
|
||||
/// <param name="enableIncreSeed">增量种子</param>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetInitSeedDataTypeList(bool enableIncreSeed = false)
|
||||
{
|
||||
return GetAllSeedDataTypeList()
|
||||
.WhereIF(enableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
|
||||
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
||||
.SeedOrder()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据表实体获取种子数据类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetSeedDataTypeList<T>()
|
||||
{
|
||||
return GetAllSeedDataTypeList().Where(u => u.GetInterfaces().FirstOrDefault()
|
||||
?.GetGenericArguments().FirstOrDefault() == typeof(T)).SeedOrder().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据表实体获取种子数据类型
|
||||
/// </summary>
|
||||
/// <param name="genericType">表实体类型</param>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetSeedDataTypeList(Type genericType)
|
||||
{
|
||||
return GetAllSeedDataTypeList().Where(u => u.GetInterfaces().FirstOrDefault()
|
||||
?.GetGenericArguments().FirstOrDefault() == genericType).SeedOrder().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据委托方法获取种子数据类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetSeedDataTypeList(Func<Type, Type, bool> func = null)
|
||||
{
|
||||
return GetAllSeedDataTypeList().WhereIF(func != null, u => func!(u, u.GetInterfaces().FirstOrDefault()
|
||||
?.GetGenericArguments().FirstOrDefault())).SeedOrder().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取租户种子数据类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetTenantSeedDataTypeList()
|
||||
{
|
||||
return GetAllSeedDataTypeList().Where(u => u.IsDefined(typeof(TenantSeedAttribute), false)).SeedOrder().ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有继承了ISqlSugarEntitySeedData接口的类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<Type> GetAllSeedDataTypeList()
|
||||
{
|
||||
return App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass)
|
||||
.Where(u => u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 种子类型排序
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<Type> SeedOrder(this IEnumerable<Type> types)
|
||||
{
|
||||
return types.OrderBy(u => u.GetCustomAttribute<SeedDataAttribute>(false)?.Order ?? 0).ToList();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user