public UpdateResult<ProductViewModel> SaveProduct(ProductViewModel productViewModel)
        {
            UpdateResult<ProductViewModel> result = new UpdateResult<ProductViewModel>() { Success = false };

            Guid categoryId = productViewModel.CategorySubCategoryMap.CategoryId;
            Guid subCategoryId = productViewModel.CategorySubCategoryMap.SubCategoryId;

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                try
                {

                    UpdateResult<ProductViewModel> updateResult = productRepository.Update(dbContext, productViewModel);

                    if (!updateResult.Success)
                    {
                        return updateResult;
                    }

                    foreach (ProductVariantViewModel prvm in productViewModel.ProductVariants)
                    {

                        prvm.ProductId = updateResult.Entity.ProductId;

                        ProductVariant pv = dbContext.ProductVariantSet.Where(x => x.ProductVariantId == prvm.ProductVariantId).FirstOrDefault();

                        if (pv == null)
                        {
                            prvm.ProductVariantId = Guid.Empty;
                        }

                        UpdateResult<ProductVariantViewModel> pvUpdateResult = productVariantRepository.Update(prvm);

                        if (!pvUpdateResult.Success)
                        {
                            result.Success = false;
                            result.ErrorMessage = pvUpdateResult.ErrorMessage;
                            return result;
                        }

                        List<ProductVariantAttributeValue> productVariantAttributeValueMaps = dbContext.ProductVariantAttributeValueSet
                                            .Where(x => x.ProductVariantId == prvm.ProductVariantId).ToList();

                        List<Guid> attributeSelectValueIds = new List<Guid>();

                        foreach (var item in prvm.ProductVariantAttributeValues)
                        {
                            if (item.ProductAttributeId != Guid.Empty)
                            {

                                if ((!item.AttributeValueListId.HasValue
                                     || item.AttributeValueListId.Value == Guid.Empty)
                                     && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    SubCategoryAttributeMap scAttrMap = dbContext.SubCategoryAttributeMapSet
                                        .Where(x => x.ProductAttributeId == item.ProductAttributeId && x.SubCategoryId == subCategoryId).FirstOrDefault();

                                    AttributeValueList avl = new AttributeValueList()
                                    {

                                        AttributeValueListId = Guid.NewGuid(),
                                        AttributeValue = item.ProductAttributeValue,
                                        CategoryId = categoryId,
                                        SubCategoryAttributeMapID = scAttrMap.SubCategoryAttributeMapId,
                                    };

                                    dbContext.AttributeValueListSet.Add(avl);

                                    item.AttributeValueListId = avl.AttributeValueListId;

                                    ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                    {
                                        ProductVariantId = prvm.ProductVariantId,
                                        AttributeValueListId = item.AttributeValueListId.Value,
                                    };

                                    dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                    dbContext.SaveChanges();

                                }
                                else if (item.AttributeValueListId.HasValue && item.AttributeValueListId != Guid.Empty && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    ProductVariantAttributeValue pvAttrValue = productVariantAttributeValueMaps
                                                        .Where(x => x.AttributeValueListId == item.AttributeValueListId.Value)
                                                        .FirstOrDefault();

                                    if (pvAttrValue != null)
                                    {
                                        productVariantAttributeValueMaps.Remove(pvAttrValue);
                                    }
                                    else
                                    {
                                        ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                        {
                                            ProductVariantId = prvm.ProductVariantId,
                                            AttributeValueListId = item.AttributeValueListId.Value,
                                        };

                                        dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                        dbContext.SaveChanges();

                                    }
                                }
                            }

                        }

                        if (productVariantAttributeValueMaps != null && productVariantAttributeValueMaps.Count > 0)
                        {
                            foreach (var pvavm in productVariantAttributeValueMaps)
                            {
                                dbContext.ProductVariantAttributeValueSet.Remove(pvavm);

                            }

                            dbContext.SaveChanges();
                        }
                    }

                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.ErrorMessage = ex.Message;
                }
            }

            return result;
        }
        public UpdateResult<ProductViewModel> SaveProduct(ProductViewModel productViewModel)
        {
            UpdateResult<ProductViewModel> result = new UpdateResult<ProductViewModel>() { Success = false };

            Guid categoryId = productViewModel.CategorySubCategoryMap.CategoryId;
            Guid subCategoryId = productViewModel.CategorySubCategoryMap.SubCategoryId;

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                try
                {
                    string categoryCode = categoryRepository.GetCategoryCode(categoryId);

                    string subCategoryCode = subCategoryRepository.GetSubCategoryCode(subCategoryId);

                    string productName = Utility.GetNormalizedString(productViewModel.ProductName);

                    string firstTwo = productName.Substring(0, 2).ToUpper();

                    //string strSeed = DateTime.Now.ToString("MMddyyHHmm");

                    //int numberSeed = 0;

                    //if(!int.TryParse(strSeed, out numberSeed))
                    //{
                    //    numberSeed = int.MaxValue;
                    //}

                    //int seed = int.Parse(strSeed);

                    UpdateResult<ProductViewModel> updateResult = productRepository.Update(dbContext, productViewModel);

                    if (!updateResult.Success)
                    {
                        return updateResult;
                    }

                    foreach (ProductVariantViewModel prvm in productViewModel.ProductVariants)
                    {

                        prvm.ProductId = updateResult.Entity.ProductId;

                        ProductVariant pv = dbContext.ProductVariantSet.Where(x => x.ProductVariantId == prvm.ProductVariantId).FirstOrDefault();

                        if (pv == null)
                        {
                            prvm.ProductVariantId = Guid.Empty;
                            Random random = new Random();

                            string randomNumber = random.Next(999999).ToString().PadLeft(10, "0"[0]);

                            string skuCode = string.Format("ST-{0}{1}{2}{3}", categoryCode, subCategoryCode, firstTwo, randomNumber);

                            prvm.SKUCode = skuCode;
                        }

                        UpdateResult<ProductVariantViewModel> pvUpdateResult = productVariantRepository.Update(prvm);

                        if (!pvUpdateResult.Success)
                        {
                            result.Success = false;
                            result.ErrorMessage = pvUpdateResult.ErrorMessage;
                            return result;
                        }

                        List<ProductVariantAttributeValue> productVariantAttributeValueMaps = dbContext.ProductVariantAttributeValueSet
                                            .Where(x => x.ProductVariantId == prvm.ProductVariantId).ToList();

                        List<Guid> attributeSelectValueIds = new List<Guid>();

                        foreach (var item in prvm.ProductVariantAttributeValues)
                        {
                            if (item.ProductAttributeId != Guid.Empty)
                            {

                                if ((!item.AttributeValueListId.HasValue
                                     || item.AttributeValueListId.Value == Guid.Empty)
                                     && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    SubCategoryAttributeMap scAttrMap = dbContext.SubCategoryAttributeMapSet
                                        .Where(x => x.ProductAttributeId == item.ProductAttributeId && x.SubCategoryId == subCategoryId).FirstOrDefault();

                                    AttributeValueList avl = new AttributeValueList()
                                    {

                                        AttributeValueListId = Guid.NewGuid(),
                                        AttributeValue = item.ProductAttributeValue,
                                        CategoryId = categoryId,
                                        SubCategoryAttributeMapID = scAttrMap.SubCategoryAttributeMapId,
                                    };

                                    dbContext.AttributeValueListSet.Add(avl);

                                    item.AttributeValueListId = avl.AttributeValueListId;

                                    ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                    {
                                        ProductVariantId = prvm.ProductVariantId,
                                        AttributeValueListId = item.AttributeValueListId.Value,
                                    };

                                    dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                    dbContext.SaveChanges();

                                }
                                else if (item.AttributeValueListId.HasValue && item.AttributeValueListId != Guid.Empty && !string.IsNullOrEmpty(item.ProductAttributeValue))
                                {
                                    ProductVariantAttributeValue pvAttrValue = productVariantAttributeValueMaps
                                                        .Where(x => x.AttributeValueListId == item.AttributeValueListId.Value)
                                                        .FirstOrDefault();

                                    if (pvAttrValue != null)
                                    {
                                        productVariantAttributeValueMaps.Remove(pvAttrValue);
                                    }
                                    else
                                    {
                                        ProductVariantAttributeValue newPVAttrValue = new ProductVariantAttributeValue()
                                        {
                                            ProductVariantId = prvm.ProductVariantId,
                                            AttributeValueListId = item.AttributeValueListId.Value,
                                        };

                                        dbContext.ProductVariantAttributeValueSet.Add(newPVAttrValue);
                                        dbContext.SaveChanges();

                                    }
                                }
                            }

                        }

                        if (productVariantAttributeValueMaps != null && productVariantAttributeValueMaps.Count > 0)
                        {
                            foreach (var pvavm in productVariantAttributeValueMaps)
                            {
                                dbContext.ProductVariantAttributeValueSet.Remove(pvavm);

                            }

                            dbContext.SaveChanges();
                        }
                    }

                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Success = false;
                    result.ErrorMessage = ex.Message;
                }
            }

            return result;
        }