public void SincronizaProdutos() { RefreshToken(); var products = _productService.SearchProducts(categoryIds: null, pageSize: int.MaxValue, showHidden: true); ProductResponse[] GetProductResponse = null; ProductResponse ProductResponse = null; CategoryResponse[] CategoryResponse = null; var ContaAzulMiscSettings = _settingService.LoadSetting <ContaAzulMiscSettings>(); foreach (var item in products) { var product = new ProductMessage(); var categoria = "?name=Mercadoria para Revenda"; //busca a categoria no conta azul para obter o id: using (var getcategory = new GetCategory(ContaAzulMiscSettings.UseSandbox)) CategoryResponse = getcategory.CreateAsync(ContaAzulMiscSettings.access_token, categoria).ConfigureAwait(false).GetAwaiter().GetResult(); // var categoria = _categoryService.GetProductCategoryById(item.Id); product.name = item.Name; product.value = Math.Round(item.Price, 1); product.cost = Math.Round(item.ProductCost); product.available_stock = item.StockQuantity; product.net_weight = Math.Round(item.Weight, 3); product.category_id = CategoryResponse[0].id; product.gross_weight = Math.Round(item.Weight, 3); try { var filtro = "?name=" + item.Name; using (var getproduct = new GetProduct(ContaAzulMiscSettings.UseSandbox)) GetProductResponse = getproduct.CreateAsync(null, ContaAzulMiscSettings.access_token, filtro).ConfigureAwait(false).GetAwaiter().GetResult(); if (GetProductResponse.Count() > 0) { var productTable = _contaAzulProductService.GetProduct(GetProductResponse[0]); //caso ele não exista na tabela relacional do banco, insere e atualiza no conta azul if (productTable == null) { using (var productCreation = new ProductCreation(ContaAzulMiscSettings.UseSandbox)) ProductResponse = productCreation.CreateAsyncUpdate(product, GetProductResponse[0].id.ToString(), ContaAzulMiscSettings.access_token).ConfigureAwait(false).GetAwaiter().GetResult(); if (ProductResponse != null) { var productContaAzul = new ProductContaAzul(); productContaAzul.ContaAzulId = ProductResponse.id; productContaAzul.ProductId = item.Id; productContaAzul.DataCriacao = DateTime.Now; _contaAzulProductService.InsertProduct(productContaAzul); } } else {//caso já existe no banco, só verifica se houve alteração e faz o update no conta azul product.id = productTable.ContaAzulId.ToString(); var objetoContaAzul = JsonConvert.SerializeObject(GetProductResponse[0]); var objetoAtualizar = JsonConvert.SerializeObject(product); var data = objetoAtualizar.Equals(objetoContaAzul); if (!objetoAtualizar.Equals(objetoContaAzul)) { using (var productCreation = new ProductCreation(ContaAzulMiscSettings.UseSandbox)) ProductResponse = productCreation.CreateAsyncUpdate(product, productTable.ContaAzulId.ToString(), ContaAzulMiscSettings.access_token).ConfigureAwait(false).GetAwaiter().GetResult(); } } } else {//caso ele não exista no conta azul, faz a inserção dele no conta azul e no banco de dados var data2 = JsonConvert.SerializeObject(product); using (var productCreation = new ProductCreation(ContaAzulMiscSettings.UseSandbox)) ProductResponse = productCreation.CreateAsync(product, ContaAzulMiscSettings.access_token).ConfigureAwait(false).GetAwaiter().GetResult(); if (ProductResponse != null) { var productContaAzul = new ProductContaAzul(); productContaAzul.ContaAzulId = ProductResponse.id; productContaAzul.ProductId = item.Id; productContaAzul.DataCriacao = DateTime.Now; _contaAzulProductService.InsertProduct(productContaAzul); } } } catch (Exception ex) { _logger.Error(ex.Message, ex); } } }