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 { #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.GetTenantId()); } if (entityInfo.PropertyName == "OrgId") { var orgId = ((dynamic)entityInfo.EntityValue).OrgId; if (orgId == null || orgId == 0) entityInfo.SetValue(user.GetOrgId()); } if (entityInfo.PropertyName == "CreateBy") { if (!user.UserId.IsNullOrEmpty()) { entityInfo.SetValue(user.UserId); } else { entityInfo.SetValue(1288018625843826688); } } 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); } } #endregion 更新操作 + UpdateByObjectForSqlsugar #region 加载配置文件 + ReturnConnectionConfig public static List ReturnConnectionConfig(List dbList) { 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() { } }); } return connectConfigList; } #endregion 加载配置文件 + ReturnConnectionConfig } }