public void Add(AssetAttributeValue assetAttributeValue) { _assetAttributeValueRepository.Add(assetAttributeValue); }
public void Update(AssetAttributeValue assetAttributeValue) { _assetAttributeValueRepository.Update(assetAttributeValue); }
public async Task <string> ImportAssetExcelAsync(ExcelWorksheet worksheet, int userID) { string result = "Import fail " + worksheet.Name.Trim(); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(0, 60, 0))) { try { var colCount = worksheet.Dimension.End.Column; var rowCount = worksheet.Dimension.End.Row; string locationCode = worksheet.Cells[1, 3].Value.ToString().Trim(); Location location = _locationService.GetLocationByCode(locationCode); if (location == null) { result = "Do not exist current location code: " + locationCode + ". Please check again or create new!"; return(result); } for (int i = 3; i <= rowCount; i++) { result = "Error at cell " + i; string areaCode = worksheet.Cells[i, 2].Value.ToString(); Area area = _areaService.GetAreaByCode(areaCode); if (area == null) { result = result + " in area collum do not exist current area code:" + areaCode + ". Please check again or create new!"; return(result); } string assetname = worksheet.Cells[i, 3].Value.ToString(); string assetTypeName = worksheet.Cells[i, 4].Value.ToString(); string description = worksheet.Cells[i, 8].Value.ToString(); bool active = false; if (worksheet.Cells[i, 9].Value.ToString().Equals("1")) { active = true; } string assetCode = worksheet.Cells[i, 7].Value.ToString(); int quantity = Int32.Parse(worksheet.Cells[i, 5].Value.ToString().Trim()); DateTime startDate = DateTime.Now; try { startDate = Convert.ToDateTime(worksheet.Cells[i, 6].Value.ToString()); } catch (Exception e) { //set default date startDate = Convert.ToDateTime("1900-01-01"); } AssetType assetTypes = _assetTypeService.GetAssetTypeByName(assetTypeName); if (assetTypes == null) { result = result + "in Assettype collum do not exist current type import: " + assetTypeName; return(result); } // check asset is exist or not / if exist update asset Asset asset = _assetService.GetAssetByCode(assetCode); if (asset == null) { Asset newAsset = new Asset(); newAsset.Name = assetname; newAsset.AssetCode = assetCode; newAsset.Description = description; newAsset.Quantity = quantity; newAsset.StartDate = startDate; newAsset.AssetTypeID = assetTypes.ID; newAsset.ApplicationUserID = userID; newAsset.AreaID = area.ID; newAsset.Active = active; _assetService.Add(newAsset); asset = newAsset; _assetService.SaveChanges(); } else { asset.AssetCode = assetCode; asset.Name = assetname; asset.Description = description; asset.Quantity = quantity; asset.StartDate = startDate; asset.AssetTypeID = assetTypes.ID; asset.AreaID = area.ID; asset.Active = active; _assetService.Update(asset); _assetService.SaveChanges(); } for (int j = 10; j <= colCount; j++) { string attributeName = worksheet.Cells[2, j].Value.ToString().Trim(); AssetTypeAttribute assetAttribute = _attributeService.GetAttributeByName(attributeName, assetTypes.ID); string value = ""; try { if (worksheet.Cells[i, j].Value == null) { value = "N/A"; } value = worksheet.Cells[i, j].Value.ToString().Trim(); } catch (Exception e) { value = "N/A"; } AssetAttributeValue insertValue = _attributeValueService.SearchAttributeValue(asset.ID, assetAttribute.ID); if (insertValue != null) { insertValue.Value = value; _attributeValueService.Update(insertValue); } else { insertValue = new AssetAttributeValue(); insertValue.Value = value; insertValue.AssetID = asset.ID; insertValue.AssetAttributeID = assetAttribute.ID; insertValue.Active = true; _attributeValueService.Add(insertValue); } } result = result + " in Attribute value table."; } _attributeValueService.SaveChanges(); result = "Error in Commplete transison."; scope.Complete(); result = "Import successfull!"; return(result); } catch (Exception) { return(result); } } }