using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace FreeSql.Internal { public class GlobalFilter { ConcurrentDictionary _filters = new ConcurrentDictionary(StringComparer.CurrentCultureIgnoreCase); int _id = 0; public class Item { public int Id { get; internal set; } public string Name { get; internal set; } internal Func Condition { get; set; } public LambdaExpression Where { get; internal set; } public bool Only { get; internal set; } } /// /// 创建一个过滤器 /// 提示:在 Lambda 中判断登陆身份,请参考资料 AsyncLocal /// /// /// 名字 /// 表达式 /// public GlobalFilter Apply(string name, Expression> where) => Apply(false, name, () => true, where); /// /// 创建一个动态过滤器,当 condition 返回值为 true 时才生效 /// 场景:当登陆身份是管理员,则过滤条件不生效 /// 提示:在 Lambda 中判断登陆身份,请参考资料 AsyncLocal /// /// /// 名字 /// 委托,返回值为 true 时才生效 /// 表达式 /// public GlobalFilter ApplyIf(string name, Func condition, Expression> where) => Apply(false, name, condition, where); /// /// 创建一个过滤器(实体类型 属于指定 TEntity 才会生效) /// 提示:在 Lambda 中判断登陆身份,请参考资料 AsyncLocal /// /// /// 名字 /// 表达式 /// public GlobalFilter ApplyOnly(string name, Expression> where) => Apply(true, name, () => true, where); /// /// 创建一个过滤器(实体类型 属于指定 TEntity 才会生效) /// 场景:当登陆身份是管理员,则过滤条件不生效 /// 提示:在 Lambda 中判断登陆身份,请参考资料 AsyncLocal /// /// /// 名字 /// 委托,返回值为 true 时才生效 /// 表达式 /// public GlobalFilter ApplyOnlyIf(string name, Func condition, Expression> where) => Apply(true, name, condition, where); GlobalFilter Apply(bool only, string name, Func condition, Expression> where) { if (name == null) throw new ArgumentNullException(nameof(name)); if (where == null) return this; _filters.TryGetValue(name, out var item); if (item == null) item = new Item { Id = ++_id, Name = name }; var newParameter = Expression.Parameter(typeof(TEntity), $"gf{_id}"); var newlambda = Expression.Lambda>( new CommonExpression.ReplaceParameterVisitor().Modify(where, newParameter), newParameter ); item.Where = newlambda; item.Condition = condition; item.Only = only; _filters.AddOrUpdate(name, item, (_, __) => item); return this; } public void Remove(string name) => _filters.TryRemove(name ?? throw new ArgumentNullException(nameof(name)), out var _); public List GetFilters() => _filters.Values.Where(a => a.Condition?.Invoke() != false).OrderBy(a => a.Id).ToList(); } }