Просмотр исходного кода

修复自定义排序时驼峰转下划线错误

zuohuaijun 3 лет назад
Родитель
Сommit
9c426ca258

+ 9 - 4
Admin.NET/Admin.NET.Core/Extension/RepositoryExtension.cs

@@ -76,14 +76,19 @@ public static class RepositoryExtension
         {
             orderStr = descSort ? defualtSortField + " Desc" : defualtSortField + " Asc";
         }
-        TypeAdapterConfig config = new();
-        config.ForType<T, BasePageInput>().IgnoreNullValues(true);
-        Mapper mapper = new(config); // 务必将mapper设为单实例
+        TypeAdapterConfig typeAdapterConfig = new();
+        typeAdapterConfig.ForType<T, BasePageInput>().IgnoreNullValues(true);
+        Mapper mapper = new(typeAdapterConfig); // 务必将mapper设为单实例
         var nowPagerInput = mapper.Map<BasePageInput>(pageInput);
         // 排序是否可用-排序字段和排序顺序都为非空才启用排序
         if (!string.IsNullOrEmpty(nowPagerInput.Field) && !string.IsNullOrEmpty(nowPagerInput.Order))
         {
-            orderStr = $"{nowPagerInput.Field} {(nowPagerInput.Order == nowPagerInput.DescStr ? "Desc" : "Asc")}";
+            var conConfigs = App.GetOptions<DbConnectionOptions>().ConnectionConfigs;
+            var config = queryable.Context.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId ?
+                conConfigs.FirstOrDefault(u => u.ConfigId == queryable.Context.CurrentConnectionConfig.ConfigId) :
+                conConfigs.FirstOrDefault();
+            var field = config != null && config.EnableUnderLine ? UtilMethods.ToUnderLine(nowPagerInput.Field) : nowPagerInput.Field;
+            orderStr = $"{field} {(nowPagerInput.Order == nowPagerInput.DescStr ? "Desc" : "Asc")}";
         }
         return queryable.OrderByIF(!string.IsNullOrWhiteSpace(orderStr), orderStr);
     }

+ 9 - 4
Admin.NET/Admin.NET.Core/Service/Tenant/SysTenantService.cs

@@ -1,3 +1,5 @@
+using System.Linq;
+
 namespace Admin.NET.Core.Service;
 
 /// <summary>
@@ -364,14 +366,17 @@ public class SysTenantService : IDynamicApiController, ITransient
         if (tenant.DbType == SqlSugar.DbType.Oracle)
             throw Oops.Oh(ErrorCodeEnum.Z1002);
 
+        // 默认数据库配置
+        var defautConfig = App.GetOptions<DbConnectionOptions>().ConnectionConfigs.FirstOrDefault();
+
         var config = new DbConnectionConfig
         {
-            EnableInitDb = true,
-            EnableDiffLog = false,
-            DbType = tenant.DbType,
             ConfigId = tenant.Id.ToString(),
+            DbType = tenant.DbType,
             ConnectionString = tenant.Connection,
-            IsAutoCloseConnection = true,
+            EnableInitDb = true,
+            EnableDiffLog = false,
+            EnableUnderLine = defautConfig.EnableUnderLine,
         };
         SqlSugarSetup.InitTenantDatabase(App.GetRequiredService<ISqlSugarClient>().AsTenant(), config);
     }