| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using OzonDomains; |
| | | 6 | | using OzonDomains.Models; |
| | | 7 | | using Services.CacheServices.Cache.OzonOrdersCache; |
| | | 8 | | using Services.DataServices; |
| | | 9 | | using Services.CacheServices.Cache; |
| | | 10 | | |
| | | 11 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories |
| | | 12 | | { |
| | | 13 | | [Authorize(Roles = "Admin")] |
| | | 14 | | [Area("Studio2")] |
| | | 15 | | public class ClientStatusController : Controller |
| | | 16 | | { |
| | | 17 | | private readonly ClientStatusDataServices _servcies; |
| | | 18 | | private readonly OrdersDataServices _ordersDataServices; |
| | | 19 | | private readonly OrderCache _cache; |
| | | 20 | | private readonly CacheUpdater<Order> _cacheUpdater; |
| | | 21 | | |
| | 0 | 22 | | public ClientStatusController(ClientStatusDataServices servcies, |
| | 0 | 23 | | OrdersDataServices ordersDataServices, |
| | 0 | 24 | | OrderCache orderCache, |
| | 0 | 25 | | CacheUpdater<Order> cacheUpdater) |
| | | 26 | | { |
| | 0 | 27 | | _servcies = servcies; |
| | 0 | 28 | | _ordersDataServices = ordersDataServices; |
| | 0 | 29 | | _cache = orderCache; |
| | 0 | 30 | | _cacheUpdater = cacheUpdater; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary>Справочник статусов клиентов — весь список: сортировка, фильтры и поиск в гриде AG Grid на клиенте. |
| | | 34 | | public async Task<IActionResult> Index() |
| | | 35 | | { |
| | 0 | 36 | | var clientStatuses = (await _servcies.GetClientStatuses()).OrderBy(status => status.Name).ToList(); |
| | 0 | 37 | | return View(clientStatuses); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task<IActionResult> Create() |
| | | 41 | | { |
| | 0 | 42 | | string? result = TempData["StatusResult"] as string; |
| | 0 | 43 | | ViewData["StatusResult"] = result; |
| | | 44 | | |
| | 0 | 45 | | var clientStatusList = await _servcies.GetClientStatuses(); |
| | 0 | 46 | | ViewData["ClientStatusList"] = clientStatusList; |
| | | 47 | | |
| | | 48 | | // Подготовка списка типов клиентов |
| | 0 | 49 | | ViewBag.ClientTypes = new SelectList(new[] |
| | 0 | 50 | | { |
| | 0 | 51 | | new { Value = (int?)null, Text = "Не указан" }, |
| | 0 | 52 | | new { Value = (int?)ClientType.OZON, Text = "Ozon" }, |
| | 0 | 53 | | new { Value = (int?)ClientType.YANDEX, Text = "Yandex" } |
| | 0 | 54 | | }, "Value", "Text"); |
| | | 55 | | |
| | 0 | 56 | | return View(); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | [HttpPost] |
| | | 60 | | public async Task<IActionResult> Create([Bind("Id,Name,ClientTypeId,IsFinal")] ClientStatus clientStatus) |
| | | 61 | | { |
| | 0 | 62 | | if (ModelState.IsValid) |
| | | 63 | | { |
| | 0 | 64 | | var existingStatus = await _servcies.GetClientStatusAsync(clientStatus); |
| | 0 | 65 | | if (existingStatus != null) |
| | | 66 | | { |
| | 0 | 67 | | TempData["StatusResult"] = $"Не удалось добавить статус '{clientStatus.Name}'"; |
| | 0 | 68 | | return RedirectToAction(nameof(Create)); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | await _servcies.AddClientStatus(clientStatus); |
| | | 72 | | |
| | 0 | 73 | | return RedirectToAction("Index"); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // Подготовка списка типов клиентов для повторного отображения |
| | 0 | 77 | | ViewBag.ClientTypes = new SelectList(new[] |
| | 0 | 78 | | { |
| | 0 | 79 | | new { Value = (int?)null, Text = "Не указан" }, |
| | 0 | 80 | | new { Value = (int?)ClientType.OZON, Text = "Ozon" }, |
| | 0 | 81 | | new { Value = (int?)ClientType.YANDEX, Text = "Yandex" } |
| | 0 | 82 | | }, "Value", "Text", clientStatus.ClientTypeId); |
| | | 83 | | |
| | 0 | 84 | | return View(clientStatus); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public async Task<IActionResult> Delete(int? id) |
| | | 88 | | { |
| | 0 | 89 | | if (!id.HasValue) |
| | 0 | 90 | | return RedirectToAction("Index"); |
| | | 91 | | |
| | 0 | 92 | | var clientStatus = await _servcies.GetClientStatusAsync(id.Value); |
| | 0 | 93 | | if (clientStatus != null) |
| | | 94 | | { |
| | 0 | 95 | | var defaultStatus = await _servcies.GetClientStatusByNameAsync("Не указан"); |
| | 0 | 96 | | if (defaultStatus == null) |
| | | 97 | | { |
| | 0 | 98 | | defaultStatus = new ClientStatus { Name = "Не указан" }; |
| | 0 | 99 | | await _servcies.AddClientStatus(defaultStatus); |
| | 0 | 100 | | defaultStatus = await _servcies.GetClientStatusByNameAsync("Не указан"); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | var ordersWithDeletedStatus = (await _ordersDataServices.GetOrders()) |
| | 0 | 104 | | .Where(o => o.ClientStatusId == id) |
| | 0 | 105 | | .ToList(); |
| | | 106 | | |
| | 0 | 107 | | foreach (var order in ordersWithDeletedStatus) |
| | | 108 | | { |
| | 0 | 109 | | order.ClientStatus = defaultStatus; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | await _servcies.DeleteClientStatus(clientStatus); |
| | 0 | 113 | | await _cacheUpdater.Update(_cache); |
| | 0 | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return RedirectToAction("Index"); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public async Task<IActionResult> Edit(int id) |
| | | 120 | | { |
| | 0 | 121 | | var clientStatus = await _servcies.GetClientStatusAsync(id); |
| | 0 | 122 | | if (clientStatus == null) |
| | | 123 | | { |
| | 0 | 124 | | return NotFound(); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | // Подготовка списка типов клиентов |
| | 0 | 128 | | ViewBag.ClientTypes = new SelectList(new[] |
| | 0 | 129 | | { |
| | 0 | 130 | | new { Value = (int?)null, Text = "Не указан" }, |
| | 0 | 131 | | new { Value = (int?)ClientType.OZON, Text = "Ozon" }, |
| | 0 | 132 | | new { Value = (int?)ClientType.YANDEX, Text = "Yandex" } |
| | 0 | 133 | | }, "Value", "Text", clientStatus.ClientTypeId); |
| | | 134 | | |
| | 0 | 135 | | return View(clientStatus); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | [HttpPost] |
| | | 139 | | [ValidateAntiForgeryToken] |
| | | 140 | | public async Task<IActionResult> Edit(int id, ClientStatus clientStatus) |
| | | 141 | | { |
| | 0 | 142 | | if (id != clientStatus.Id) |
| | | 143 | | { |
| | 0 | 144 | | return NotFound(); |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | if (ModelState.IsValid) |
| | | 148 | | { |
| | | 149 | | try |
| | | 150 | | { |
| | 0 | 151 | | await _servcies.UpdateClientStatus(clientStatus); |
| | 0 | 152 | | await _cacheUpdater.Update(_cache); |
| | 0 | 153 | | } |
| | 0 | 154 | | catch (DbUpdateConcurrencyException) |
| | | 155 | | { |
| | 0 | 156 | | if (await _servcies.GetClientStatusAsync(id) == null) |
| | | 157 | | { |
| | 0 | 158 | | return NotFound(); |
| | | 159 | | } |
| | | 160 | | else |
| | | 161 | | { |
| | 0 | 162 | | throw; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | return RedirectToAction(nameof(Index)); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | // Подготовка списка типов клиентов для повторного отображения |
| | 0 | 170 | | ViewBag.ClientTypes = new SelectList(new[] |
| | 0 | 171 | | { |
| | 0 | 172 | | new { Value = (int?)null, Text = "Не указан" }, |
| | 0 | 173 | | new { Value = (int?)ClientType.OZON, Text = "Ozon" }, |
| | 0 | 174 | | new { Value = (int?)ClientType.YANDEX, Text = "Yandex" } |
| | 0 | 175 | | }, "Value", "Text", clientStatus.ClientTypeId); |
| | | 176 | | |
| | 0 | 177 | | return View(clientStatus); |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | [HttpPost] |
| | | 181 | | public async Task<IActionResult> UpdateStatusColor(int statusId, string colorCode) |
| | | 182 | | { |
| | | 183 | | try |
| | | 184 | | { |
| | 0 | 185 | | await _servcies.UpdateStatusColor(statusId, colorCode); |
| | 0 | 186 | | await _cacheUpdater.Update(_cache); |
| | 0 | 187 | | return Json(new { success = true }); |
| | | 188 | | } |
| | 0 | 189 | | catch (Exception ex) |
| | | 190 | | { |
| | 0 | 191 | | return Json(new { success = false, error = ex.Message }); |
| | | 192 | | } |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | [HttpPost] |
| | | 196 | | public async Task<IActionResult> ResetStatusColor(int statusId) |
| | | 197 | | { |
| | | 198 | | try |
| | | 199 | | { |
| | 0 | 200 | | await _servcies.ResetStatusColor(statusId); |
| | 0 | 201 | | await _cacheUpdater.Update(_cache); |
| | 0 | 202 | | return Json(new { success = true }); |
| | | 203 | | } |
| | 0 | 204 | | catch (Exception ex) |
| | | 205 | | { |
| | 0 | 206 | | return Json(new { success = false, error = ex.Message }); |
| | | 207 | | } |
| | 0 | 208 | | } |
| | | 209 | | } |
| | | 210 | | } |