Exemplo n.º 1
0
        public async Task <bool> Upload(IFormFile file, Models.ImportType importType)
        {
            var id = Guid.NewGuid();

            var pathToSave = Path.Combine(_appSettings.Value.MassiveImportPath, importType.ToString(), id.ToString()); //TODO: maybe savnig the user full name shuold be usefull but I wanto to keep away from strange chars..

            Directory.CreateDirectory(pathToSave);

            var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            var fullPath = Path.Combine(pathToSave, fileName);

            using (var stream = new FileStream(fullPath, FileMode.Create))
            {
                file.CopyTo(stream);
            }
            var item = new MassiveRequest();

            item.Description  = $"Import '{fileName}'";
            item.FileToImport = fullPath;
            item.Id           = id;
            item.ImportType   = (dal.Entities.ImportType)importType;
            item.ImportStatus = dal.Entities.ImportStatus.New;
            await _dbCtx.MassiveRequests.AddAsync(item);

            await _dbCtx.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 2
0
        public static MassiveRequest ToEntity(this MassiveRequestEditDto e)
        {
            if (e == null)
            {
                return(null);
            }

            var res = new MassiveRequest();

            res.Id           = e.Id;
            res.Description  = e.Description;
            res.ImportStatus = dal.Entities.ImportStatus.New;
            return(res);
        }
Exemplo n.º 3
0
        public static MassiveRequestDto ToDto(this MassiveRequest e)
        {
            if (e == null)
            {
                return(null);
            }

            var res = new MassiveRequestDto();

            res.Id            = e.Id;
            res.Description   = e.Description;
            res.Error         = e.Error;
            res.FileToImport  = e.FileToImport;
            res.ImportStatus  = (Models.ImportStatus)e.ImportStatus;
            res.ImportType    = (Models.ImportType)e.ImportType;
            res.LastExecution = e.LastExecution;
            return(res);
        }
Exemplo n.º 4
0
        private async Task ImportCustomers(MassiveRequest item)
        {
            if (!File.Exists(item.FileToImport))
            {
                item.ImportStatus  = dal.Entities.ImportStatus.Failed;
                item.Error         = $"Cannot find the specified file [{item.FileToImport}]";
                item.LastExecution = DateTime.UtcNow;
                _dbCtx.MassiveRequests.Update(item);
                await _dbCtx.SaveChangesAsync();

                return;
            }
            var          errors              = string.Empty;
            var          deatils             = string.Empty;
            CustomerType deafultCustomerType = null;
            var          customerTypes       = await _dbCtx.CustomerTypes.Where(x => !x.XDeleteDate.HasValue).ToArrayAsync();

            if (customerTypes.Length > 0)
            {
                deafultCustomerType = customerTypes[0];
            }
            var fileName = (new FileInfo(item.FileToImport)).Name;

            using (var excelWorkbook = new XLWorkbook(item.FileToImport))
            {
                var nonEmptyDataRows = excelWorkbook.Worksheet(1).RowsUsed().Skip(1); // Skip header row
                var commitBlock      = _commitBlock;
                var now = DateTime.UtcNow;
                foreach (var dataRow in nonEmptyDataRows)
                {
                    if (commitBlock < 0)
                    {
                        await _dbCtx.SaveChangesAsync();

                        commitBlock = _commitBlock;
                    }

                    var firstName = (string)dataRow.Cell("C").Value;
                    var lastName  = (string)dataRow.Cell("B").Value;
                    var cf        = (string)dataRow.Cell("G").Value;
                    var customer  = await _dbCtx.Customers.FirstOrDefaultAsync(x =>
                                                                               x.FiscalCode.ToLower() == cf.Trim().ToLower() &&
                                                                               x.LastName.ToLower() == lastName.Trim().ToLower() &&
                                                                               x.FirstName.ToLower() == firstName.Trim().ToLower()
                                                                               );

                    if (customer != null)
                    {
                        deatils += $"Skipping [{lastName} {firstName}] FiscalCode=[{cf}]: already present\n";
                        continue;
                    }
                    else
                    {
                        try
                        {
                            customer             = new Customer();
                            customer.Id          = Guid.NewGuid();
                            customer.LastName    = (string)dataRow.Cell("B").Value;
                            customer.FirstName   = (string)dataRow.Cell("C").Value;
                            customer.FullName    = $"{customer.LastName} {customer.FirstName}";
                            customer.Gender      = (string)dataRow.Cell("D").Value;
                            customer.BirthPlace  = (string)dataRow.Cell("E").Value;
                            customer.BirthDate   = (DateTime)dataRow.Cell("F").Value;
                            customer.FiscalCode  = (string)dataRow.Cell("G").Value;
                            customer.Address     = (string)dataRow.Cell("H").Value;
                            customer.City        = (string)dataRow.Cell("I").Value;
                            customer.PostalCode  = (string)dataRow.Cell("J").Value;
                            customer.State       = (string)dataRow.Cell("K").Value;
                            customer.Email       = (string)dataRow.Cell("L").Value;
                            customer.PhoneNumber = (string)dataRow.Cell("M").Value;
                            if (!dataRow.Cell("N").IsEmpty())
                            {
                                customer.MembershipFeeExpiryDate = (DateTime)dataRow.Cell("N").Value;
                                customer.MembershipLastPayDate   = now;
                                customer.IdType = deafultCustomerType == null ? Guid.Empty : deafultCustomerType.Id;
                                customer.Note   = $"Imported from {fileName} at {now:dd/MM/yyyy HH:mm}";
                            }
                            await _dbCtx.Customers.AddAsync(customer);

                            commitBlock--;
                        }
                        catch (Exception ex)
                        {
                            errors += $"Failed {cf}: {ex.Message}\n";
                        }
                    }
                }
                item.ImportStatus  = string.IsNullOrEmpty(errors) ? dal.Entities.ImportStatus.Completed : dal.Entities.ImportStatus.Failed;
                item.Description   = string.IsNullOrEmpty(deatils) ? "OK" : deatils.TrimEnd();
                item.Error         = errors.TrimEnd();
                item.LastExecution = DateTime.UtcNow;
                _dbCtx.MassiveRequests.Update(item);
                await _dbCtx.SaveChangesAsync();
            }
        }