< Summary

Information
Class: OzonOrdersWeb.Controllers.MatchFileDataController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/MatchFileDataController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 180
Coverable lines: 180
Total lines: 319
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 78
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Index()100%210%
MatchFiles()0%812280%
GetMatchResult()0%7280%
ExtendMatchedData()0%620%
ExtendMatchedData()0%420200%
ProcessMatchedRows()0%7280%
DeleteMatchResult()100%210%
GetMappingById()0%620%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/MatchFileDataController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc;
 2using OzonDomains;
 3using OzonDomains.Models.MatchedRowSys;
 4using OzonOrdersWeb.ViewModels.MatchFileDataViewModel;
 5using OzonOrdersWeb.ViewModels.OrderViewModels;
 6using OzonRepositories.Context;
 7using Services.DataServices;
 8using Services.ParserServices.FielParsers;
 9using Services.ParserServices.FielParsers.Models;
 10
 11namespace 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;
 021        public MatchFileDataController(ExcelParser excelParser,
 022                                       MatchedResultDataServices matchedResultDataServices,
 023                                       MatchedRowDataServices matchedRowDataServices,
 024                                       SavedMatchingColumnDataServices savedMatchingColumnDataServices,
 025                                       ApplicationDbContext ApplicationDbContext)
 26        {
 027            _excelParser = excelParser;
 028            _matchedResultDataServices = matchedResultDataServices;
 029            _matchedRowDataServices = matchedRowDataServices;
 030            _savedMatchingColumnDataServices = savedMatchingColumnDataServices;
 031            _ozonOrderContext = ApplicationDbContext;
 032        }
 33
 34        public async Task<IActionResult> Index()
 35        {
 036            List<MatchedResultInfo> matchedResults = (await _matchedResultDataServices.GetBaseMatchedResults()).Select(m
 037            {
 038                Id = mr.Id,
 039                MainFileName = mr.MainFileName,
 040                ScondaryFileName = mr.ScondaryFileName,
 041                СreationDate = mr.СreationDate
 042            }).ToList();
 043            return View(new MatchFileDataViewModel() { MatchedResults = matchedResults.OrderByDescending(d => d.Сreation
 044                                                       SavedMatchingColumns = await _savedMatchingColumnDataServices.Get
 045                                                     });
 046        }
 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        {
 061            if (file1 == null || file2 == null || matchingColumns == null || !matchingColumns.Any())
 62            {
 063                ModelState.AddModelError("", "Необходимо загрузить два файла и указать столбцы для сопоставления.");
 064                return View();
 65            }
 66
 067            if(matchName != null)
 68            {
 069                await _savedMatchingColumnDataServices.AddSavedMatchingColumn(new SavedMatchingColumn() {
 070                                                                                                            Name = match
 071                                                                                                            MatchingColu
 072                                                                                                            StartRow1 = 
 073                                                                                                            StartColumn1
 074                                                                                                            StartRow2 = 
 075                                                                                                            StartColumn2
 076                                                                                                        });
 77            }
 78
 079            int matchedResultsId = 0;
 080            using (var file1Stream = file1.OpenReadStream())
 081            using (var file2Stream = file2.OpenReadStream())
 82            {
 83
 084                List<MatchedRowModel> matchedResults = await _excelParser.MatchExcelDataAsync(file1Stream,
 085                                                                                              file2Stream,
 086                                                                                              matchingColumns,
 087                                                                                              startRow1,
 088                                                                                              startColumn1,
 089                                                                                              startRow2,
 090                                                                                              startColumn2);
 91
 092                if (!matchedResults.Any())
 93                {
 094                    ViewBag.Message = "Совпадений не найдено.";
 95                }
 96                else
 97                {
 098                    ViewBag.Message = $"{matchedResults.Count} совпадений найдено.";
 99                }
 100
 0101                if (matchedResults.Count() > 0)
 102                {
 0103                    var matchedRows = matchedResults.Select(mr => new MatchedRow
 0104                    {
 0105                        File1Data = mr.File1Data ?? new Dictionary<string, string>(),
 0106                        File2Data = mr.File2Data ?? new List<Dictionary<string, string>>()
 0107                    }).ToList();
 108
 0109                    var matchedResult = new MatchedResult
 0110                    {
 0111                        MainFileName = file1.FileName,
 0112                        ScondaryFileName = file2.FileName,
 0113                        MatchedRows = matchedRows,
 0114                        MatchedColumns = matchingColumns,
 0115                        СreationDate = DateTime.Today
 0116                    };
 117
 0118                    var result = await _matchedResultDataServices.AddMatchedResult(matchedResult);
 119
 0120                    matchedResultsId = matchedResult.Id;
 0121                    int matchedCount = matchedResult.MatchedRows.Where(mr => mr.File1Data != null && mr.File2Data != nul
 0122                    if (result > 0)
 123                    {
 0124                        ViewBag.Message = $"Данные сохранены в базу. {matchedCount} совпадений найдено.";
 125                    }
 126                    else
 127                    {
 0128                        ViewBag.Message = "Ошибка при сохранении данных.";
 129                    }
 0130                }
 131
 132
 0133                return View("MatchResults", new MatchedRowViewModel() { MatchedResultsId = matchedResultsId,
 0134                                                                        MatchedResults = matchedResults,
 0135                                                                        MatchingColumns = matchingColumns,
 0136                                                                        MainFileName = file1.FileName,
 0137                                                                        ScondaryFileName = file2.FileName });;
 138            }
 0139        }
 140
 141        public async Task<IActionResult> GetMatchResult(int id)
 142        {
 0143            var matchedResult = await _matchedResultDataServices.GetMatchedResultAsync(id);
 0144            if (matchedResult == null)
 0145                return NotFound();
 146
 0147            var matchedRowModel = (matchedResult.MatchedRows ?? new List<MatchedRow>()).Select(mr => new MatchedRowModel
 0148            {
 0149                File1Data = mr.File1Data,
 0150                File2Data = mr.File2Data
 0151            }).ToList();
 152
 0153            return View("MatchResults", new MatchedRowViewModel()
 0154            {
 0155                MatchedResultsId = matchedResult.Id,
 0156                MatchedResults = matchedRowModel,
 0157                MainFileName = matchedResult.MainFileName,
 0158                ScondaryFileName = matchedResult.ScondaryFileName,
 0159                MatchingColumns = matchedResult.MatchedColumns?.ToList() ?? new List<MatchingColumn>()
 0160            });
 0161        }
 162
 163        public async Task<IActionResult> ExtendMatchedData(int id)
 164        {
 0165            var selectMatchedResult = await _matchedResultDataServices.GetMatchedResultAsync(id);
 0166            if (selectMatchedResult == null)
 0167                return NotFound();
 168
 0169            return View(new ExtendMatchedDataViewModel()
 0170            {
 0171                Id = selectMatchedResult.Id,
 0172                MainFileName = selectMatchedResult.MainFileName,
 0173                ScondaryFileName = selectMatchedResult.ScondaryFileName
 0174            });
 0175        }
 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            // Проверяем, что файлы были загружены
 0188            if (file1 == null || file2 == null || matchingColumns == null || !matchingColumns.Any())
 189            {
 0190                ModelState.AddModelError("", "Необходимо загрузить два файла и указать столбцы для сопоставления.");
 0191                return View();
 192            }
 193
 194            // Открываем потоки файлов
 0195            using (var file1Stream = file1.OpenReadStream())
 0196            using (var file2Stream = file2.OpenReadStream())
 197            {
 0198                var selectMatchedResult = await _matchedResultDataServices.GetMatchedResultAsync(id);
 0199                if (selectMatchedResult == null)
 200                {
 0201                    ModelState.AddModelError("", "Сопоставление не найдено.");
 0202                    return View();
 203                }
 204
 0205                var matchedRowModel = (selectMatchedResult.MatchedRows ?? new List<MatchedRow>()).Select(mr => new Match
 0206                {
 0207                    File1Data = mr.File1Data,
 0208                    File2Data = mr.File2Data,
 0209                }).ToList();
 210
 0211                List<MatchedRowModel> matchedResults = await _excelParser.MatchExcelDataAsync(matchedRowModel,
 0212                                                                                              file1Stream,
 0213                                                                                              file2Stream,
 0214                                                                                              matchingColumns,
 0215                                                                                              startRow1,
 0216                                                                                              startColumn1,
 0217                                                                                              startRow2,
 0218                                                                                              startColumn2);
 219
 0220                if (!matchedResults.Any())
 221                {
 0222                    ViewBag.Message = "Совпадений не найдено.";
 223                }
 224                else
 225                {
 0226                    ViewBag.Message = $"{matchedResults.Count} совпадений найдено.";
 227                }
 228
 0229                if (matchedResults.Count() > 0)
 230                {
 0231                    var matchedRows = matchedResults.Select(mr => new MatchedRow
 0232                    {
 0233                        File1Data = mr.File1Data ?? new Dictionary<string, string>(),
 0234                        File2Data = mr.File2Data ?? new List<Dictionary<string, string>>()
 0235                    }).ToList();
 236
 0237                    var result = await _matchedResultDataServices.UpdateMatchedResult(new MatchedResult()
 0238                                                                                        {
 0239                                                                                           Id = selectMatchedResult.Id,
 0240                                                                                           MainFileName = file1.FileName
 0241                                                                                           ScondaryFileName = file2.File
 0242                                                                                           MatchedColumns = matchingColu
 0243                                                                                           MatchedRows = matchedRows,
 0244                                                                                           СreationDate= DateTime.Today
 0245                                                                                        });
 246
 247                }
 248
 249
 0250                return View("MatchResults", new MatchedRowViewModel() { MatchedResultsId = id,
 0251                                                                        MatchedResults = matchedResults,
 0252                                                                        MainFileName = file1.FileName,
 0253                                                                        MatchingColumns = matchingColumns,
 0254                                                                        ScondaryFileName = file2.FileName });
 255            }
 0256        }
 257
 258        [HttpPost]
 259        public async Task<IActionResult> ProcessMatchedRows([FromBody] MatchedRowsRequestModel request)
 260        {
 0261            if (request.MatchedRows == null || !request.MatchedRows.Any())
 262            {
 0263                return BadRequest(new { message = "Нет данных для обработки." });
 264            }
 265
 0266            var matchedResult = await _matchedResultDataServices.GetMatchedResultAsync(request.MatchedResultsId);
 0267            if (matchedResult == null)
 268            {
 0269                return BadRequest(new { message = "Не удалось найти данное сопоставление" });
 270            }
 271
 272            try
 273            {
 0274                List<MatchedRow> matchedRows = request.MatchedRows
 0275                .Select(mr => new MatchedRow
 0276                {
 0277                    File1Data = mr.File1Data ?? new Dictionary<string, string>(),
 0278                    File2Data = mr.File2Data ?? new List<Dictionary<string, string>>(),
 0279                })
 0280                .ToList();
 281
 0282                var result = await _matchedResultDataServices.UpdateMatchedResult(new MatchedResult()
 0283                {
 0284                    Id = matchedResult.Id,
 0285                    MainFileName = matchedResult.MainFileName,
 0286                    ScondaryFileName = matchedResult.ScondaryFileName,
 0287                    MatchedColumns = matchedResult.MatchedColumns ?? new List<MatchingColumn>(),
 0288                    MatchedRows = matchedRows,
 0289                    СreationDate = matchedResult.СreationDate
 0290                });
 0291                return Ok(new { message = $"Данные успешно обработаны." });
 292            }
 0293            catch (Exception ex)
 294            {
 0295                return BadRequest(new { error = ex.Message });
 296            }
 0297        }
 298
 299
 300        public async Task<IActionResult> DeleteMatchResult(int id)
 301        {
 0302            await _matchedResultDataServices.DeleteMatchedResult(id);
 0303            return RedirectToAction(nameof(Index));
 0304        }
 305
 306        public async Task<IActionResult> GetMappingById(int id)
 307        {
 0308            var savedMatchingColumn = await _savedMatchingColumnDataServices.GetSavedMatchingColumnAsync(id);
 309
 0310            if (savedMatchingColumn == null)
 311            {
 0312                return NotFound();
 313            }
 314
 0315            return Json(savedMatchingColumn);
 0316        }
 317
 318    }
 319}