| | | 1 | | using Microsoft.AspNetCore.Mvc; |
| | | 2 | | using OzonDomains; |
| | | 3 | | using OzonDomains.Models.MatchedRowSys; |
| | | 4 | | using OzonOrdersWeb.ViewModels.MatchFileDataViewModel; |
| | | 5 | | using OzonOrdersWeb.ViewModels.OrderViewModels; |
| | | 6 | | using OzonRepositories.Context; |
| | | 7 | | using Services.DataServices; |
| | | 8 | | using Services.ParserServices.FielParsers; |
| | | 9 | | using Services.ParserServices.FielParsers.Models; |
| | | 10 | | |
| | | 11 | | namespace OzonOrdersWeb.Controllers |
| | | 12 | | { |
| | | 13 | | [Area("Studio2")] |
| | | 14 | | public class MatchFileDataController : Controller |
| | | 15 | | { |
| | | 16 | | private readonly ExcelParser _excelParser; |
| | | 17 | | private readonly MatchedResultDataServices _matchedResultDataServices; |
| | | 18 | | private readonly MatchedRowDataServices _matchedRowDataServices; |
| | | 19 | | private readonly SavedMatchingColumnDataServices _savedMatchingColumnDataServices; |
| | | 20 | | private readonly ApplicationDbContext _ozonOrderContext; |
| | 0 | 21 | | public MatchFileDataController(ExcelParser excelParser, |
| | 0 | 22 | | MatchedResultDataServices matchedResultDataServices, |
| | 0 | 23 | | MatchedRowDataServices matchedRowDataServices, |
| | 0 | 24 | | SavedMatchingColumnDataServices savedMatchingColumnDataServices, |
| | 0 | 25 | | ApplicationDbContext ApplicationDbContext) |
| | | 26 | | { |
| | 0 | 27 | | _excelParser = excelParser; |
| | 0 | 28 | | _matchedResultDataServices = matchedResultDataServices; |
| | 0 | 29 | | _matchedRowDataServices = matchedRowDataServices; |
| | 0 | 30 | | _savedMatchingColumnDataServices = savedMatchingColumnDataServices; |
| | 0 | 31 | | _ozonOrderContext = ApplicationDbContext; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<IActionResult> Index() |
| | | 35 | | { |
| | 0 | 36 | | List<MatchedResultInfo> matchedResults = (await _matchedResultDataServices.GetBaseMatchedResults()).Select(m |
| | 0 | 37 | | { |
| | 0 | 38 | | Id = mr.Id, |
| | 0 | 39 | | MainFileName = mr.MainFileName, |
| | 0 | 40 | | ScondaryFileName = mr.ScondaryFileName, |
| | 0 | 41 | | СreationDate = mr.СreationDate |
| | 0 | 42 | | }).ToList(); |
| | 0 | 43 | | return View(new MatchFileDataViewModel() { MatchedResults = matchedResults.OrderByDescending(d => d.Сreation |
| | 0 | 44 | | SavedMatchingColumns = await _savedMatchingColumnDataServices.Get |
| | 0 | 45 | | }); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | |
| | | 49 | | |
| | | 50 | | [HttpPost] |
| | | 51 | | public async Task<IActionResult> MatchFiles( |
| | | 52 | | IFormFile file1, |
| | | 53 | | IFormFile file2, |
| | | 54 | | List<MatchingColumn> matchingColumns, |
| | | 55 | | string matchName, |
| | | 56 | | int startRow1 = 1, |
| | | 57 | | int startColumn1 = 1, |
| | | 58 | | int startRow2 = 1, |
| | | 59 | | int startColumn2 = 1) |
| | | 60 | | { |
| | 0 | 61 | | if (file1 == null || file2 == null || matchingColumns == null || !matchingColumns.Any()) |
| | | 62 | | { |
| | 0 | 63 | | ModelState.AddModelError("", "Необходимо загрузить два файла и указать столбцы для сопоставления."); |
| | 0 | 64 | | return View(); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | if(matchName != null) |
| | | 68 | | { |
| | 0 | 69 | | await _savedMatchingColumnDataServices.AddSavedMatchingColumn(new SavedMatchingColumn() { |
| | 0 | 70 | | Name = match |
| | 0 | 71 | | MatchingColu |
| | 0 | 72 | | StartRow1 = |
| | 0 | 73 | | StartColumn1 |
| | 0 | 74 | | StartRow2 = |
| | 0 | 75 | | StartColumn2 |
| | 0 | 76 | | }); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | int matchedResultsId = 0; |
| | 0 | 80 | | using (var file1Stream = file1.OpenReadStream()) |
| | 0 | 81 | | using (var file2Stream = file2.OpenReadStream()) |
| | | 82 | | { |
| | | 83 | | |
| | 0 | 84 | | List<MatchedRowModel> matchedResults = await _excelParser.MatchExcelDataAsync(file1Stream, |
| | 0 | 85 | | file2Stream, |
| | 0 | 86 | | matchingColumns, |
| | 0 | 87 | | startRow1, |
| | 0 | 88 | | startColumn1, |
| | 0 | 89 | | startRow2, |
| | 0 | 90 | | startColumn2); |
| | | 91 | | |
| | 0 | 92 | | if (!matchedResults.Any()) |
| | | 93 | | { |
| | 0 | 94 | | ViewBag.Message = "Совпадений не найдено."; |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | ViewBag.Message = $"{matchedResults.Count} совпадений найдено."; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if (matchedResults.Count() > 0) |
| | | 102 | | { |
| | 0 | 103 | | var matchedRows = matchedResults.Select(mr => new MatchedRow |
| | 0 | 104 | | { |
| | 0 | 105 | | File1Data = mr.File1Data ?? new Dictionary<string, string>(), |
| | 0 | 106 | | File2Data = mr.File2Data ?? new List<Dictionary<string, string>>() |
| | 0 | 107 | | }).ToList(); |
| | | 108 | | |
| | 0 | 109 | | var matchedResult = new MatchedResult |
| | 0 | 110 | | { |
| | 0 | 111 | | MainFileName = file1.FileName, |
| | 0 | 112 | | ScondaryFileName = file2.FileName, |
| | 0 | 113 | | MatchedRows = matchedRows, |
| | 0 | 114 | | MatchedColumns = matchingColumns, |
| | 0 | 115 | | СreationDate = DateTime.Today |
| | 0 | 116 | | }; |
| | | 117 | | |
| | 0 | 118 | | var result = await _matchedResultDataServices.AddMatchedResult(matchedResult); |
| | | 119 | | |
| | 0 | 120 | | matchedResultsId = matchedResult.Id; |
| | 0 | 121 | | int matchedCount = matchedResult.MatchedRows.Where(mr => mr.File1Data != null && mr.File2Data != nul |
| | 0 | 122 | | if (result > 0) |
| | | 123 | | { |
| | 0 | 124 | | ViewBag.Message = $"Данные сохранены в базу. {matchedCount} совпадений найдено."; |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 0 | 128 | | ViewBag.Message = "Ошибка при сохранении данных."; |
| | | 129 | | } |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | |
| | 0 | 133 | | return View("MatchResults", new MatchedRowViewModel() { MatchedResultsId = matchedResultsId, |
| | 0 | 134 | | MatchedResults = matchedResults, |
| | 0 | 135 | | MatchingColumns = matchingColumns, |
| | 0 | 136 | | MainFileName = file1.FileName, |
| | 0 | 137 | | ScondaryFileName = file2.FileName });; |
| | | 138 | | } |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | public async Task<IActionResult> GetMatchResult(int id) |
| | | 142 | | { |
| | 0 | 143 | | var matchedResult = await _matchedResultDataServices.GetMatchedResultAsync(id); |
| | 0 | 144 | | if (matchedResult == null) |
| | 0 | 145 | | return NotFound(); |
| | | 146 | | |
| | 0 | 147 | | var matchedRowModel = (matchedResult.MatchedRows ?? new List<MatchedRow>()).Select(mr => new MatchedRowModel |
| | 0 | 148 | | { |
| | 0 | 149 | | File1Data = mr.File1Data, |
| | 0 | 150 | | File2Data = mr.File2Data |
| | 0 | 151 | | }).ToList(); |
| | | 152 | | |
| | 0 | 153 | | return View("MatchResults", new MatchedRowViewModel() |
| | 0 | 154 | | { |
| | 0 | 155 | | MatchedResultsId = matchedResult.Id, |
| | 0 | 156 | | MatchedResults = matchedRowModel, |
| | 0 | 157 | | MainFileName = matchedResult.MainFileName, |
| | 0 | 158 | | ScondaryFileName = matchedResult.ScondaryFileName, |
| | 0 | 159 | | MatchingColumns = matchedResult.MatchedColumns?.ToList() ?? new List<MatchingColumn>() |
| | 0 | 160 | | }); |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | public async Task<IActionResult> ExtendMatchedData(int id) |
| | | 164 | | { |
| | 0 | 165 | | var selectMatchedResult = await _matchedResultDataServices.GetMatchedResultAsync(id); |
| | 0 | 166 | | if (selectMatchedResult == null) |
| | 0 | 167 | | return NotFound(); |
| | | 168 | | |
| | 0 | 169 | | return View(new ExtendMatchedDataViewModel() |
| | 0 | 170 | | { |
| | 0 | 171 | | Id = selectMatchedResult.Id, |
| | 0 | 172 | | MainFileName = selectMatchedResult.MainFileName, |
| | 0 | 173 | | ScondaryFileName = selectMatchedResult.ScondaryFileName |
| | 0 | 174 | | }); |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | [HttpPost] |
| | | 178 | | public async Task<IActionResult> ExtendMatchedData(int id, |
| | | 179 | | IFormFile file1, |
| | | 180 | | IFormFile file2, |
| | | 181 | | List<MatchingColumn> matchingColumns, |
| | | 182 | | int startRow1 = 1, |
| | | 183 | | int startColumn1 = 1, |
| | | 184 | | int startRow2 = 1, |
| | | 185 | | int startColumn2 = 1) |
| | | 186 | | { |
| | | 187 | | // Проверяем, что файлы были загружены |
| | 0 | 188 | | if (file1 == null || file2 == null || matchingColumns == null || !matchingColumns.Any()) |
| | | 189 | | { |
| | 0 | 190 | | ModelState.AddModelError("", "Необходимо загрузить два файла и указать столбцы для сопоставления."); |
| | 0 | 191 | | return View(); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | // Открываем потоки файлов |
| | 0 | 195 | | using (var file1Stream = file1.OpenReadStream()) |
| | 0 | 196 | | using (var file2Stream = file2.OpenReadStream()) |
| | | 197 | | { |
| | 0 | 198 | | var selectMatchedResult = await _matchedResultDataServices.GetMatchedResultAsync(id); |
| | 0 | 199 | | if (selectMatchedResult == null) |
| | | 200 | | { |
| | 0 | 201 | | ModelState.AddModelError("", "Сопоставление не найдено."); |
| | 0 | 202 | | return View(); |
| | | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | var matchedRowModel = (selectMatchedResult.MatchedRows ?? new List<MatchedRow>()).Select(mr => new Match |
| | 0 | 206 | | { |
| | 0 | 207 | | File1Data = mr.File1Data, |
| | 0 | 208 | | File2Data = mr.File2Data, |
| | 0 | 209 | | }).ToList(); |
| | | 210 | | |
| | 0 | 211 | | List<MatchedRowModel> matchedResults = await _excelParser.MatchExcelDataAsync(matchedRowModel, |
| | 0 | 212 | | file1Stream, |
| | 0 | 213 | | file2Stream, |
| | 0 | 214 | | matchingColumns, |
| | 0 | 215 | | startRow1, |
| | 0 | 216 | | startColumn1, |
| | 0 | 217 | | startRow2, |
| | 0 | 218 | | startColumn2); |
| | | 219 | | |
| | 0 | 220 | | if (!matchedResults.Any()) |
| | | 221 | | { |
| | 0 | 222 | | ViewBag.Message = "Совпадений не найдено."; |
| | | 223 | | } |
| | | 224 | | else |
| | | 225 | | { |
| | 0 | 226 | | ViewBag.Message = $"{matchedResults.Count} совпадений найдено."; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | if (matchedResults.Count() > 0) |
| | | 230 | | { |
| | 0 | 231 | | var matchedRows = matchedResults.Select(mr => new MatchedRow |
| | 0 | 232 | | { |
| | 0 | 233 | | File1Data = mr.File1Data ?? new Dictionary<string, string>(), |
| | 0 | 234 | | File2Data = mr.File2Data ?? new List<Dictionary<string, string>>() |
| | 0 | 235 | | }).ToList(); |
| | | 236 | | |
| | 0 | 237 | | var result = await _matchedResultDataServices.UpdateMatchedResult(new MatchedResult() |
| | 0 | 238 | | { |
| | 0 | 239 | | Id = selectMatchedResult.Id, |
| | 0 | 240 | | MainFileName = file1.FileName |
| | 0 | 241 | | ScondaryFileName = file2.File |
| | 0 | 242 | | MatchedColumns = matchingColu |
| | 0 | 243 | | MatchedRows = matchedRows, |
| | 0 | 244 | | СreationDate= DateTime.Today |
| | 0 | 245 | | }); |
| | | 246 | | |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | |
| | 0 | 250 | | return View("MatchResults", new MatchedRowViewModel() { MatchedResultsId = id, |
| | 0 | 251 | | MatchedResults = matchedResults, |
| | 0 | 252 | | MainFileName = file1.FileName, |
| | 0 | 253 | | MatchingColumns = matchingColumns, |
| | 0 | 254 | | ScondaryFileName = file2.FileName }); |
| | | 255 | | } |
| | 0 | 256 | | } |
| | | 257 | | |
| | | 258 | | [HttpPost] |
| | | 259 | | public async Task<IActionResult> ProcessMatchedRows([FromBody] MatchedRowsRequestModel request) |
| | | 260 | | { |
| | 0 | 261 | | if (request.MatchedRows == null || !request.MatchedRows.Any()) |
| | | 262 | | { |
| | 0 | 263 | | return BadRequest(new { message = "Нет данных для обработки." }); |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | var matchedResult = await _matchedResultDataServices.GetMatchedResultAsync(request.MatchedResultsId); |
| | 0 | 267 | | if (matchedResult == null) |
| | | 268 | | { |
| | 0 | 269 | | return BadRequest(new { message = "Не удалось найти данное сопоставление" }); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | try |
| | | 273 | | { |
| | 0 | 274 | | List<MatchedRow> matchedRows = request.MatchedRows |
| | 0 | 275 | | .Select(mr => new MatchedRow |
| | 0 | 276 | | { |
| | 0 | 277 | | File1Data = mr.File1Data ?? new Dictionary<string, string>(), |
| | 0 | 278 | | File2Data = mr.File2Data ?? new List<Dictionary<string, string>>(), |
| | 0 | 279 | | }) |
| | 0 | 280 | | .ToList(); |
| | | 281 | | |
| | 0 | 282 | | var result = await _matchedResultDataServices.UpdateMatchedResult(new MatchedResult() |
| | 0 | 283 | | { |
| | 0 | 284 | | Id = matchedResult.Id, |
| | 0 | 285 | | MainFileName = matchedResult.MainFileName, |
| | 0 | 286 | | ScondaryFileName = matchedResult.ScondaryFileName, |
| | 0 | 287 | | MatchedColumns = matchedResult.MatchedColumns ?? new List<MatchingColumn>(), |
| | 0 | 288 | | MatchedRows = matchedRows, |
| | 0 | 289 | | СreationDate = matchedResult.СreationDate |
| | 0 | 290 | | }); |
| | 0 | 291 | | return Ok(new { message = $"Данные успешно обработаны." }); |
| | | 292 | | } |
| | 0 | 293 | | catch (Exception ex) |
| | | 294 | | { |
| | 0 | 295 | | return BadRequest(new { error = ex.Message }); |
| | | 296 | | } |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | |
| | | 300 | | public async Task<IActionResult> DeleteMatchResult(int id) |
| | | 301 | | { |
| | 0 | 302 | | await _matchedResultDataServices.DeleteMatchedResult(id); |
| | 0 | 303 | | return RedirectToAction(nameof(Index)); |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | public async Task<IActionResult> GetMappingById(int id) |
| | | 307 | | { |
| | 0 | 308 | | var savedMatchingColumn = await _savedMatchingColumnDataServices.GetSavedMatchingColumnAsync(id); |
| | | 309 | | |
| | 0 | 310 | | if (savedMatchingColumn == null) |
| | | 311 | | { |
| | 0 | 312 | | return NotFound(); |
| | | 313 | | } |
| | | 314 | | |
| | 0 | 315 | | return Json(savedMatchingColumn); |
| | 0 | 316 | | } |
| | | 317 | | |
| | | 318 | | } |
| | | 319 | | } |