| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonDomains.Models.SupplierStatuses; |
| | | 5 | | using Services.CacheServices.Cache; |
| | | 6 | | using Services.CacheServices.Cache.OzonOrdersCache; |
| | | 7 | | using Services.SupplierStatusServices; |
| | | 8 | | |
| | | 9 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Справочник статусов API-поставщиков. Записи создаются автоматически при первой встрече |
| | | 13 | | /// статуса, здесь администратор задаёт им цвет, отображаемое имя и признак конечного статуса. |
| | | 14 | | /// </summary> |
| | | 15 | | [Area("Studio2")] |
| | | 16 | | [Authorize(Roles = "Admin")] |
| | | 17 | | public class SupplierStatusController : Controller |
| | | 18 | | { |
| | | 19 | | private readonly ISupplierStatusDirectoryService _directory; |
| | | 20 | | private readonly ISupplierStatusResolver _resolver; |
| | | 21 | | private readonly OrderCache _cache; |
| | | 22 | | private readonly CacheUpdater<Order> _cacheUpdater; |
| | | 23 | | |
| | 0 | 24 | | public SupplierStatusController( |
| | 0 | 25 | | ISupplierStatusDirectoryService directory, |
| | 0 | 26 | | ISupplierStatusResolver resolver, |
| | 0 | 27 | | OrderCache cache, |
| | 0 | 28 | | CacheUpdater<Order> cacheUpdater) |
| | | 29 | | { |
| | 0 | 30 | | _directory = directory; |
| | 0 | 31 | | _resolver = resolver; |
| | 0 | 32 | | _cache = cache; |
| | 0 | 33 | | _cacheUpdater = cacheUpdater; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary>Весь справочник: поставщик, стадия и прочие срезы — фильтры грида AG Grid на клиенте.</summary> |
| | | 37 | | public async Task<IActionResult> Index(CancellationToken ct) |
| | | 38 | | { |
| | 0 | 39 | | var statuses = await _directory.GetAsync(null, null, ct); |
| | 0 | 40 | | return View(statuses); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | [HttpPost, ValidateAntiForgeryToken] |
| | | 44 | | public async Task<IActionResult> Update( |
| | | 45 | | int id, string name, bool isFinal, int? sortOrder, CancellationToken ct) |
| | | 46 | | { |
| | 0 | 47 | | await _directory.UpdateAsync(id, name, isFinal, sortOrder, ct); |
| | 0 | 48 | | _resolver.InvalidateCache(); |
| | 0 | 49 | | await _cacheUpdater.Update(_cache); |
| | | 50 | | // Срез (фильтры грида) восстанавливается из последнего вида пользователя. |
| | 0 | 51 | | return RedirectToAction(nameof(Index)); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | [HttpPost, ValidateAntiForgeryToken] |
| | | 55 | | public async Task<IActionResult> UpdateStatusColor(int statusId, string colorCode, CancellationToken ct) |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | 0 | 59 | | await _directory.UpdateStatusColorAsync(statusId, colorCode, ct); |
| | 0 | 60 | | await _cacheUpdater.Update(_cache); |
| | 0 | 61 | | return Json(new { success = true }); |
| | | 62 | | } |
| | 0 | 63 | | catch (Exception ex) |
| | | 64 | | { |
| | 0 | 65 | | return Json(new { success = false, error = ex.Message }); |
| | | 66 | | } |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | [HttpPost, ValidateAntiForgeryToken] |
| | | 70 | | public async Task<IActionResult> ResetStatusColor(int statusId, CancellationToken ct) |
| | | 71 | | { |
| | | 72 | | try |
| | | 73 | | { |
| | 0 | 74 | | await _directory.ResetStatusColorAsync(statusId, ct); |
| | 0 | 75 | | await _cacheUpdater.Update(_cache); |
| | 0 | 76 | | return Json(new { success = true }); |
| | | 77 | | } |
| | 0 | 78 | | catch (Exception ex) |
| | | 79 | | { |
| | 0 | 80 | | return Json(new { success = false, error = ex.Message }); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | [HttpPost, ValidateAntiForgeryToken] |
| | | 85 | | public async Task<IActionResult> Delete(int id, CancellationToken ct) |
| | | 86 | | { |
| | 0 | 87 | | var status = await _directory.GetByIdAsync(id, ct); |
| | 0 | 88 | | if (status is null) return NotFound(); |
| | | 89 | | |
| | 0 | 90 | | await _directory.DeleteAsync(id, ct); |
| | 0 | 91 | | _resolver.InvalidateCache(); |
| | 0 | 92 | | await _cacheUpdater.Update(_cache); |
| | 0 | 93 | | TempData["SupplierStatusMessage"] = $"Статус «{status.Name}» удалён. При следующем опросе поставщика он создастс |
| | 0 | 94 | | return RedirectToAction(nameof(Index)); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary>Человекочитаемое название стадии для интерфейса.</summary> |
| | 0 | 98 | | public static string DescribeKind(string statusKind) => statusKind switch |
| | 0 | 99 | | { |
| | 0 | 100 | | SupplierStatusKind.Cart => "Корзина", |
| | 0 | 101 | | SupplierStatusKind.Order => "Заказ", |
| | 0 | 102 | | _ => statusKind |
| | 0 | 103 | | }; |
| | | 104 | | |
| | | 105 | | /// <summary>Название поставщика по ключу провайдера — для фильтра и таблицы.</summary> |
| | 0 | 106 | | public static string DescribeProvider(string providerKey) => providerKey switch |
| | 0 | 107 | | { |
| | 0 | 108 | | ApiSupplierSettings.AvdProviderKey => "АВД", |
| | 0 | 109 | | ApiSupplierSettings.ArmtekProviderKey => "Армтек RU", |
| | 0 | 110 | | ApiSupplierSettings.ArmtekByProviderKey => "Армтек BY", |
| | 0 | 111 | | ApiSupplierSettings.MlAutoProviderKey => "ML-Auto BY", |
| | 0 | 112 | | ApiSupplierSettings.MlAutoRuProviderKey => "ML-Auto RU", |
| | 0 | 113 | | _ => providerKey |
| | 0 | 114 | | }; |
| | | 115 | | } |