| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.Warehouse; |
| | | 6 | | using Services.ApiServices; |
| | | 7 | | using Services.Authorization.Services; |
| | | 8 | | using Services.DataServices; |
| | | 9 | | |
| | | 10 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 11 | | |
| | | 12 | | [Authorize(Roles = "User,Admin")] |
| | | 13 | | [Area("Studio2")] |
| | | 14 | | public class WarehouseMappingController : Controller |
| | | 15 | | { |
| | | 16 | | private readonly WarehouseMappingDataServices _service; |
| | | 17 | | private readonly WarehouseDataServices _warehouseDataServices; |
| | | 18 | | private readonly OzonClientServices _clientDataServices; |
| | | 19 | | private readonly SupplierDataServices _supplierDataServices; |
| | | 20 | | private readonly OneCDataManager _cDataManager; |
| | | 21 | | private readonly IScopeFilterValuesService _scopeFilterValuesService; |
| | | 22 | | |
| | 0 | 23 | | public WarehouseMappingController(WarehouseMappingDataServices service, |
| | 0 | 24 | | WarehouseDataServices warehouseDataServices, |
| | 0 | 25 | | OzonClientServices clientDataServices, |
| | 0 | 26 | | SupplierDataServices supplierDataServices, |
| | 0 | 27 | | OneCDataManager oneCDataManager, |
| | 0 | 28 | | IScopeFilterValuesService scopeFilterValuesService) |
| | | 29 | | { |
| | 0 | 30 | | _service = service; |
| | 0 | 31 | | _warehouseDataServices = warehouseDataServices; |
| | 0 | 32 | | _clientDataServices = clientDataServices; |
| | 0 | 33 | | _supplierDataServices = supplierDataServices; |
| | 0 | 34 | | _cDataManager = oneCDataManager; |
| | 0 | 35 | | _scopeFilterValuesService = scopeFilterValuesService; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>Весь справочник связей складов для грида AG Grid: фильтры, сортировка и поиск — на клиенте.</summary> |
| | | 39 | | public async Task<IActionResult> Index() |
| | | 40 | | { |
| | 0 | 41 | | var mappings = (await _service.GetWarehouseMappings()) |
| | 0 | 42 | | .OrderBy(m => m.OzonClient?.Name) |
| | 0 | 43 | | .ThenBy(m => m.OzonWarehouseName) |
| | 0 | 44 | | .ThenBy(m => m.Priority) |
| | 0 | 45 | | .ToList(); |
| | 0 | 46 | | return View(mappings); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task<IActionResult> Create( |
| | | 50 | | int? ozonClientId = null, |
| | | 51 | | string? ozonWarehouseName = null, |
| | | 52 | | string? deliveryType = null) |
| | | 53 | | { |
| | 0 | 54 | | var prefilled = new WarehouseMapping |
| | 0 | 55 | | { |
| | 0 | 56 | | OzonClientId = ozonClientId, |
| | 0 | 57 | | OzonWarehouseName = ozonWarehouseName?.Trim() ?? string.Empty, |
| | 0 | 58 | | DeliveryType = string.IsNullOrWhiteSpace(deliveryType) ? null : deliveryType.Trim() |
| | 0 | 59 | | }; |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 0 | 63 | | await PopulateDropdownsAsync(prefilled.OzonClientId, selectedOzonWarehouse: prefilled.OzonWarehouseName); |
| | 0 | 64 | | return View(prefilled); |
| | | 65 | | } |
| | 0 | 66 | | catch (Exception ex) |
| | | 67 | | { |
| | 0 | 68 | | PopulateEmptyDropdowns(); |
| | 0 | 69 | | TempData["ErrorMessage"] = $"Ошибка загрузки данных для создания: {ex.Message}"; |
| | 0 | 70 | | return View(prefilled); |
| | | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | [HttpPost] |
| | | 75 | | [ValidateAntiForgeryToken] |
| | | 76 | | public async Task<IActionResult> Create(WarehouseMapping model) |
| | | 77 | | { |
| | 0 | 78 | | if (!ModelState.IsValid) |
| | | 79 | | { |
| | 0 | 80 | | await PopulateDropdownsAsync(model.OzonClientId, model.BitrixWarehouseName, model.OzonWarehouseName, model.S |
| | 0 | 81 | | return View(model); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | try |
| | | 85 | | { |
| | 0 | 86 | | var duplicate = await FindDuplicateAsync(model, null); |
| | 0 | 87 | | if (duplicate != null) |
| | | 88 | | { |
| | 0 | 89 | | ModelState.AddModelError(string.Empty, |
| | 0 | 90 | | "Такая связь уже существует для выбранного клиента, Bitrix склада и Ozon склада."); |
| | 0 | 91 | | await PopulateDropdownsAsync(model.OzonClientId, model.BitrixWarehouseName, model.OzonWarehouseName, mod |
| | 0 | 92 | | return View(model); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | await _service.AddWarehouseMapping(model); |
| | 0 | 96 | | TempData["SuccessMessage"] = "Связь складов успешно создана!"; |
| | 0 | 97 | | return RedirectToAction(nameof(Index)); |
| | | 98 | | } |
| | 0 | 99 | | catch (Exception ex) |
| | | 100 | | { |
| | 0 | 101 | | ModelState.AddModelError(string.Empty, $"Ошибка при создании связи: {ex.Message}"); |
| | 0 | 102 | | await PopulateDropdownsAsync(model.OzonClientId, model.BitrixWarehouseName, model.OzonWarehouseName, model.S |
| | 0 | 103 | | return View(model); |
| | | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | public async Task<IActionResult> Edit(int id) |
| | | 108 | | { |
| | | 109 | | try |
| | | 110 | | { |
| | 0 | 111 | | var mapping = await _service.GetWarehouseMappingAsync(id); |
| | 0 | 112 | | if (mapping == null) |
| | | 113 | | { |
| | 0 | 114 | | TempData["ErrorMessage"] = $"Связь складов с ID {id} не найдена."; |
| | 0 | 115 | | return RedirectToAction(nameof(Index)); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | await PopulateDropdownsAsync(mapping.OzonClientId, mapping.BitrixWarehouseName, mapping.OzonWarehouseName, m |
| | 0 | 119 | | return View(mapping); |
| | | 120 | | } |
| | 0 | 121 | | catch (Exception ex) |
| | | 122 | | { |
| | 0 | 123 | | TempData["ErrorMessage"] = $"Ошибка при загрузке связи для редактирования: {ex.Message}"; |
| | 0 | 124 | | return RedirectToAction(nameof(Index)); |
| | | 125 | | } |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | [HttpPost] |
| | | 129 | | [ValidateAntiForgeryToken] |
| | | 130 | | public async Task<IActionResult> Edit(WarehouseMapping model) |
| | | 131 | | { |
| | | 132 | | // Пустой тип доставки означает общую связку склада и не должен блокировать сохранение. |
| | 0 | 133 | | model.DeliveryType = string.IsNullOrWhiteSpace(model.DeliveryType) ? null : model.DeliveryType.Trim(); |
| | 0 | 134 | | ModelState.Remove(nameof(WarehouseMapping.DeliveryType)); |
| | | 135 | | |
| | 0 | 136 | | if (!ModelState.IsValid) |
| | | 137 | | { |
| | 0 | 138 | | await PopulateDropdownsAsync(model.OzonClientId, model.BitrixWarehouseName, model.OzonWarehouseName, model.S |
| | 0 | 139 | | return View(model); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | try |
| | | 143 | | { |
| | 0 | 144 | | var existing = await _service.GetWarehouseMappingAsync(model.Id); |
| | 0 | 145 | | if (existing == null) |
| | | 146 | | { |
| | 0 | 147 | | TempData["ErrorMessage"] = $"Связь складов с ID {model.Id} не найдена."; |
| | 0 | 148 | | return RedirectToAction(nameof(Index)); |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | var duplicate = await FindDuplicateAsync(model, model.Id); |
| | 0 | 152 | | if (duplicate != null) |
| | | 153 | | { |
| | 0 | 154 | | ModelState.AddModelError(string.Empty, |
| | 0 | 155 | | "Такая связь уже существует для выбранного клиента, Bitrix склада и Ozon склада."); |
| | 0 | 156 | | await PopulateDropdownsAsync(model.OzonClientId, model.BitrixWarehouseName, model.OzonWarehouseName, mod |
| | 0 | 157 | | return View(model); |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | await _service.UpdateWarehouseMapping(model); |
| | 0 | 161 | | TempData["SuccessMessage"] = "Связь складов успешно обновлена!"; |
| | 0 | 162 | | return RedirectToAction(nameof(Index)); |
| | | 163 | | } |
| | 0 | 164 | | catch (Exception ex) |
| | | 165 | | { |
| | 0 | 166 | | ModelState.AddModelError(string.Empty, $"Ошибка при обновлении: {ex.Message}"); |
| | 0 | 167 | | await PopulateDropdownsAsync(model.OzonClientId, model.BitrixWarehouseName, model.OzonWarehouseName, model.S |
| | 0 | 168 | | return View(model); |
| | | 169 | | } |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | public async Task<IActionResult> Details(int id) |
| | | 173 | | { |
| | 0 | 174 | | var mapping = await _service.GetWarehouseMappingAsync(id); |
| | 0 | 175 | | if (mapping == null) |
| | 0 | 176 | | return NotFound(); |
| | | 177 | | |
| | 0 | 178 | | return View(mapping); |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | public async Task<IActionResult> Delete(int id) |
| | | 182 | | { |
| | 0 | 183 | | var mapping = await _service.GetWarehouseMappingAsync(id); |
| | 0 | 184 | | if (mapping == null) |
| | 0 | 185 | | return NotFound(); |
| | | 186 | | |
| | 0 | 187 | | return View(mapping); |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | [HttpPost, ActionName("Delete")] |
| | | 191 | | [ValidateAntiForgeryToken] |
| | | 192 | | public async Task<IActionResult> DeleteConfirmed(int id) |
| | | 193 | | { |
| | 0 | 194 | | var mapping = await _service.GetWarehouseMappingAsync(id); |
| | 0 | 195 | | if (mapping != null) |
| | | 196 | | { |
| | 0 | 197 | | await _service.DeleteWarehouseMapping(mapping); |
| | 0 | 198 | | TempData["SuccessMessage"] = "Связь складов удалена!"; |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | return RedirectToAction(nameof(Index)); |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | private async Task PopulateDropdownsAsync(int? selectedClientId = null, string? selectedBitrixWarehouse = null, |
| | | 205 | | string? selectedOzonWarehouse = null, int? selectedSupplierId = null) |
| | | 206 | | { |
| | 0 | 207 | | var warehouses = await _warehouseDataServices.GetWarehouses(); |
| | 0 | 208 | | var bitrixWarehouses = await _cDataManager.GetWarehouseListAsync(); |
| | 0 | 209 | | var clients = await _clientDataServices.GetOzonClients(); |
| | 0 | 210 | | var suppliers = await _supplierDataServices.GetSuppliers(); |
| | | 211 | | |
| | 0 | 212 | | ViewBag.Warehouses = warehouses != null |
| | 0 | 213 | | ? new SelectList(warehouses.OrderBy(a => a.Name), "Name", "Name", selectedOzonWarehouse) |
| | 0 | 214 | | : new SelectList(Enumerable.Empty<SelectListItem>()); |
| | | 215 | | |
| | 0 | 216 | | var bitrixWarehouseNames = bitrixWarehouses? |
| | 0 | 217 | | .Where(wh => wh != null && !string.IsNullOrEmpty(wh.Name)) |
| | 0 | 218 | | .Select(wh => wh.Name!) |
| | 0 | 219 | | .OrderBy(name => name) |
| | 0 | 220 | | .ToList() ?? new List<string>(); |
| | | 221 | | |
| | 0 | 222 | | var userId = User?.Claims?.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier)?.Valu |
| | 0 | 223 | | if (!string.IsNullOrWhiteSpace(userId)) |
| | | 224 | | { |
| | 0 | 225 | | var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync(userId, "BitrixWarehouse", HttpContext.R |
| | 0 | 226 | | if (allowed.TryGetValue("Name", out var allowedNames) && allowedNames.Count > 0) |
| | | 227 | | { |
| | 0 | 228 | | bitrixWarehouseNames = bitrixWarehouseNames |
| | 0 | 229 | | .Where(n => allowedNames.Contains(n, StringComparer.Ordinal)) |
| | 0 | 230 | | .OrderBy(n => n) |
| | 0 | 231 | | .ToList(); |
| | | 232 | | } |
| | 0 | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | ViewBag.BitrixWarehouses = new SelectList(bitrixWarehouseNames, selectedBitrixWarehouse); |
| | 0 | 236 | | ViewBag.Clients = clients != null |
| | 0 | 237 | | ? new SelectList(clients, "Id", "Name", selectedClientId) |
| | 0 | 238 | | : new SelectList(Enumerable.Empty<SelectListItem>()); |
| | 0 | 239 | | ViewBag.Suppliers = new SelectList(suppliers.OrderBy(s => s.Name), "Id", "Name", selectedSupplierId); |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | private void PopulateEmptyDropdowns() |
| | | 243 | | { |
| | 0 | 244 | | ViewBag.Warehouses = new SelectList(Enumerable.Empty<SelectListItem>()); |
| | 0 | 245 | | ViewBag.BitrixWarehouses = new SelectList(Enumerable.Empty<string>()); |
| | 0 | 246 | | ViewBag.Clients = new SelectList(Enumerable.Empty<SelectListItem>()); |
| | 0 | 247 | | ViewBag.Suppliers = new SelectList(Enumerable.Empty<SelectListItem>()); |
| | 0 | 248 | | } |
| | | 249 | | |
| | | 250 | | private async Task<WarehouseMapping?> FindDuplicateAsync(WarehouseMapping model, int? excludeId) |
| | | 251 | | { |
| | 0 | 252 | | var mappings = await _service.GetWarehouseMappings(); |
| | 0 | 253 | | return mappings.FirstOrDefault(m => |
| | 0 | 254 | | m.Id != excludeId && |
| | 0 | 255 | | m.OzonClientId == model.OzonClientId && |
| | 0 | 256 | | m.SupplierId == model.SupplierId && |
| | 0 | 257 | | string.Equals(m.BitrixWarehouseName?.Trim(), model.BitrixWarehouseName?.Trim(), |
| | 0 | 258 | | StringComparison.OrdinalIgnoreCase) && |
| | 0 | 259 | | string.Equals(m.OzonWarehouseName?.Trim(), model.OzonWarehouseName?.Trim(), |
| | 0 | 260 | | StringComparison.OrdinalIgnoreCase)); |
| | 0 | 261 | | } |
| | | 262 | | } |