using System; using System.Collections.Generic; using System.Collections.Concurrent; using System.Linq.Expressions; using System.Text; using System.Linq; namespace FreeSql { public interface IDataFilter where TEntity : class { IDataFilter Apply(string filterName, Expression> filterAndValidateExp); /// /// 开启过滤器,若使用 using 则使用完后,恢复为原有状态 /// /// 过滤器名称 /// IDisposable Enable(params string[] filterName); /// /// 开启所有过滤器,若使用 using 则使用完后,恢复为原有状态 /// /// IDisposable EnableAll(); /// /// 禁用过滤器,若使用 using 则使用完后,恢复为原有状态 /// /// /// IDisposable Disable(params string[] filterName); /// /// 禁用所有过滤器,若使用 using 则使用完后,恢复为原有状态 /// /// IDisposable DisableAll(); bool IsEnabled(string filterName); } internal class DataFilter : IDataFilter where TEntity : class { internal class FilterItem { public Expression> Expression { get; set; } Func _expressionDelegate; public Func ExpressionDelegate => _expressionDelegate ?? (_expressionDelegate = Expression?.Compile()); public bool IsEnabled { get; set; } } internal ConcurrentDictionary _filters = new ConcurrentDictionary(StringComparer.CurrentCultureIgnoreCase); public IDataFilter Apply(string filterName, Expression> filterAndValidateExp) { if (filterName == null) throw new ArgumentNullException(nameof(filterName)); if (filterAndValidateExp == null) return this; var filterItem = new FilterItem { Expression = filterAndValidateExp, IsEnabled = true }; _filters.AddOrUpdate(filterName, filterItem, (k, v) => filterItem); return this; } public IDisposable Disable(params string[] filterName) { if (filterName == null || filterName.Any() == false) return new UsingAny(() => { }); List restore = new List(); foreach (var name in filterName) { if (_filters.TryGetValue(name, out var tryfi)) { if (tryfi.IsEnabled) { restore.Add(name); tryfi.IsEnabled = false; } } } return new UsingAny(() => this.Enable(restore.ToArray())); } public IDisposable DisableAll() { List restore = new List(); foreach (var val in _filters) { if (val.Value.IsEnabled) { restore.Add(val.Key); val.Value.IsEnabled = false; } } return new UsingAny(() => this.Enable(restore.ToArray())); } class UsingAny : IDisposable { Action _ondis; public UsingAny(Action ondis) { _ondis = ondis; } public void Dispose() { _ondis?.Invoke(); } } public IDisposable Enable(params string[] filterName) { if (filterName == null || filterName.Any() == false) return new UsingAny(() => { }); List restore = new List(); foreach (var name in filterName) { if (_filters.TryGetValue(name, out var tryfi)) { if (tryfi.IsEnabled == false) { restore.Add(name); tryfi.IsEnabled = true; } } } return new UsingAny(() => this.Disable(restore.ToArray())); } public IDisposable EnableAll() { List restore = new List(); foreach (var val in _filters) { if (val.Value.IsEnabled == false) { restore.Add(val.Key); val.Value.IsEnabled = true; } } return new UsingAny(() => this.Disable(restore.ToArray())); } public bool IsEnabled(string filterName) { if (filterName == null) return false; return _filters.TryGetValue(filterName, out var tryfi) ? tryfi.IsEnabled : false; } } public class GlobalDataFilter { internal List<(Type type, string name, LambdaExpression exp)> _filters = new List<(Type type, string name, LambdaExpression exp)>(); public GlobalDataFilter Apply(string filterName, Expression> filterAndValidateExp) where TEntity : class { if (filterName == null) throw new ArgumentNullException(nameof(filterName)); if (filterAndValidateExp == null) return this; _filters.Add((typeof(TEntity), filterName, filterAndValidateExp)); return this; } } }