| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | |
| | | 4 | | namespace OzonOrdersWeb.WebServices.Orders.Grid; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Фильтр AG Grid по вычисляемому столбцу: выражение формулы (<see cref="OrdersGridFormulaTranslator"/>) плюс |
| | | 8 | | /// операция и значение из filterModel → <c>Where</c>. Семантика операций та же, что в <see cref="OrdersGridExtraFilters |
| | | 9 | | /// </summary> |
| | | 10 | | public static class OrdersGridComputedFilters |
| | | 11 | | { |
| | | 12 | | /// <summary>Фильтр по колонке: тип операции и значения как пришли из грида (уже разобранные маппером).</summary> |
| | 24 | 13 | | public sealed record ComputedFilter(string ColId, string Type, string? Text, decimal? Number, decimal? NumberTo, Dat |
| | | 14 | | |
| | 1 | 15 | | private static readonly Type Dec = typeof(decimal?); |
| | 1 | 16 | | private static readonly Type Str = typeof(string); |
| | 1 | 17 | | private static readonly Type Dt = typeof(DateTime?); |
| | 1 | 18 | | private static readonly Type Bl = typeof(bool?); |
| | | 19 | | |
| | | 20 | | public static IQueryable<Order> Apply(IQueryable<Order> query, IEnumerable<(OrdersGridFormulaTranslator.Translation |
| | | 21 | | { |
| | 24 | 22 | | foreach (var (t, f) in filters) |
| | | 23 | | { |
| | 6 | 24 | | var body = Predicate(t.Lambda.Body, f); |
| | 6 | 25 | | if (body is null) continue; |
| | 6 | 26 | | query = query.Where(Expression.Lambda<Func<Order, bool>>(body, t.Lambda.Parameters)); |
| | | 27 | | } |
| | | 28 | | |
| | 6 | 29 | | return query; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Тело предиката (bool) над телом выражения формулы; null — операция не поддерживается.</summary> |
| | | 33 | | public static Expression? Predicate(Expression value, ComputedFilter f) |
| | | 34 | | { |
| | 6 | 35 | | var type = f.Type.ToLowerInvariant(); |
| | 6 | 36 | | var nullConst = Expression.Constant(null, value.Type); |
| | 6 | 37 | | if (type == "blank") |
| | | 38 | | { |
| | 1 | 39 | | return value.Type == Str ? Expression.OrElse(Expression.Equal(value, nullConst), Expression.Equal(value, Exp |
| | | 40 | | } |
| | 5 | 41 | | if (type == "notblank") |
| | | 42 | | { |
| | 0 | 43 | | return value.Type == Str ? Expression.AndAlso(Expression.NotEqual(value, nullConst), Expression.NotEqual(val |
| | | 44 | | } |
| | | 45 | | |
| | 5 | 46 | | if (value.Type == Dec) |
| | | 47 | | { |
| | 2 | 48 | | if (f.Number is null) return null; |
| | 2 | 49 | | var n = Expression.Constant(f.Number, Dec); |
| | 2 | 50 | | var notNull = Expression.NotEqual(value, nullConst); |
| | 2 | 51 | | return type switch |
| | 2 | 52 | | { |
| | 0 | 53 | | "equals" => Expression.Equal(value, n), |
| | 0 | 54 | | "notequal" => Expression.OrElse(Expression.Equal(value, nullConst), Expression.NotEqual(value, n)), |
| | 1 | 55 | | "greaterthan" => Expression.AndAlso(notNull, Expression.GreaterThan(value, n)), |
| | 0 | 56 | | "greaterthanorequal" => Expression.AndAlso(notNull, Expression.GreaterThanOrEqual(value, n)), |
| | 0 | 57 | | "lessthan" => Expression.AndAlso(notNull, Expression.LessThan(value, n)), |
| | 0 | 58 | | "lessthanorequal" => Expression.AndAlso(notNull, Expression.LessThanOrEqual(value, n)), |
| | 1 | 59 | | "inrange" => f.NumberTo is null ? null |
| | 1 | 60 | | : Expression.AndAlso(notNull, Expression.AndAlso(Expression.GreaterThanOrEqual(value, n), Expression |
| | 0 | 61 | | _ => null |
| | 2 | 62 | | }; |
| | | 63 | | } |
| | | 64 | | |
| | 3 | 65 | | if (value.Type == Str) |
| | | 66 | | { |
| | 1 | 67 | | var text = f.Text ?? (f.Values.Count > 0 ? f.Values[0] : null); |
| | 1 | 68 | | if (string.IsNullOrEmpty(text)) return null; |
| | 1 | 69 | | var s = Expression.Constant(text, Str); |
| | 1 | 70 | | var notNull = Expression.NotEqual(value, nullConst); |
| | 1 | 71 | | Expression Call(string method) => Expression.AndAlso(notNull, Expression.Call(value, typeof(string).GetMetho |
| | 1 | 72 | | return type switch |
| | 1 | 73 | | { |
| | 1 | 74 | | "contains" => Call(nameof(string.Contains)), |
| | 0 | 75 | | "notcontains" => Expression.OrElse(Expression.Equal(value, nullConst), Expression.Not(Expression.Call(va |
| | 0 | 76 | | "equals" => Expression.Equal(value, s), |
| | 0 | 77 | | "notequal" => Expression.OrElse(Expression.Equal(value, nullConst), Expression.NotEqual(value, s)), |
| | 0 | 78 | | "startswith" => Call(nameof(string.StartsWith)), |
| | 0 | 79 | | "endswith" => Call(nameof(string.EndsWith)), |
| | 0 | 80 | | _ => null |
| | 1 | 81 | | }; |
| | | 82 | | } |
| | | 83 | | |
| | 2 | 84 | | if (value.Type == Dt) |
| | | 85 | | { |
| | 1 | 86 | | if (f.DateFrom is null) return null; |
| | 1 | 87 | | var from = Expression.Constant((DateTime?)f.DateFrom.Value.Date, Dt); |
| | 1 | 88 | | var nextDay = Expression.Constant((DateTime?)f.DateFrom.Value.Date.AddDays(1), Dt); |
| | 1 | 89 | | var notNull = Expression.NotEqual(value, nullConst); |
| | 1 | 90 | | return type switch |
| | 1 | 91 | | { |
| | 0 | 92 | | "equals" => Expression.AndAlso(notNull, Expression.AndAlso(Expression.GreaterThanOrEqual(value, from), E |
| | 0 | 93 | | "notequal" => Expression.OrElse(Expression.Equal(value, nullConst), Expression.OrElse(Expression.LessTha |
| | 1 | 94 | | "greaterthan" => Expression.AndAlso(notNull, Expression.GreaterThanOrEqual(value, nextDay)), |
| | 0 | 95 | | "lessthan" => Expression.AndAlso(notNull, Expression.LessThan(value, from)), |
| | 0 | 96 | | "inrange" => f.DateTo is null ? null |
| | 0 | 97 | | : Expression.AndAlso(notNull, Expression.AndAlso(Expression.GreaterThanOrEqual(value, from), Express |
| | 0 | 98 | | _ => null |
| | 1 | 99 | | }; |
| | | 100 | | } |
| | | 101 | | |
| | 1 | 102 | | if (value.Type == Bl) |
| | | 103 | | { |
| | 1 | 104 | | var raw = f.Values.Count > 0 ? f.Values[0] : f.Text; |
| | 1 | 105 | | if (raw is null) return null; |
| | 1 | 106 | | var want = raw.Equals("true", StringComparison.OrdinalIgnoreCase) || raw == "1" || raw.Equals("да", StringCo |
| | 1 | 107 | | return want ? Expression.Equal(value, Expression.Constant((bool?)true, Bl)) : Expression.NotEqual(value, Exp |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | return null; |
| | | 111 | | } |
| | | 112 | | } |