😎1、优化种子处理 2、获取外网IP时增加异常处理
This commit is contained in:
parent
944e372e1e
commit
e7617303bc
@ -33,7 +33,7 @@ public abstract class EntityBase : EntityBaseId, IDeletedFilter
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新时间
|
/// 更新时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SugarColumn(ColumnDescription = "更新时间", IsOnlyIgnoreInsert = true)]
|
[SugarColumn(ColumnDescription = "更新时间")]
|
||||||
public virtual DateTime? UpdateTime { get; set; }
|
public virtual DateTime? UpdateTime { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -11,6 +11,9 @@ public static class SqlSugarSetup
|
|||||||
// 多租户实例
|
// 多租户实例
|
||||||
public static ITenant ITenant { get; set; }
|
public static ITenant ITenant { get; set; }
|
||||||
|
|
||||||
|
// 是否正在处理种子数据
|
||||||
|
private static bool _isHandlingSeedData = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// SqlSugar 上下文初始化
|
/// SqlSugar 上下文初始化
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -165,6 +168,9 @@ public static class SqlSugarSetup
|
|||||||
// 数据审计
|
// 数据审计
|
||||||
db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
||||||
{
|
{
|
||||||
|
// 若正在处理种子数据则直接返回
|
||||||
|
if (_isHandlingSeedData) return;
|
||||||
|
|
||||||
// 新增/插入
|
// 新增/插入
|
||||||
if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
||||||
{
|
{
|
||||||
@ -322,6 +328,8 @@ public static class SqlSugarSetup
|
|||||||
// 初始化种子数据
|
// 初始化种子数据
|
||||||
if (config.SeedSettings.EnableInitSeed)
|
if (config.SeedSettings.EnableInitSeed)
|
||||||
{
|
{
|
||||||
|
_isHandlingSeedData = true;
|
||||||
|
|
||||||
Log.Information($"初始化种子数据 {config.DbType} - {config.ConfigId}");
|
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<>))))
|
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
||||||
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
|
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
|
||||||
@ -353,12 +361,12 @@ public static class SqlSugarSetup
|
|||||||
if (seedData == null) continue;
|
if (seedData == null) continue;
|
||||||
|
|
||||||
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
|
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
|
||||||
Console.WriteLine($"添加数据 {entityInfo.DbTableName} ({config.ConfigId} - {++count}/{sum})");
|
Console.WriteLine($"添加数据 {entityInfo.DbTableName} ({config.ConfigId} - {++count}/{sum},数据量:{seedData.Count()})");
|
||||||
|
|
||||||
if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
|
if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
|
||||||
{
|
{
|
||||||
//拆分表的操作需要实体类型,而通过反射很难实现
|
// 拆分表的操作需要实体类型,而通过反射很难实现
|
||||||
//所以,这里将Init方法写在“种子数据类”内部,再传入 db 反射调用
|
// 所以,这里将Init方法写在“种子数据类”内部,再传入 db 反射调用
|
||||||
var hasInitMethod = seedType.GetMethod("Init");
|
var hasInitMethod = seedType.GetMethod("Init");
|
||||||
var parameters = new object[] { db };
|
var parameters = new object[] { db };
|
||||||
hasInitMethod?.Invoke(instance, parameters);
|
hasInitMethod?.Invoke(instance, parameters);
|
||||||
@ -369,9 +377,15 @@ public static class SqlSugarSetup
|
|||||||
{
|
{
|
||||||
// 按主键进行批量增加和更新
|
// 按主键进行批量增加和更新
|
||||||
var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
|
var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
|
||||||
storage.AsInsertable.ExecuteCommand();
|
|
||||||
|
// 先修改再插入,否则会更新修改时间字段
|
||||||
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
|
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
|
||||||
storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand();
|
{
|
||||||
|
int updateCount = storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand();
|
||||||
|
Console.WriteLine($" 修改 {updateCount}/{seedData.Count()} 条记录");
|
||||||
|
}
|
||||||
|
int insertCount = storage.AsInsertable.ExecuteCommand();
|
||||||
|
Console.WriteLine($" 插入 {insertCount}/{seedData.Count()} 条记录");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -381,6 +395,8 @@ public static class SqlSugarSetup
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_isHandlingSeedData = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -147,10 +147,17 @@ public static class ComputerUtil
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetIpFromOnline()
|
public static string GetIpFromOnline()
|
||||||
{
|
{
|
||||||
var url = "https://www.ip.cn/api/index?ip&type=0";
|
try
|
||||||
var str = url.GetAsStringAsync().GetAwaiter().GetResult();
|
{
|
||||||
var resp = JSON.Deserialize<IpCnResp>(str);
|
var url = "https://www.ip.cn/api/index?ip&type=0";
|
||||||
return resp.Ip + " " + resp.Address;
|
var str = url.GetAsStringAsync().GetAwaiter().GetResult();
|
||||||
|
var resp = JSON.Deserialize<IpCnResp>(str);
|
||||||
|
return resp.Ip + " " + resp.Address;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return "unknow";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsUnix()
|
public static bool IsUnix()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user