using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using Fasterflect;
using SqlSugar;
namespace DS.Module.SqlSugar
{
public static class SqlsugarHelper
{
#region 新增操作 + InsertByObjectForSqlsugar
///
/// 新增操作
///
///
///
public static void InsertByObjectForSqlsugar(DataFilterModel entityInfo, IUser user)
{
// 新增操作
if (entityInfo.OperationType == DataFilterType.InsertByObject)
{
if (entityInfo.PropertyName == "Id")
{
if (entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(string))
{
entityInfo.SetValue(GuidHelper.GetSnowflakeId());
}
if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
{
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
if (id == null || (long)id == 0)
entityInfo.SetValue(SnowFlakeSingle.Instance.NextId());
}
}
if (entityInfo.PropertyName == "CreateTime")
entityInfo.SetValue(DateTime.Now);
if (entityInfo.PropertyName == "TenantId")
{
var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
if (tenantId == null || tenantId == 0)
entityInfo.SetValue(user.TenantId);
}
if (entityInfo.PropertyName == "TenantName")
{
var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
if (tenantId == null || tenantId == 0)
entityInfo.SetValue(user.TenantName);
}
if (entityInfo.PropertyName == "OrgId")
{
var orgId = ((dynamic)entityInfo.EntityValue).OrgId;
if (orgId == null || orgId == 0)
entityInfo.SetValue(user.OrgId);
}
if (entityInfo.PropertyName == "CreateBy")
{
object createByValue = PropertyExtensions.GetPropertyValue(entityInfo.EntityValue, entityInfo.PropertyName, Flags.InstancePublic);
if (createByValue == null || Equals(createByValue, 0L))
{
if (!user.UserId.IsNullOrEmpty())
entityInfo.SetValue(user.UserId);
else
entityInfo.SetValue(1288018625843826688);
}
}
if (entityInfo.PropertyName == "CreateUserName")
{
var createUserName = PropertyExtensions.GetPropertyValue(entityInfo.EntityValue, entityInfo.PropertyName, Flags.InstancePublic) as string;
if (string.IsNullOrEmpty(createUserName))
{
if (!user.UserId.IsNullOrEmpty())
entityInfo.SetValue(user.UserName);
else
entityInfo.SetValue("超级管理员");
}
}
if (entityInfo.PropertyName == "Deleted")
entityInfo.SetValue(false);
}
}
#endregion 新增操作 + InsertByObjectForSqlsugar
#region 更新操作 + UpdateByObjectForSqlsugar
///
/// 更新操作
///
///
///
public static void UpdateByObjectForSqlsugar(DataFilterModel entityInfo, IUser user)
{
// 更新操作
if (entityInfo.OperationType == DataFilterType.UpdateByObject)
{
if (entityInfo.PropertyName == "UpdateTime")
entityInfo.SetValue(DateTime.Now);
if (entityInfo.PropertyName == "UpdateBy" && user != null)
entityInfo.SetValue(user.UserId);
if (entityInfo.PropertyName == "UpdateUserName" && user != null)
entityInfo.SetValue(user.UserName);
}
}
#endregion 更新操作 + UpdateByObjectForSqlsugar
#region 加载配置文件 + ReturnConnectionConfig
public static List ReturnConnectionConfig(List dbList, ICacheService cache)
{
var connectConfigList = new List();
foreach (var item in dbList)
{
//防止数据库重复,导致的事务异常
if (connectConfigList.Any(a =>
a.ConfigId == (dynamic)item.ConnId || a.ConnectionString == item.Connection))
{
continue;
}
connectConfigList.Add(new ConnectionConfig()
{
ConnectionString = item.Connection,
DbType = (DbType)item.DbType,
IsAutoCloseConnection = true,
ConfigId = item.ConnId,
InitKeyType = InitKeyType.Attribute,
MoreSettings = new ConnMoreSettings()
{
IsAutoRemoveDataCache = true //自动清理缓存
},
// 自定义特性
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = cache
}
});
}
return connectConfigList;
}
#endregion 加载配置文件 + ReturnConnectionConfig
}
}