| | | 1 | | using Services.BitrixStockServices; |
| | | 2 | | |
| | | 3 | | namespace Services.AiAssistant; |
| | | 4 | | |
| | | 5 | | public static class BitrixStockSyncUiMapper |
| | | 6 | | { |
| | | 7 | | public static AiBitrixSyncUiDto? Map(BitrixStockArticleHistoryPageDto? page) |
| | | 8 | | { |
| | 80 | 9 | | if (page == null || string.IsNullOrWhiteSpace(page.Article) || page.Reports.Count == 0) |
| | 19 | 10 | | return null; |
| | | 11 | | |
| | 61 | 12 | | return new AiBitrixSyncUiDto |
| | 61 | 13 | | { |
| | 61 | 14 | | Article = page.Article!, |
| | 61 | 15 | | TotalReportCount = page.TotalReportCount, |
| | 62 | 16 | | Reports = page.Reports.Select(r => new AiBitrixSyncReportUiDto |
| | 62 | 17 | | { |
| | 62 | 18 | | ReportIdShort = ShortId(r.ReportId), |
| | 62 | 19 | | GeneratedAtMsk = BitrixStockMskDateTime.Format(r.GeneratedAtUtc), |
| | 62 | 20 | | ClientName = r.IsMultiClient |
| | 62 | 21 | | ? string.Join(", ", r.MultiClientNames.Values.DefaultIfEmpty(r.OzonClientName).Distinct()) |
| | 62 | 22 | | : r.OzonClientName ?? "", |
| | 65 | 23 | | Rows = r.Rows.Select(row => MapRow(row, page.HasPrices, page.HasPushes)).ToList() |
| | 62 | 24 | | }).ToList() |
| | 61 | 25 | | }; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | private static AiBitrixSyncRowUiDto MapRow( |
| | | 29 | | BitrixStockArticleHistoryRowDto row, |
| | | 30 | | bool hasPrices, |
| | | 31 | | bool hasPushes) |
| | | 32 | | { |
| | 65 | 33 | | var w = row.Line; |
| | 65 | 34 | | var pi = row.Price; |
| | 65 | 35 | | BitrixStockSyncReportRowPresenter.FormatDeltaItog(w, out var deltaCss); |
| | | 36 | | |
| | 65 | 37 | | var ui = new AiBitrixSyncRowUiDto |
| | 65 | 38 | | { |
| | 65 | 39 | | OneCStore = w.OneCStore ?? string.Empty, |
| | 65 | 40 | | OzonWarehouse = w.OzonWarehouseName ?? string.Empty, |
| | 65 | 41 | | OneCQty = w.OneCQty, |
| | 65 | 42 | | OzonQty = w.OzonQty, |
| | 65 | 43 | | TargetQty = BitrixStockSyncReportRowPresenter.GetTargetQty(w), |
| | 65 | 44 | | OzonQtyAfter = w.OzonQtyAfter, |
| | 65 | 45 | | DeltaItog = BitrixStockSyncReportRowPresenter.FormatDeltaItog(w, out _), |
| | 65 | 46 | | DeltaItogTone = ToneFromCss(deltaCss), |
| | 65 | 47 | | ActionLabel = w.ActionLabel ?? string.Empty, |
| | 65 | 48 | | ActionIcon = w.ActionIcon ?? string.Empty, |
| | 65 | 49 | | ApplyStatus = BitrixStockSyncReportRowPresenter.FormatOzonApplyStatus(w), |
| | 65 | 50 | | ApplyTone = ApplyTone(w), |
| | 65 | 51 | | OneCPrice = w.OneCPrice, |
| | 65 | 52 | | DeltaC0 = BitrixStockSyncReportRowPresenter.FormatDeltaC0(w.OneCPrice, w.PrevOneCPrice), |
| | 65 | 53 | | DeltaC0Tone = ToneFromCss( |
| | 65 | 54 | | BitrixStockSyncReportRowPresenter.DeltaC0CssClass(w.OneCPrice, w.PrevOneCPrice)) |
| | 65 | 55 | | }; |
| | | 56 | | |
| | 65 | 57 | | if (hasPrices && pi != null) |
| | | 58 | | { |
| | 12 | 59 | | ui.OzonPrice = pi.TruePrice; |
| | 12 | 60 | | ui.SellingPrice = pi.SellingPrice; |
| | 12 | 61 | | ui.PriceIndex = MapIndex(pi.ColorIndex); |
| | | 62 | | } |
| | | 63 | | |
| | 65 | 64 | | if (hasPushes && pi != null && |
| | 65 | 65 | | (pi.PricePushedNew.HasValue || pi.PricePushedOld.HasValue || !string.IsNullOrEmpty(pi.PricePushStatus))) |
| | | 66 | | { |
| | 6 | 67 | | ui.PricePush = new AiPricePushUiDto |
| | 6 | 68 | | { |
| | 6 | 69 | | OldPrice = pi.PricePushedOld, |
| | 6 | 70 | | NewPrice = pi.PricePushedNew, |
| | 6 | 71 | | Status = pi.PricePushStatus, |
| | 6 | 72 | | Tone = ToneFromCss(BitrixStockSyncReportRowPresenter.PushedNewCssClass(pi)), |
| | 6 | 73 | | IndexBefore = MapIndex(pi.PushedColorIndexBefore), |
| | 6 | 74 | | IndexAfter = MapIndex(pi.PushedColorIndexAfter), |
| | 6 | 75 | | MarkupPct = pi.PushedMarkupPct, |
| | 6 | 76 | | PushedAtMsk = pi.PricePushedAtUtc.HasValue |
| | 6 | 77 | | ? BitrixStockMskDateTime.Format(pi.PricePushedAtUtc.Value) |
| | 6 | 78 | | : null |
| | 6 | 79 | | }; |
| | | 80 | | } |
| | | 81 | | |
| | 65 | 82 | | return ui; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private static AiPriceIndexUiDto? MapIndex(string? colorIndex) |
| | | 86 | | { |
| | 36 | 87 | | if (string.IsNullOrWhiteSpace(colorIndex)) return null; |
| | 12 | 88 | | if (!BitrixStockSyncReportRowPresenter.IndexStyles.TryGetValue(colorIndex, out var s)) |
| | 2 | 89 | | return null; |
| | | 90 | | |
| | 10 | 91 | | return new AiPriceIndexUiDto |
| | 10 | 92 | | { |
| | 10 | 93 | | Key = colorIndex, |
| | 10 | 94 | | Label = s.Label, |
| | 10 | 95 | | Bg = s.BgHex, |
| | 10 | 96 | | Fg = s.FgHex |
| | 10 | 97 | | }; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private static string ApplyTone(BitrixStockSyncReportWarehouseLineDto w) => |
| | 65 | 101 | | w.OzonApplyStatus switch |
| | 65 | 102 | | { |
| | 58 | 103 | | "Success" => "success", |
| | 3 | 104 | | "Failed" => "danger", |
| | 1 | 105 | | "Pending" => "warn", |
| | 3 | 106 | | _ => "muted" |
| | 65 | 107 | | }; |
| | | 108 | | |
| | | 109 | | private static string ToneFromCss(string css) |
| | | 110 | | { |
| | 141 | 111 | | if (css.Contains("text-success", StringComparison.Ordinal)) return "success"; |
| | 134 | 112 | | if (css.Contains("text-danger", StringComparison.Ordinal)) return "danger"; |
| | 128 | 113 | | if (css.Contains("text-warning", StringComparison.Ordinal)) return "warn"; |
| | 128 | 114 | | return "muted"; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private static string ShortId(string? id) => |
| | 62 | 118 | | string.IsNullOrEmpty(id) ? "" : id.Length > 8 ? id[..8] : id; |
| | | 119 | | } |