示例#1
0
        public ImportResult ProcessImport(FileStream dataStream, ImportParametersViewModel parameters, string userId)
        {
            var user = UserService.GetUserById(userId);

            if (user == null)
            {
                throw new Exception("Не вдалось завантажити дані користувача.");
            }

            SetImportStrategy(parameters.ImportMethod);

            var parsedImport  = ImportStrategy.ImportData(dataStream, parameters);
            var currentImport = ImportRepository.SaveImport(new UserImport
            {
                User         = user,
                FileName     = new FileInfo(dataStream.Name).Name,
                RecordsCount = parsedImport.Data.Count + parsedImport.ErrorsCount,
                ErrorsCount  = parsedImport.ErrorsCount,
                ImportDate   = DateTime.Now
            });

            ImportRepository.SaveImportParameters(ParseImportParameters(currentImport, parameters.Parameters));

            ImportRepository.SaveImportHeaders(parameters.HeaderParameters.Select(hp => new ImportHeader
            {
                Import        = currentImport,
                HeaderName    = hp.HeaderName,
                DataType      = ImportRepository.LoadDataTypeById((int)hp.DataType),
                PriorityLevel = ImportRepository.LoadPriorityLevelByPriority((int)hp.PriorityLevel)
            }).ToList());

            var importHeaders = ImportRepository.LoadImportHeadersByImportId(currentImport.Id);
            var importData    = new List <ImportData>();

            foreach (var dataRow in parsedImport.Data)
            {
                var rowCells = new List <ImportData>();
                foreach (var header in importHeaders)
                {
                    if (dataRow.ContainsKey(header.HeaderName))
                    {
                        rowCells.Add(new ImportData
                        {
                            Header    = header,
                            RowNumber = parsedImport.Data.IndexOf(dataRow) + 1,
                            Value     = dataRow[header.HeaderName]
                        });
                    }
                }
                importData.AddRange(rowCells);
            }

            ImportRepository.SaveImportData(importData);

            return(new ImportResult
            {
                Result = OperationResult.Success,
                ErrorFilePath = parsedImport.ErrorFilePath,
                Message = $"Опрацьовано записів: {currentImport.RecordsCount}. З них не розпізнано: {parsedImport.ErrorsCount}"
            });
        }