| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonOrdersWeb.WebServices.Orders; |
| | | 6 | | |
| | | 7 | | public interface IOrderReturnInfoService |
| | | 8 | | { |
| | | 9 | | Task PopulateForOrdersAsync(IReadOnlyList<Order> ordersForPage, CancellationToken cancellationToken = default); |
| | | 10 | | |
| | | 11 | | Task<List<string>> GetWarehouseOptionsAsync(CancellationToken cancellationToken = default); |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | public sealed class OrderReturnInfoService : IOrderReturnInfoService |
| | | 15 | | { |
| | | 16 | | private readonly ApplicationDbContext _context; |
| | | 17 | | |
| | 0 | 18 | | public OrderReturnInfoService(ApplicationDbContext context) |
| | | 19 | | { |
| | 0 | 20 | | _context = context; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task PopulateForOrdersAsync(IReadOnlyList<Order> ordersForPage, CancellationToken cancellationToken = d |
| | | 24 | | { |
| | 0 | 25 | | var returnedOrderIds = ordersForPage.Where(o => o.IsReturned).Select(o => o.Id).ToList(); |
| | 0 | 26 | | if (returnedOrderIds.Count == 0) |
| | | 27 | | { |
| | 0 | 28 | | return; |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | var returnInfoMap = await _context.OrderReturns |
| | 0 | 32 | | .Where(r => r.OrderId.HasValue && returnedOrderIds.Contains(r.OrderId.Value)) |
| | 0 | 33 | | .GroupBy(r => r.OrderId!.Value) |
| | 0 | 34 | | .Select(g => new |
| | 0 | 35 | | { |
| | 0 | 36 | | OrderId = g.Key, |
| | 0 | 37 | | Barcode = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.LogisticBarcode).FirstOrDefault(), |
| | 0 | 38 | | ReturnStatusSysName = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.VisualStatusSysName).FirstOrDe |
| | 0 | 39 | | ReturnStatusDisplayName = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.VisualStatusDisplayName).F |
| | 0 | 40 | | ReturnWarehouseName = g.OrderByDescending(x => x.UpdatedAt).Select(x => |
| | 0 | 41 | | x.PlaceName ?? x.TargetPlaceName).FirstOrDefault(), |
| | 0 | 42 | | ReturnStatusChangeDate = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.VisualStatusChangeMoment).F |
| | 0 | 43 | | }) |
| | 0 | 44 | | .ToDictionaryAsync(x => x.OrderId, x => x, cancellationToken); |
| | | 45 | | |
| | 0 | 46 | | foreach (var order in ordersForPage) |
| | | 47 | | { |
| | 0 | 48 | | if (!returnInfoMap.TryGetValue(order.Id, out var returnInfo)) |
| | | 49 | | { |
| | | 50 | | continue; |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | order.ReturnLogisticBarcode = returnInfo.Barcode; |
| | 0 | 54 | | order.ReturnStatusSysName = returnInfo.ReturnStatusSysName; |
| | 0 | 55 | | order.ReturnStatusDisplayName = returnInfo.ReturnStatusDisplayName; |
| | 0 | 56 | | order.ReturnWarehouseName = returnInfo.ReturnWarehouseName; |
| | 0 | 57 | | order.ReturnStatusChangeDate = returnInfo.ReturnStatusChangeDate; |
| | | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task<List<string>> GetWarehouseOptionsAsync(CancellationToken cancellationToken = default) |
| | | 62 | | { |
| | 0 | 63 | | var placeNames = await _context.OrderReturns |
| | 0 | 64 | | .Where(r => !string.IsNullOrWhiteSpace(r.PlaceName)) |
| | 0 | 65 | | .Select(r => r.PlaceName!) |
| | 0 | 66 | | .Distinct() |
| | 0 | 67 | | .ToListAsync(cancellationToken); |
| | | 68 | | |
| | 0 | 69 | | var targetPlaceNames = await _context.OrderReturns |
| | 0 | 70 | | .Where(r => !string.IsNullOrWhiteSpace(r.TargetPlaceName)) |
| | 0 | 71 | | .Select(r => r.TargetPlaceName!) |
| | 0 | 72 | | .Distinct() |
| | 0 | 73 | | .ToListAsync(cancellationToken); |
| | | 74 | | |
| | 0 | 75 | | return placeNames |
| | 0 | 76 | | .Concat(targetPlaceNames) |
| | 0 | 77 | | .Where(n => !string.IsNullOrWhiteSpace(n)) |
| | 0 | 78 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 79 | | .OrderBy(n => n) |
| | 0 | 80 | | .ToList(); |
| | 0 | 81 | | } |
| | | 82 | | } |