| | | 1 | | using OzonDomains.Models.BitrixModels; |
| | | 2 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.Bitrix; |
| | | 3 | | |
| | | 4 | | namespace Services.BitrixStockServices; |
| | | 5 | | |
| | | 6 | | /// <summary>Правила фильтрации как в BitrixStockController.Index (без доп. пресетов задач).</summary> |
| | | 7 | | public sealed class BitrixStockIndexFilterContext |
| | | 8 | | { |
| | | 9 | | public bool HasLoadOzonWarehousesInQuery { get; init; } |
| | | 10 | | public bool HasOnlyInactiveInQuery { get; init; } |
| | | 11 | | public bool HasOnlyActiveInQuery { get; init; } |
| | | 12 | | public bool HasSelectedStoreTitlesInQuery { get; init; } |
| | | 13 | | public bool StoresFilterApplied { get; init; } |
| | | 14 | | public bool IsBarePageLoad { get; init; } |
| | | 15 | | public IReadOnlyCollection<string>? ActiveStores { get; init; } |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public static class BitrixStockIndexFilterRules |
| | | 19 | | { |
| | | 20 | | public static void ApplyLikeStocksPage(RemainingStockFilter filter, BitrixStockIndexFilterContext ctx) |
| | | 21 | | { |
| | 43 | 22 | | var hasAnyBusinessFilter = |
| | 43 | 23 | | filter.ProductId.HasValue || |
| | 43 | 24 | | !string.IsNullOrWhiteSpace(filter.Article) || |
| | 43 | 25 | | !string.IsNullOrWhiteSpace(filter.PreviewText) || |
| | 43 | 26 | | !string.IsNullOrWhiteSpace(filter.Active) || |
| | 43 | 27 | | !string.IsNullOrWhiteSpace(filter.Available) || |
| | 43 | 28 | | !string.IsNullOrWhiteSpace(filter.Supplier) || |
| | 43 | 29 | | filter.QuantityFrom.HasValue || |
| | 43 | 30 | | filter.QuantityTo.HasValue || |
| | 43 | 31 | | filter.DateFrom.HasValue || |
| | 43 | 32 | | filter.DateTo.HasValue || |
| | 43 | 33 | | filter.PriceValueFrom.HasValue || |
| | 43 | 34 | | filter.PriceValueTo.HasValue || |
| | 43 | 35 | | filter.RegistrationPriceFrom.HasValue || |
| | 43 | 36 | | filter.RegistrationPriceTo.HasValue || |
| | 43 | 37 | | filter.StoreId.HasValue || |
| | 43 | 38 | | !string.IsNullOrWhiteSpace(filter.StoreTitle) || |
| | 43 | 39 | | (filter.SelectedStoreTitles?.Any() == true) || |
| | 43 | 40 | | filter.AmountFrom.HasValue || |
| | 43 | 41 | | filter.AmountTo.HasValue || |
| | 43 | 42 | | filter.SelectedClientId.HasValue || |
| | 43 | 43 | | !string.IsNullOrWhiteSpace(filter.OzonStoreTitle) || |
| | 43 | 44 | | filter.OzonAmountFrom.HasValue || |
| | 43 | 45 | | filter.OzonAmountTo.HasValue || |
| | 43 | 46 | | filter.PriceIndexValueFrom.HasValue || |
| | 43 | 47 | | filter.PriceIndexValueTo.HasValue || |
| | 43 | 48 | | filter.MinPriceFrom.HasValue || |
| | 43 | 49 | | filter.MinPriceTo.HasValue || |
| | 43 | 50 | | filter.LoadOzonWarehouses || |
| | 43 | 51 | | filter.LoadOzonPriceIndexes || |
| | 43 | 52 | | filter.DiffOzonWarehouses || |
| | 43 | 53 | | filter.RedOzonPriceIndexes || |
| | 43 | 54 | | filter.OnlyActive || |
| | 43 | 55 | | filter.OnlyInactive; |
| | | 56 | | |
| | 43 | 57 | | filter.DisablePagination = hasAnyBusinessFilter; |
| | 43 | 58 | | if (!filter.DisablePagination && filter.PageSize <= 0) |
| | | 59 | | { |
| | 1 | 60 | | filter.PageSize = 100; |
| | | 61 | | } |
| | | 62 | | |
| | 43 | 63 | | if (filter.OnlyInactive) |
| | | 64 | | { |
| | 3 | 65 | | if (!ctx.HasLoadOzonWarehousesInQuery) |
| | | 66 | | { |
| | 2 | 67 | | filter.LoadOzonWarehouses = true; |
| | | 68 | | } |
| | | 69 | | |
| | 3 | 70 | | filter.Active = "N"; |
| | 3 | 71 | | filter.OzonAmountFrom = 1; |
| | | 72 | | } |
| | | 73 | | |
| | 43 | 74 | | if (filter.OnlyActive) |
| | | 75 | | { |
| | 3 | 76 | | if (!ctx.HasLoadOzonWarehousesInQuery) |
| | | 77 | | { |
| | 2 | 78 | | filter.LoadOzonWarehouses = true; |
| | | 79 | | } |
| | | 80 | | |
| | 3 | 81 | | filter.Active = "Y"; |
| | | 82 | | } |
| | | 83 | | |
| | 43 | 84 | | if (!ctx.HasSelectedStoreTitlesInQuery) |
| | | 85 | | { |
| | 42 | 86 | | if (ctx.StoresFilterApplied) |
| | | 87 | | { |
| | 1 | 88 | | filter.SelectedStoreTitles = new List<string>(); |
| | | 89 | | } |
| | 41 | 90 | | else if (!string.IsNullOrWhiteSpace(filter.StoreTitle)) |
| | | 91 | | { |
| | 2 | 92 | | filter.SelectedStoreTitles = new List<string> { filter.StoreTitle }; |
| | | 93 | | } |
| | 39 | 94 | | else if (ctx.IsBarePageLoad && ctx.ActiveStores != null) |
| | | 95 | | { |
| | 1 | 96 | | filter.SelectedStoreTitles = BitrixStockFilterNormalizer.DefaultStoreTitlesFor(ctx.ActiveStores).ToList( |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | 43 | 100 | | if (filter.Page <= 0) |
| | | 101 | | { |
| | 34 | 102 | | filter.Page = 1; |
| | | 103 | | } |
| | | 104 | | |
| | 43 | 105 | | if (filter.DisablePagination && filter.PageSize <= 0) |
| | | 106 | | { |
| | 33 | 107 | | filter.PageSize = 10_000; |
| | | 108 | | } |
| | 43 | 109 | | } |
| | | 110 | | |
| | | 111 | | public static void ApplyOzonMemoryFilters(List<RemainingStockBitrix> items, RemainingStockFilter filter) |
| | | 112 | | { |
| | 4 | 113 | | if (!filter.LoadOzonWarehouses) |
| | | 114 | | { |
| | 1 | 115 | | return; |
| | | 116 | | } |
| | | 117 | | |
| | 3 | 118 | | if (!string.IsNullOrEmpty(filter.OzonStoreTitle)) |
| | | 119 | | { |
| | 4 | 120 | | items.RemoveAll(x => !x.OzonStores.Any(s => |
| | 6 | 121 | | s.Title.Contains(filter.OzonStoreTitle, StringComparison.OrdinalIgnoreCase))); |
| | | 122 | | } |
| | | 123 | | |
| | 3 | 124 | | if (filter.OzonAmountFrom.HasValue) |
| | | 125 | | { |
| | 7 | 126 | | items.RemoveAll(x => !x.OzonStores.Any(s => s.Amount >= filter.OzonAmountFrom.Value)); |
| | | 127 | | } |
| | | 128 | | |
| | 3 | 129 | | if (filter.OzonAmountTo.HasValue) |
| | | 130 | | { |
| | 5 | 131 | | items.RemoveAll(x => !x.OzonStores.Any(s => s.Amount <= filter.OzonAmountTo.Value)); |
| | | 132 | | } |
| | | 133 | | |
| | 3 | 134 | | if (filter.DiffOzonWarehouses) |
| | | 135 | | { |
| | 5 | 136 | | items.RemoveAll(x => !x.OzonStores.Any(oz => |
| | 10 | 137 | | !(x.Stores?.Any(s => s.Amount == oz.Amount) ?? false))); |
| | | 138 | | } |
| | 3 | 139 | | } |
| | | 140 | | } |