| | | 1 | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | 2 | | using OzonDomains; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonDomains.Models.AccessControl; |
| | | 5 | | using Services.Authorization.Services; |
| | | 6 | | using Services.DataServices; |
| | | 7 | | using Services.SupplierStatusServices; |
| | | 8 | | |
| | | 9 | | namespace OzonOrdersWeb.WebServices.Orders; |
| | | 10 | | |
| | | 11 | | public sealed class OrderFilterListsService : IOrderFilterListsService |
| | | 12 | | { |
| | | 13 | | private readonly OrderDictionaryCache _dictionaryCache; |
| | | 14 | | private readonly AppStatusDataServices _appStatusServices; |
| | | 15 | | private readonly ClientStatusDataServices _clientStatusServices; |
| | | 16 | | private readonly SupplierDataServices _supplierDataServices; |
| | | 17 | | private readonly WarehouseDataServices _warehouseDataServices; |
| | | 18 | | private readonly OzonClientServices _ozonClientServices; |
| | | 19 | | private readonly DeliveryDataServices _deliveryDataServices; |
| | | 20 | | private readonly IScopeFilterValuesService _scopeFilterValuesService; |
| | | 21 | | private readonly ISupplierStatusDirectoryService _supplierStatusDirectoryService; |
| | | 22 | | |
| | 0 | 23 | | public OrderFilterListsService( |
| | 0 | 24 | | OrderDictionaryCache dictionaryCache, |
| | 0 | 25 | | AppStatusDataServices appStatusServices, |
| | 0 | 26 | | ClientStatusDataServices clientStatusServices, |
| | 0 | 27 | | SupplierDataServices supplierDataServices, |
| | 0 | 28 | | WarehouseDataServices warehouseDataServices, |
| | 0 | 29 | | OzonClientServices ozonClientServices, |
| | 0 | 30 | | DeliveryDataServices deliveryDataServices, |
| | 0 | 31 | | IScopeFilterValuesService scopeFilterValuesService, |
| | 0 | 32 | | ISupplierStatusDirectoryService supplierStatusDirectoryService) |
| | | 33 | | { |
| | 0 | 34 | | _dictionaryCache = dictionaryCache; |
| | 0 | 35 | | _appStatusServices = appStatusServices; |
| | 0 | 36 | | _clientStatusServices = clientStatusServices; |
| | 0 | 37 | | _supplierDataServices = supplierDataServices; |
| | 0 | 38 | | _warehouseDataServices = warehouseDataServices; |
| | 0 | 39 | | _ozonClientServices = ozonClientServices; |
| | 0 | 40 | | _deliveryDataServices = deliveryDataServices; |
| | 0 | 41 | | _scopeFilterValuesService = scopeFilterValuesService; |
| | 0 | 42 | | _supplierStatusDirectoryService = supplierStatusDirectoryService; |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<OrderFilterListsViewData> BuildAsync(OrderFilterListsBuildRequest request, CancellationToken ct) |
| | | 46 | | { |
| | 0 | 47 | | var data = new OrderFilterListsViewData(); |
| | | 48 | | |
| | 0 | 49 | | if (!request.UseFilteredData) |
| | | 50 | | { |
| | 0 | 51 | | if (request.AccessProfile != null) |
| | | 52 | | { |
| | 0 | 53 | | await SetFilteredListsAsync(data, request.AccessProfile, ct); |
| | | 54 | | } |
| | | 55 | | else |
| | | 56 | | { |
| | 0 | 57 | | await SetFilterListsWithoutRestrictionsAsync(data, ct); |
| | | 58 | | } |
| | | 59 | | } |
| | 0 | 60 | | else if (request.AccessProfile == null) |
| | | 61 | | { |
| | 0 | 62 | | await SetFilterListsFromFilteredDataOnlyAsync(data, request.FilteredData ?? [], ct); |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 0 | 66 | | await SetFilteredListsFromDataAsync(data, request.FilteredData ?? [], request.AccessProfile, ct); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | await ApplyDataScopeToFilterListsAsync(data, request.UserId, request.OrderScopeExpression, ct); |
| | | 70 | | |
| | | 71 | | // Справочник статусов поставщиков меняется только при ручной правке админом — держим |
| | | 72 | | // в том же 10-мин memcache, что и остальные словари фильтров, чтобы не делать |
| | | 73 | | // запрос с двумя Include на каждой загрузке страницы заказов. |
| | 0 | 74 | | data.SupplierStatuses = await _dictionaryCache.GetOrCreateAsync( |
| | 0 | 75 | | "dict_supplier_statuses", |
| | 0 | 76 | | () => _supplierStatusDirectoryService.GetAsync(null, null, ct), |
| | 0 | 77 | | ct); |
| | | 78 | | |
| | 0 | 79 | | return data; |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | private async Task SetFilterListsFromFilteredDataOnlyAsync( |
| | | 83 | | OrderFilterListsViewData data, |
| | | 84 | | List<Order> filteredData, |
| | | 85 | | CancellationToken ct) |
| | | 86 | | { |
| | 0 | 87 | | var appStatusNames = filteredData |
| | 0 | 88 | | .Where(o => o.AppStatus != null && !string.IsNullOrEmpty(o.AppStatus.Name)) |
| | 0 | 89 | | .Select(o => o.AppStatus!.Name!) |
| | 0 | 90 | | .Distinct() |
| | 0 | 91 | | .OrderBy(n => n) |
| | 0 | 92 | | .ToList(); |
| | | 93 | | |
| | 0 | 94 | | var hasUnassigned = |
| | 0 | 95 | | filteredData.Any(o => o.AppStatus == null || string.IsNullOrEmpty(o.AppStatus.Name)); |
| | | 96 | | |
| | 0 | 97 | | if (hasUnassigned && !appStatusNames.Contains("Не указан")) |
| | | 98 | | { |
| | 0 | 99 | | appStatusNames.Insert(0, "Не указан"); |
| | | 100 | | } |
| | 0 | 101 | | else if (appStatusNames.Contains("Не указан")) |
| | | 102 | | { |
| | 0 | 103 | | appStatusNames.Remove("Не указан"); |
| | 0 | 104 | | appStatusNames.Insert(0, "Не указан"); |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | var allAppStatuses = |
| | 0 | 108 | | await _dictionaryCache.GetOrCreateAsync("dict_app_statuses_v3", () => _appStatusServices.GetAppStatuses(), c |
| | | 109 | | |
| | 0 | 110 | | var statusColors = allAppStatuses |
| | 0 | 111 | | .Where(s => s.Name != null && appStatusNames.Contains(s.Name)) |
| | 0 | 112 | | .ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | | 113 | | |
| | 0 | 114 | | if (appStatusNames.Contains("Не указан") && !statusColors.ContainsKey("Не указан")) |
| | | 115 | | { |
| | 0 | 116 | | var unassignedStatus = allAppStatuses.FirstOrDefault(s => s.Name == "Не указан"); |
| | | 117 | | |
| | 0 | 118 | | if (unassignedStatus != null) |
| | | 119 | | { |
| | 0 | 120 | | statusColors["Не указан"] = unassignedStatus.GetStatusColor(); |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | data.AppStatuses = new SelectList(appStatusNames.Select(n => new { Name = n }), "Name", "Name"); |
| | 0 | 125 | | data.StatusColors = statusColors; |
| | | 126 | | |
| | 0 | 127 | | var supplierNames = filteredData |
| | 0 | 128 | | .Where(o => o.Supplier != null && !string.IsNullOrEmpty(o.Supplier.Name)) |
| | 0 | 129 | | .Select(o => o.Supplier!.Name!) |
| | 0 | 130 | | .Distinct() |
| | 0 | 131 | | .OrderBy(n => n) |
| | 0 | 132 | | .ToList(); |
| | | 133 | | |
| | 0 | 134 | | data.Suppliers = new SelectList(supplierNames.Select(n => new { Name = n }), "Name", "Name"); |
| | | 135 | | |
| | 0 | 136 | | var clientNames = filteredData |
| | 0 | 137 | | .Where(o => o.OzonClient != null && !string.IsNullOrEmpty(o.OzonClient.Name)) |
| | 0 | 138 | | .Select(o => o.OzonClient!.Name!) |
| | 0 | 139 | | .Distinct() |
| | 0 | 140 | | .OrderBy(n => n) |
| | 0 | 141 | | .ToList(); |
| | | 142 | | |
| | 0 | 143 | | data.Clients = new SelectList(clientNames.Select(n => new { Name = n }), "Name", "Name"); |
| | | 144 | | |
| | 0 | 145 | | var warehouseNames = filteredData |
| | 0 | 146 | | .Where(o => o.ShipmentWarehouse != null && !string.IsNullOrEmpty(o.ShipmentWarehouse.Name)) |
| | 0 | 147 | | .Select(o => o.ShipmentWarehouse!.Name!) |
| | 0 | 148 | | .Distinct() |
| | 0 | 149 | | .OrderBy(n => n) |
| | 0 | 150 | | .ToList(); |
| | | 151 | | |
| | 0 | 152 | | data.Warehouses = new SelectList(warehouseNames.Select(w => new { Name = w }), "Name", "Name"); |
| | | 153 | | |
| | 0 | 154 | | var statusNames = filteredData |
| | 0 | 155 | | .Where(o => o.ClientStatus != null && !string.IsNullOrEmpty(o.ClientStatus.Name)) |
| | 0 | 156 | | .Select(o => o.ClientStatus!.Name!) |
| | 0 | 157 | | .Distinct() |
| | 0 | 158 | | .OrderBy(n => n) |
| | 0 | 159 | | .ToList(); |
| | | 160 | | |
| | 0 | 161 | | data.Statuses = new SelectList(statusNames.Select(n => new { Name = n }), "Name", "Name"); |
| | | 162 | | |
| | 0 | 163 | | var providerNames = filteredData |
| | 0 | 164 | | .Where(o => o.Delivery != null && o.Delivery.Provider != null && |
| | 0 | 165 | | !string.IsNullOrEmpty(o.Delivery.Provider.Name)) |
| | 0 | 166 | | .Select(o => o.Delivery!.Provider!.Name!) |
| | 0 | 167 | | .Distinct() |
| | 0 | 168 | | .OrderBy(n => n) |
| | 0 | 169 | | .ToList(); |
| | | 170 | | |
| | 0 | 171 | | data.Providers = new SelectList(providerNames.Select(n => new { Name = n }), "Name", "Name"); |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | private async Task SetFilteredListsFromDataAsync( |
| | | 175 | | OrderFilterListsViewData data, |
| | | 176 | | List<Order> filteredData, |
| | | 177 | | AccessProfile accessProfile, |
| | | 178 | | CancellationToken ct) |
| | | 179 | | { |
| | 0 | 180 | | var allAppStatuses = |
| | 0 | 181 | | await _dictionaryCache.GetOrCreateAsync("dict_app_statuses_v3", () => _appStatusServices.GetAppStatuses(), c |
| | | 182 | | |
| | 0 | 183 | | var allSuppliers = |
| | 0 | 184 | | await _dictionaryCache.GetOrCreateAsync("dict_suppliers", () => _supplierDataServices.GetSuppliers(), ct); |
| | | 185 | | |
| | 0 | 186 | | var allClients = |
| | 0 | 187 | | await _dictionaryCache.GetOrCreateAsync("dict_ozon_clients", () => _ozonClientServices.GetOzonClients(), ct) |
| | | 188 | | |
| | 0 | 189 | | var allWarehouses = |
| | 0 | 190 | | await _dictionaryCache.GetOrCreateAsync("dict_warehouses", () => _warehouseDataServices.GetWarehouses(), ct) |
| | | 191 | | |
| | 0 | 192 | | var allProviders = |
| | 0 | 193 | | await _dictionaryCache.GetOrCreateAsync("dict_delivery_providers", () => _deliveryDataServices.GetProviders( |
| | | 194 | | |
| | 0 | 195 | | var statusIds = accessProfile.ScopeItems |
| | 0 | 196 | | .Where(x => x.ScopeType == DataScopeType.AppStatus) |
| | 0 | 197 | | .Select(x => x.ReferenceId) |
| | 0 | 198 | | .ToList(); |
| | | 199 | | |
| | 0 | 200 | | var appStatusNames = filteredData |
| | 0 | 201 | | .Where(o => o.AppStatus != null && !string.IsNullOrEmpty(o.AppStatus.Name)) |
| | 0 | 202 | | .Select(o => o.AppStatus!.Name!) |
| | 0 | 203 | | .Distinct() |
| | 0 | 204 | | .ToList(); |
| | | 205 | | |
| | 0 | 206 | | if (statusIds.Any()) |
| | | 207 | | { |
| | 0 | 208 | | var allowedStatusNames = allAppStatuses |
| | 0 | 209 | | .Where(s => statusIds.Contains(s.Id)) |
| | 0 | 210 | | .Select(s => s.Name) |
| | 0 | 211 | | .ToList(); |
| | | 212 | | |
| | 0 | 213 | | appStatusNames = appStatusNames.Where(n => allowedStatusNames.Contains(n)).ToList(); |
| | | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | appStatusNames = appStatusNames.OrderBy(n => n).ToList(); |
| | | 217 | | |
| | 0 | 218 | | var hasUnassigned = filteredData.Any(o => o.AppStatus == null || string.IsNullOrEmpty(o.AppStatus.Name)); |
| | | 219 | | |
| | 0 | 220 | | if (hasUnassigned && !appStatusNames.Contains("Не указан")) |
| | | 221 | | { |
| | 0 | 222 | | appStatusNames.Insert(0, "Не указан"); |
| | | 223 | | } |
| | 0 | 224 | | else if (appStatusNames.Contains("Не указан")) |
| | | 225 | | { |
| | 0 | 226 | | appStatusNames.Remove("Не указан"); |
| | 0 | 227 | | appStatusNames.Insert(0, "Не указан"); |
| | | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | var statusColors = allAppStatuses |
| | 0 | 231 | | .Where(s => s.Name != null && appStatusNames.Contains(s.Name)) |
| | 0 | 232 | | .ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | | 233 | | |
| | 0 | 234 | | if (appStatusNames.Contains("Не указан") && !statusColors.ContainsKey("Не указан")) |
| | | 235 | | { |
| | 0 | 236 | | var unassignedStatus = allAppStatuses.FirstOrDefault(s => s.Name == "Не указан"); |
| | | 237 | | |
| | 0 | 238 | | if (unassignedStatus != null) |
| | | 239 | | { |
| | 0 | 240 | | statusColors["Не указан"] = unassignedStatus.GetStatusColor(); |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | data.AppStatuses = new SelectList(appStatusNames.Select(n => new { Name = n }), "Name", "Name"); |
| | 0 | 245 | | data.StatusColors = statusColors; |
| | | 246 | | |
| | 0 | 247 | | var supplierIds = accessProfile.ScopeItems |
| | 0 | 248 | | .Where(x => x.ScopeType == DataScopeType.Supplier) |
| | 0 | 249 | | .Select(x => x.ReferenceId) |
| | 0 | 250 | | .ToList(); |
| | | 251 | | |
| | 0 | 252 | | var supplierNames = filteredData |
| | 0 | 253 | | .Where(o => o.Supplier != null && !string.IsNullOrEmpty(o.Supplier.Name)) |
| | 0 | 254 | | .Select(o => o.Supplier!.Name!) |
| | 0 | 255 | | .Distinct() |
| | 0 | 256 | | .ToList(); |
| | | 257 | | |
| | 0 | 258 | | if (supplierIds.Any()) |
| | | 259 | | { |
| | 0 | 260 | | var allowedSupplierNames = allSuppliers |
| | 0 | 261 | | .Where(s => supplierIds.Contains(s.Id)) |
| | 0 | 262 | | .Select(s => s.Name) |
| | 0 | 263 | | .ToList(); |
| | | 264 | | |
| | 0 | 265 | | supplierNames = supplierNames.Where(n => allowedSupplierNames.Contains(n)).ToList(); |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | data.Suppliers = |
| | 0 | 269 | | new SelectList(supplierNames.OrderBy(n => n).Select(n => new { Name = n }), "Name", "Name"); |
| | | 270 | | |
| | 0 | 271 | | var clientIds = accessProfile.ScopeItems |
| | 0 | 272 | | .Where(x => x.ScopeType == DataScopeType.OzonClient) |
| | 0 | 273 | | .Select(x => x.ReferenceId) |
| | 0 | 274 | | .ToList(); |
| | | 275 | | |
| | 0 | 276 | | var clientNames = filteredData |
| | 0 | 277 | | .Where(o => o.OzonClient != null && !string.IsNullOrEmpty(o.OzonClient.Name)) |
| | 0 | 278 | | .Select(o => o.OzonClient!.Name!) |
| | 0 | 279 | | .Distinct() |
| | 0 | 280 | | .ToList(); |
| | | 281 | | |
| | 0 | 282 | | if (clientIds.Any()) |
| | | 283 | | { |
| | 0 | 284 | | var allowedClientNames = allClients |
| | 0 | 285 | | .Where(c => clientIds.Contains(c.Id)) |
| | 0 | 286 | | .Select(c => c.Name) |
| | 0 | 287 | | .ToList(); |
| | | 288 | | |
| | 0 | 289 | | clientNames = clientNames.Where(n => allowedClientNames.Contains(n)).ToList(); |
| | | 290 | | } |
| | | 291 | | |
| | 0 | 292 | | data.Clients = new SelectList(clientNames.OrderBy(n => n).Select(n => new { Name = n }), "Name", "Name"); |
| | | 293 | | |
| | 0 | 294 | | var warehouseIds = accessProfile.ScopeItems |
| | 0 | 295 | | .Where(x => x.ScopeType == DataScopeType.Warehouse) |
| | 0 | 296 | | .Select(x => x.ReferenceId) |
| | 0 | 297 | | .ToList(); |
| | | 298 | | |
| | 0 | 299 | | var warehouseNames = filteredData |
| | 0 | 300 | | .Where(o => o.ShipmentWarehouse != null && !string.IsNullOrEmpty(o.ShipmentWarehouse.Name)) |
| | 0 | 301 | | .Select(o => o.ShipmentWarehouse!.Name!) |
| | 0 | 302 | | .Distinct() |
| | 0 | 303 | | .ToList(); |
| | | 304 | | |
| | 0 | 305 | | if (warehouseIds.Any()) |
| | | 306 | | { |
| | 0 | 307 | | var allowedWarehouseNames = allWarehouses |
| | 0 | 308 | | .Where(w => warehouseIds.Contains(w.Id)) |
| | 0 | 309 | | .Select(w => w.Name) |
| | 0 | 310 | | .ToList(); |
| | | 311 | | |
| | 0 | 312 | | warehouseNames = warehouseNames.Where(n => allowedWarehouseNames.Contains(n)).ToList(); |
| | | 313 | | } |
| | | 314 | | |
| | 0 | 315 | | var allWarehouseNames = warehouseNames.OrderBy(n => n).ToList(); |
| | | 316 | | |
| | 0 | 317 | | data.Warehouses = new SelectList(allWarehouseNames.Select(w => new { Name = w }), "Name", "Name"); |
| | | 318 | | |
| | 0 | 319 | | var statusNames = filteredData |
| | 0 | 320 | | .Where(o => o.ClientStatus != null && !string.IsNullOrEmpty(o.ClientStatus.Name)) |
| | 0 | 321 | | .Select(o => o.ClientStatus!.Name!) |
| | 0 | 322 | | .Distinct() |
| | 0 | 323 | | .OrderBy(n => n) |
| | 0 | 324 | | .ToList(); |
| | | 325 | | |
| | 0 | 326 | | data.Statuses = new SelectList(statusNames.Select(n => new { Name = n }), "Name", "Name"); |
| | | 327 | | |
| | 0 | 328 | | var providerIds = accessProfile.ScopeItems |
| | 0 | 329 | | .Where(x => x.ScopeType == DataScopeType.DeliveryProvider) |
| | 0 | 330 | | .Select(x => x.ReferenceId) |
| | 0 | 331 | | .ToList(); |
| | | 332 | | |
| | 0 | 333 | | var providerNames = filteredData |
| | 0 | 334 | | .Where(o => o.Delivery != null && o.Delivery.Provider != null && |
| | 0 | 335 | | !string.IsNullOrEmpty(o.Delivery.Provider.Name)) |
| | 0 | 336 | | .Select(o => o.Delivery!.Provider!.Name!) |
| | 0 | 337 | | .Distinct() |
| | 0 | 338 | | .ToList(); |
| | | 339 | | |
| | 0 | 340 | | if (providerIds.Any()) |
| | | 341 | | { |
| | 0 | 342 | | var allowedProviderNames = allProviders |
| | 0 | 343 | | .Where(p => providerIds.Contains(p.Id)) |
| | 0 | 344 | | .Select(p => p.Name) |
| | 0 | 345 | | .ToList(); |
| | | 346 | | |
| | 0 | 347 | | providerNames = providerNames.Where(n => allowedProviderNames.Contains(n)).ToList(); |
| | | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | data.Providers = |
| | 0 | 351 | | new SelectList(providerNames.OrderBy(n => n).Select(n => new { Name = n }), "Name", "Name"); |
| | | 352 | | |
| | 0 | 353 | | var filteredDeliveryNames = filteredData |
| | 0 | 354 | | .Where(o => o.Delivery != null && !string.IsNullOrEmpty(o.Delivery.Name)) |
| | 0 | 355 | | .Select(o => o.Delivery!.Name!) |
| | 0 | 356 | | .Distinct() |
| | 0 | 357 | | .OrderBy(n => n) |
| | 0 | 358 | | .ToList(); |
| | | 359 | | |
| | 0 | 360 | | data.DeliveryNames = new SelectList(filteredDeliveryNames.Select(n => new { Name = n }), "Name", "Name"); |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | private async Task SetFilteredListsAsync( |
| | | 364 | | OrderFilterListsViewData data, |
| | | 365 | | AccessProfile accessProfile, |
| | | 366 | | CancellationToken ct) |
| | | 367 | | { |
| | 0 | 368 | | var statusIds = accessProfile.ScopeItems |
| | 0 | 369 | | .Where(x => x.ScopeType == DataScopeType.AppStatus) |
| | 0 | 370 | | .Select(x => x.ReferenceId) |
| | 0 | 371 | | .ToList(); |
| | | 372 | | |
| | 0 | 373 | | var allAppStatuses = |
| | 0 | 374 | | await _dictionaryCache.GetOrCreateAsync("dict_app_statuses_v3", () => _appStatusServices.GetAppStatuses(), c |
| | | 375 | | |
| | 0 | 376 | | var appStatuses = statusIds.Any() |
| | 0 | 377 | | ? allAppStatuses.Where(s => statusIds.Contains(s.Id)).OrderBy(a => a.Name).ToList() |
| | 0 | 378 | | : allAppStatuses.OrderBy(a => a.Name).ToList(); |
| | | 379 | | |
| | 0 | 380 | | var unassignedStatus = appStatuses.FirstOrDefault(s => s.Name == "Не указан"); |
| | | 381 | | |
| | 0 | 382 | | if (unassignedStatus != null) |
| | | 383 | | { |
| | 0 | 384 | | appStatuses.Remove(unassignedStatus); |
| | 0 | 385 | | appStatuses.Insert(0, unassignedStatus); |
| | | 386 | | } |
| | | 387 | | |
| | 0 | 388 | | data.AppStatuses = new SelectList(appStatuses, nameof(AppStatus.Name), nameof(AppStatus.Name)); |
| | 0 | 389 | | data.StatusColors = appStatuses.ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | 0 | 390 | | data.AppStatusIsFinal = appStatuses.ToDictionary(s => s.Name!, s => s.IsFinal.ToString()); |
| | | 391 | | |
| | 0 | 392 | | var supplierIds = accessProfile.ScopeItems |
| | 0 | 393 | | .Where(x => x.ScopeType == DataScopeType.Supplier) |
| | 0 | 394 | | .Select(x => x.ReferenceId) |
| | 0 | 395 | | .ToList(); |
| | | 396 | | |
| | 0 | 397 | | var allSuppliers = |
| | 0 | 398 | | await _dictionaryCache.GetOrCreateAsync("dict_suppliers", () => _supplierDataServices.GetSuppliers(), ct); |
| | | 399 | | |
| | 0 | 400 | | var suppliers = supplierIds.Any() |
| | 0 | 401 | | ? allSuppliers.Where(s => supplierIds.Contains(s.Id)).OrderBy(a => a.Name).ToList() |
| | 0 | 402 | | : allSuppliers.OrderBy(a => a.Name).ToList(); |
| | | 403 | | |
| | 0 | 404 | | data.Suppliers = new SelectList(suppliers, "Name", "Name"); |
| | | 405 | | |
| | 0 | 406 | | var clientIds = accessProfile.ScopeItems |
| | 0 | 407 | | .Where(x => x.ScopeType == DataScopeType.OzonClient) |
| | 0 | 408 | | .Select(x => x.ReferenceId) |
| | 0 | 409 | | .ToList(); |
| | | 410 | | |
| | 0 | 411 | | var allClients = |
| | 0 | 412 | | await _dictionaryCache.GetOrCreateAsync("dict_ozon_clients", () => _ozonClientServices.GetOzonClients(), ct) |
| | | 413 | | |
| | 0 | 414 | | var clients = clientIds.Any() |
| | 0 | 415 | | ? allClients.Where(c => clientIds.Contains(c.Id)).OrderBy(a => a.Name).ToList() |
| | 0 | 416 | | : allClients.OrderBy(a => a.Name).ToList(); |
| | | 417 | | |
| | 0 | 418 | | data.Clients = new SelectList(clients, "Name", "Name"); |
| | | 419 | | |
| | 0 | 420 | | var warehouseIds = accessProfile.ScopeItems |
| | 0 | 421 | | .Where(x => x.ScopeType == DataScopeType.Warehouse) |
| | 0 | 422 | | .Select(x => x.ReferenceId) |
| | 0 | 423 | | .ToList(); |
| | | 424 | | |
| | | 425 | | List<string> warehouseNames; |
| | | 426 | | |
| | 0 | 427 | | var allWarehouses = |
| | 0 | 428 | | await _dictionaryCache.GetOrCreateAsync("dict_warehouses", () => _warehouseDataServices.GetWarehouses(), ct) |
| | | 429 | | |
| | 0 | 430 | | if (warehouseIds.Any()) |
| | | 431 | | { |
| | 0 | 432 | | warehouseNames = allWarehouses |
| | 0 | 433 | | .Where(w => warehouseIds.Contains(w.Id)) |
| | 0 | 434 | | .Select(w => w.Name) |
| | 0 | 435 | | .Where(n => !string.IsNullOrEmpty(n)) |
| | 0 | 436 | | .Select(n => n!) |
| | 0 | 437 | | .Distinct() |
| | 0 | 438 | | .OrderBy(w => w) |
| | 0 | 439 | | .ToList(); |
| | | 440 | | } |
| | | 441 | | else |
| | | 442 | | { |
| | 0 | 443 | | warehouseNames = allWarehouses |
| | 0 | 444 | | .Select(w => w.Name) |
| | 0 | 445 | | .Where(n => !string.IsNullOrEmpty(n)) |
| | 0 | 446 | | .Select(n => n!) |
| | 0 | 447 | | .Distinct() |
| | 0 | 448 | | .OrderBy(w => w) |
| | 0 | 449 | | .ToList(); |
| | | 450 | | } |
| | | 451 | | |
| | 0 | 452 | | data.Warehouses = new SelectList(warehouseNames.Select(w => new { Name = w }), "Name", "Name"); |
| | | 453 | | |
| | 0 | 454 | | var allClientStatuses = |
| | 0 | 455 | | await _dictionaryCache.GetOrCreateAsync("dict_client_statuses_v3", () => _clientStatusServices.GetClientStat |
| | | 456 | | |
| | 0 | 457 | | var orderedStatuses = allClientStatuses.OrderBy(s => s.Name).ToList(); |
| | | 458 | | |
| | 0 | 459 | | data.Statuses = new SelectList(orderedStatuses, nameof(ClientStatus.Name), nameof(ClientStatus.Name)); |
| | 0 | 460 | | data.StatusesIsFinal = orderedStatuses.ToDictionary(s => s.Name!, s => s.IsFinal.ToString()); |
| | 0 | 461 | | data.ClientStatusColors = orderedStatuses.ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | 0 | 462 | | data.ClientStatusTypesForPrint = orderedStatuses |
| | 0 | 463 | | .ToDictionary( |
| | 0 | 464 | | s => s.Name, |
| | 0 | 465 | | s => s.ClientType.HasValue ? s.ClientType.Value.ToString() : ClientType.ALL.ToString()); |
| | | 466 | | |
| | 0 | 467 | | var providerIds = accessProfile.ScopeItems |
| | 0 | 468 | | .Where(x => x.ScopeType == DataScopeType.DeliveryProvider) |
| | 0 | 469 | | .Select(x => x.ReferenceId) |
| | 0 | 470 | | .ToList(); |
| | | 471 | | |
| | 0 | 472 | | var allProviders = |
| | 0 | 473 | | await _dictionaryCache.GetOrCreateAsync("dict_delivery_providers", () => _deliveryDataServices.GetProviders( |
| | | 474 | | |
| | 0 | 475 | | var providers = providerIds.Any() |
| | 0 | 476 | | ? allProviders.Where(p => providerIds.Contains(p.Id)).OrderBy(a => a.Name).ToList() |
| | 0 | 477 | | : allProviders.OrderBy(a => a.Name).ToList(); |
| | | 478 | | |
| | 0 | 479 | | data.Providers = new SelectList(providers, "Name", "Name"); |
| | | 480 | | |
| | 0 | 481 | | var allDeliveriesForFilter = |
| | 0 | 482 | | await _dictionaryCache.GetOrCreateAsync("dict_deliveries", () => _deliveryDataServices.GetDeliveriesAsync(), |
| | | 483 | | |
| | 0 | 484 | | data.DeliveryNames = new SelectList( |
| | 0 | 485 | | allDeliveriesForFilter.OrderBy(d => d.Name).Select(d => new { Name = d.Name }), |
| | 0 | 486 | | "Name", |
| | 0 | 487 | | "Name"); |
| | 0 | 488 | | } |
| | | 489 | | |
| | | 490 | | private async Task SetFilterListsWithoutRestrictionsAsync(OrderFilterListsViewData data, CancellationToken ct) |
| | | 491 | | { |
| | 0 | 492 | | var allAppStatuses = |
| | 0 | 493 | | await _dictionaryCache.GetOrCreateAsync("dict_app_statuses_v3", () => _appStatusServices.GetAppStatuses(), c |
| | | 494 | | |
| | 0 | 495 | | var allSuppliers = |
| | 0 | 496 | | await _dictionaryCache.GetOrCreateAsync("dict_suppliers", () => _supplierDataServices.GetSuppliers(), ct); |
| | | 497 | | |
| | 0 | 498 | | var allClients = |
| | 0 | 499 | | await _dictionaryCache.GetOrCreateAsync("dict_ozon_clients", () => _ozonClientServices.GetOzonClients(), ct) |
| | | 500 | | |
| | 0 | 501 | | var allWarehouses = |
| | 0 | 502 | | await _dictionaryCache.GetOrCreateAsync("dict_warehouses", () => _warehouseDataServices.GetWarehouses(), ct) |
| | | 503 | | |
| | 0 | 504 | | var allClientStatuses = |
| | 0 | 505 | | await _dictionaryCache.GetOrCreateAsync("dict_client_statuses_v3", () => _clientStatusServices.GetClientStat |
| | | 506 | | |
| | 0 | 507 | | var allProviders = |
| | 0 | 508 | | await _dictionaryCache.GetOrCreateAsync("dict_delivery_providers", () => _deliveryDataServices.GetProviders( |
| | | 509 | | |
| | 0 | 510 | | var allDeliveriesForFilterUnrestricted = |
| | 0 | 511 | | await _dictionaryCache.GetOrCreateAsync("dict_deliveries", () => _deliveryDataServices.GetDeliveriesAsync(), |
| | | 512 | | |
| | 0 | 513 | | var appStatuses = allAppStatuses.OrderBy(a => a.Name).ToList(); |
| | | 514 | | |
| | 0 | 515 | | var unassignedStatus = appStatuses.FirstOrDefault(s => s.Name == "Не указан"); |
| | | 516 | | |
| | 0 | 517 | | if (unassignedStatus != null) |
| | | 518 | | { |
| | 0 | 519 | | appStatuses.Remove(unassignedStatus); |
| | 0 | 520 | | appStatuses.Insert(0, unassignedStatus); |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | data.AppStatuses = new SelectList(appStatuses, "Name", "Name"); |
| | 0 | 524 | | data.StatusColors = appStatuses.ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | 0 | 525 | | data.AppStatusIsFinal = appStatuses.ToDictionary(s => s.Name!, s => s.IsFinal.ToString()); |
| | 0 | 526 | | data.Suppliers = new SelectList(allSuppliers.OrderBy(a => a.Name), "Name", "Name"); |
| | | 527 | | |
| | 0 | 528 | | var orderedClients = allClients.OrderBy(a => a.Name).ToList(); |
| | | 529 | | |
| | 0 | 530 | | data.Clients = new SelectList(orderedClients, "Name", "Name"); |
| | 0 | 531 | | data.ClientTypesForPrint = orderedClients |
| | 0 | 532 | | .ToDictionary(c => c.Name!, c => c.ClientType.ToString()); |
| | | 533 | | |
| | 0 | 534 | | var warehouseNames = allWarehouses |
| | 0 | 535 | | .Select(w => w.Name) |
| | 0 | 536 | | .Where(n => !string.IsNullOrEmpty(n)) |
| | 0 | 537 | | .Distinct() |
| | 0 | 538 | | .OrderBy(w => w) |
| | 0 | 539 | | .ToList(); |
| | | 540 | | |
| | 0 | 541 | | data.Warehouses = new SelectList(warehouseNames.Select(w => new { Name = w }), "Name", "Name"); |
| | | 542 | | |
| | 0 | 543 | | var orderedStatuses = allClientStatuses.OrderBy(s => s.Name).ToList(); |
| | | 544 | | |
| | 0 | 545 | | data.Statuses = new SelectList(orderedStatuses, nameof(ClientStatus.Name), nameof(ClientStatus.Name)); |
| | 0 | 546 | | data.StatusesIsFinal = orderedStatuses.ToDictionary(s => s.Name!, s => s.IsFinal.ToString()); |
| | 0 | 547 | | data.ClientStatusColors = orderedStatuses.ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | 0 | 548 | | data.ClientStatusTypesForPrint = orderedStatuses |
| | 0 | 549 | | .ToDictionary( |
| | 0 | 550 | | s => s.Name, |
| | 0 | 551 | | s => s.ClientType.HasValue ? s.ClientType.Value.ToString() : ClientType.ALL.ToString()); |
| | | 552 | | |
| | 0 | 553 | | data.Providers = new SelectList(allProviders.OrderBy(a => a.Name), "Name", "Name"); |
| | 0 | 554 | | data.DeliveryNames = new SelectList( |
| | 0 | 555 | | allDeliveriesForFilterUnrestricted.OrderBy(d => d.Name).Select(d => new { Name = d.Name }), |
| | 0 | 556 | | "Name", |
| | 0 | 557 | | "Name"); |
| | 0 | 558 | | } |
| | | 559 | | |
| | | 560 | | private async Task ApplyDataScopeToFilterListsAsync( |
| | | 561 | | OrderFilterListsViewData data, |
| | | 562 | | string? userId, |
| | | 563 | | string? orderScopeExpression, |
| | | 564 | | CancellationToken ct) |
| | | 565 | | { |
| | 0 | 566 | | if (string.IsNullOrEmpty(userId)) |
| | | 567 | | { |
| | 0 | 568 | | return; |
| | | 569 | | } |
| | | 570 | | |
| | 0 | 571 | | if (string.IsNullOrEmpty(orderScopeExpression)) |
| | | 572 | | { |
| | 0 | 573 | | return; |
| | | 574 | | } |
| | | 575 | | |
| | 0 | 576 | | var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync(userId, "Order", ct); |
| | 0 | 577 | | if (allowed.Count == 0) |
| | | 578 | | { |
| | 0 | 579 | | return; |
| | | 580 | | } |
| | | 581 | | |
| | 0 | 582 | | if (allowed.TryGetValue("ShipmentWarehouseId", out var whIds) && whIds.Count > 0) |
| | | 583 | | { |
| | 0 | 584 | | var allWarehouses = |
| | 0 | 585 | | await _dictionaryCache.GetOrCreateAsync("dict_warehouses", () => _warehouseDataServices.GetWarehouses(), |
| | | 586 | | |
| | 0 | 587 | | var names = allWarehouses |
| | 0 | 588 | | .Where(w => whIds.Contains(w.Id.ToString())) |
| | 0 | 589 | | .Select(w => w.Name) |
| | 0 | 590 | | .Where(n => !string.IsNullOrEmpty(n)) |
| | 0 | 591 | | .Distinct() |
| | 0 | 592 | | .OrderBy(w => w) |
| | 0 | 593 | | .ToList(); |
| | | 594 | | |
| | 0 | 595 | | data.Warehouses = new SelectList(names.Select(w => new { Name = w }), "Name", "Name"); |
| | | 596 | | } |
| | | 597 | | |
| | 0 | 598 | | if (allowed.TryGetValue("SupplierId", out var supIds) && supIds.Count > 0) |
| | | 599 | | { |
| | 0 | 600 | | var allSuppliers = |
| | 0 | 601 | | await _dictionaryCache.GetOrCreateAsync("dict_suppliers", () => _supplierDataServices.GetSuppliers(), ct |
| | | 602 | | |
| | 0 | 603 | | var filtered = allSuppliers.Where(s => supIds.Contains(s.Id.ToString())).OrderBy(s => s.Name).ToList(); |
| | 0 | 604 | | data.Suppliers = new SelectList(filtered, "Name", "Name"); |
| | | 605 | | } |
| | | 606 | | |
| | 0 | 607 | | if (allowed.TryGetValue("OzonClientId", out var ozIds) && ozIds.Count > 0) |
| | | 608 | | { |
| | 0 | 609 | | var allClients = |
| | 0 | 610 | | await _dictionaryCache.GetOrCreateAsync("dict_ozon_clients", () => _ozonClientServices.GetOzonClients(), |
| | | 611 | | |
| | 0 | 612 | | var filtered = allClients.Where(c => ozIds.Contains(c.Id.ToString())).OrderBy(c => c.Name).ToList(); |
| | 0 | 613 | | data.Clients = new SelectList(filtered, "Name", "Name"); |
| | 0 | 614 | | data.ClientTypesForPrint = filtered.ToDictionary(c => c.Name!, c => c.ClientType.ToString()); |
| | | 615 | | } |
| | | 616 | | |
| | 0 | 617 | | if (allowed.TryGetValue("AppStatusId", out var appStIds) && appStIds.Count > 0) |
| | | 618 | | { |
| | 0 | 619 | | var allAppStatuses = |
| | 0 | 620 | | await _dictionaryCache.GetOrCreateAsync("dict_app_statuses_v3", () => _appStatusServices.GetAppStatuses( |
| | | 621 | | |
| | 0 | 622 | | var filtered = allAppStatuses.Where(s => appStIds.Contains(s.Id.ToString())).OrderBy(s => s.Name).ToList(); |
| | 0 | 623 | | var unassigned = filtered.FirstOrDefault(s => s.Name == "Не указан"); |
| | 0 | 624 | | if (unassigned != null) |
| | | 625 | | { |
| | 0 | 626 | | filtered.Remove(unassigned); |
| | 0 | 627 | | filtered.Insert(0, unassigned); |
| | | 628 | | } |
| | | 629 | | |
| | 0 | 630 | | data.AppStatuses = new SelectList(filtered, nameof(AppStatus.Name), nameof(AppStatus.Name)); |
| | 0 | 631 | | data.StatusColors = filtered.ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | 0 | 632 | | data.AppStatusIsFinal = filtered.ToDictionary(s => s.Name!, s => s.IsFinal.ToString()); |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | if (allowed.TryGetValue("ClientStatusId", out var clStIds) && clStIds.Count > 0) |
| | | 636 | | { |
| | 0 | 637 | | var allClientStatuses = |
| | 0 | 638 | | await _dictionaryCache.GetOrCreateAsync("dict_client_statuses_v3", () => _clientStatusServices.GetClient |
| | | 639 | | |
| | 0 | 640 | | var filtered = allClientStatuses.Where(s => clStIds.Contains(s.Id.ToString())).OrderBy(s => s.Name).ToList() |
| | 0 | 641 | | data.Statuses = new SelectList(filtered, nameof(ClientStatus.Name), nameof(ClientStatus.Name)); |
| | 0 | 642 | | data.StatusesIsFinal = filtered.ToDictionary(s => s.Name!, s => s.IsFinal.ToString()); |
| | 0 | 643 | | data.ClientStatusColors = filtered.ToDictionary(s => s.Name!, s => s.GetStatusColor()); |
| | 0 | 644 | | data.ClientStatusTypesForPrint = filtered.ToDictionary( |
| | 0 | 645 | | s => s.Name, |
| | 0 | 646 | | s => s.ClientType.HasValue ? s.ClientType.Value.ToString() : ClientType.ALL.ToString()); |
| | | 647 | | } |
| | | 648 | | |
| | 0 | 649 | | if (allowed.TryGetValue("DeliveryId", out var delIds) && delIds.Count > 0) |
| | | 650 | | { |
| | 0 | 651 | | var allDeliveries = |
| | 0 | 652 | | await _dictionaryCache.GetOrCreateAsync("dict_deliveries", () => _deliveryDataServices.GetDeliveriesAsyn |
| | | 653 | | |
| | 0 | 654 | | var filteredDeliveries = allDeliveries.Where(d => delIds.Contains(d.Id.ToString())).OrderBy(d => d.Name).ToL |
| | 0 | 655 | | data.DeliveryNames = new SelectList(filteredDeliveries.Select(d => new { Name = d.Name }), "Name", "Name"); |
| | | 656 | | } |
| | 0 | 657 | | } |
| | | 658 | | } |