Пример #1
0
        public IResult ImportMaterialData(IFormFile uploadFile)
        {
            var importedFileResult = new ImportedData();

            if (Path.GetExtension(uploadFile.FileName) == ".csv")
            {
                importedFileResult = new CsvFileReader().ProcessFile(uploadFile.OpenReadStream());
            }
            var result = _materialManagerService.ImportMaterials(importedFileResult);

            return(result);
        }
Пример #2
0
        internal void InitReadData()
        {
            while (NextRow())
            {
                var newRow = new T();
                foreach (var column in Columns)
                {
                    var value = TryReadColumnInfo(column.Index);
                    SetColumnValue(newRow, column, value);
                }

                ImportedData.Add(newRow);
            }
        }
        /// <summary>
        /// Map imported row key value pair to material model
        /// </summary>
        /// <param name="dataToImport"></param>
        /// <returns></returns>
        private List <Material> MapImportedMaterialToModel(ImportedData dataToImport)
        {
            var emailClaim = ((ClaimsIdentity)_principal.Identity).GetActiveUserId();
            var materials  = new List <Material>();

            if (dataToImport.RowData != null && dataToImport.RowData.Any())
            {
                materials = dataToImport.RowData.Select(t =>
                {
                    var materialModel = new Material
                    {
                        Materialnumber       = t["Material"],
                        Description          = t["Description"],
                        ManufactorPartNumber = t["Manufacturer Part no"],
                        ManufactorName       = t["Manufacturer Name"],
                        Owner                   = t["Eier"],
                        License                 = t["Lisens"],
                        Type                    = t["Type"],
                        GrossWeight             = t["Gross Weight"],
                        SizeDimension           = t["Size/dimens."],
                        Quantity                = t["Antall på lager pr dato"],
                        IsSparePart             = false,
                        PartProperties          = new List <PartProperty>(),
                        Documents               = new List <ObjectId>(),
                        TechnicalSpecifications = new List <TechnicalSpecification>(),
                        IsActive                = true,
                        CreatedBy               = emailClaim,
                        CreatedDate             = GenericHelper.CurrentDate,
                        ModifiedBy              = emailClaim,
                        ModifiedDate            = GenericHelper.CurrentDate
                    };
                    return(materialModel);
                }).ToList();
            }
            return(materials);
        }
        /// <summary>
        /// Import material data
        /// </summary>
        /// <param name="dataToImport"></param>
        /// <returns></returns>
        public IResult ImportMaterials(ImportedData dataToImport)
        {
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                // 1. Map the incoming data to models
                var mappedMaterialModels = MapImportedMaterialToModel(dataToImport);

                // 2. Get the list of new material number(material) as whole.
                var newMaterialNums = new List <string>();
                if (mappedMaterialModels.Any())
                {
                    newMaterialNums = mappedMaterialModels.Select(t => t.Materialnumber).ToList();
                }

                // 3. Search the above new material numbers in db and find out the already exist in db.
                var materialNumsDb = new List <string>();
                if (newMaterialNums.Any())
                {
                    materialNumsDb = GetAlreadyAddedMaterialNumber(newMaterialNums);
                }

                // 4. Prepare seperate list for items to insert and update collection
                List <Material> materialsForUpdate;
                List <Material> materialsForInsert;
                if (materialNumsDb != null && materialNumsDb.Any())
                {
                    materialsForInsert = mappedMaterialModels.Where(t => !materialNumsDb.Contains(t.Materialnumber))
                                         .ToList();
                    materialsForUpdate = mappedMaterialModels.Where(t => materialNumsDb.Contains(t.Materialnumber))
                                         .ToList();
                }
                else
                {
                    materialsForInsert = mappedMaterialModels;
                    materialsForUpdate = new List <Material>();
                }

                // 5. Insert the material in bulk to db except the already exist
                if (materialsForInsert.Any())
                {
                    _materialRepository.InsertMany(materialsForInsert);
                }

                // 6. Update the Already exist materials
                if (materialsForUpdate.Any())
                {
                    materialsForUpdate.ForEach(m =>
                    {
                        var updateDefinition = Builders <Material> .Update.
                                               Set(x => x.Description, m.Description).
                                               Set(x => x.ManufactorPartNumber, m.ManufactorPartNumber).
                                               Set(x => x.ManufactorName, m.ManufactorName).
                                               Set(x => x.Owner, m.Owner).
                                               Set(x => x.License, m.License).
                                               Set(x => x.Type, m.Type).
                                               Set(x => x.GrossWeight, m.GrossWeight).
                                               Set(x => x.SizeDimension, m.SizeDimension).
                                               Set(x => x.Quantity, m.Quantity).
                                               Set(x => x.ModifiedDate, GenericHelper.CurrentDate);
                        _materialRepository.UpdateOne(t => t.Materialnumber.Equals(m.Materialnumber), updateDefinition);
                    });
                }

                // 7. Map the result with viewmodel
                var importResult = new MaterialImportResult
                {
                    InsertedCount = materialsForInsert.Count,
                    UpdatedCount  = materialsForUpdate.Count
                };

                result.Body = importResult;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status  = Status.Fail;
            }
            return(result);
        }