| | | 1 | | using OzonDomains.Models; |
| | | 2 | | using OzonOrdersWeb.Areas.Studio2.Controllers.Base; |
| | | 3 | | using OzonRepositories.Data; |
| | | 4 | | using Services.CacheServices.Cache; |
| | | 5 | | using Services.CacheServices.Cache.OzonOrdersCache; |
| | | 6 | | using Services.SignalRServices; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.WebServices.Transactions.Grid.Commit.Steps; |
| | | 9 | | |
| | | 10 | | /// <summary>Обновление кэшей заказов/транзакций и SignalR-уведомление всем о проведённом документе.</summary> |
| | | 11 | | public sealed class CachesAndNotifyStep : ITransactionCommitStep |
| | | 12 | | { |
| | | 13 | | private readonly CacheUpdater<Order> _cacheUpdater; |
| | | 14 | | private readonly OrderCache _orderCache; |
| | | 15 | | private readonly TransactionCache _transactionCache; |
| | | 16 | | private readonly IOrderChangeNotifier _changeNotifier; |
| | | 17 | | |
| | 0 | 18 | | public CachesAndNotifyStep(CacheUpdater<Order> cacheUpdater, OrderCache orderCache, TransactionCache transactionCach |
| | | 19 | | { |
| | 0 | 20 | | _changeNotifier = changeNotifier; |
| | 0 | 21 | | _cacheUpdater = cacheUpdater; |
| | 0 | 22 | | _orderCache = orderCache; |
| | 0 | 23 | | _transactionCache = transactionCache; |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | public string Key => TransactionCommitStepKeys.CachesAndNotify; |
| | | 27 | | |
| | | 28 | | public async Task ExecuteAsync(TransactionCommitContext ctx) |
| | | 29 | | { |
| | | 30 | | // TransactOrder сохраняет напрямую (мимо OrderRepository.Update) — без явного уведомления индекс ES |
| | | 31 | | // (по нему фильтруется список заказов по статусу) отстаёт до минутного incremental sync. |
| | 0 | 32 | | var changedIds = ctx.Orders.Select(o => o.Id) |
| | 0 | 33 | | .Concat(ctx.UpdatedOrders.Select(o => o.Id)) |
| | 0 | 34 | | .Concat(ctx.OrdersToTransaction.Select(o => o.Id)) |
| | 0 | 35 | | .Concat(ctx.DeferredOrderIds) |
| | 0 | 36 | | .Where(id => id > 0) |
| | 0 | 37 | | .Distinct() |
| | 0 | 38 | | .ToList(); |
| | 0 | 39 | | if (changedIds.Count > 0) |
| | | 40 | | { |
| | 0 | 41 | | _changeNotifier.NotifyOrdersChanged(changedIds); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | if (ctx.ChangedCount > 0) |
| | | 45 | | { |
| | 0 | 46 | | await _cacheUpdater.Update(_orderCache); |
| | | 47 | | |
| | 0 | 48 | | var statusName = string.Join(", ", ctx.OrdersToTransaction.Select(o => o.AppStatus?.Name).Where(n => !string |
| | 0 | 49 | | if (string.IsNullOrWhiteSpace(statusName)) |
| | | 50 | | { |
| | 0 | 51 | | statusName = ctx.TargetStatus?.Name ?? ctx.Definition.Title; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | var cancelCount = ctx.OrdersToTransaction.Count(o => o.AppStatus?.Name == "Отменен"); |
| | 0 | 55 | | var msg = TransactionConfig.BuildStandardSuccessMessage( |
| | 0 | 56 | | ctx.ChangedCount, cancelCount, ctx.TransactionDateTime, ctx.User.Name, ctx.UserComment, |
| | 0 | 57 | | ctx.OrdersToTransaction, statusName, ctx.SummaryBlocks.Count > 0 ? string.Join("<br/>", ctx.SummaryBlock |
| | 0 | 58 | | await NotificationService.NotifyAllAsync(msg); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | await _transactionCache.Update(); |
| | 0 | 62 | | } |
| | | 63 | | } |