/// <summary> /// 激活自动ModelFilter,将泛型ModelFilter添加到Filter容器里 /// </summary> public void ActivateAutoModelFilter() { if (!ModelFilterDict.IsNullOrEmptySet()) { var func = ModelFilter(); Add(func, ModelFilterDict); } }
/// <summary> /// 在泛型ModelFilter列表中添加一个字段的过滤规则,最后需要调用AddModelFilterIntoContainer将其添加到Filter容器里,不然不会生效 /// </summary> /// <param name="modelFieldName">Model对应的字段名,使用namof(Model.字段名)</param> /// <param name="compareValue">传入与Model字段需要比较的值</param> /// <param name="fieldFilterMode">指定字段过滤模式(模糊以及忽略大小写仅对String有效),其他类型字段默认为精确匹配,string字段默认模糊并忽略大小写匹配</param> /// <param name="sortType">指定字段排序规则,默认不排序</param> public void AddAutoModelFilter(string modelFieldName, object compareValue, FilterType fieldFilterMode = FilterType.Default, SortType sortType = SortType.None) { if (compareValue == null && !fieldFilterMode.HasFlag(FilterType.SupportNull)) { return; } switch (sortType) { case SortType.Ascending: ModelSortRuleDict.Add(modelFieldName, new KeyValuePair <int, bool>(1, false)); break; case SortType.Descending: ModelSortRuleDict.Add(modelFieldName, new KeyValuePair <int, bool>(1, true)); break; case SortType.StringAuto: if (compareValue is string sortStr) { Regex regex = new Regex(@"[,;\\]?\d*(asc|dsc|升序|降序|desc)[,;\\]?", RegexOptions.IgnoreCase); if (regex.IsMatch(sortStr)) { string sortRule = regex.Match(sortStr)?.Value?.ToLower(); //转小写 compareValue = regex.Replace(sortStr, "", 1); //这里替换掉以防影响过滤 if (!int.TryParse(sortRule.Match("\\d{1,3}"), out int weight)) { weight = 1; //若未指定,权重默认是1,权重最多3位数 } bool isDescending = sortRule.IsMatch("(dsc|降序|desc)"); //指定是否为降序,否则为升序 ModelSortRuleDict.Add(modelFieldName, new KeyValuePair <int, bool>(weight, isDescending)); if (sortStr.Trim().IsMatch(@"^\d*(asc|dsc|升序|降序|desc)$")) //若该字段仅排序 { if (fieldFilterMode.HasFlag(FilterType.SorterNoFilterNull)) { return; //除非不要添加过滤无效值的过滤器 } else { fieldFilterMode |= FilterType.OnlyFilterNull; //默认自动添加一个过滤无效值(null)的过滤器 } } } } break; default: break; } ModelFilterDict.Add(modelFieldName, new KeyValuePair <object, FilterType>(compareValue, fieldFilterMode)); }