| | | 1 | | using ConsoleApp1; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using TreeGrouping.Application.CategoryService; |
| | | 5 | | using TreeGrouping.Application.DbService; |
| | | 6 | | using TreeGrouping.Application.DbService.Models; |
| | | 7 | | using TreeGrouping.Web.Models; |
| | | 8 | | |
| | | 9 | | namespace TreeGrouping.Web.Controllers; |
| | | 10 | | |
| | | 11 | | [Area("CategoryTree")] |
| | | 12 | | public class CategoryController : Controller |
| | | 13 | | { |
| | | 14 | | private readonly IDatabaseService _dbService; |
| | | 15 | | private readonly CategoryCacheService _cacheService; |
| | | 16 | | private readonly CategoryFilterService _filterService; |
| | | 17 | | private readonly CategoryTreeService _treeService; |
| | | 18 | | |
| | 0 | 19 | | public CategoryController( |
| | 0 | 20 | | IDatabaseService dbService, |
| | 0 | 21 | | CategoryCacheService cacheService, |
| | 0 | 22 | | CategoryFilterService filterService, |
| | 0 | 23 | | CategoryTreeService treeService) |
| | | 24 | | { |
| | 0 | 25 | | _dbService = dbService; |
| | 0 | 26 | | _cacheService = cacheService; |
| | 0 | 27 | | _filterService = filterService; |
| | 0 | 28 | | _treeService = treeService; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<IActionResult> Index() |
| | | 32 | | { |
| | 0 | 33 | | await Task.CompletedTask; |
| | 0 | 34 | | return View(); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | private async Task<List<CategoryModel>> GetCategoriesWithCache(string cacheKey, StoredProcedureType storedProcedureT |
| | | 38 | | { |
| | 0 | 39 | | var categories = await _cacheService.GetCategoriesFromCache(cacheKey, storedProcedureType); |
| | 0 | 40 | | if (!string.IsNullOrEmpty(name)) |
| | | 41 | | { |
| | 0 | 42 | | categories = _filterService.FilterCategoriesByName(categories, name); |
| | | 43 | | } |
| | 0 | 44 | | return _filterService.OrderCategoriesByName(categories); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | [HttpGet] |
| | | 48 | | public async Task<IActionResult> GetVolnaCategories(string? name = null) |
| | | 49 | | { |
| | 0 | 50 | | var categories = await GetCategoriesWithCache("volna_categories", StoredProcedureType.GetVolnaCategories, name); |
| | 0 | 51 | | var tree = _treeService.BuildTree(categories); |
| | 0 | 52 | | var model = Tuple.Create(tree, "Volna"); |
| | | 53 | | |
| | 0 | 54 | | return PartialView("_CategoryTree", model); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | [HttpGet] |
| | | 58 | | public async Task<IActionResult> GetVolnaCategoriesById(int id) |
| | | 59 | | { |
| | 0 | 60 | | var categories = await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.SearchVolnaCategoryById, id); |
| | 0 | 61 | | foreach (var category in categories) |
| | | 62 | | { |
| | 0 | 63 | | category.IsFiltred = true; |
| | | 64 | | } |
| | 0 | 65 | | var orderedCategories = _filterService.OrderCategoriesByName(categories.ToList()); |
| | 0 | 66 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 67 | | var model = Tuple.Create(tree, "Volna"); |
| | | 68 | | |
| | 0 | 69 | | return PartialView("_CategoryTree", model); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | [HttpGet] |
| | | 73 | | public async Task<IActionResult> UpdateVolnaCategories() |
| | | 74 | | { |
| | 0 | 75 | | var categories = await _cacheService.UpdateCache("volna_categories", StoredProcedureType.GetVolnaCategories); |
| | 0 | 76 | | var orderedCategories = _filterService.OrderCategoriesByName(categories); |
| | 0 | 77 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 78 | | var model = Tuple.Create(tree, "Volna"); |
| | | 79 | | |
| | 0 | 80 | | return PartialView("_CategoryTree", model); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | [HttpGet] |
| | | 84 | | public async Task<IActionResult> GetOzonCategories(string? name = null) |
| | | 85 | | { |
| | 0 | 86 | | var categories = await GetCategoriesWithCache("ozon_categories", StoredProcedureType.GetOzonCategories, name); |
| | 0 | 87 | | var tree = _treeService.BuildTree(categories); |
| | 0 | 88 | | var model = Tuple.Create(tree, "Ozon"); |
| | | 89 | | |
| | 0 | 90 | | return PartialView("_CategoryTree", model); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | [HttpGet] |
| | | 94 | | public async Task<IActionResult> GetOzonCategoriesById(int id) |
| | | 95 | | { |
| | 0 | 96 | | var categories = await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.SearchOzonCategoryById, id); |
| | 0 | 97 | | foreach (var category in categories) |
| | | 98 | | { |
| | 0 | 99 | | category.IsFiltred = true; |
| | | 100 | | } |
| | 0 | 101 | | var orderedCategories = _filterService.OrderCategoriesByName(categories.ToList()); |
| | 0 | 102 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 103 | | var model = Tuple.Create(tree, "Ozon"); |
| | | 104 | | |
| | 0 | 105 | | return PartialView("_CategoryTree", model); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | [HttpGet] |
| | | 109 | | public async Task<IActionResult> UpdateOzonCategories() |
| | | 110 | | { |
| | 0 | 111 | | var categories = await _cacheService.UpdateCache("ozon_categories", StoredProcedureType.GetOzonCategories); |
| | 0 | 112 | | var orderedCategories = _filterService.OrderCategoriesByName(categories); |
| | 0 | 113 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 114 | | var model = Tuple.Create(tree, "Ozon"); |
| | | 115 | | |
| | 0 | 116 | | return PartialView("_CategoryTree", model); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | [HttpGet] |
| | | 120 | | public async Task<IActionResult> GetCtCategories(string? name = null) |
| | | 121 | | { |
| | 0 | 122 | | var categories = await _cacheService.GetCategoriesFromCache("ct_categories", StoredProcedureType.GetCtCategories |
| | 0 | 123 | | var links = await _dbService.GetAllCategoryLinksAsync(); |
| | 0 | 124 | | var newCategories = await _treeService.CategoryLinkToModel(_dbService, categories, links.ToList()); |
| | 0 | 125 | | newCategories = string.IsNullOrEmpty(name) |
| | 0 | 126 | | ? newCategories |
| | 0 | 127 | | : _filterService.FilterCategoriesByName(newCategories, name); |
| | 0 | 128 | | var orderedCategories = _filterService.OrderCategoriesByName(newCategories); |
| | 0 | 129 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 130 | | var model = Tuple.Create(tree, "CT"); |
| | | 131 | | |
| | 0 | 132 | | return PartialView("_CategoryTree", model); |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | [HttpGet] |
| | | 136 | | public async Task<IActionResult> UpdateCtCategories() |
| | | 137 | | { |
| | 0 | 138 | | var categories = await _cacheService.UpdateCache("ct_categories", StoredProcedureType.GetCtCategories); |
| | 0 | 139 | | var links = await _dbService.GetAllCategoryLinksAsync(); |
| | 0 | 140 | | var newCategories = await _treeService.CategoryLinkToModel(_dbService, categories, links.ToList()); |
| | 0 | 141 | | var orderedCategories = _filterService.OrderCategoriesByName(newCategories); |
| | 0 | 142 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 143 | | var model = Tuple.Create(tree, "CT"); |
| | | 144 | | |
| | 0 | 145 | | return PartialView("_CategoryTree", model); |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | [HttpGet] |
| | | 149 | | public async Task<IActionResult> GetCtCategoriesById(int id) |
| | | 150 | | { |
| | 0 | 151 | | var categories = await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.SearchCtCategoryById, id); |
| | 0 | 152 | | var orderedCategories = _filterService.OrderCategoriesByName(categories.ToList()); |
| | 0 | 153 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 154 | | var model = Tuple.Create(tree, "CT"); |
| | | 155 | | |
| | 0 | 156 | | return PartialView("_CategoryTree", model); |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | [HttpGet] |
| | | 160 | | public async Task<IActionResult> GetCatTreeCategories(string? name = null) |
| | | 161 | | { |
| | 0 | 162 | | var categories = await GetCategoriesWithCache("cat_tree_categories", StoredProcedureType.GetCatTreeCategories, n |
| | 0 | 163 | | var tree = _treeService.BuildTree(categories); |
| | 0 | 164 | | var model = Tuple.Create(tree, "CatTree"); |
| | 0 | 165 | | return PartialView("_CategoryTree", model); |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | [HttpGet] |
| | | 169 | | public async Task<IActionResult> GetCatTreeCategoriesById(int id) |
| | | 170 | | { |
| | 0 | 171 | | var categories = await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.SearchCatTreeCategoryById, id) |
| | 0 | 172 | | var orderedCategories = _filterService.OrderCategoriesByName(categories.ToList()); |
| | 0 | 173 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 174 | | var model = Tuple.Create(tree, "CatTree"); |
| | | 175 | | |
| | 0 | 176 | | return PartialView("_CategoryTree", model); |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | [HttpGet] |
| | | 180 | | public async Task<IActionResult> UpdateCatTreeCategories() |
| | | 181 | | { |
| | 0 | 182 | | var categories = await _cacheService.UpdateCache("cat_tree_categories", StoredProcedureType.GetCatTreeCategories |
| | 0 | 183 | | var orderedCategories = _filterService.OrderCategoriesByName(categories); |
| | 0 | 184 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 185 | | var model = Tuple.Create(tree, "CatTree"); |
| | | 186 | | |
| | 0 | 187 | | return PartialView("_CategoryTree", model); |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | [HttpGet] |
| | | 191 | | public async Task<IActionResult> GetICGroupsCategories(string? name = null) |
| | | 192 | | { |
| | 0 | 193 | | var categories = await GetCategoriesWithCache("ic_groups", StoredProcedureType.GetICGroups, name); |
| | 0 | 194 | | var tree = _treeService.BuildTree(categories); |
| | 0 | 195 | | var model = Tuple.Create(tree, "IC"); |
| | | 196 | | |
| | 0 | 197 | | return PartialView("_CategoryTree", model); |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | [HttpGet] |
| | | 201 | | public async Task<IActionResult> GetICGroupsCategoriesById(int id) |
| | | 202 | | { |
| | 0 | 203 | | var categories = await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.SearchICGroupById, id); |
| | 0 | 204 | | var orderedCategories = _filterService.OrderCategoriesByName(categories.ToList()); |
| | 0 | 205 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 206 | | var model = Tuple.Create(tree, "IC"); |
| | | 207 | | |
| | 0 | 208 | | return PartialView("_CategoryTree", model); |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | [HttpGet] |
| | | 212 | | public async Task<IActionResult> UpdateICGroupsCategories() |
| | | 213 | | { |
| | 0 | 214 | | var categories = await _cacheService.UpdateCache("ic_groups", StoredProcedureType.GetICGroups); |
| | 0 | 215 | | var orderedCategories = _filterService.OrderCategoriesByName(categories); |
| | 0 | 216 | | var tree = _treeService.BuildTree(orderedCategories); |
| | 0 | 217 | | var model = Tuple.Create(tree, "IC"); |
| | | 218 | | |
| | 0 | 219 | | return PartialView("_CategoryTree", model); |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | // Остальные методы остаются без изменений |
| | | 223 | | [HttpPost] |
| | | 224 | | public async Task<IActionResult> LinkCategories([FromBody] Dictionary<string, int> selectedCategories) |
| | | 225 | | { |
| | | 226 | | try |
| | | 227 | | { |
| | 0 | 228 | | if (selectedCategories == null || selectedCategories.Count <= 1 || !selectedCategories.ContainsKey("CT")) |
| | | 229 | | { |
| | 0 | 230 | | return BadRequest(new |
| | 0 | 231 | | { |
| | 0 | 232 | | success = false, |
| | 0 | 233 | | message = "Ошибка: Должен быть выбран элемент с ключом 'CT' и должно быть более одной категории." |
| | 0 | 234 | | }); |
| | | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | var ctValue = selectedCategories["CT"]; |
| | | 238 | | |
| | 0 | 239 | | var categoryPairs = new List<Dictionary<string, int>>(); |
| | | 240 | | |
| | 0 | 241 | | foreach (var key in selectedCategories.Keys.Where(k => k != "CT")) |
| | | 242 | | { |
| | 0 | 243 | | var pair = new Dictionary<string, int> |
| | 0 | 244 | | { |
| | 0 | 245 | | { "CT", ctValue }, |
| | 0 | 246 | | { key, selectedCategories[key] } |
| | 0 | 247 | | }; |
| | | 248 | | |
| | 0 | 249 | | categoryPairs.Add(pair); |
| | | 250 | | } |
| | 0 | 251 | | foreach (var pair in categoryPairs) |
| | | 252 | | { |
| | 0 | 253 | | int ct = pair.ContainsKey("CT") ? pair["CT"] : 0; |
| | | 254 | | |
| | 0 | 255 | | var secondKey = pair.Keys.FirstOrDefault(k => k != "CT"); |
| | 0 | 256 | | var secondValue = secondKey != null ? pair[secondKey] : 0; |
| | 0 | 257 | | await _dbService.ExecuteStoredProcedureAsync( |
| | 0 | 258 | | StoredProcedureType.AddCategoryLink, |
| | 0 | 259 | | (ct, secondValue, secondKey) |
| | 0 | 260 | | ); |
| | | 261 | | } |
| | 0 | 262 | | return Ok(new { success = true, message = "Категории успешно связаны!", data = categoryPairs }); |
| | | 263 | | } |
| | 0 | 264 | | catch (Exception ex) |
| | | 265 | | { |
| | 0 | 266 | | return BadRequest(new |
| | 0 | 267 | | { success = false, message = "Ошибка при связывании категорий!", error = ex.Message }); |
| | | 268 | | } |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | [HttpPost] |
| | | 272 | | [ValidateAntiForgeryToken] |
| | | 273 | | public async Task<IActionResult> Unlink(int id) |
| | | 274 | | { |
| | 0 | 275 | | await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.DeleteCategoryLink, id); |
| | 0 | 276 | | return Ok(); |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | [HttpPost] |
| | | 280 | | public async Task<IActionResult> CreateUnifiedCategory([FromBody] Dictionary<string, string> categoryNames) |
| | | 281 | | { |
| | 0 | 282 | | int? ozonId = TryGetIntValue(categoryNames, "Ozon"); |
| | 0 | 283 | | int? volnaId = TryGetIntValue(categoryNames, "Volna"); |
| | 0 | 284 | | int? icId = TryGetIntValue(categoryNames, "IC"); |
| | 0 | 285 | | await _dbService.ExecuteStoredProcedureAsync(StoredProcedureType.InsertUnifiedCategoryIfNotExists, (ozonId, voln |
| | | 286 | | |
| | 0 | 287 | | return Ok(new { ozonId, volnaId, icId }); |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | [HttpPost] |
| | | 291 | | public async Task<IActionResult> CreateCategoryTranslation([FromBody] TranslationRequest request) |
| | | 292 | | { |
| | | 293 | | try |
| | | 294 | | { |
| | 0 | 295 | | var results = new List<object>(); |
| | | 296 | | |
| | 0 | 297 | | foreach (var translation in request.CategoryNames) |
| | | 298 | | { |
| | 0 | 299 | | var result = await _dbService.ExecuteStoredProcedureAsync( |
| | 0 | 300 | | StoredProcedureType.AddCategoryTranslations, |
| | 0 | 301 | | (translation.Value, // category_id |
| | 0 | 302 | | translation.Key, // link_type_name |
| | 0 | 303 | | request.Translation)); |
| | | 304 | | |
| | 0 | 305 | | results.Add(new { |
| | 0 | 306 | | CategoryId = translation.Value, |
| | 0 | 307 | | LinkType = translation.Key, |
| | 0 | 308 | | Result = result |
| | 0 | 309 | | }); |
| | 0 | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | return Ok(new { |
| | 0 | 313 | | Success = true, |
| | 0 | 314 | | Results = results |
| | 0 | 315 | | }); |
| | | 316 | | } |
| | 0 | 317 | | catch (Exception ex) |
| | | 318 | | { |
| | 0 | 319 | | return StatusCode(500, new { |
| | 0 | 320 | | Success = false, |
| | 0 | 321 | | Error = ex.Message |
| | 0 | 322 | | }); |
| | | 323 | | } |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | private int? TryGetIntValue(Dictionary<string, string> dict, string key) |
| | | 327 | | { |
| | 0 | 328 | | if (dict.TryGetValue(key, out var value) && int.TryParse(value, out var result)) |
| | 0 | 329 | | return result; |
| | | 330 | | |
| | 0 | 331 | | return null; |
| | | 332 | | } |
| | | 333 | | } |