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