| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.AspNetCore.Authorization; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonDomains.Models.Authorization.Enums; |
| | | 6 | | using OzonOrdersWeb.Authorization; |
| | | 7 | | using OzonOrdersWeb.WebServices.Directories; |
| | | 8 | | using OzonOrdersWeb.WebServices.Grid; |
| | | 9 | | using OzonRepositories.Context; |
| | | 10 | | using Services.DataServices; |
| | | 11 | | using Services.CacheServices.Cache.EtProducerCache; |
| | | 12 | | |
| | | 13 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 14 | | |
| | | 15 | | [Authorize(Roles = "Admin")] |
| | | 16 | | [Area("Studio2")] |
| | | 17 | | public class ProducersController : Controller |
| | | 18 | | { |
| | | 19 | | private const string PageCode = "Studio2.Producers.Index"; |
| | | 20 | | |
| | | 21 | | private readonly EtProducerDataServices _producerDataService; |
| | | 22 | | private readonly ApplicationDbContext _context; |
| | | 23 | | private readonly EtProducerCache _producerCache; |
| | | 24 | | |
| | | 25 | | |
| | 0 | 26 | | public ProducersController(EtProducerDataServices producerDataService, |
| | 0 | 27 | | ApplicationDbContext context, |
| | 0 | 28 | | EtProducerCache producerCache) |
| | | 29 | | { |
| | 0 | 30 | | _producerDataService = producerDataService; |
| | 0 | 31 | | _context = context; |
| | 0 | 32 | | _producerCache = producerCache; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Каркас страницы: строки (~20 тыс.) грид подгружает блоками через <see cref="GridRows"/>.</summary> |
| | | 36 | | public async Task<IActionResult> Index(CancellationToken cancellationToken) |
| | | 37 | | { |
| | 0 | 38 | | ViewBag.TotalCount = await _context.EtProducers.CountAsync(cancellationToken); |
| | 0 | 39 | | ViewBag.WithoutMarketPrefix = await _context.EtProducers.CountAsync(p => p.MarketPrefix == null || p.MarketPrefi |
| | 0 | 40 | | return View(); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | [HttpPost("Studio2/Producers/grid")] |
| | | 44 | | [ValidateAntiForgeryToken] |
| | | 45 | | [PageAuthorize(PageCode, AccessType.View)] |
| | | 46 | | public async Task<IActionResult> GridRows([FromBody] ServerGridRequest? request, CancellationToken cancellationToken |
| | | 47 | | { |
| | 0 | 48 | | var error = ServerGridQuery.Validate(request); |
| | 0 | 49 | | if (error != null) return BadRequest(new { success = false, error }); |
| | | 50 | | |
| | 0 | 51 | | var def = DirectoryGridDefinitions.Producers; |
| | 0 | 52 | | var filtered = ServerGridQuery.ApplyFilters(_context.EtProducers.AsNoTracking(), def, request!); |
| | 0 | 53 | | var total = await filtered.CountAsync(cancellationToken); |
| | 0 | 54 | | var start = Math.Max(0, request!.StartRow); |
| | 0 | 55 | | var take = Math.Clamp(request.EndRow - start, 1, ServerGridQuery.MaxBlockSize); |
| | 0 | 56 | | var rows = start >= total |
| | 0 | 57 | | ? [] |
| | 0 | 58 | | : await ServerGridQuery.ApplySort(filtered, def, request.SortModel).Skip(start).Take(take) |
| | 0 | 59 | | .Select(p => new |
| | 0 | 60 | | { |
| | 0 | 61 | | p.Id, p.RealId, p.MarketPrefix, p.Name, p.Prefix, p.Address, p.Www, p.ExistName, p.ExistId, |
| | 0 | 62 | | p.Domain, p.Rating, p.TecdocSupplierId |
| | 0 | 63 | | }) |
| | 0 | 64 | | .ToListAsync(cancellationToken); |
| | 0 | 65 | | var lastRow = start + rows.Count >= total ? total : -1; |
| | 0 | 66 | | return Ok(new { success = true, rows, totalCount = total, lastRow }); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | [HttpPost("Studio2/Producers/grid/facets")] |
| | | 70 | | [ValidateAntiForgeryToken] |
| | | 71 | | [PageAuthorize(PageCode, AccessType.View)] |
| | | 72 | | public async Task<IActionResult> GridFacets([FromBody] ServerGridRequest? request, CancellationToken cancellationTok |
| | | 73 | | { |
| | 0 | 74 | | if (request is null || string.IsNullOrWhiteSpace(request.ColId)) return BadRequest(new { success = false, error |
| | 0 | 75 | | request.StartRow = 0; |
| | 0 | 76 | | request.EndRow = 1; |
| | 0 | 77 | | var error = ServerGridQuery.Validate(request); |
| | 0 | 78 | | if (error != null) return BadRequest(new { success = false, error }); |
| | 0 | 79 | | var values = await ServerGridQuery.FacetsAsync(_context.EtProducers.AsNoTracking(), DirectoryGridDefinitions.Pro |
| | 0 | 80 | | return Ok(new { success = true, values }); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | public IActionResult Create() |
| | | 84 | | { |
| | 0 | 85 | | return View(); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | [HttpPost] |
| | | 89 | | [ValidateAntiForgeryToken] |
| | | 90 | | public async Task<IActionResult> Create( |
| | | 91 | | [Bind("Id,RealId,Prefix,Name,Address,Www,Rating,ExistName,ExistId,Domain,TecdocSupplierId,MarketPrefix")] |
| | | 92 | | EtProducer producer) |
| | | 93 | | { |
| | 0 | 94 | | EtProducer? isOrig = (await _producerDataService.GetEtProducers()).FirstOrDefault(p => |
| | 0 | 95 | | p.Name?.ToLower() == producer?.Name?.ToLower() && |
| | 0 | 96 | | p.Prefix?.ToLower() == producer?.Prefix?.ToLower()); |
| | 0 | 97 | | if (isOrig == null) |
| | | 98 | | { |
| | 0 | 99 | | if (ModelState.IsValid) |
| | | 100 | | { |
| | 0 | 101 | | await _producerDataService.AddEtProducer(producer); |
| | 0 | 102 | | return RedirectToAction(nameof(Index)); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | return View(producer); |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | 0 | 109 | | throw new Exception("Producer already exists"); |
| | | 110 | | } |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | public async Task<IActionResult> Edit(string id) |
| | | 114 | | { |
| | 0 | 115 | | if (string.IsNullOrEmpty(id)) |
| | | 116 | | { |
| | 0 | 117 | | return NotFound(); |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | var producer = await _producerDataService.GetEtProducerAsync(int.Parse(id)); |
| | 0 | 121 | | if (producer == null) |
| | | 122 | | { |
| | 0 | 123 | | return NotFound(); |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | return View(producer); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | [HttpPost] |
| | | 130 | | [ValidateAntiForgeryToken] |
| | | 131 | | public async Task<IActionResult> Edit(string id, EtProducer producer) |
| | | 132 | | { |
| | 0 | 133 | | if (int.Parse(id) != producer.Id) |
| | | 134 | | { |
| | 0 | 135 | | return NotFound(); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | if (ModelState.IsValid) |
| | | 139 | | { |
| | 0 | 140 | | EtProducer? isOrig = (await _producerDataService.GetEtProducers()).FirstOrDefault(p => |
| | 0 | 141 | | p.Name?.ToLower() == producer?.Name?.ToLower() && |
| | 0 | 142 | | p.Prefix?.ToLower() == producer?.Prefix?.ToLower()); |
| | | 143 | | |
| | | 144 | | try |
| | | 145 | | { |
| | 0 | 146 | | if (isOrig == null || isOrig.Id == producer.Id) |
| | | 147 | | { |
| | 0 | 148 | | await _producerDataService.UpdateEtProducer(producer); |
| | 0 | 149 | | await _producerCache.Update(); |
| | | 150 | | } |
| | | 151 | | else |
| | | 152 | | { |
| | 0 | 153 | | TempData["ErrorMessage"] = $"Производитель «{producer.Name}» с префиксом «{producer.Prefix}» уже ест |
| | | 154 | | } |
| | 0 | 155 | | } |
| | 0 | 156 | | catch (DbUpdateConcurrencyException) |
| | | 157 | | { |
| | 0 | 158 | | if (await _producerDataService.GetEtProducerAsync(int.Parse(id)) == null) |
| | | 159 | | { |
| | 0 | 160 | | return NotFound(); |
| | | 161 | | } |
| | | 162 | | else |
| | | 163 | | { |
| | 0 | 164 | | throw; |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | return RedirectToAction(nameof(Index)); |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return View(producer); |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | [HttpPost] |
| | | 175 | | [ValidateAntiForgeryToken] |
| | | 176 | | public async Task<IActionResult> UpdateFromApi(int id) |
| | | 177 | | { |
| | | 178 | | try |
| | | 179 | | { |
| | 0 | 180 | | var updatedProducer = await _producerDataService.UpdateEtProducerFromApi(id); |
| | | 181 | | |
| | 0 | 182 | | if (updatedProducer != null) |
| | | 183 | | { |
| | 0 | 184 | | await _producerCache.Update(); |
| | 0 | 185 | | TempData["SuccessMessage"] = $"Производитель {updatedProducer.Name} успешно обновлен из API"; |
| | | 186 | | } |
| | | 187 | | else |
| | | 188 | | { |
| | 0 | 189 | | TempData["ErrorMessage"] = "Производитель не найден в API или не удовлетворяет условию Id eq RealId"; |
| | | 190 | | } |
| | 0 | 191 | | } |
| | 0 | 192 | | catch (Exception ex) |
| | | 193 | | { |
| | 0 | 194 | | TempData["ErrorMessage"] = $"Ошибка при обновлении из API: {ex.Message}"; |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | return RedirectToAction(nameof(Index)); |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | [HttpPost] |
| | | 201 | | [ValidateAntiForgeryToken] |
| | | 202 | | public async Task<IActionResult> UpdateMultipleFromApi(List<int> ids) |
| | | 203 | | { |
| | | 204 | | try |
| | | 205 | | { |
| | 0 | 206 | | var updatedProducers = await _producerDataService.UpdateMultipleEtProducersFromApi(ids); |
| | | 207 | | |
| | 0 | 208 | | if (updatedProducers.Any()) |
| | | 209 | | { |
| | 0 | 210 | | await _producerCache.Update(); |
| | 0 | 211 | | TempData["SuccessMessage"] = $"Успешно обновлено {updatedProducers.Count} производителей из API"; |
| | | 212 | | } |
| | | 213 | | else |
| | | 214 | | { |
| | 0 | 215 | | TempData["ErrorMessage"] = "Не удалось обновить ни одного производителя из API"; |
| | | 216 | | } |
| | 0 | 217 | | } |
| | 0 | 218 | | catch (Exception ex) |
| | | 219 | | { |
| | 0 | 220 | | TempData["ErrorMessage"] = $"Ошибка при массовом обновлении из API: {ex.Message}"; |
| | 0 | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | return RedirectToAction(nameof(Index)); |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | [HttpPost] |
| | | 227 | | [ValidateAntiForgeryToken] |
| | | 228 | | public async Task<IActionResult> UpdateByInputFromApi(string producerIdInput) |
| | | 229 | | { |
| | 0 | 230 | | if (string.IsNullOrEmpty(producerIdInput)) |
| | | 231 | | { |
| | 0 | 232 | | TempData["ErrorMessage"] = "Введите ID производителя"; |
| | 0 | 233 | | return RedirectToAction(nameof(Index)); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | if (int.TryParse(producerIdInput, out int id)) |
| | | 237 | | { |
| | 0 | 238 | | return await UpdateFromApi(id); |
| | | 239 | | } |
| | | 240 | | else |
| | | 241 | | { |
| | 0 | 242 | | TempData["ErrorMessage"] = "Некорректный формат ID производителя"; |
| | 0 | 243 | | return RedirectToAction(nameof(Index)); |
| | | 244 | | } |
| | 0 | 245 | | } |
| | | 246 | | } |