public void Execute() { _contaAzulService.RefreshToken(); var customers = _customerService.GetAllCustomers(); // var storeScope = GetActiveStoreScopeConfiguration(_storeService, _workContext); CustomerResponse[] GetCustomerResponse = null; CustomerResponse CustomerResponse = null; var ContaAzulMiscSettings = _settingService.LoadSetting <ContaAzulMiscSettings>(); var number = string.Empty; var complement = string.Empty; var cpfCnpj = string.Empty; foreach (var item in customers) { var customer = new CustomerMessage(); new AddressHelper(_addressAttributeParser, _workContext).GetCustomNumberAndComplement(item.BillingAddress != null ? item.BillingAddress.CustomAttributes : null, out number, out complement, out cpfCnpj); customer.name = item.BillingAddress != null?AddressHelper.GetFullName(item.BillingAddress) : null; customer.companyName = item.BillingAddress != null ? item.BillingAddress.Company : null; customer.email = item.Email; customer.personType = "NATURAL"; customer.stateRegistrationType = "NO_CONTRIBUTOR"; customer.mobilePhone = item.BillingAddress != null ? item.BillingAddress.PhoneNumber : null; customer.address.city.name = item.BillingAddress != null ? item.BillingAddress.City : null; customer.address.state.name = item.BillingAddress != null ? item.BillingAddress.StateProvince != null ? item.BillingAddress.StateProvince.Name : null : null; customer.address.zipCode = item.BillingAddress != null ? item.BillingAddress.ZipPostalCode : null; customer.address.street = item.BillingAddress != null ? item.BillingAddress.Address1 : null; customer.address.complement = complement; customer.address.number = number; customer.document = cpfCnpj == "" ? null : cpfCnpj; try { var filtro = "?search="; if (cpfCnpj == string.Empty) { filtro = filtro + item.Email; } else { filtro = filtro + cpfCnpj; } using (var getcustomer = new GetCustomer(ContaAzulMiscSettings.UseSandbox)) GetCustomerResponse = getcustomer.CreateAsync(null, ContaAzulMiscSettings.access_token, filtro).ConfigureAwait(false).GetAwaiter().GetResult(); //busca por cpf conta azul, se existir, verifica se já foi adicionado na tabela do banco if (GetCustomerResponse.Count() > 0) { var customerTable = _contaAzulCustomerService.GetCustomer(GetCustomerResponse[0]); //caso ele não exista na tabela relacional do banco, insere e atualiza no conta azul if (customerTable == null) { var customerContaAzul = new CustomerContaAzul(); customerContaAzul.ContaAzulId = GetCustomerResponse[0].id; customerContaAzul.CustomerId = item.Id; customerContaAzul.DataCriacao = DateTime.Now; _contaAzulCustomerService.InsertCustomer(customerContaAzul); customer.id = customerTable.ContaAzulId.ToString(); customer.address.city.name = null; var data1 = JsonConvert.SerializeObject(GetCustomerResponse[0]); var data2 = JsonConvert.SerializeObject(customer); // var data = data2.Equals(data1); if (!data1.Equals(data2)) { //se ele já existe na tabela, só faz o update no conta azul using (var customerCreation = new CustomerCreation(ContaAzulMiscSettings.UseSandbox)) CustomerResponse = customerCreation.CreateAsyncUpdate(customer, GetCustomerResponse[0].id.ToString(), ContaAzulMiscSettings.access_token).ConfigureAwait(false).GetAwaiter().GetResult(); } } else { customer.id = customerTable.ContaAzulId.ToString(); customer.address.city.name = null; var data1 = JsonConvert.SerializeObject(GetCustomerResponse[0]); var data2 = JsonConvert.SerializeObject(customer); var data = data2.Equals(data1); if (!data1.Equals(data2)) { //se ele já existe na tabela, só faz o update no conta azul using (var customerCreation = new CustomerCreation(ContaAzulMiscSettings.UseSandbox)) CustomerResponse = customerCreation.CreateAsyncUpdate(customer, customerTable.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(customer); using (var customerCreation = new CustomerCreation(ContaAzulMiscSettings.UseSandbox)) CustomerResponse = customerCreation.CreateAsync(customer, ContaAzulMiscSettings.access_token).ConfigureAwait(false).GetAwaiter().GetResult(); if (CustomerResponse != null) { var customerContaAzul = new CustomerContaAzul(); customerContaAzul.ContaAzulId = CustomerResponse.id; customerContaAzul.CustomerId = item.Id; customerContaAzul.DataCriacao = DateTime.Now; _contaAzulCustomerService.InsertCustomer(customerContaAzul); } } } catch (Exception ex) { _logger.Error(ex.Message, ex); } } }
public void Execute() { _contaAzulService.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>(); 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(); foreach (var item in products) { var product = new ProductMessage(); 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, verifica se houve alteração e atualiza no conta azul if (productTable == null) { var productContaAzul = new ProductContaAzul(); productContaAzul.ContaAzulId = GetProductResponse[0].id; productContaAzul.ProductId = item.Id; productContaAzul.DataCriacao = DateTime.Now; _contaAzulProductService.InsertProduct(productContaAzul); product.category_id = null; product.id = GetProductResponse[0].id.ToString(); var objetoContaAzul = JsonConvert.SerializeObject(GetProductResponse[0]); var objetoAtualizar = JsonConvert.SerializeObject(product); if (!objetoAtualizar.Equals(objetoContaAzul)) { using (var productCreation = new ProductCreation(ContaAzulMiscSettings.UseSandbox)) ProductResponse = productCreation.CreateAsyncUpdate(product, GetProductResponse[0].id.ToString(), ContaAzulMiscSettings.access_token).ConfigureAwait(false).GetAwaiter().GetResult(); } } else {//caso já existe no banco, só verifica se houve alteração e faz o update no conta azul product.id = productTable.ContaAzulId.ToString(); product.category_id = null; var objetoContaAzul = JsonConvert.SerializeObject(GetProductResponse[0]); var objetoAtualizar = JsonConvert.SerializeObject(product); 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 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); } } }