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.
154 lines
5.6 KiB
C#
154 lines
5.6 KiB
C#
using DS.Module.Core;
|
|
using DS.Module.Core.Extensions;
|
|
using DS.Module.UserModule;
|
|
using SqlSugar;
|
|
|
|
namespace DS.Module.SqlSugar
|
|
{
|
|
public static class SqlsugarHelper
|
|
{
|
|
#region 新增操作 + InsertByObjectForSqlsugar
|
|
|
|
/// <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.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" && entityInfo.EntityValue == null)
|
|
{
|
|
if (!user.UserId.IsNullOrEmpty())
|
|
{
|
|
entityInfo.SetValue(user.UserId);
|
|
}
|
|
else
|
|
{
|
|
entityInfo.SetValue(1288018625843826688);
|
|
}
|
|
}
|
|
if (entityInfo.PropertyName == "CreateUserName")
|
|
{
|
|
if (!user.UserId.IsNullOrEmpty())
|
|
{
|
|
entityInfo.SetValue(user.UserName);
|
|
}
|
|
else
|
|
{
|
|
entityInfo.SetValue("超级管理员");
|
|
}
|
|
}
|
|
if (entityInfo.PropertyName == "Deleted")
|
|
entityInfo.SetValue(false);
|
|
}
|
|
}
|
|
|
|
#endregion 新增操作 + InsertByObjectForSqlsugar
|
|
|
|
#region 更新操作 + UpdateByObjectForSqlsugar
|
|
|
|
/// <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);
|
|
if (entityInfo.PropertyName == "UpdateUserName" && user != null)
|
|
entityInfo.SetValue(user.UserName);
|
|
|
|
}
|
|
}
|
|
|
|
#endregion 更新操作 + UpdateByObjectForSqlsugar
|
|
|
|
#region 加载配置文件 + ReturnConnectionConfig
|
|
|
|
public static List<ConnectionConfig> ReturnConnectionConfig(List<DbConfig> dbList,ICacheService cache)
|
|
{
|
|
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()
|
|
{
|
|
DataInfoCacheService = cache
|
|
}
|
|
});
|
|
}
|
|
|
|
return connectConfigList;
|
|
}
|
|
|
|
#endregion 加载配置文件 + ReturnConnectionConfig
|
|
|
|
|
|
}
|
|
} |