Ejemplo n.º 1
0
        public async Task <ActionResult> Import(IFormFile file)
        {
            if (UnAuthorization)
            {
                return(Forbid());
            }
            if (!ModelState.IsValid)
            {
                throw new BindException(ModelState);
            }
            CustomerTypeFilter CustomerTypeFilter = new CustomerTypeFilter
            {
                Skip    = 0,
                Take    = int.MaxValue,
                Selects = CustomerTypeSelect.ALL
            };
            List <CustomerType> CustomerTypes = await CustomerTypeService.List(CustomerTypeFilter);

            StatusFilter StatusFilter = new StatusFilter
            {
                Skip    = 0,
                Take    = int.MaxValue,
                Selects = StatusSelect.ALL
            };
            List <Status> Statuses = await StatusService.List(StatusFilter);

            List <CustomerGrouping> CustomerGroupings = new List <CustomerGrouping>();

            using (ExcelPackage excelPackage = new ExcelPackage(file.OpenReadStream()))
            {
                ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault();
                if (worksheet == null)
                {
                    return(Ok(CustomerGroupings));
                }
                int StartColumn          = 1;
                int StartRow             = 1;
                int IdColumn             = 0 + StartColumn;
                int CodeColumn           = 1 + StartColumn;
                int NameColumn           = 2 + StartColumn;
                int CustomerTypeIdColumn = 3 + StartColumn;
                int ParentIdColumn       = 4 + StartColumn;
                int PathColumn           = 5 + StartColumn;
                int LevelColumn          = 6 + StartColumn;
                int StatusIdColumn       = 7 + StartColumn;
                int DescriptionColumn    = 8 + StartColumn;

                for (int i = StartRow; i <= worksheet.Dimension.End.Row; i++)
                {
                    if (string.IsNullOrEmpty(worksheet.Cells[i + StartRow, StartColumn].Value?.ToString()))
                    {
                        break;
                    }
                    string IdValue             = worksheet.Cells[i + StartRow, IdColumn].Value?.ToString();
                    string CodeValue           = worksheet.Cells[i + StartRow, CodeColumn].Value?.ToString();
                    string NameValue           = worksheet.Cells[i + StartRow, NameColumn].Value?.ToString();
                    string CustomerTypeIdValue = worksheet.Cells[i + StartRow, CustomerTypeIdColumn].Value?.ToString();
                    string ParentIdValue       = worksheet.Cells[i + StartRow, ParentIdColumn].Value?.ToString();
                    string PathValue           = worksheet.Cells[i + StartRow, PathColumn].Value?.ToString();
                    string LevelValue          = worksheet.Cells[i + StartRow, LevelColumn].Value?.ToString();
                    string StatusIdValue       = worksheet.Cells[i + StartRow, StatusIdColumn].Value?.ToString();
                    string DescriptionValue    = worksheet.Cells[i + StartRow, DescriptionColumn].Value?.ToString();

                    CustomerGrouping CustomerGrouping = new CustomerGrouping();
                    CustomerGrouping.Code        = CodeValue;
                    CustomerGrouping.Name        = NameValue;
                    CustomerGrouping.Path        = PathValue;
                    CustomerGrouping.Level       = long.TryParse(LevelValue, out long Level) ? Level : 0;
                    CustomerGrouping.Description = DescriptionValue;
                    CustomerType CustomerType = CustomerTypes.Where(x => x.Id.ToString() == CustomerTypeIdValue).FirstOrDefault();
                    CustomerGrouping.CustomerTypeId = CustomerType == null ? 0 : CustomerType.Id;
                    CustomerGrouping.CustomerType   = CustomerType;
                    Status Status = Statuses.Where(x => x.Id.ToString() == StatusIdValue).FirstOrDefault();
                    CustomerGrouping.StatusId = Status == null ? 0 : Status.Id;
                    CustomerGrouping.Status   = Status;

                    CustomerGroupings.Add(CustomerGrouping);
                }
            }
            CustomerGroupings = await CustomerGroupingService.Import(CustomerGroupings);

            if (CustomerGroupings.All(x => x.IsValidated))
            {
                return(Ok(true));
            }
            else
            {
                List <string> Errors = new List <string>();
                for (int i = 0; i < CustomerGroupings.Count; i++)
                {
                    CustomerGrouping CustomerGrouping = CustomerGroupings[i];
                    if (!CustomerGrouping.IsValidated)
                    {
                        string Error = $"Dòng {i + 2} có lỗi:";
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.Id)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.Id)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.Code)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.Code)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.Name)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.Name)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.CustomerTypeId)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.CustomerTypeId)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.ParentId)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.ParentId)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.Path)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.Path)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.Level)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.Level)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.StatusId)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.StatusId)];
                        }
                        if (CustomerGrouping.Errors.ContainsKey(nameof(CustomerGrouping.Description)))
                        {
                            Error += CustomerGrouping.Errors[nameof(CustomerGrouping.Description)];
                        }
                        Errors.Add(Error);
                    }
                }
                return(BadRequest(Errors));
            }
        }