| | | 1 | | using Newtonsoft.Json; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Data; |
| | | 4 | | using Services.ApiServices._1CApi.Models; |
| | | 5 | | using Services.OneCServices; |
| | | 6 | | |
| | | 7 | | namespace Services.DataServices; |
| | | 8 | | |
| | | 9 | | public class OrderHistoryOneCPostingService : IOrderHistoryOneCPostingService |
| | | 10 | | { |
| | | 11 | | private readonly IOrderHistoryOneCPostingRepository _repository; |
| | | 12 | | |
| | 12 | 13 | | public OrderHistoryOneCPostingService(IOrderHistoryOneCPostingRepository repository) |
| | | 14 | | { |
| | 12 | 15 | | _repository = repository; |
| | 12 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task RecordPostingsAsync( |
| | | 19 | | IReadOnlyList<Order> orders, |
| | | 20 | | IReadOnlyList<OneCResponse> responses, |
| | | 21 | | string operationKind, |
| | | 22 | | string changedBy, |
| | | 23 | | int? transactionId, |
| | | 24 | | DateTime historyWindowStart, |
| | | 25 | | string? warehouseSender = null, |
| | | 26 | | string? warehouseRecipient = null, |
| | | 27 | | DateTime? postedAt = null) |
| | | 28 | | { |
| | 13 | 29 | | if (orders == null || orders.Count == 0 || responses == null || responses.Count == 0) |
| | 4 | 30 | | return; |
| | | 31 | | |
| | | 32 | | // Локальное время — как в orders_history.ChangedAt (триггер MySQL). |
| | 9 | 33 | | var posted = postedAt ?? DateTime.Now; |
| | 9 | 34 | | var windowStart = historyWindowStart.AddSeconds(-30); |
| | 9 | 35 | | var windowEnd = posted.AddMinutes(5); |
| | 9 | 36 | | var detailsJson = JsonConvert.SerializeObject(new |
| | 9 | 37 | | { |
| | 9 | 38 | | responses, |
| | 9 | 39 | | warehouseSender, |
| | 9 | 40 | | warehouseRecipient |
| | 9 | 41 | | }); |
| | | 42 | | |
| | 9 | 43 | | var postings = new List<OrderHistoryOneCPosting>(); |
| | | 44 | | |
| | 40 | 45 | | foreach (var order in orders) |
| | | 46 | | { |
| | | 47 | | // Связь только по времени: ChangedBy в истории = UpdatedBy заказа (часто «Excel»), не пользователь транзакц |
| | 11 | 48 | | var historyIds = await _repository.GetHistoryIdsInWindowAsync( |
| | 11 | 49 | | order.Id, |
| | 11 | 50 | | windowStart, |
| | 11 | 51 | | windowEnd, |
| | 11 | 52 | | changedBy: null); |
| | | 53 | | |
| | 11 | 54 | | var latestHistory = await _repository.GetLatestHistoryInWindowAsync( |
| | 11 | 55 | | order.Id, |
| | 11 | 56 | | windowStart, |
| | 11 | 57 | | windowEnd); |
| | | 58 | | |
| | 11 | 59 | | var batchAnchor = latestHistory?.ChangedAt ?? posted; |
| | 11 | 60 | | var historyChangedBy = latestHistory?.ChangedBy; |
| | 11 | 61 | | var batchKey = OneCPostingSummaryBuilder.BuildHistoryBatchKey( |
| | 11 | 62 | | order.Id, |
| | 11 | 63 | | batchAnchor, |
| | 11 | 64 | | historyChangedBy); |
| | | 65 | | |
| | 11 | 66 | | var summary = OneCPostingSummaryBuilder.Build( |
| | 11 | 67 | | new List<Order> { order }, |
| | 11 | 68 | | responses, |
| | 11 | 69 | | warehouseSender, |
| | 11 | 70 | | warehouseRecipient); |
| | | 71 | | |
| | 11 | 72 | | var posting = new OrderHistoryOneCPosting |
| | 11 | 73 | | { |
| | 11 | 74 | | OrderId = order.Id, |
| | 11 | 75 | | TransactionId = transactionId, |
| | 11 | 76 | | OperationKind = operationKind, |
| | 11 | 77 | | Success = summary.Success, |
| | 11 | 78 | | PostedAt = posted, |
| | 11 | 79 | | ChangedBy = changedBy, |
| | 11 | 80 | | HistoryBatchKey = batchKey, |
| | 11 | 81 | | OrganizationName = summary.OrganizationName, |
| | 11 | 82 | | OrganizationInn = summary.OrganizationInn, |
| | 11 | 83 | | SenderName = summary.SenderName, |
| | 11 | 84 | | SenderInn = summary.SenderInn, |
| | 11 | 85 | | WarehouseSender = summary.WarehouseSender, |
| | 11 | 86 | | WarehouseRecipient = summary.WarehouseRecipient, |
| | 11 | 87 | | OrdersCount = summary.OrdersCount, |
| | 11 | 88 | | ProductsCount = summary.ProductsCount, |
| | 11 | 89 | | ArticlesSummary = summary.ArticlesSummary, |
| | 11 | 90 | | DocumentsCount = summary.DocumentsCount, |
| | 11 | 91 | | DocumentNumbers = summary.DocumentNumbers, |
| | 11 | 92 | | DocumentGuids = summary.DocumentGuids, |
| | 11 | 93 | | IsPosted = summary.IsPosted, |
| | 11 | 94 | | ErrorSummary = summary.ErrorSummary, |
| | 11 | 95 | | DetailsJson = detailsJson, |
| | 11 | 96 | | CreatedAt = DateTime.Now |
| | 11 | 97 | | }; |
| | | 98 | | |
| | 28 | 99 | | foreach (var historyId in historyIds) |
| | | 100 | | { |
| | 3 | 101 | | posting.HistoryLinks.Add(new OrderHistoryOneCPostingLink |
| | 3 | 102 | | { |
| | 3 | 103 | | OrderHistoryId = historyId |
| | 3 | 104 | | }); |
| | | 105 | | } |
| | | 106 | | |
| | 11 | 107 | | postings.Add(posting); |
| | 11 | 108 | | } |
| | | 109 | | |
| | 9 | 110 | | await _repository.AddPostingsAsync(postings); |
| | 13 | 111 | | } |
| | | 112 | | |
| | | 113 | | public Task<List<OrderHistoryOneCPosting>> GetByOrderIdAsync(int orderId) |
| | 1 | 114 | | => _repository.GetByOrderIdAsync(orderId); |
| | | 115 | | } |