diff --git a/Infrastructure/DynamicLinq.cs b/Infrastructure/DynamicLinq.cs
index 8f334dcf..0e0761da 100644
--- a/Infrastructure/DynamicLinq.cs
+++ b/Infrastructure/DynamicLinq.cs
@@ -1,306 +1,324 @@
-// ***********************************************************************
-// Assembly : FairUtility
-// Author : Yubao Li
-// Created : 08-18-2015
-//
-// Last Modified By : Yubao Li
-// Last Modified On : 08-18-2015
-// ***********************************************************************
-//
-// Copyright (c) . All rights reserved.
-//
-// 动态linq
-// ***********************************************************************
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Linq.Expressions;
-using System.Reflection;
-
-namespace Infrastructure
-{
- public static class DynamicLinq
- {
- public static ParameterExpression CreateLambdaParam(string name)
- {
- return Expression.Parameter(typeof(T), name);
- }
-
- ///
- /// 创建linq表达示的body部分
- ///
- public static Expression GenerateBody(this ParameterExpression param, Filter filterObj)
- {
- PropertyInfo property = typeof(T).GetProperty(filterObj.Key);
-
- Expression left = null; //组装左边
- //组装右边
- Expression right = null;
-
- if (property != null)
- {
- left = Expression.Property(param, property);
- if (property.PropertyType == typeof(int))
- {
- right = Expression.Constant(int.Parse(filterObj.Value));
- }
- else if (property.PropertyType == typeof(DateTime))
- {
- right = Expression.Constant(DateTime.Parse(filterObj.Value));
- }
- else if (property.PropertyType == typeof(string))
- {
- right = Expression.Constant(filterObj.Value);
- }
- else if (property.PropertyType == typeof(decimal))
- {
- right = Expression.Constant(decimal.Parse(filterObj.Value));
- }
- else if (property.PropertyType == typeof(Guid))
- {
- right = Expression.Constant(Guid.Parse(filterObj.Value));
- }
- else if (property.PropertyType == typeof(bool))
- {
- right = Expression.Constant(filterObj.Value.Equals("1"));
- }
- else if (property.PropertyType == typeof(Guid?))
- {
- left = Expression.Property(left, "Value");
- right = Expression.Constant(Guid.Parse(filterObj.Value));
- }
- else
- {
- throw new Exception("暂不能解析该Key的类型");
- }
- }
- else //如果左边不是属性,直接是值的情况
- {
- left = Expression.Constant(filterObj.Key);
- right = Expression.Constant(filterObj.Value);
- }
-
- //c.XXX=="XXX"
- Expression filter = Expression.Equal(left, right);
- switch (filterObj.Contrast)
- {
- case "<=":
- filter = Expression.LessThanOrEqual(left, right);
- break;
-
- case "<":
- filter = Expression.LessThan(left, right);
- break;
-
- case ">":
- filter = Expression.GreaterThan(left, right);
- break;
-
- case ">=":
- filter = Expression.GreaterThanOrEqual(left, right);
- break;
- case "!=":
- filter = Expression.NotEqual(left, right);
- break;
- case "contains":
- filter = Expression.Call(left, typeof(string).GetMethod("Contains", new Type[] {typeof(string)}),
- Expression.Constant(filterObj.Value));
- break;
- case "in":
- var lExp = Expression.Constant(filterObj.Value.Split(',').ToList()); //数组
- var methodInfo = typeof(List).GetMethod("Contains",
- new Type[] {typeof(string)}); //Contains语句
- filter = Expression.Call(lExp, methodInfo, left);
- break;
- case "not in":
- var listExpression = Expression.Constant(filterObj.Value.Split(',').ToList()); //数组
- var method = typeof(List).GetMethod("Contains", new Type[] {typeof(string)}); //Contains语句
- filter = Expression.Not(Expression.Call(listExpression, method, left));
- break;
- //交集,使用交集时左值必须时固定的值
- case "intersect": //交集
- if (property != null)
- {
- throw new Exception("交集模式下,表达式左边不能为变量,请调整数据规则,如:c=>\"A,B,C\" intersect \"B,D\"");
- }
-
- var rightval = filterObj.Value.Split(',').ToList();
- var leftval = filterObj.Key.Split(',').ToList();
- var val = rightval.Intersect(leftval);
-
- filter = Expression.Constant(val.Count() > 0);
- break;
- }
-
- return filter;
- }
-
- public static Expression> GenerateTypeBody(this ParameterExpression param, Filter filterObj)
- {
- return (Expression>) (param.GenerateBody(filterObj));
- }
-
- ///
- /// 创建完整的lambda
- ///
- public static LambdaExpression GenerateLambda(this ParameterExpression param, Expression body)
- {
- //c=>c.XXX=="XXX"
- return Expression.Lambda(body, param);
- }
-
- public static Expression> GenerateTypeLambda(this ParameterExpression param, Expression body)
- {
- return (Expression>) (param.GenerateLambda(body));
- }
-
- public static Expression AndAlso(this Expression expression, Expression expressionRight)
- {
- return Expression.AndAlso(expression, expressionRight);
- }
-
- public static Expression Or(this Expression expression, Expression expressionRight)
- {
- return Expression.Or(expression, expressionRight);
- }
-
- public static Expression And(this Expression expression, Expression expressionRight)
- {
- return Expression.And(expression, expressionRight);
- }
-
- public static IQueryable GenerateFilter(this IQueryable query, string parametername, string filterjson)
- {
- if (!string.IsNullOrEmpty(filterjson))
- {
- var filterGroup = JsonHelper.Instance.Deserialize(filterjson);
- query = GenerateFilter(query, parametername, filterGroup);
- }
-
- return query;
- }
-
- ///
- /// 转换FilterGroup为Lambda表达式
- ///
- ///
- ///
- ///
- ///
- ///
- public static IQueryable GenerateFilter(this IQueryable query, string parametername,
- FilterGroup filterGroup)
- {
- var param = CreateLambdaParam(parametername);
- Expression result = ConvertGroup(filterGroup, param);
- query = query.Where(param.GenerateTypeLambda(result));
- return query;
- }
-
- ///
- /// 转换filtergroup为表达式
- ///
- ///
- ///
- ///
- ///
- public static Expression ConvertGroup(FilterGroup filterGroup, ParameterExpression param)
- {
- if (filterGroup == null) return null;
-
- if (filterGroup.Filters.Length == 1 &&(filterGroup.Children == null || !filterGroup.Children.Any())) //只有一个条件
- {
- return param.GenerateBody(filterGroup.Filters[0]);
- }
-
- Expression result = ConvertFilters(filterGroup.Filters, param, filterGroup.Operation);
- Expression gresult = ConvertGroup(filterGroup.Children, param, filterGroup.Operation);
- if (gresult == null) return result;
- if (result == null) return gresult;
-
- if (filterGroup.Operation == "and")
- {
- return result.AndAlso(gresult);
- }
- else //or
- {
- return result.Or(gresult);
- }
- }
-
- ///
- /// 转换FilterGroup[]为表达式,不管FilterGroup里面的Filters
- ///
- ///
- ///
- ///
- ///
- ///
- private static Expression ConvertGroup(FilterGroup[] groups, ParameterExpression param, string operation)
- {
- if (groups == null || !groups.Any()) return null;
-
- Expression result = ConvertGroup(groups[0], param);
-
- if (groups.Length == 1) return result;
-
- if (operation == "and")
- {
- foreach (var filter in groups.Skip(1))
- {
- result = result.AndAlso(ConvertGroup(filter, param));
- }
- }
- else
- {
- foreach (var filter in groups.Skip(1))
- {
- result = result.Or(ConvertGroup(filter, param));
- }
- }
-
- return result;
- }
-
- ///
- /// 转换Filter数组为表达式
- ///
- ///
- ///
- ///
- ///
- ///
- private static Expression ConvertFilters(Filter[] filters, ParameterExpression param, string operation)
- {
- if (filters == null || !filters.Any())
- {
- return null;
- }
-
- Expression result = param.GenerateBody(filters[0]);
-
- if (filters.Length == 1)
- {
- return result;
- }
-
- if (operation == "and")
- {
- foreach (var filter in filters.Skip(1))
- {
- result = result.AndAlso(param.GenerateBody(filter));
- }
- }
- else
- {
- foreach (var filter in filters.Skip(1))
- {
- result = result.Or(param.GenerateBody(filter));
- }
- }
-
- return result;
- }
- }
+// ***********************************************************************
+// Assembly : FairUtility
+// Author : Yubao Li
+// Created : 08-18-2015
+//
+// Last Modified By : Yubao Li
+// Last Modified On : 08-18-2015
+// ***********************************************************************
+//
+// Copyright (c) . All rights reserved.
+//
+// 动态linq
+// ***********************************************************************
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using SqlSugar;
+
+namespace Infrastructure
+{
+ public static class DynamicLinq
+ {
+ public static ParameterExpression CreateLambdaParam(string name)
+ {
+ return Expression.Parameter(typeof(T), name);
+ }
+
+ ///
+ /// 创建linq表达示的body部分
+ ///
+ public static Expression GenerateBody(this ParameterExpression param, Filter filterObj)
+ {
+ PropertyInfo property = typeof(T).GetProperty(filterObj.Key);
+
+ Expression left = null; //组装左边
+ //组装右边
+ Expression right = null;
+
+ if (property != null)
+ {
+ left = Expression.Property(param, property);
+ if (property.PropertyType == typeof(int))
+ {
+ right = Expression.Constant(int.Parse(filterObj.Value));
+ }
+ else if (property.PropertyType == typeof(DateTime))
+ {
+ right = Expression.Constant(DateTime.Parse(filterObj.Value));
+ }
+ else if (property.PropertyType == typeof(string))
+ {
+ right = Expression.Constant(filterObj.Value);
+ }
+ else if (property.PropertyType == typeof(decimal))
+ {
+ right = Expression.Constant(decimal.Parse(filterObj.Value));
+ }
+ else if (property.PropertyType == typeof(Guid))
+ {
+ right = Expression.Constant(Guid.Parse(filterObj.Value));
+ }
+ else if (property.PropertyType == typeof(bool))
+ {
+ right = Expression.Constant(filterObj.Value.Equals("1"));
+ }
+ else if (property.PropertyType == typeof(Guid?))
+ {
+ left = Expression.Property(left, "Value");
+ right = Expression.Constant(Guid.Parse(filterObj.Value));
+ }
+ else
+ {
+ throw new Exception("暂不能解析该Key的类型");
+ }
+ }
+ else //如果左边不是属性,直接是值的情况
+ {
+ left = Expression.Constant(filterObj.Key);
+ right = Expression.Constant(filterObj.Value);
+ }
+
+ //c.XXX=="XXX"
+ Expression filter = Expression.Equal(left, right);
+ switch (filterObj.Contrast)
+ {
+ case "<=":
+ filter = Expression.LessThanOrEqual(left, right);
+ break;
+
+ case "<":
+ filter = Expression.LessThan(left, right);
+ break;
+
+ case ">":
+ filter = Expression.GreaterThan(left, right);
+ break;
+
+ case ">=":
+ filter = Expression.GreaterThanOrEqual(left, right);
+ break;
+ case "!=":
+ filter = Expression.NotEqual(left, right);
+ break;
+ case "contains":
+ filter = Expression.Call(left, typeof(string).GetMethod("Contains", new Type[] {typeof(string)}),
+ Expression.Constant(filterObj.Value));
+ break;
+ case "in":
+ var lExp = Expression.Constant(filterObj.Value.Split(',').ToList()); //数组
+ var methodInfo = typeof(List).GetMethod("Contains",
+ new Type[] {typeof(string)}); //Contains语句
+ filter = Expression.Call(lExp, methodInfo, left);
+ break;
+ case "not in":
+ var listExpression = Expression.Constant(filterObj.Value.Split(',').ToList()); //数组
+ var method = typeof(List).GetMethod("Contains", new Type[] {typeof(string)}); //Contains语句
+ filter = Expression.Not(Expression.Call(listExpression, method, left));
+ break;
+ //交集,使用交集时左值必须时固定的值
+ case "intersect": //交集
+ if (property != null)
+ {
+ throw new Exception("交集模式下,表达式左边不能为变量,请调整数据规则,如:c=>\"A,B,C\" intersect \"B,D\"");
+ }
+
+ var rightval = filterObj.Value.Split(',').ToList();
+ var leftval = filterObj.Key.Split(',').ToList();
+ var val = rightval.Intersect(leftval);
+
+ filter = Expression.Constant(val.Count() > 0);
+ break;
+ }
+
+ return filter;
+ }
+
+ public static Expression> GenerateTypeBody(this ParameterExpression param, Filter filterObj)
+ {
+ return (Expression>) (param.GenerateBody(filterObj));
+ }
+
+ ///
+ /// 创建完整的lambda
+ ///
+ public static LambdaExpression GenerateLambda(this ParameterExpression param, Expression body)
+ {
+ //c=>c.XXX=="XXX"
+ return Expression.Lambda(body, param);
+ }
+
+ public static Expression> GenerateTypeLambda(this ParameterExpression param, Expression body)
+ {
+ return (Expression>) (param.GenerateLambda(body));
+ }
+
+ public static Expression AndAlso(this Expression expression, Expression expressionRight)
+ {
+ return Expression.AndAlso(expression, expressionRight);
+ }
+
+ public static Expression Or(this Expression expression, Expression expressionRight)
+ {
+ return Expression.Or(expression, expressionRight);
+ }
+
+ public static Expression And(this Expression expression, Expression expressionRight)
+ {
+ return Expression.And(expression, expressionRight);
+ }
+
+ public static IQueryable GenerateFilter(this IQueryable query, string parametername, string filterjson)
+ {
+ if (!string.IsNullOrEmpty(filterjson))
+ {
+ var filterGroup = JsonHelper.Instance.Deserialize(filterjson);
+ query = GenerateFilter(query, parametername, filterGroup);
+ }
+
+ return query;
+ }
+
+ ///
+ /// 转换FilterGroup为Lambda表达式
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static IQueryable GenerateFilter(this IQueryable query, string parametername,
+ FilterGroup filterGroup)
+ {
+ var param = CreateLambdaParam(parametername);
+ Expression result = ConvertGroup(filterGroup, param);
+ query = query.Where(param.GenerateTypeLambda(result));
+ return query;
+ }
+
+ ///
+ /// 转换FilterGroup为Lambda表达式
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static ISugarQueryable GenerateFilter(this ISugarQueryable query, string parametername,
+ FilterGroup filterGroup)
+ {
+ var param = CreateLambdaParam(parametername);
+ Expression result = ConvertGroup(filterGroup, param);
+ query = query.Where(param.GenerateTypeLambda(result));
+ return query;
+ }
+
+ ///
+ /// 转换filtergroup为表达式
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static Expression ConvertGroup(FilterGroup filterGroup, ParameterExpression param)
+ {
+ if (filterGroup == null) return null;
+
+ if (filterGroup.Filters.Length == 1 &&(filterGroup.Children == null || !filterGroup.Children.Any())) //只有一个条件
+ {
+ return param.GenerateBody(filterGroup.Filters[0]);
+ }
+
+ Expression result = ConvertFilters(filterGroup.Filters, param, filterGroup.Operation);
+ Expression gresult = ConvertGroup(filterGroup.Children, param, filterGroup.Operation);
+ if (gresult == null) return result;
+ if (result == null) return gresult;
+
+ if (filterGroup.Operation == "and")
+ {
+ return result.AndAlso(gresult);
+ }
+ else //or
+ {
+ return result.Or(gresult);
+ }
+ }
+
+ ///
+ /// 转换FilterGroup[]为表达式,不管FilterGroup里面的Filters
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ private static Expression ConvertGroup(FilterGroup[] groups, ParameterExpression param, string operation)
+ {
+ if (groups == null || !groups.Any()) return null;
+
+ Expression result = ConvertGroup(groups[0], param);
+
+ if (groups.Length == 1) return result;
+
+ if (operation == "and")
+ {
+ foreach (var filter in groups.Skip(1))
+ {
+ result = result.AndAlso(ConvertGroup(filter, param));
+ }
+ }
+ else
+ {
+ foreach (var filter in groups.Skip(1))
+ {
+ result = result.Or(ConvertGroup(filter, param));
+ }
+ }
+
+ return result;
+ }
+
+ ///
+ /// 转换Filter数组为表达式
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ private static Expression ConvertFilters(Filter[] filters, ParameterExpression param, string operation)
+ {
+ if (filters == null || !filters.Any())
+ {
+ return null;
+ }
+
+ Expression result = param.GenerateBody(filters[0]);
+
+ if (filters.Length == 1)
+ {
+ return result;
+ }
+
+ if (operation == "and")
+ {
+ foreach (var filter in filters.Skip(1))
+ {
+ result = result.AndAlso(param.GenerateBody(filter));
+ }
+ }
+ else
+ {
+ foreach (var filter in filters.Skip(1))
+ {
+ result = result.Or(param.GenerateBody(filter));
+ }
+ }
+
+ return result;
+ }
+ }
}
\ No newline at end of file
diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj
index c9e93520..0788b5b7 100644
--- a/Infrastructure/Infrastructure.csproj
+++ b/Infrastructure/Infrastructure.csproj
@@ -21,6 +21,7 @@
+
diff --git a/OpenAuth.App/Base/SqlSugarBaseApp.cs b/OpenAuth.App/Base/SqlSugarBaseApp.cs
new file mode 100644
index 00000000..918d288f
--- /dev/null
+++ b/OpenAuth.App/Base/SqlSugarBaseApp.cs
@@ -0,0 +1,99 @@
+using System;
+using System.Linq;
+using Infrastructure;
+using Microsoft.EntityFrameworkCore;
+using OpenAuth.App.Interface;
+using OpenAuth.Repository.Core;
+using OpenAuth.Repository.Domain;
+using OpenAuth.Repository.Interface;
+using SqlSugar;
+
+namespace OpenAuth.App
+{
+ public abstract class SqlSugarBaseApp
+ {
+ protected ISqlSugarClient SugarClient;
+
+ protected IAuth _auth;
+
+ public SqlSugarBaseApp(ISqlSugarClient client, IAuth auth)
+ {
+ SugarClient = client;
+ _auth = auth;
+ }
+
+ ///
+ /// 获取当前登录用户的数据访问权限
+ ///
+ /// linq表达式参数的名称,如u=>u.name中的"u"
+ ///
+ protected ISugarQueryable GetDataPrivilege(string parametername)
+ {
+ var loginUser = _auth.GetCurrentUser();
+ if (loginUser.User.Account == Define.SYSTEM_USERNAME) return SugarClient.Queryable(); //超级管理员特权
+
+ var moduleName = typeof(T).Name;
+ var rule = SugarClient.Queryable().First(u => u.SourceCode == moduleName);
+ if (rule == null) return SugarClient.Queryable(); //没有设置数据规则,那么视为该资源允许被任何主体查看
+ if (rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINUSER) ||
+ rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINROLE)||
+ rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINORG))
+ {
+
+ //即把{loginUser} =='xxxxxxx'换为 loginUser.User.Id =='xxxxxxx',从而把当前登录的用户名与当时设计规则时选定的用户id对比
+ rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINUSER, loginUser.User.Id);
+
+ var roles = loginUser.Roles.Select(u => u.Id).ToList();
+ roles.Sort(); //按字母排序,这样可以进行like操作
+ rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINROLE,
+ string.Join(',',roles));
+
+ var orgs = loginUser.Orgs.Select(u => u.Id).ToList();
+ orgs.Sort();
+ rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINORG,
+ string.Join(',',orgs));
+ }
+ return SugarClient.Queryable().GenerateFilter(parametername,
+ JsonHelper.Instance.Deserialize(rule.PrivilegeRules));
+ }
+
+ ///
+ /// 计算实体更新的层级信息
+ ///
+ /// U必须是一个继承TreeEntity的结构
+ ///
+ public void CaculateCascade(U entity) where U : TreeEntity
+ {
+ if (entity.ParentId == "") entity.ParentId = null;
+ string cascadeId;
+ int currentCascadeId = 1; //当前结点的级联节点最后一位
+ var sameLevels = SugarClient.Queryable().Where(o => o.ParentId == entity.ParentId && o.Id != entity.Id);
+ foreach (var obj in sameLevels.ToList())
+ {
+ int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
+ if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
+ }
+
+ if (!string.IsNullOrEmpty(entity.ParentId))
+ {
+ var parentOrg = SugarClient.Queryable().First(o => o.Id == entity.ParentId);
+ if (parentOrg != null)
+ {
+ cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
+ entity.ParentName = parentOrg.Name;
+ }
+ else
+ {
+ throw new Exception("未能找到该组织的父节点信息");
+ }
+ }
+ else
+ {
+ cascadeId = ".0." + currentCascadeId + ".";
+ entity.ParentName = "根节点";
+ }
+
+ entity.CascadeId = cascadeId;
+ }
+ }
+}
\ No newline at end of file
diff --git a/OpenAuth.Repository/Test/TestBase.cs b/OpenAuth.Repository/Test/TestBase.cs
index c9f89c5d..4fc337cb 100644
--- a/OpenAuth.Repository/Test/TestBase.cs
+++ b/OpenAuth.Repository/Test/TestBase.cs
@@ -15,6 +15,7 @@ using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using OpenAuth.Repository.Interface;
+using SqlSugar;
namespace OpenAuth.Repository.Test
{
@@ -65,6 +66,58 @@ namespace OpenAuth.Repository.Test
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
serviceCollection.AddDbContext();
+
+ var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
+ .ToDictionary(x => x.Key, x => x.Value);
+
+ var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"];
+ Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}");
+
+ serviceCollection.AddScoped(s =>
+ {
+
+ SqlSugarClient sqlSugar;
+ if(dbtypes.ContainsValue(Define.DBTYPE_SQLSERVER))
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.SqlServer,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+ else if(dbtypes.ContainsValue(Define.DBTYPE_MYSQL)) //mysql
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.MySql,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+ else if(dbtypes.ContainsValue(Define.DBTYPE_PostgreSQL)) //PostgreSQL
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.PostgreSQL,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+ else
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.Oracle,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+
+ return sqlSugar;
+ });
+
+
var builder = new ContainerBuilder();
@@ -75,12 +128,6 @@ namespace OpenAuth.Repository.Test
var _container = builder.Build();
_autofacServiceProvider = new AutofacServiceProvider(_container);
-
- var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
- .ToDictionary(x => x.Key, x => x.Value);
-
- Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
-
}
diff --git a/OpenAuth.Repository/Test/TestSugarDynamicLinq.cs b/OpenAuth.Repository/Test/TestSugarDynamicLinq.cs
new file mode 100644
index 00000000..4834d512
--- /dev/null
+++ b/OpenAuth.Repository/Test/TestSugarDynamicLinq.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Linq;
+using Infrastructure;
+using NUnit.Framework;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace OpenAuth.Repository.Test
+{
+ class TestSugarDynamicLinq : TestBase
+ {
+ [Test]
+ public void GenerateFilter()
+ {
+ var dbcontext = _autofacServiceProvider.GetService();
+ var json = @"
+ {
+ ""Operation"": ""and"",
+ ""Filters"": [{
+ ""Key"": ""Name"",
+ ""Contrast"": ""=="",
+ ""Value"": ""admin""
+
+ },
+ {
+ ""Key"": ""Name"",
+ ""Contrast"": ""=="",
+ ""Value"": ""admin""
+ }
+ ],
+ ""Children"": [{
+ ""Operation"": ""or"",
+ ""Filters"": [{
+ ""Key"": ""Name"",
+ ""Contrast"": ""=="",
+ ""Value"": ""admin""
+ },
+ {
+ ""Key"": ""Name"",
+ ""Contrast"": ""=="",
+ ""Value"": ""admin""
+ }
+ ],
+ ""Children"": [
+
+
+ ]
+ }
+
+ ]
+ }
+ ";
+
+ var query = dbcontext.Users.GenerateFilter("c",json);
+ Console.WriteLine(query.Expression.ToString());
+
+ Console.WriteLine(JsonHelper.Instance.Serialize(query.ToList()));
+ }
+
+ [Test]
+ public void TestDynamic()
+ {
+ FilterGroup sub = new FilterGroup
+ {
+ Operation = "or"
+ };
+ sub.Filters = new[]
+ {
+ new Filter {Key = "Name", Value = "name", Contrast = "=="},
+ new Filter {Key = "Sex", Value = "10", Contrast = "=="}
+ };
+
+ FilterGroup filterGroup = new FilterGroup
+ {
+ Operation = "and"
+ };
+ filterGroup.Filters = new[]
+ {
+ new Filter {Key = "Account", Value = "name", Contrast = "=="},
+ new Filter {Key = "Password", Value = "10", Contrast = "=="}
+ };
+
+ filterGroup.Children = new[]
+ {
+ sub
+ };
+
+ var dbcontext = _autofacServiceProvider.GetService();
+
+ var query = dbcontext.Users.GenerateFilter("c",JsonHelper.Instance.Serialize(filterGroup));
+ Console.WriteLine(query.Expression.ToString());
+ }
+ }
+}
\ No newline at end of file
diff --git a/OpenAuth.WebApi/Startup.cs b/OpenAuth.WebApi/Startup.cs
index 45457afe..081b0a34 100644
--- a/OpenAuth.WebApi/Startup.cs
+++ b/OpenAuth.WebApi/Startup.cs
@@ -22,6 +22,7 @@ using OpenAuth.App;
using OpenAuth.App.HostedService;
using OpenAuth.Repository;
using OpenAuth.WebApi.Model;
+using SqlSugar;
using Swashbuckle.AspNetCore.SwaggerUI;
namespace OpenAuth.WebApi
@@ -172,6 +173,50 @@ namespace OpenAuth.WebApi
services.AddHttpClient();
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Configuration["DataProtection"]));
+
+ services.AddScoped(s =>
+ {
+
+ SqlSugarClient sqlSugar;
+ if(dbtypes.ContainsValue(Define.DBTYPE_SQLSERVER))
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.SqlServer,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+ else if(dbtypes.ContainsValue(Define.DBTYPE_MYSQL)) //mysql
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.MySql,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+ else if(dbtypes.ContainsValue(Define.DBTYPE_PostgreSQL)) //PostgreSQL
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.PostgreSQL,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+ else
+ {
+ sqlSugar = new SqlSugarClient (new ConnectionConfig()
+ {
+ DbType = SqlSugar.DbType.Oracle,
+ ConnectionString = connectionString,
+ IsAutoCloseConnection = true,
+ });
+ }
+
+ return sqlSugar;
+ });
//设置定时启动的任务
@@ -232,7 +277,7 @@ namespace OpenAuth.WebApi
app.UseSwaggerUI(c =>
{
c.IndexStream = () =>
- GetType().GetTypeInfo().Assembly.GetManifestResourceStream("OpenAuth.WebApi.index.html");
+ IntrospectionExtensions.GetTypeInfo(GetType()).Assembly.GetManifestResourceStream("OpenAuth.WebApi.index.html");
foreach (var controller in GetControllers())
{