| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using OzonDomains; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonOrdersWeb.Areas.Studio2.Helpers; |
| | | 6 | | using OzonRepositories.Context; |
| | | 7 | | using Services.ApiServices._1CApi.Models; |
| | | 8 | | using Services.TransactionCommentAi; |
| | | 9 | | using Services.TransactionUtilsServices; |
| | | 10 | | |
| | | 11 | | namespace OzonOrdersWeb.TransactionServices; |
| | | 12 | | |
| | | 13 | | public readonly struct TransactionCommentAiFlowState |
| | | 14 | | { |
| | | 15 | | public string UserCommentOnly { get; init; } |
| | | 16 | | public bool ScheduleAiComment { get; init; } |
| | | 17 | | public TransactionCommentAiContextBuilder? Builder { get; init; } |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | public static class TransactionCommentAiSchedulingHelper |
| | | 21 | | { |
| | | 22 | | public static TransactionCommentAiFlowState BeginFlow( |
| | | 23 | | string? comment, |
| | | 24 | | TransactionType transactionType, |
| | | 25 | | string userName, |
| | | 26 | | bool processIn1C, |
| | | 27 | | bool shipInOzonRequested = false) |
| | | 28 | | { |
| | 27 | 29 | | var userCommentOnly = TransactionCommentNormalizer.NormalizeUserComment(comment); |
| | 27 | 30 | | var scheduleAiComment = string.IsNullOrEmpty(userCommentOnly); |
| | 27 | 31 | | TransactionCommentAiContextBuilder? builder = null; |
| | 27 | 32 | | if (scheduleAiComment) |
| | | 33 | | { |
| | 27 | 34 | | builder = new TransactionCommentAiContextBuilder(transactionType, userName, DateTime.UtcNow) |
| | 27 | 35 | | .WithFlags(processIn1C, shipInOzonRequested); |
| | | 36 | | } |
| | | 37 | | |
| | 27 | 38 | | return new TransactionCommentAiFlowState |
| | 27 | 39 | | { |
| | 27 | 40 | | UserCommentOnly = userCommentOnly, |
| | 27 | 41 | | ScheduleAiComment = scheduleAiComment, |
| | 27 | 42 | | Builder = builder |
| | 27 | 43 | | }; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public static string ResolveDatabaseComment(TransactionCommentAiFlowState flow, string commentWithTechnical) |
| | | 47 | | { |
| | 0 | 48 | | if (flow.ScheduleAiComment) |
| | 0 | 49 | | return TransactionCommentAiConstants.DefaultPlaceholder; |
| | | 50 | | |
| | 0 | 51 | | return commentWithTechnical; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public static void AppendOneCCommentDetails( |
| | | 55 | | TransactionCommentAiFlowState flow, |
| | | 56 | | ref string comment, |
| | | 57 | | List<Order> orders, |
| | | 58 | | List<OneCResponse> transferResult, |
| | | 59 | | string? warehouseSender = null, |
| | | 60 | | string? warehouseRecipient = null) |
| | | 61 | | { |
| | 0 | 62 | | if (transferResult.Count == 0) |
| | 0 | 63 | | return; |
| | | 64 | | |
| | 0 | 65 | | if (flow.ScheduleAiComment && flow.Builder != null) |
| | | 66 | | { |
| | 0 | 67 | | flow.Builder.WithOneCResults(transferResult, warehouseSender, warehouseRecipient); |
| | 0 | 68 | | return; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | comment += OneCTransactionSummaryHelper.BuildCommentDetails( |
| | 0 | 72 | | orders, |
| | 0 | 73 | | transferResult, |
| | 0 | 74 | | warehouseSender, |
| | 0 | 75 | | warehouseRecipient); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public static string ResolveCommentForDatabase(string userCommentOnly, bool scheduleAiComment, string? appendedTechn |
| | | 79 | | { |
| | 0 | 80 | | if (scheduleAiComment) |
| | 0 | 81 | | return TransactionCommentAiConstants.DefaultPlaceholder; |
| | | 82 | | |
| | 0 | 83 | | return string.IsNullOrEmpty(appendedTechnicalComment) |
| | 0 | 84 | | ? userCommentOnly |
| | 0 | 85 | | : userCommentOnly + appendedTechnicalComment; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public static async Task<Dictionary<int, string>> LoadStatusNamesAsync( |
| | | 89 | | ApplicationDbContext context, |
| | | 90 | | IReadOnlyDictionary<int, int> fromStatusByOrderId, |
| | | 91 | | CancellationToken ct = default) |
| | | 92 | | { |
| | 0 | 93 | | var statusIds = fromStatusByOrderId.Values.Distinct().ToList(); |
| | 0 | 94 | | if (statusIds.Count == 0) |
| | 0 | 95 | | return new Dictionary<int, string>(); |
| | | 96 | | |
| | 0 | 97 | | return await context.AppStatuses.AsNoTracking() |
| | 0 | 98 | | .Where(s => statusIds.Contains(s.Id)) |
| | 0 | 99 | | .ToDictionaryAsync(s => s.Id, s => s.Name ?? string.Empty, ct); |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public static async Task TryScheduleAsync( |
| | | 103 | | IServiceProvider services, |
| | | 104 | | TransactionCommentAiContextBuilder builder, |
| | | 105 | | bool scheduleAiComment, |
| | | 106 | | int transactionId, |
| | | 107 | | IReadOnlyList<Order> orders, |
| | | 108 | | IReadOnlyDictionary<int, int> fromStatusByOrderId, |
| | | 109 | | ApplicationDbContext context, |
| | | 110 | | IReadOnlyDictionary<int, List<string>>? changedFieldsByOrder = null, |
| | | 111 | | CancellationToken ct = default) |
| | | 112 | | { |
| | 0 | 113 | | if (!scheduleAiComment || transactionId <= 0) |
| | 0 | 114 | | return; |
| | | 115 | | |
| | 0 | 116 | | var enqueuer = services.GetService(typeof(ITransactionCommentAiEnqueuer)) as ITransactionCommentAiEnqueuer; |
| | 0 | 117 | | if (enqueuer == null) |
| | 0 | 118 | | return; |
| | | 119 | | |
| | 0 | 120 | | var fromStatusNameById = await LoadStatusNamesAsync(context, fromStatusByOrderId, ct); |
| | | 121 | | |
| | 0 | 122 | | builder |
| | 0 | 123 | | .WithTransactionId(transactionId) |
| | 0 | 124 | | .WithUserComment(string.Empty) |
| | 0 | 125 | | .WithOrders(orders, fromStatusByOrderId, fromStatusNameById, changedFieldsByOrder); |
| | | 126 | | |
| | 0 | 127 | | enqueuer.TryEnqueue(builder.Build()); |
| | 0 | 128 | | } |
| | | 129 | | } |