| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using OzonDomains.Models.Authorization.Enums; |
| | | 7 | | using OzonOrdersWeb.Authorization; |
| | | 8 | | using OzonOrdersWeb.WebServices.Grid; |
| | | 9 | | using OzonOrdersWeb.WebServices.Journals; |
| | | 10 | | using OzonRepositories.Context; |
| | | 11 | | using Services.DataServices; |
| | | 12 | | |
| | | 13 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers |
| | | 14 | | { |
| | | 15 | | [Area("Studio2")] |
| | | 16 | | [Authorize(Roles = "User,Admin")] |
| | | 17 | | public class OrderArchiveController : Controller |
| | | 18 | | { |
| | | 19 | | private readonly OrderArchiveDataService _orderArchiveService; |
| | | 20 | | private readonly ApplicationDbContext _context; |
| | | 21 | | |
| | 0 | 22 | | public OrderArchiveController(OrderArchiveDataService orderArchiveService, ApplicationDbContext context) |
| | | 23 | | { |
| | 0 | 24 | | _orderArchiveService = orderArchiveService; |
| | 0 | 25 | | _context = context; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | private const string PageCode = "Studio2.OrderArchive.Index"; |
| | | 29 | | |
| | | 30 | | /// <summary>Каркас страницы: строки архива грид подгружает блоками через <see cref="GridRows"/>.</summary> |
| | | 31 | | public async Task<IActionResult> Index(CancellationToken cancellationToken) |
| | | 32 | | { |
| | 0 | 33 | | ViewBag.TotalCount = await _context.OrderArchives.CountAsync(cancellationToken); |
| | 0 | 34 | | return View(); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | [HttpPost("Studio2/OrderArchive/grid")] |
| | | 38 | | [ValidateAntiForgeryToken] |
| | | 39 | | [PageAuthorize(PageCode, AccessType.View)] |
| | | 40 | | public async Task<IActionResult> GridRows([FromBody] ServerGridRequest? request, CancellationToken cancellationT |
| | | 41 | | { |
| | 0 | 42 | | var error = ServerGridQuery.Validate(request); |
| | 0 | 43 | | if (error != null) return BadRequest(new { success = false, error }); |
| | | 44 | | |
| | 0 | 45 | | var def = JournalGridDefinitions.OrderArchive; |
| | 0 | 46 | | var filtered = ServerGridQuery.ApplyFilters(_context.OrderArchives.AsNoTracking(), def, request!); |
| | 0 | 47 | | var total = await filtered.CountAsync(cancellationToken); |
| | 0 | 48 | | var start = Math.Max(0, request!.StartRow); |
| | 0 | 49 | | var take = Math.Clamp(request.EndRow - start, 1, ServerGridQuery.MaxBlockSize); |
| | 0 | 50 | | var page = start >= total |
| | 0 | 51 | | ? [] |
| | 0 | 52 | | : await ServerGridQuery.ApplySort(filtered, def, request.SortModel).Skip(start).Take(take) |
| | 0 | 53 | | .Select(a => new |
| | 0 | 54 | | { |
| | 0 | 55 | | a.Id, a.OrderId, a.ArchivedAt, a.ArchivedBy, a.ArchiveReason, |
| | 0 | 56 | | a.Order.ShipmentNumber, a.Order.ProcessingDate, a.Order.ProductName, a.Order.Article, |
| | 0 | 57 | | a.Order.Quantity, a.Order.ProductKey, a.Order.PurchasePrice, |
| | 0 | 58 | | AppStatus = a.Order.AppStatus != null ? a.Order.AppStatus.Name : null, |
| | 0 | 59 | | ClientStatus = a.Order.ClientStatus != null ? a.Order.ClientStatus.Name : null, |
| | 0 | 60 | | ClientName = a.Order.OzonClient != null ? a.Order.OzonClient.Name : null, |
| | 0 | 61 | | ImageUrl = a.Order.ProductInfo != null ? a.Order.ProductInfo.ImageUrl : null |
| | 0 | 62 | | }) |
| | 0 | 63 | | .ToListAsync(cancellationToken); |
| | | 64 | | // Количество — с кратностью упаковки из ключа товара, как «Истинное количество» в заказах. |
| | 0 | 65 | | var rows = page.Select(a => new |
| | 0 | 66 | | { |
| | 0 | 67 | | a.Id, a.OrderId, a.ArchivedAt, a.ArchivedBy, a.ArchiveReason, a.ShipmentNumber, a.ProcessingDate, |
| | 0 | 68 | | a.ProductName, a.Article, a.Quantity, |
| | 0 | 69 | | TrueQuantity = a.Quantity.HasValue ? Order.ApplyProductKeyMultiplier(a.Quantity.Value, a.ProductKey) : ( |
| | 0 | 70 | | a.PurchasePrice, a.AppStatus, a.ClientStatus, a.ClientName, a.ImageUrl |
| | 0 | 71 | | }).ToList(); |
| | 0 | 72 | | var lastRow = start + rows.Count >= total ? total : -1; |
| | 0 | 73 | | return Ok(new { success = true, rows, totalCount = total, lastRow }); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | [HttpPost("Studio2/OrderArchive/grid/facets")] |
| | | 77 | | [ValidateAntiForgeryToken] |
| | | 78 | | [PageAuthorize(PageCode, AccessType.View)] |
| | | 79 | | public async Task<IActionResult> GridFacets([FromBody] ServerGridRequest? request, CancellationToken cancellatio |
| | | 80 | | { |
| | 0 | 81 | | if (request is null || string.IsNullOrWhiteSpace(request.ColId)) return BadRequest(new { success = false, er |
| | 0 | 82 | | request.StartRow = 0; |
| | 0 | 83 | | request.EndRow = 1; |
| | 0 | 84 | | var error = ServerGridQuery.Validate(request); |
| | 0 | 85 | | if (error != null) return BadRequest(new { success = false, error }); |
| | 0 | 86 | | var values = await ServerGridQuery.FacetsAsync(_context.OrderArchives.AsNoTracking(), JournalGridDefinitions |
| | 0 | 87 | | return Ok(new { success = true, values }); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | // POST: Studio2/OrderArchive/RestoreOrder/5 |
| | | 91 | | [HttpPost] |
| | | 92 | | public async Task<IActionResult> RestoreOrder(int id) |
| | | 93 | | { |
| | | 94 | | try |
| | | 95 | | { |
| | 0 | 96 | | await _orderArchiveService.RestoreFromArchiveAsync(id); |
| | 0 | 97 | | TempData["SuccessMessage"] = "Заказ успешно восстановлен из архива"; |
| | 0 | 98 | | } |
| | 0 | 99 | | catch (Exception ex) |
| | | 100 | | { |
| | 0 | 101 | | TempData["ErrorMessage"] = $"Ошибка при восстановлении заказа: {ex.Message}"; |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | return RedirectToAction(nameof(Index)); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | // POST: Studio2/OrderArchive/RestoreOrderToOrders/5 |
| | | 108 | | [HttpPost] |
| | | 109 | | public async Task<IActionResult> RestoreOrderToOrders(int id, int page = 1) |
| | | 110 | | { |
| | | 111 | | try |
| | | 112 | | { |
| | 0 | 113 | | await _orderArchiveService.RestoreFromArchiveAsync(id); |
| | 0 | 114 | | TempData["SuccessMessage"] = "Заказ успешно восстановлен из архива"; |
| | 0 | 115 | | } |
| | 0 | 116 | | catch (Exception ex) |
| | | 117 | | { |
| | 0 | 118 | | TempData["ErrorMessage"] = $"Ошибка при восстановлении заказа: {ex.Message}"; |
| | 0 | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | HttpContext.Session.SetString("selectedIds", JsonConvert.SerializeObject(new List<int>())); |
| | 0 | 122 | | return RedirectToAction( |
| | 0 | 123 | | "Index", |
| | 0 | 124 | | "Orders", |
| | 0 | 125 | | new { area = "Studio2", page = page } |
| | 0 | 126 | | ); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | // POST: Studio2/OrderArchive/ArchiveMultiple |
| | | 130 | | [HttpPost] |
| | | 131 | | public async Task<IActionResult> ArchiveMultiple(string orderIds, int page = 1) |
| | | 132 | | { |
| | 0 | 133 | | if (string.IsNullOrEmpty(orderIds)) |
| | | 134 | | { |
| | 0 | 135 | | TempData["ErrorMessage"] = "Не выбраны заказы для архивации"; |
| | 0 | 136 | | return RedirectToAction("Index", "Orders", new { page }); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // Парсим строку с ID в список |
| | 0 | 140 | | var orderIdList = orderIds.Split(',') |
| | 0 | 141 | | .Where(id => !string.IsNullOrEmpty(id)) |
| | 0 | 142 | | .Select(id => int.Parse(id.Trim())) |
| | 0 | 143 | | .ToList(); |
| | | 144 | | |
| | 0 | 145 | | var successCount = 0; |
| | 0 | 146 | | var errorCount = 0; |
| | | 147 | | |
| | 0 | 148 | | foreach (var orderId in orderIdList) |
| | | 149 | | { |
| | | 150 | | try |
| | | 151 | | { |
| | 0 | 152 | | if (!await _orderArchiveService.IsOrderArchivedAsync(orderId)) |
| | | 153 | | { |
| | 0 | 154 | | await _orderArchiveService.ArchiveOrderAsync(orderId, User.Identity?.Name ?? "System", |
| | 0 | 155 | | "Массовая архивация"); |
| | 0 | 156 | | successCount++; |
| | | 157 | | } |
| | 0 | 158 | | } |
| | 0 | 159 | | catch |
| | | 160 | | { |
| | 0 | 161 | | errorCount++; |
| | 0 | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | if (successCount > 0) |
| | | 166 | | { |
| | 0 | 167 | | TempData["SuccessMessage"] = $"Успешно архивировано заказов: {successCount}"; |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | if (errorCount > 0) |
| | | 171 | | { |
| | 0 | 172 | | TempData["ErrorMessage"] = $"Не удалось архивировать заказов: {errorCount}"; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | HttpContext.Session.SetString("selectedIds", JsonConvert.SerializeObject(new List<int>())); |
| | 0 | 176 | | return RedirectToAction("Index", "Orders", new { area = "Studio2", page = page }); |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | // POST: Studio2/OrderArchive/ArchiveOrder |
| | | 180 | | [HttpPost] |
| | | 181 | | public async Task<IActionResult> ArchiveOrder(int id, int page = 1) |
| | | 182 | | { |
| | | 183 | | try |
| | | 184 | | { |
| | 0 | 185 | | await _orderArchiveService.ArchiveOrderAsync(id, User.Identity?.Name ?? "System", |
| | 0 | 186 | | "Одиночная архивация"); |
| | 0 | 187 | | TempData["SuccessMessage"] = "Заказ успешно архивирован"; |
| | 0 | 188 | | } |
| | 0 | 189 | | catch (Exception ex) |
| | | 190 | | { |
| | 0 | 191 | | TempData["ErrorMessage"] = $"Ошибка при архивации заказа: {ex.Message}"; |
| | 0 | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | HttpContext.Session.SetString("selectedIds", JsonConvert.SerializeObject(new List<int>())); |
| | 0 | 195 | | return RedirectToAction( |
| | 0 | 196 | | "Index", |
| | 0 | 197 | | "Orders", |
| | 0 | 198 | | new { area = "Studio2", page = page } |
| | 0 | 199 | | ); |
| | 0 | 200 | | } |
| | | 201 | | } |
| | | 202 | | } |