| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | using Microsoft.Extensions.Caching.Memory; |
| | | 7 | | using OfficeOpenXml; |
| | | 8 | | using OfficeOpenXml.Style; |
| | | 9 | | using QuestPDF.Fluent; |
| | | 10 | | using QuestPDF.Helpers; |
| | | 11 | | using QuestPDF.Infrastructure; |
| | | 12 | | using Services.ApiServices.OzonApi; |
| | | 13 | | using Services.DataServices; |
| | | 14 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.AssemblyList; |
| | | 15 | | |
| | | 16 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 17 | | |
| | | 18 | | [Authorize(Roles = "User,Admin")] |
| | | 19 | | [Area("Studio2")] |
| | | 20 | | public class AssemblyListController : Controller |
| | | 21 | | { |
| | 0 | 22 | | private static readonly JsonSerializerOptions CamelCaseOptions = new() |
| | 0 | 23 | | { |
| | 0 | 24 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 0 | 25 | | }; |
| | | 26 | | |
| | | 27 | | private readonly OzonClientServices _ozonClientServices; |
| | | 28 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 29 | | private readonly IMemoryCache _cache; |
| | | 30 | | private readonly AssemblyListRunRepository _runRepository; |
| | | 31 | | private readonly AssemblyListGenerationOrchestrator _orchestrator; |
| | | 32 | | |
| | 0 | 33 | | public AssemblyListController( |
| | 0 | 34 | | OzonClientServices ozonClientServices, |
| | 0 | 35 | | IHttpClientFactory httpClientFactory, |
| | 0 | 36 | | IMemoryCache cache, |
| | 0 | 37 | | AssemblyListRunRepository runRepository, |
| | 0 | 38 | | AssemblyListGenerationOrchestrator orchestrator) |
| | | 39 | | { |
| | 0 | 40 | | _ozonClientServices = ozonClientServices; |
| | 0 | 41 | | _httpClientFactory = httpClientFactory; |
| | 0 | 42 | | _cache = cache; |
| | 0 | 43 | | _runRepository = runRepository; |
| | 0 | 44 | | _orchestrator = orchestrator; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | [HttpGet] |
| | | 48 | | public async Task<IActionResult> Index() |
| | | 49 | | { |
| | 0 | 50 | | var clients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 51 | | var lastRun = await _runRepository.GetLastCompletedRunAsync(); |
| | 0 | 52 | | var lastRunItems = lastRun != null |
| | 0 | 53 | | ? await _runRepository.GetRunItemsAsync(lastRun.Id) |
| | 0 | 54 | | : new List<OzonDomains.Models.AssemblyListRunItem>(); |
| | | 55 | | |
| | 0 | 56 | | return View(new AssemblyListIndexViewModel |
| | 0 | 57 | | { |
| | 0 | 58 | | Clients = clients, |
| | 0 | 59 | | LastRun = lastRun, |
| | 0 | 60 | | LastRunItems = lastRunItems, |
| | 0 | 61 | | }); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | [HttpPost] |
| | | 65 | | public async Task<IActionResult> Generate([FromBody] AssemblyListFilterDto filter) |
| | | 66 | | { |
| | 0 | 67 | | if (filter.OzonClientId <= 0) |
| | 0 | 68 | | return BadRequest(new { error = "Выберите клиента" }); |
| | | 69 | | |
| | 0 | 70 | | var client = await _ozonClientServices.GetOzonClientAsync(filter.OzonClientId); |
| | 0 | 71 | | if (client?.DecryptClientId == null || client.DecryptApiKey == null) |
| | 0 | 72 | | return BadRequest(new { error = "Клиент не найден или нет API-ключей" }); |
| | | 73 | | |
| | 0 | 74 | | var req = new GenerationRequest( |
| | 0 | 75 | | OzonClientId: filter.OzonClientId, |
| | 0 | 76 | | OzonClientName: client.Name ?? client.DecryptClientId, |
| | 0 | 77 | | DecryptedClientId: client.DecryptClientId, |
| | 0 | 78 | | DecryptedApiKey: client.DecryptApiKey, |
| | 0 | 79 | | CutoffFrom: filter.CutoffFrom ?? DateTime.Today, |
| | 0 | 80 | | CutoffTo: filter.CutoffTo ?? DateTime.Today.AddDays(7), |
| | 0 | 81 | | Status: filter.Status ?? "awaiting_deliver", |
| | 0 | 82 | | WarehouseFilter: null, |
| | 0 | 83 | | PersistItemsInBackground: true); |
| | | 84 | | |
| | 0 | 85 | | var (jobId, dbRunId) = await _orchestrator.StartAsync(req); |
| | 0 | 86 | | return Ok(new { jobId, dbRunId }); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | [HttpGet] |
| | | 90 | | public async Task Progress(string jobId) |
| | | 91 | | { |
| | 0 | 92 | | Response.Headers["Content-Type"] = "text/event-stream"; |
| | 0 | 93 | | Response.Headers["Cache-Control"] = "no-cache"; |
| | 0 | 94 | | Response.Headers["Connection"] = "keep-alive"; |
| | | 95 | | |
| | 0 | 96 | | var timeout = DateTime.UtcNow.AddMinutes(10); |
| | | 97 | | |
| | 0 | 98 | | ConcurrentQueue<AssemblyListProgress>? queue = null; |
| | 0 | 99 | | for (var attempt = 0; attempt < 10 && queue == null; attempt++) |
| | | 100 | | { |
| | 0 | 101 | | queue = _cache.Get<ConcurrentQueue<AssemblyListProgress>>($"al_progress_{jobId}"); |
| | 0 | 102 | | if (queue == null) |
| | 0 | 103 | | await Task.Delay(500); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | if (queue == null) |
| | | 107 | | { |
| | 0 | 108 | | await WriteSSE(Response, "progress", |
| | 0 | 109 | | JsonSerializer.Serialize(new AssemblyListProgress { IsError = true, ErrorMessage = "Job not found" }, Ca |
| | 0 | 110 | | await Response.Body.FlushAsync(); |
| | 0 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | while (DateTime.UtcNow < timeout) |
| | | 115 | | { |
| | 0 | 116 | | while (queue.TryDequeue(out var prog)) |
| | | 117 | | { |
| | 0 | 118 | | var json = JsonSerializer.Serialize(prog, CamelCaseOptions); |
| | 0 | 119 | | await WriteSSE(Response, "progress", json); |
| | 0 | 120 | | await Response.Body.FlushAsync(); |
| | | 121 | | |
| | 0 | 122 | | if (prog.IsComplete || prog.IsError) |
| | 0 | 123 | | return; |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | await Task.Delay(400); |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | [HttpGet] |
| | | 131 | | public IActionResult Result(string jobId) |
| | | 132 | | { |
| | 0 | 133 | | var result = _cache.Get<AssemblyListResult>($"al_result_{jobId}"); |
| | 0 | 134 | | if (result == null) |
| | 0 | 135 | | return NotFound(new { error = "Данные не найдены или истёк срок хранения" }); |
| | | 136 | | |
| | 0 | 137 | | return Content(JsonSerializer.Serialize(result, CamelCaseOptions), "application/json"); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | [HttpGet] |
| | | 141 | | public IActionResult DownloadExcel(string jobId) |
| | | 142 | | { |
| | 0 | 143 | | var result = _cache.Get<AssemblyListResult>($"al_result_{jobId}"); |
| | 0 | 144 | | if (result == null) |
| | 0 | 145 | | return NotFound("Данные не найдены или истёк срок хранения"); |
| | | 146 | | |
| | 0 | 147 | | ExcelPackage.LicenseContext = LicenseContext.NonCommercial; |
| | 0 | 148 | | using var package = new ExcelPackage(); |
| | 0 | 149 | | var ws = package.Workbook.Worksheets.Add("Лист подбора"); |
| | | 150 | | |
| | 0 | 151 | | var headers = new[] { "№", "Отправление", "Артикул", "Название", "Склад", "Статус", "Кол-во", "Дедлайн", "Постав |
| | 0 | 152 | | for (var col = 1; col <= headers.Length; col++) |
| | | 153 | | { |
| | 0 | 154 | | ws.Cells[1, col].Value = headers[col - 1]; |
| | 0 | 155 | | ws.Cells[1, col].Style.Font.Bold = true; |
| | 0 | 156 | | ws.Cells[1, col].Style.Fill.PatternType = ExcelFillStyle.Solid; |
| | 0 | 157 | | ws.Cells[1, col].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(70, 130, 180)); |
| | 0 | 158 | | ws.Cells[1, col].Style.Font.Color.SetColor(System.Drawing.Color.White); |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | var row = 2; |
| | 0 | 162 | | foreach (var item in result.Items) |
| | | 163 | | { |
| | 0 | 164 | | ws.Cells[row, 1].Value = item.Index; |
| | 0 | 165 | | ws.Cells[row, 2].Value = item.PostingNumber; |
| | 0 | 166 | | ws.Cells[row, 3].Value = item.OfferId; |
| | 0 | 167 | | ws.Cells[row, 4].Value = item.ProductName; |
| | 0 | 168 | | ws.Cells[row, 5].Value = item.WarehouseName; |
| | 0 | 169 | | ws.Cells[row, 6].Value = item.StatusLabel; |
| | 0 | 170 | | ws.Cells[row, 7].Value = item.Quantity; |
| | 0 | 171 | | ws.Cells[row, 8].Value = item.CutoffDate?.ToString("dd.MM.yyyy HH:mm"); |
| | 0 | 172 | | ws.Cells[row, 9].Value = item.SupplierName ?? "—"; |
| | 0 | 173 | | ws.Cells[row, 10].Value = item.OrderedToSupplierDate?.ToString("dd.MM.yyyy HH:mm") ?? "—"; |
| | 0 | 174 | | ws.Cells[row, 11].Value = item.OneCBalance.HasValue ? (object)item.OneCBalance.Value : "—"; |
| | 0 | 175 | | ws.Cells[row, 12].Value = item.OneCReceiptDoc ?? "—"; |
| | 0 | 176 | | ws.Cells[row, 13].Value = item.LabelUrl ?? item.LabelError ?? "—"; |
| | 0 | 177 | | row++; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | ws.Cells[ws.Dimension.Address].AutoFitColumns(); |
| | | 181 | | |
| | 0 | 182 | | var bytes = package.GetAsByteArray(); |
| | 0 | 183 | | var fileName = $"assembly_list_{result.GeneratedAt:yyyyMMdd_HHmm}.xlsx"; |
| | 0 | 184 | | return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName); |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | [HttpGet] |
| | | 188 | | public async Task<IActionResult> DownloadPdf(string jobId) |
| | | 189 | | { |
| | 0 | 190 | | var result = _cache.Get<AssemblyListResult>($"al_result_{jobId}"); |
| | 0 | 191 | | if (result == null) |
| | 0 | 192 | | return NotFound("Данные не найдены или истёк срок хранения"); |
| | | 193 | | |
| | 0 | 194 | | QuestPDF.Settings.License = LicenseType.Community; |
| | | 195 | | |
| | 0 | 196 | | var imageCache = new Dictionary<string, byte[]?>(); |
| | 0 | 197 | | var httpClient = _httpClientFactory.CreateClient(); |
| | 0 | 198 | | foreach (var item in result.Items.Where(i => !string.IsNullOrEmpty(i.ImageUrl))) |
| | | 199 | | { |
| | 0 | 200 | | var url = item.ImageUrl!; |
| | 0 | 201 | | if (!imageCache.ContainsKey(url)) |
| | | 202 | | { |
| | 0 | 203 | | try { imageCache[url] = await httpClient.GetByteArrayAsync(url); } |
| | 0 | 204 | | catch { imageCache[url] = null; } |
| | | 205 | | } |
| | 0 | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | var pdfBytes = Document.Create(container => |
| | 0 | 209 | | { |
| | 0 | 210 | | container.Page(page => |
| | 0 | 211 | | { |
| | 0 | 212 | | page.Size(PageSizes.A4.Landscape()); |
| | 0 | 213 | | page.Margin(15, Unit.Millimetre); |
| | 0 | 214 | | page.DefaultTextStyle(x => x.FontSize(8).FontFamily("Arial")); |
| | 0 | 215 | | |
| | 0 | 216 | | page.Header().Column(col => |
| | 0 | 217 | | { |
| | 0 | 218 | | col.Item().Row(row => |
| | 0 | 219 | | { |
| | 0 | 220 | | row.RelativeItem().Text("Лист подбора FBS").FontSize(14).Bold(); |
| | 0 | 221 | | row.ConstantItem(200).AlignRight() |
| | 0 | 222 | | .Text($"Сформирован: {result.GeneratedAt:dd.MM.yyyy HH:mm}") |
| | 0 | 223 | | .FontSize(8).FontColor("#9E9E9E"); |
| | 0 | 224 | | }); |
| | 0 | 225 | | col.Item().PaddingTop(4).LineHorizontal(0.5f).LineColor("#E0E0E0"); |
| | 0 | 226 | | }); |
| | 0 | 227 | | |
| | 0 | 228 | | page.Content().PaddingTop(8).Table(table => |
| | 0 | 229 | | { |
| | 0 | 230 | | table.ColumnsDefinition(cols => |
| | 0 | 231 | | { |
| | 0 | 232 | | cols.ConstantColumn(22); |
| | 0 | 233 | | cols.ConstantColumn(35); |
| | 0 | 234 | | cols.RelativeColumn(1.2f); |
| | 0 | 235 | | cols.RelativeColumn(1.2f); |
| | 0 | 236 | | cols.RelativeColumn(3); |
| | 0 | 237 | | cols.RelativeColumn(1.5f); |
| | 0 | 238 | | cols.RelativeColumn(1.2f); |
| | 0 | 239 | | cols.ConstantColumn(30); |
| | 0 | 240 | | cols.RelativeColumn(1); |
| | 0 | 241 | | }); |
| | 0 | 242 | | |
| | 0 | 243 | | table.Header(h => |
| | 0 | 244 | | { |
| | 0 | 245 | | foreach (var hdr in new[] { "№", "Фото", "Отправление", "Артикул", "Название", "Склад", "Статус" |
| | 0 | 246 | | h.Cell().Background("#1565C0").Padding(4).AlignCenter() |
| | 0 | 247 | | .Text(hdr).FontColor("#FFFFFF").Bold().FontSize(7); |
| | 0 | 248 | | }); |
| | 0 | 249 | | |
| | 0 | 250 | | var rowIdx = 0; |
| | 0 | 251 | | foreach (var item in result.Items) |
| | 0 | 252 | | { |
| | 0 | 253 | | rowIdx++; |
| | 0 | 254 | | var bg = rowIdx % 2 == 0 ? "#F5F5F5" : "#FFFFFF"; |
| | 0 | 255 | | |
| | 0 | 256 | | IContainer Cell(IContainer c) => |
| | 0 | 257 | | c.Background(bg).BorderBottom(0.3f).BorderColor("#E0E0E0").Padding(3).AlignMiddle(); |
| | 0 | 258 | | |
| | 0 | 259 | | table.Cell().Element(Cell).Text(item.Index.ToString()).AlignCenter().FontSize(7); |
| | 0 | 260 | | |
| | 0 | 261 | | var imgCell = table.Cell().Element(Cell).AlignCenter(); |
| | 0 | 262 | | if (!string.IsNullOrEmpty(item.ImageUrl) |
| | 0 | 263 | | && imageCache.TryGetValue(item.ImageUrl, out var imgBytes) |
| | 0 | 264 | | && imgBytes != null) |
| | 0 | 265 | | imgCell.Width(30).Height(30).Image(imgBytes).FitArea(); |
| | 0 | 266 | | else |
| | 0 | 267 | | imgCell.Text("—").FontColor("#9E9E9E").FontSize(7); |
| | 0 | 268 | | |
| | 0 | 269 | | table.Cell().Element(Cell).Text(item.PostingNumber).FontSize(7); |
| | 0 | 270 | | table.Cell().Element(Cell).Text(item.OfferId).FontSize(7); |
| | 0 | 271 | | table.Cell().Element(Cell).Text(item.ProductName).FontSize(7); |
| | 0 | 272 | | table.Cell().Element(Cell).Text(item.WarehouseName).FontSize(7); |
| | 0 | 273 | | table.Cell().Element(Cell).Text(item.StatusLabel).FontSize(7); |
| | 0 | 274 | | table.Cell().Element(Cell).Text(item.Quantity.ToString()).AlignCenter().Bold(); |
| | 0 | 275 | | table.Cell().Element(Cell).Text(item.CutoffDate?.ToString("dd.MM HH:mm") ?? "—").FontSize(7); |
| | 0 | 276 | | } |
| | 0 | 277 | | }); |
| | 0 | 278 | | |
| | 0 | 279 | | page.Footer().AlignCenter().Text(t => |
| | 0 | 280 | | { |
| | 0 | 281 | | t.Span("Страница ").FontSize(7); |
| | 0 | 282 | | t.CurrentPageNumber().FontSize(7); |
| | 0 | 283 | | t.Span(" из ").FontSize(7); |
| | 0 | 284 | | t.TotalPages().FontSize(7); |
| | 0 | 285 | | }); |
| | 0 | 286 | | }); |
| | 0 | 287 | | }).GeneratePdf(); |
| | | 288 | | |
| | 0 | 289 | | return File(pdfBytes, "application/pdf", $"assembly_list_{result.GeneratedAt:yyyyMMdd_HHmm}.pdf"); |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | [HttpGet] |
| | | 293 | | public async Task<IActionResult> History() |
| | | 294 | | { |
| | 0 | 295 | | var runs = await _runRepository.GetRecentRunsAsync(100); |
| | 0 | 296 | | return View(runs); |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | [HttpGet] |
| | | 300 | | public async Task<IActionResult> HistoryDetail(long id) |
| | | 301 | | { |
| | 0 | 302 | | var run = await _runRepository.GetRunAsync(id); |
| | 0 | 303 | | if (run == null) return NotFound(); |
| | 0 | 304 | | var items = await _runRepository.GetRunItemsAsync(id); |
| | 0 | 305 | | ViewBag.RunId = id; |
| | 0 | 306 | | ViewBag.Run = run; |
| | 0 | 307 | | return View(items); |
| | 0 | 308 | | } |
| | | 309 | | |
| | | 310 | | [HttpGet] |
| | | 311 | | public async Task<IActionResult> DownloadHistoryExcel(long runId) |
| | | 312 | | { |
| | 0 | 313 | | var items = await _runRepository.GetRunItemsAsync(runId); |
| | 0 | 314 | | if (items.Count == 0) |
| | 0 | 315 | | return NotFound("Данные не найдены"); |
| | | 316 | | |
| | 0 | 317 | | ExcelPackage.LicenseContext = LicenseContext.NonCommercial; |
| | 0 | 318 | | using var package = new ExcelPackage(); |
| | 0 | 319 | | var ws = package.Workbook.Worksheets.Add("Лист подбора"); |
| | | 320 | | |
| | 0 | 321 | | var headers = new[] { "№", "Отправление", "Артикул", "Название", "Склад", "Статус", "Кол-во", "Дедлайн", "Постав |
| | 0 | 322 | | for (var col = 1; col <= headers.Length; col++) |
| | | 323 | | { |
| | 0 | 324 | | ws.Cells[1, col].Value = headers[col - 1]; |
| | 0 | 325 | | ws.Cells[1, col].Style.Font.Bold = true; |
| | 0 | 326 | | ws.Cells[1, col].Style.Fill.PatternType = ExcelFillStyle.Solid; |
| | 0 | 327 | | ws.Cells[1, col].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(70, 130, 180)); |
| | 0 | 328 | | ws.Cells[1, col].Style.Font.Color.SetColor(System.Drawing.Color.White); |
| | | 329 | | } |
| | | 330 | | |
| | 0 | 331 | | var row = 2; |
| | 0 | 332 | | foreach (var item in items) |
| | | 333 | | { |
| | 0 | 334 | | ws.Cells[row, 1].Value = item.ItemIndex; |
| | 0 | 335 | | ws.Cells[row, 2].Value = item.PostingNumber; |
| | 0 | 336 | | ws.Cells[row, 3].Value = item.OfferId; |
| | 0 | 337 | | ws.Cells[row, 4].Value = item.ProductName; |
| | 0 | 338 | | ws.Cells[row, 5].Value = item.WarehouseName; |
| | 0 | 339 | | ws.Cells[row, 6].Value = item.StatusLabel; |
| | 0 | 340 | | ws.Cells[row, 7].Value = item.Quantity; |
| | 0 | 341 | | ws.Cells[row, 8].Value = item.CutoffDate?.ToString("dd.MM.yyyy HH:mm"); |
| | 0 | 342 | | ws.Cells[row, 9].Value = item.SupplierName ?? "—"; |
| | 0 | 343 | | ws.Cells[row, 10].Value = item.OrderedToSupplierDate?.ToString("dd.MM.yyyy HH:mm") ?? "—"; |
| | 0 | 344 | | ws.Cells[row, 11].Value = item.OneCBalance.HasValue ? (object)item.OneCBalance.Value : "—"; |
| | 0 | 345 | | ws.Cells[row, 12].Value = item.OneCReceiptDoc ?? "—"; |
| | 0 | 346 | | ws.Cells[row, 13].Value = item.LabelUrl ?? item.LabelError ?? "—"; |
| | 0 | 347 | | row++; |
| | | 348 | | } |
| | 0 | 349 | | ws.Cells[ws.Dimension.Address].AutoFitColumns(); |
| | | 350 | | |
| | 0 | 351 | | return File(package.GetAsByteArray(), |
| | 0 | 352 | | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| | 0 | 353 | | $"assembly_list_run{runId}.xlsx"); |
| | 0 | 354 | | } |
| | | 355 | | |
| | | 356 | | [HttpGet] |
| | | 357 | | public async Task<IActionResult> DownloadHistoryPdf(long runId) |
| | | 358 | | { |
| | 0 | 359 | | var run = await _runRepository.GetRunAsync(runId); |
| | 0 | 360 | | var items = await _runRepository.GetRunItemsAsync(runId); |
| | 0 | 361 | | if (items.Count == 0) |
| | 0 | 362 | | return NotFound("Данные не найдены"); |
| | | 363 | | |
| | 0 | 364 | | QuestPDF.Settings.License = LicenseType.Community; |
| | | 365 | | |
| | 0 | 366 | | var imageCache = new Dictionary<string, byte[]?>(); |
| | 0 | 367 | | var httpClient = _httpClientFactory.CreateClient(); |
| | 0 | 368 | | foreach (var item in items.Where(i => !string.IsNullOrEmpty(i.ImageUrl))) |
| | | 369 | | { |
| | 0 | 370 | | var url = item.ImageUrl!; |
| | 0 | 371 | | if (!imageCache.ContainsKey(url)) |
| | | 372 | | { |
| | 0 | 373 | | try { imageCache[url] = await httpClient.GetByteArrayAsync(url); } |
| | 0 | 374 | | catch { imageCache[url] = null; } |
| | | 375 | | } |
| | 0 | 376 | | } |
| | | 377 | | |
| | 0 | 378 | | var generatedAt = run?.StartedAtUtc.ToLocalTime() ?? DateTime.Now; |
| | 0 | 379 | | var pdfBytes = Document.Create(container => |
| | 0 | 380 | | { |
| | 0 | 381 | | container.Page(page => |
| | 0 | 382 | | { |
| | 0 | 383 | | page.Size(PageSizes.A4.Landscape()); |
| | 0 | 384 | | page.Margin(15, Unit.Millimetre); |
| | 0 | 385 | | page.DefaultTextStyle(x => x.FontSize(8).FontFamily("Arial")); |
| | 0 | 386 | | |
| | 0 | 387 | | page.Header().Column(col => |
| | 0 | 388 | | { |
| | 0 | 389 | | col.Item().Row(row => |
| | 0 | 390 | | { |
| | 0 | 391 | | row.RelativeItem().Text("Лист подбора FBS").FontSize(14).Bold(); |
| | 0 | 392 | | row.ConstantItem(200).AlignRight() |
| | 0 | 393 | | .Text($"Сформирован: {generatedAt:dd.MM.yyyy HH:mm}") |
| | 0 | 394 | | .FontSize(8).FontColor("#9E9E9E"); |
| | 0 | 395 | | }); |
| | 0 | 396 | | col.Item().PaddingTop(4).LineHorizontal(0.5f).LineColor("#E0E0E0"); |
| | 0 | 397 | | }); |
| | 0 | 398 | | |
| | 0 | 399 | | page.Content().PaddingTop(8).Table(table => |
| | 0 | 400 | | { |
| | 0 | 401 | | table.ColumnsDefinition(cols => |
| | 0 | 402 | | { |
| | 0 | 403 | | cols.ConstantColumn(22); |
| | 0 | 404 | | cols.ConstantColumn(35); |
| | 0 | 405 | | cols.RelativeColumn(1.2f); |
| | 0 | 406 | | cols.RelativeColumn(1.2f); |
| | 0 | 407 | | cols.RelativeColumn(3); |
| | 0 | 408 | | cols.RelativeColumn(1.5f); |
| | 0 | 409 | | cols.RelativeColumn(1.2f); |
| | 0 | 410 | | cols.ConstantColumn(30); |
| | 0 | 411 | | cols.RelativeColumn(1); |
| | 0 | 412 | | }); |
| | 0 | 413 | | |
| | 0 | 414 | | table.Header(h => |
| | 0 | 415 | | { |
| | 0 | 416 | | foreach (var hdr in new[] { "№", "Фото", "Отправление", "Артикул", "Название", "Склад", "Статус" |
| | 0 | 417 | | h.Cell().Background("#1565C0").Padding(4).AlignCenter() |
| | 0 | 418 | | .Text(hdr).FontColor("#FFFFFF").Bold().FontSize(7); |
| | 0 | 419 | | }); |
| | 0 | 420 | | |
| | 0 | 421 | | var rowIdx = 0; |
| | 0 | 422 | | foreach (var item in items) |
| | 0 | 423 | | { |
| | 0 | 424 | | rowIdx++; |
| | 0 | 425 | | var bg = rowIdx % 2 == 0 ? "#F5F5F5" : "#FFFFFF"; |
| | 0 | 426 | | |
| | 0 | 427 | | IContainer Cell(IContainer c) => |
| | 0 | 428 | | c.Background(bg).BorderBottom(0.3f).BorderColor("#E0E0E0").Padding(3).AlignMiddle(); |
| | 0 | 429 | | |
| | 0 | 430 | | table.Cell().Element(Cell).Text(item.ItemIndex.ToString()).AlignCenter().FontSize(7); |
| | 0 | 431 | | |
| | 0 | 432 | | var imgCell = table.Cell().Element(Cell).AlignCenter(); |
| | 0 | 433 | | if (!string.IsNullOrEmpty(item.ImageUrl) |
| | 0 | 434 | | && imageCache.TryGetValue(item.ImageUrl, out var imgBytes) |
| | 0 | 435 | | && imgBytes != null) |
| | 0 | 436 | | imgCell.Width(30).Height(30).Image(imgBytes).FitArea(); |
| | 0 | 437 | | else |
| | 0 | 438 | | imgCell.Text("—").FontColor("#9E9E9E").FontSize(7); |
| | 0 | 439 | | |
| | 0 | 440 | | table.Cell().Element(Cell).Text(item.PostingNumber).FontSize(7); |
| | 0 | 441 | | table.Cell().Element(Cell).Text(item.OfferId).FontSize(7); |
| | 0 | 442 | | table.Cell().Element(Cell).Text(item.ProductName).FontSize(7); |
| | 0 | 443 | | table.Cell().Element(Cell).Text(item.WarehouseName).FontSize(7); |
| | 0 | 444 | | table.Cell().Element(Cell).Text(item.StatusLabel).FontSize(7); |
| | 0 | 445 | | table.Cell().Element(Cell).Text(item.Quantity.ToString()).AlignCenter().Bold(); |
| | 0 | 446 | | table.Cell().Element(Cell).Text(item.CutoffDate?.ToLocalTime().ToString("dd.MM HH:mm") ?? "—").F |
| | 0 | 447 | | } |
| | 0 | 448 | | }); |
| | 0 | 449 | | |
| | 0 | 450 | | page.Footer().AlignCenter().Text(t => |
| | 0 | 451 | | { |
| | 0 | 452 | | t.Span("Страница ").FontSize(7); |
| | 0 | 453 | | t.CurrentPageNumber().FontSize(7); |
| | 0 | 454 | | t.Span(" из ").FontSize(7); |
| | 0 | 455 | | t.TotalPages().FontSize(7); |
| | 0 | 456 | | }); |
| | 0 | 457 | | }); |
| | 0 | 458 | | }).GeneratePdf(); |
| | | 459 | | |
| | 0 | 460 | | return File(pdfBytes, "application/pdf", $"assembly_list_run{runId}.pdf"); |
| | 0 | 461 | | } |
| | | 462 | | |
| | | 463 | | [HttpGet] |
| | | 464 | | public async Task<IActionResult> LabelProxy(string url) |
| | | 465 | | { |
| | 0 | 466 | | if (string.IsNullOrWhiteSpace(url) || !Uri.TryCreate(url, UriKind.Absolute, out var uri)) |
| | 0 | 467 | | return BadRequest(); |
| | | 468 | | |
| | 0 | 469 | | var host = uri.Host; |
| | 0 | 470 | | if (!host.EndsWith(".ozon.ru", StringComparison.OrdinalIgnoreCase) |
| | 0 | 471 | | && !host.EndsWith(".ozone.ru", StringComparison.OrdinalIgnoreCase) |
| | 0 | 472 | | && !host.EndsWith(".ozonusercontent.com", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 473 | | return BadRequest(); |
| | | 474 | | |
| | | 475 | | try |
| | | 476 | | { |
| | 0 | 477 | | var bytes = await _httpClientFactory.CreateClient().GetByteArrayAsync(url); |
| | 0 | 478 | | var fileName = Path.GetFileName(uri.LocalPath); |
| | 0 | 479 | | Response.Headers["Content-Disposition"] = $"attachment; filename=\"{fileName}\""; |
| | 0 | 480 | | return File(bytes, "application/pdf"); |
| | | 481 | | } |
| | 0 | 482 | | catch |
| | | 483 | | { |
| | 0 | 484 | | return NotFound(); |
| | | 485 | | } |
| | 0 | 486 | | } |
| | | 487 | | |
| | | 488 | | public async Task<IActionResult> ImageProxy(string url) |
| | | 489 | | { |
| | 0 | 490 | | if (string.IsNullOrWhiteSpace(url) || !Uri.TryCreate(url, UriKind.Absolute, out var uri)) |
| | 0 | 491 | | return BadRequest(); |
| | | 492 | | |
| | 0 | 493 | | var host = uri.Host; |
| | 0 | 494 | | if (!host.EndsWith(".ozon.ru", StringComparison.OrdinalIgnoreCase) |
| | 0 | 495 | | && !host.EndsWith(".ozone.ru", StringComparison.OrdinalIgnoreCase) |
| | 0 | 496 | | && !host.EndsWith(".ozonusercontent.com", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 497 | | return BadRequest(); |
| | | 498 | | |
| | | 499 | | try |
| | | 500 | | { |
| | 0 | 501 | | var bytes = await _httpClientFactory.CreateClient().GetByteArrayAsync(url); |
| | 0 | 502 | | return File(bytes, "image/jpeg"); |
| | | 503 | | } |
| | 0 | 504 | | catch |
| | | 505 | | { |
| | 0 | 506 | | return NotFound(); |
| | | 507 | | } |
| | 0 | 508 | | } |
| | | 509 | | |
| | | 510 | | private static async Task WriteSSE(HttpResponse response, string eventName, string data) |
| | | 511 | | { |
| | 0 | 512 | | var sb = new StringBuilder(); |
| | 0 | 513 | | sb.AppendLine($"event: {eventName}"); |
| | 0 | 514 | | sb.AppendLine($"data: {data}"); |
| | 0 | 515 | | sb.AppendLine(); |
| | 0 | 516 | | await response.WriteAsync(sb.ToString()); |
| | 0 | 517 | | } |
| | | 518 | | } |