| | | 1 | | using OzonDomains; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using Services.ApiServices._1CApi.Models; |
| | | 4 | | |
| | | 5 | | namespace Services.TransactionCommentAi; |
| | | 6 | | |
| | | 7 | | public sealed class TransactionCommentAiContextBuilder |
| | | 8 | | { |
| | | 9 | | private readonly TransactionCommentAiContext _context; |
| | | 10 | | |
| | 31 | 11 | | public TransactionCommentAiContextBuilder( |
| | 31 | 12 | | TransactionType transactionType, |
| | 31 | 13 | | string userName, |
| | 31 | 14 | | DateTime performedAtUtc) |
| | | 15 | | { |
| | 31 | 16 | | _context = new TransactionCommentAiContext |
| | 31 | 17 | | { |
| | 31 | 18 | | TransactionType = transactionType, |
| | 31 | 19 | | TransactionTypeDisplayName = transactionType.GetDisplayName(), |
| | 31 | 20 | | UserName = userName, |
| | 31 | 21 | | PerformedAtUtc = performedAtUtc |
| | 31 | 22 | | }; |
| | 31 | 23 | | } |
| | | 24 | | |
| | | 25 | | public TransactionCommentAiContextBuilder WithTransactionId(int transactionId) |
| | | 26 | | { |
| | 0 | 27 | | _context.TransactionId = transactionId; |
| | 0 | 28 | | return this; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public TransactionCommentAiContextBuilder WithUserComment(string? userComment) |
| | | 32 | | { |
| | 0 | 33 | | _context.UserComment = userComment; |
| | 0 | 34 | | return this; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public TransactionCommentAiContextBuilder SkipGeneration(bool skip = true) |
| | | 38 | | { |
| | 0 | 39 | | _context.SkipGeneration = skip; |
| | 0 | 40 | | return this; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public TransactionCommentAiContextBuilder WithFlags(bool processIn1C, bool shipInOzonRequested) |
| | | 44 | | { |
| | 28 | 45 | | _context.ProcessIn1C = processIn1C; |
| | 28 | 46 | | _context.ShipInOzonRequested = shipInOzonRequested; |
| | 28 | 47 | | return this; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public TransactionCommentAiContextBuilder WithOzonCancelNotes(IEnumerable<string>? notes) |
| | | 51 | | { |
| | 1 | 52 | | if (notes != null) |
| | 4 | 53 | | _context.OzonCancelNotes.AddRange(notes.Where(n => !string.IsNullOrWhiteSpace(n))); |
| | 1 | 54 | | return this; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public TransactionCommentAiContextBuilder WithOneCResults( |
| | | 58 | | IReadOnlyList<OneCResponse> results, |
| | | 59 | | string? warehouseSender = null, |
| | | 60 | | string? warehouseRecipient = null) |
| | | 61 | | { |
| | 1 | 62 | | if (results.Count == 0) |
| | 0 | 63 | | return this; |
| | | 64 | | |
| | 1 | 65 | | var summary = new TransactionCommentAiOneCSummary |
| | 1 | 66 | | { |
| | 1 | 67 | | WarehouseSender = warehouseSender, |
| | 1 | 68 | | WarehouseRecipient = warehouseRecipient |
| | 1 | 69 | | }; |
| | | 70 | | |
| | 6 | 71 | | foreach (var r in results) |
| | | 72 | | { |
| | 2 | 73 | | if (r.Success) |
| | | 74 | | { |
| | 1 | 75 | | summary.Documents.Add(new TransactionCommentAiOneCDocument |
| | 1 | 76 | | { |
| | 1 | 77 | | DocumentNumber = r.DocumentNumber, |
| | 1 | 78 | | TransactionId = r.TransactionId, |
| | 1 | 79 | | IsPosted = r.IsPosted |
| | 1 | 80 | | }); |
| | | 81 | | |
| | 1 | 82 | | if (!r.IsPosted && !string.IsNullOrWhiteSpace(r.PostingError)) |
| | 0 | 83 | | summary.PostingErrors.Add(r.PostingError); |
| | | 84 | | |
| | 1 | 85 | | if (!string.IsNullOrWhiteSpace(r.MissingNomenclature)) |
| | 0 | 86 | | summary.MissingNomenclature.Add(r.MissingNomenclature); |
| | | 87 | | } |
| | 1 | 88 | | else if (!string.IsNullOrWhiteSpace(r.Message)) |
| | | 89 | | { |
| | 1 | 90 | | summary.Errors.Add(r.Message); |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | _context.OneC = summary; |
| | 1 | 95 | | return this; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public TransactionCommentAiContextBuilder WithOzonShip(TransactionCommentAiOzonShipSummary? summary) |
| | | 99 | | { |
| | 0 | 100 | | _context.OzonShip = summary; |
| | 0 | 101 | | return this; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | public TransactionCommentAiContextBuilder WithOrders( |
| | | 105 | | IReadOnlyList<Order> orders, |
| | | 106 | | IReadOnlyDictionary<int, int> fromStatusByOrderId, |
| | | 107 | | IReadOnlyDictionary<int, string> fromStatusNameById, |
| | | 108 | | IReadOnlyDictionary<int, List<string>>? changedFieldsByOrder = null) |
| | | 109 | | { |
| | 1 | 110 | | var snapshots = new List<TransactionCommentAiOrderSnapshot>(); |
| | 1 | 111 | | var splitCount = 0; |
| | 1 | 112 | | var toCancellationCount = 0; |
| | | 113 | | |
| | 6 | 114 | | foreach (var order in orders) |
| | | 115 | | { |
| | 2 | 116 | | var shipment = order.ShipmentNumber ?? string.Empty; |
| | 2 | 117 | | var isSplitPart = shipment.Contains('/', StringComparison.Ordinal); |
| | 2 | 118 | | var isToCancellation = string.Equals(order.AppStatus?.Name, "К отмене", StringComparison.OrdinalIgnoreCase) |
| | 2 | 119 | | || string.Equals(order.AppStatus?.Name, "Отменен", StringComparison.OrdinalIgnoreCase |
| | | 120 | | |
| | 4 | 121 | | if (isSplitPart) splitCount++; |
| | 3 | 122 | | if (isToCancellation) toCancellationCount++; |
| | | 123 | | |
| | 2 | 124 | | fromStatusByOrderId.TryGetValue(order.Id, out var fromStatusId); |
| | 2 | 125 | | fromStatusNameById.TryGetValue(fromStatusId, out var fromStatusName); |
| | | 126 | | |
| | 2 | 127 | | List<string> changedFieldsList = []; |
| | 2 | 128 | | if (changedFieldsByOrder != null && changedFieldsByOrder.TryGetValue(order.Id, out var cf)) |
| | 0 | 129 | | changedFieldsList = cf; |
| | | 130 | | |
| | 2 | 131 | | snapshots.Add(new TransactionCommentAiOrderSnapshot |
| | 2 | 132 | | { |
| | 2 | 133 | | OrderId = order.Id, |
| | 2 | 134 | | ShipmentNumber = order.ShipmentNumber, |
| | 2 | 135 | | FromStatusName = fromStatusName, |
| | 2 | 136 | | ToStatusName = order.AppStatus?.Name, |
| | 2 | 137 | | Article = order.Article, |
| | 2 | 138 | | ProductName = order.ProductName, |
| | 2 | 139 | | Manufacturer = order.EtProducer?.Name, |
| | 2 | 140 | | Quantity = order.Quantity, |
| | 2 | 141 | | OriginalQuantity = null, |
| | 2 | 142 | | IsSplitPart = isSplitPart, |
| | 2 | 143 | | IsToCancellationPart = isToCancellation, |
| | 2 | 144 | | SupplierName = order.Supplier?.Name, |
| | 2 | 145 | | ShipmentWarehouseName = order.ShipmentWarehouse?.Name, |
| | 2 | 146 | | OzonClientName = order.OzonClient?.Name, |
| | 2 | 147 | | DeliveryCity = order.DeliveryCity, |
| | 2 | 148 | | PurchasePrice = order.PurchasePrice, |
| | 2 | 149 | | MinProfit = order.MinProfit, |
| | 2 | 150 | | MaxProfit = order.MaxProfit, |
| | 2 | 151 | | ChangedFields = changedFieldsList ?? [] |
| | 2 | 152 | | }); |
| | | 153 | | } |
| | | 154 | | |
| | 1 | 155 | | _context.Orders = snapshots; |
| | 1 | 156 | | _context.Batch = new TransactionCommentAiBatchSummary |
| | 1 | 157 | | { |
| | 1 | 158 | | TotalOrders = orders.Count, |
| | 2 | 159 | | CancelledCount = orders.Count(o => string.Equals(o.AppStatus?.Name, "Отменен", StringComparison.OrdinalIgnor |
| | 1 | 160 | | SplitCount = splitCount, |
| | 1 | 161 | | ToCancellationCount = toCancellationCount, |
| | 1 | 162 | | TargetStatusName = orders |
| | 1 | 163 | | .Select(o => o.AppStatus?.Name) |
| | 1 | 164 | | .Where(n => !string.IsNullOrWhiteSpace(n)) |
| | 1 | 165 | | .Distinct() |
| | 1 | 166 | | .FirstOrDefault() |
| | 1 | 167 | | }; |
| | | 168 | | |
| | 1 | 169 | | return this; |
| | | 170 | | } |
| | | 171 | | |
| | 4 | 172 | | public TransactionCommentAiContext Build() => _context; |
| | | 173 | | } |