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.

159 lines
5.9 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
1 month ago
using Fasterflect;
using SqlSugar;
namespace DS.Module.SqlSugar
{
public static class SqlsugarHelper
{
10 months ago
#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")
10 months ago
entityInfo.SetValue(DateTime.Now);
10 months ago
if (entityInfo.PropertyName == "TenantId")
{
10 months ago
var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
if (tenantId == null || tenantId == 0)
entityInfo.SetValue(user.TenantId);
}
4 months ago
if (entityInfo.PropertyName == "TenantName")
{
4 months ago
var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
if (tenantId == null || tenantId == 0)
entityInfo.SetValue(user.TenantName);
4 months ago
}
if (entityInfo.PropertyName == "OrgId")
{
var orgId = ((dynamic)entityInfo.EntityValue).OrgId;
if (orgId == null || orgId == 0)
3 months ago
entityInfo.SetValue(user.OrgId);
}
1 month ago
if (entityInfo.PropertyName == "CreateBy")
{
1 month ago
object createByValue = PropertyExtensions.GetPropertyValue(entityInfo.EntityValue, entityInfo.PropertyName, Flags.InstancePublic);
if (createByValue == null || Equals(createByValue, 0L))
{
1 month ago
if (!user.UserId.IsNullOrEmpty())
{
entityInfo.SetValue(user.UserId);
}
else
{
entityInfo.SetValue(1288018625843826688);
}
}
}
4 months ago
if (entityInfo.PropertyName == "CreateUserName")
{
if (!user.UserId.IsNullOrEmpty())
{
entityInfo.SetValue(user.UserName);
}
else
{
entityInfo.SetValue("超级管理员");
}
}
if (entityInfo.PropertyName == "Deleted")
entityInfo.SetValue(false);
}
}
10 months ago
#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);
4 months ago
if (entityInfo.PropertyName == "UpdateUserName" && user != null)
entityInfo.SetValue(user.UserName);
}
}
10 months ago
#endregion 更新操作 + UpdateByObjectForSqlsugar
#region 加载配置文件 + ReturnConnectionConfig
public static List<ConnectionConfig> ReturnConnectionConfig(List<DbConfig> dbList, ICacheService cache)
{
var connectConfigList = new List<ConnectionConfig>();
foreach (var item in dbList)
{
//防止数据库重复,导致的事务异常
10 months ago
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
}
});
}
10 months ago
return connectConfigList;
}
10 months ago
#endregion 加载配置文件 + ReturnConnectionConfig
}
}