You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.8 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using SqlSugar;
namespace DS.Module.SqlSugar
{
public static class SqlsugarHelper
{
/// <summary>
/// 新增操作
/// </summary>
/// <param name="entityInfo"></param>
/// <param name="user"></param>
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.PropertyInfo.PropertyType == typeof(long))
{
entityInfo.SetValue(SnowFlakeSingle.Instance.NextId());
}
}
if (entityInfo.PropertyName == "CreateTime")
entityInfo.SetValue(DateTime.Now);
if (!user.UserId.IsNullOrEmpty())
{
if (entityInfo.PropertyName == "TenantId")
entityInfo.SetValue(user.GetTenantId());
}
if (entityInfo.PropertyName == "CreateBy")
{
if (!user.UserId.IsNullOrEmpty())
{
entityInfo.SetValue(user.UserId);
}
else
{
entityInfo.SetValue(1288018625843826688);
}
}
if (entityInfo.PropertyName == "Deleted")
entityInfo.SetValue(false);
}
}
/// <summary>
/// 更新操作
/// </summary>
/// <param name="entityInfo"></param>
/// <param name="user"></param>
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);
}
}
public static List<ConnectionConfig> ReturnConnectionConfig(List<DbConfig> dbList)
{
var connectConfigList = new List<ConnectionConfig>();
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()
{
}
});
}
return connectConfigList;
}
}
}