| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models.BitrixModels; |
| | | 3 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.Bitrix; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace Services.BitrixStockServices; |
| | | 7 | | |
| | | 8 | | public interface IWarehouseStockBitrixViewService |
| | | 9 | | { |
| | | 10 | | Task<List<string>> GetActiveStoresAsync(); |
| | | 11 | | Task<PagedResult<RemainingStockBitrix>> GetRemainingStockAsync(RemainingStockFilter filter); |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | public class WarehouseStockBitrixViewService : IWarehouseStockBitrixViewService |
| | | 15 | | { |
| | 1 | 16 | | private static readonly IReadOnlyList<(string Code, int Id)> StoreMap = new List<(string Code, int Id)> |
| | 1 | 17 | | { |
| | 1 | 18 | | ("B1", 1), |
| | 1 | 19 | | ("B2", 2), |
| | 1 | 20 | | ("M1", 5), |
| | 1 | 21 | | ("M2", 3), |
| | 1 | 22 | | ("M3", 4) |
| | 1 | 23 | | }; |
| | | 24 | | |
| | | 25 | | private readonly ApplicationDbContext _context; |
| | | 26 | | |
| | 1 | 27 | | public WarehouseStockBitrixViewService(ApplicationDbContext context) |
| | | 28 | | { |
| | 1 | 29 | | _context = context; |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | public Task<List<string>> GetActiveStoresAsync() |
| | | 33 | | { |
| | 0 | 34 | | return Task.FromResult(StoreMap.Select(x => x.Code).ToList()); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<PagedResult<RemainingStockBitrix>> GetRemainingStockAsync(RemainingStockFilter filter) |
| | | 38 | | { |
| | 1 | 39 | | var baseQuery = _context.WarehouseStockSnapshots.AsNoTracking().AsQueryable(); |
| | | 40 | | |
| | 1 | 41 | | if (!string.IsNullOrWhiteSpace(filter.Article)) |
| | | 42 | | { |
| | 0 | 43 | | var article = filter.Article.Trim(); |
| | 0 | 44 | | baseQuery = baseQuery.Where(x => x.Article != null && x.Article.Contains(article)); |
| | | 45 | | } |
| | | 46 | | |
| | 1 | 47 | | if (!string.IsNullOrWhiteSpace(filter.PreviewText)) |
| | | 48 | | { |
| | 0 | 49 | | var preview = filter.PreviewText.Trim(); |
| | 0 | 50 | | baseQuery = baseQuery.Where(x => x.ProductName != null && x.ProductName.Contains(preview)); |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | if (!string.IsNullOrWhiteSpace(filter.Supplier)) |
| | | 54 | | { |
| | 0 | 55 | | var supplier = filter.Supplier.Trim(); |
| | 0 | 56 | | baseQuery = baseQuery.Where(x => x.Manufacturer != null && x.Manufacturer.Contains(supplier)); |
| | | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | if (filter.DateFrom.HasValue) |
| | 0 | 60 | | baseQuery = baseQuery.Where(x => x.SyncedAtUtc >= filter.DateFrom.Value); |
| | 1 | 61 | | if (filter.DateTo.HasValue) |
| | 0 | 62 | | baseQuery = baseQuery.Where(x => x.SyncedAtUtc < filter.DateTo.Value.Date.AddDays(1)); |
| | | 63 | | |
| | 1 | 64 | | var groupedQuery = baseQuery |
| | 1 | 65 | | .Where(x => x.Article != null && x.Article != string.Empty) |
| | 1 | 66 | | .GroupBy(x => x.Article!) |
| | 1 | 67 | | .Select(g => new SnapshotAggregateRow |
| | 1 | 68 | | { |
| | 1 | 69 | | Article = g.Key, |
| | 1 | 70 | | ProductId = g.Max(x => x.SourceRowNumber), |
| | 1 | 71 | | PreviewText = g.Max(x => x.ProductName), |
| | 1 | 72 | | Supplier = g.Max(x => x.Manufacturer), |
| | 1 | 73 | | TimestampX = g.Max(x => x.SyncedAtUtc), |
| | 1 | 74 | | M1Qty = g.Sum(x => x.M1Qty ?? 0m), |
| | 1 | 75 | | M2Qty = g.Sum(x => x.M2Qty ?? 0m), |
| | 1 | 76 | | M3Qty = g.Sum(x => x.M3Qty ?? 0m), |
| | 1 | 77 | | B1Qty = g.Sum(x => x.B1Qty ?? 0m), |
| | 1 | 78 | | B2Qty = g.Sum(x => x.B2Qty ?? 0m), |
| | 1 | 79 | | Price = g.Average(x => x.AccountingPriceUnit ?? 0m), |
| | 1 | 80 | | UnitCost = g.Average(x => x.UnitCost ?? 0m), |
| | 1 | 81 | | C0 = g.Max(x => x.C0) |
| | 1 | 82 | | }); |
| | | 83 | | |
| | 1 | 84 | | if (filter.ProductId.HasValue) |
| | 0 | 85 | | groupedQuery = groupedQuery.Where(x => x.ProductId == filter.ProductId.Value); |
| | | 86 | | |
| | 1 | 87 | | if (filter.QuantityFrom.HasValue) |
| | 0 | 88 | | groupedQuery = groupedQuery.Where(x => x.M1Qty + x.M2Qty + x.M3Qty + x.B1Qty + x.B2Qty >= filter.QuantityFro |
| | 1 | 89 | | if (filter.QuantityTo.HasValue) |
| | 0 | 90 | | groupedQuery = groupedQuery.Where(x => x.M1Qty + x.M2Qty + x.M3Qty + x.B1Qty + x.B2Qty <= filter.QuantityTo. |
| | | 91 | | |
| | 1 | 92 | | if (filter.PriceValueFrom.HasValue) |
| | 0 | 93 | | groupedQuery = groupedQuery.Where(x => x.Price >= filter.PriceValueFrom.Value); |
| | 1 | 94 | | if (filter.PriceValueTo.HasValue) |
| | 0 | 95 | | groupedQuery = groupedQuery.Where(x => x.Price <= filter.PriceValueTo.Value); |
| | | 96 | | |
| | 1 | 97 | | if (filter.RegistrationPriceFrom.HasValue) |
| | 0 | 98 | | groupedQuery = groupedQuery.Where(x => x.UnitCost >= filter.RegistrationPriceFrom.Value); |
| | 1 | 99 | | if (filter.RegistrationPriceTo.HasValue) |
| | 0 | 100 | | groupedQuery = groupedQuery.Where(x => x.UnitCost <= filter.RegistrationPriceTo.Value); |
| | | 101 | | |
| | 1 | 102 | | var selectedStores = ResolveSelectedStores(filter); |
| | 1 | 103 | | var hasM1 = selectedStores.Contains("M1", StringComparer.OrdinalIgnoreCase); |
| | 1 | 104 | | var hasM2 = selectedStores.Contains("M2", StringComparer.OrdinalIgnoreCase); |
| | 1 | 105 | | var hasM3 = selectedStores.Contains("M3", StringComparer.OrdinalIgnoreCase); |
| | 1 | 106 | | var hasB1 = selectedStores.Contains("B1", StringComparer.OrdinalIgnoreCase); |
| | 1 | 107 | | var hasB2 = selectedStores.Contains("B2", StringComparer.OrdinalIgnoreCase); |
| | 1 | 108 | | if (filter.AmountFrom.HasValue) |
| | | 109 | | { |
| | 0 | 110 | | var amountFrom = (decimal)filter.AmountFrom.Value; |
| | 0 | 111 | | groupedQuery = groupedQuery.Where(x => |
| | 0 | 112 | | (hasM1 && x.M1Qty >= amountFrom) || |
| | 0 | 113 | | (hasM2 && x.M2Qty >= amountFrom) || |
| | 0 | 114 | | (hasM3 && x.M3Qty >= amountFrom) || |
| | 0 | 115 | | (hasB1 && x.B1Qty >= amountFrom) || |
| | 0 | 116 | | (hasB2 && x.B2Qty >= amountFrom)); |
| | | 117 | | } |
| | 1 | 118 | | if (filter.AmountTo.HasValue) |
| | | 119 | | { |
| | 0 | 120 | | var amountTo = (decimal)filter.AmountTo.Value; |
| | 0 | 121 | | groupedQuery = groupedQuery.Where(x => |
| | 0 | 122 | | (hasM1 && x.M1Qty <= amountTo) || |
| | 0 | 123 | | (hasM2 && x.M2Qty <= amountTo) || |
| | 0 | 124 | | (hasM3 && x.M3Qty <= amountTo) || |
| | 0 | 125 | | (hasB1 && x.B1Qty <= amountTo) || |
| | 0 | 126 | | (hasB2 && x.B2Qty <= amountTo)); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | groupedQuery = ApplySorting(groupedQuery, filter.SortColumn, filter.SortDirection); |
| | | 130 | | |
| | 1 | 131 | | var totalCount = await groupedQuery.CountAsync(); |
| | 1 | 132 | | if (!filter.DisablePagination) |
| | 1 | 133 | | groupedQuery = groupedQuery.Skip((filter.Page - 1) * filter.PageSize).Take(filter.PageSize); |
| | | 134 | | |
| | 1 | 135 | | var rows = await groupedQuery.ToListAsync(); |
| | 1 | 136 | | var items = rows.Select(x => |
| | 1 | 137 | | { |
| | 1 | 138 | | var quantity = (double)(x.M1Qty + x.M2Qty + x.M3Qty + x.B1Qty + x.B2Qty); |
| | 1 | 139 | | return new RemainingStockBitrix |
| | 1 | 140 | | { |
| | 1 | 141 | | ProductId = x.ProductId, |
| | 1 | 142 | | Article = x.Article, |
| | 1 | 143 | | PreviewText = x.PreviewText, |
| | 1 | 144 | | Active = "Y", |
| | 1 | 145 | | Available = quantity > 0 ? "Y" : "N", |
| | 1 | 146 | | Supplier = x.Supplier, |
| | 1 | 147 | | TimestampX = x.TimestampX, |
| | 1 | 148 | | Quantity = quantity, |
| | 1 | 149 | | Price = (double)x.Price, |
| | 1 | 150 | | C0 = x.C0, |
| | 1 | 151 | | PriceHistory = new OzonRepositories.Data.Bitrix.PriceHistory |
| | 1 | 152 | | { |
| | 1 | 153 | | BitrixId = x.ProductId, |
| | 1 | 154 | | Article = x.Article ?? string.Empty, |
| | 1 | 155 | | CreateDateTime = x.TimestampX, |
| | 1 | 156 | | PriceValue = x.UnitCost, |
| | 1 | 157 | | RegistrationPrice = x.UnitCost, |
| | 1 | 158 | | ValueSource = "WarehouseStockSnapshot", |
| | 1 | 159 | | Author = "WarehouseStock" |
| | 1 | 160 | | }, |
| | 1 | 161 | | Stores = BuildStores(x, selectedStores) |
| | 1 | 162 | | }; |
| | 1 | 163 | | }).ToList(); |
| | | 164 | | |
| | 1 | 165 | | return new PagedResult<RemainingStockBitrix> |
| | 1 | 166 | | { |
| | 1 | 167 | | Items = items, |
| | 1 | 168 | | TotalCount = totalCount, |
| | 1 | 169 | | Page = filter.Page, |
| | 1 | 170 | | PageSize = filter.PageSize |
| | 1 | 171 | | }; |
| | 1 | 172 | | } |
| | | 173 | | |
| | | 174 | | private static List<string> ResolveSelectedStores(RemainingStockFilter filter) |
| | | 175 | | { |
| | 1 | 176 | | if (filter.SelectedStoreTitles is { Count: > 0 }) |
| | 3 | 177 | | return filter.SelectedStoreTitles.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToList(); |
| | | 178 | | |
| | 0 | 179 | | if (!string.IsNullOrWhiteSpace(filter.StoreTitle)) |
| | 0 | 180 | | return StoreMap.Where(x => x.Code.Contains(filter.StoreTitle.Trim(), StringComparison.OrdinalIgnoreCase)).Se |
| | | 181 | | |
| | 0 | 182 | | return StoreMap.Select(x => x.Code).ToList(); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | private static List<StockByStore> BuildStores(SnapshotAggregateRow row, IReadOnlyCollection<string> selectedStores) |
| | | 186 | | { |
| | 1 | 187 | | var values = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase) |
| | 1 | 188 | | { |
| | 1 | 189 | | ["M1"] = (double)row.M1Qty, |
| | 1 | 190 | | ["M2"] = (double)row.M2Qty, |
| | 1 | 191 | | ["M3"] = (double)row.M3Qty, |
| | 1 | 192 | | ["B1"] = (double)row.B1Qty, |
| | 1 | 193 | | ["B2"] = (double)row.B2Qty |
| | 1 | 194 | | }; |
| | | 195 | | |
| | 1 | 196 | | return StoreMap |
| | 5 | 197 | | .Where(s => selectedStores.Contains(s.Code, StringComparer.OrdinalIgnoreCase)) |
| | 1 | 198 | | .Select(s => new StockByStore |
| | 1 | 199 | | { |
| | 1 | 200 | | StoreId = s.Id, |
| | 1 | 201 | | Title = s.Code, |
| | 1 | 202 | | Amount = values.GetValueOrDefault(s.Code, 0d) |
| | 1 | 203 | | }) |
| | 1 | 204 | | .ToList(); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | private static IQueryable<SnapshotAggregateRow> ApplySorting( |
| | | 208 | | IQueryable<SnapshotAggregateRow> items, |
| | | 209 | | string? sortColumn, |
| | | 210 | | string? sortDirection) |
| | | 211 | | { |
| | 1 | 212 | | var desc = string.Equals(sortDirection, "desc", StringComparison.OrdinalIgnoreCase); |
| | 1 | 213 | | return (sortColumn ?? string.Empty).ToLowerInvariant() switch |
| | 1 | 214 | | { |
| | 0 | 215 | | "article" => desc ? items.OrderByDescending(x => x.Article) : items.OrderBy(x => x.Article), |
| | 0 | 216 | | "previewtext" => desc ? items.OrderByDescending(x => x.PreviewText) : items.OrderBy(x => x.PreviewText), |
| | 0 | 217 | | "supplier" => desc ? items.OrderByDescending(x => x.Supplier) : items.OrderBy(x => x.Supplier), |
| | 0 | 218 | | "quantity" => desc |
| | 0 | 219 | | ? items.OrderByDescending(x => x.M1Qty + x.M2Qty + x.M3Qty + x.B1Qty + x.B2Qty) |
| | 0 | 220 | | : items.OrderBy(x => x.M1Qty + x.M2Qty + x.M3Qty + x.B1Qty + x.B2Qty), |
| | 0 | 221 | | "price" => desc ? items.OrderByDescending(x => x.Price) : items.OrderBy(x => x.Price), |
| | 0 | 222 | | "registrationprice" => desc ? items.OrderByDescending(x => x.UnitCost) : items.OrderBy(x => x.UnitCost), |
| | 0 | 223 | | "timestampx" => desc ? items.OrderByDescending(x => x.TimestampX) : items.OrderBy(x => x.TimestampX), |
| | 1 | 224 | | _ => desc ? items.OrderByDescending(x => x.ProductId) : items.OrderBy(x => x.ProductId) |
| | 1 | 225 | | }; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | private sealed class SnapshotAggregateRow |
| | | 229 | | { |
| | 3 | 230 | | public int ProductId { get; set; } |
| | 3 | 231 | | public string? Article { get; set; } |
| | 2 | 232 | | public string? PreviewText { get; set; } |
| | 2 | 233 | | public string? Supplier { get; set; } |
| | 3 | 234 | | public DateTime TimestampX { get; set; } |
| | 3 | 235 | | public decimal M1Qty { get; set; } |
| | 3 | 236 | | public decimal M2Qty { get; set; } |
| | 3 | 237 | | public decimal M3Qty { get; set; } |
| | 3 | 238 | | public decimal B1Qty { get; set; } |
| | 3 | 239 | | public decimal B2Qty { get; set; } |
| | 2 | 240 | | public decimal Price { get; set; } |
| | 3 | 241 | | public decimal UnitCost { get; set; } |
| | 2 | 242 | | public decimal? C0 { get; set; } |
| | | 243 | | } |
| | | 244 | | } |