Files
Partsource/PartSource.Automation/Jobs/Archive/UpdatePricing.cs

113 lines
3.3 KiB
C#

//using Microsoft.EntityFrameworkCore;
//using Microsoft.Extensions.Logging.Abstractions;
//using Ratermania.Automation.Interfaces;
//using PartSource.Automation.Models;
//using PartSource.Data;
//using PartSource.Data.Models;
//using Ratermania.Shopify;
//using Ratermania.Shopify.Resources;
//using Ratermania.Shopify.Resources.Enums;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
//using Microsoft.Extensions.Logging;
//namespace PartSource.Automation.Jobs
//{
// public class UpdatePricing : IAutomationJob
// {
// private readonly ILogger<UpdatePricing> _logger;
// private readonly PartSourceContext _partSourceContext;
// private readonly ShopifyClient _shopifyClient;
// public UpdatePricing(ILogger<UpdatePricing> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient)
// {
// _logger = logger;
// _partSourceContext = partSourceContext;
// _shopifyClient = shopifyClient;
// }
// public async Task Run()
// {
// IEnumerable<Product> products = null;
// IEnumerable<PartPrice> prices = null;
// int updateCount = 0;
// try
// {
// products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
// prices = await _partSourceContext.PartPrices.ToListAsync();
// }
// catch (Exception ex)
// {
// _logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
// throw;
// }
// while (products != null && products.Any())
// {
// foreach (Product product in products)
// {
// if (product.Variants.Length > 0)
// {
// Variant variant = product.Variants[0];
// PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
// if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
// {
// continue;
// }
// if (product.Variants[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
// {
// product.Variants[0].Price = partPrice.Your_Price.Value;
// product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
// product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
// product.PublishedScope = PublishedScope.Global;
// Metafield metafield = new Metafield
// {
// Namespace = "Pricing",
// Key = "CorePrice",
// Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
// ValueType = "string",
// OwnerResource = "product",
// OwnerId = product.Id
// };
// try
// {
// await _shopifyClient.Metafields.Add(metafield);
// await _shopifyClient.Products.Update(product);
// updateCount++;
// }
// catch (Exception ex)
// {
// _logger.LogWarning(ex, $"Failed to update pricing for SKU {variant.Sku}");
// }
// }
// }
// }
// try
// {
// products = await _shopifyClient.Products.GetNext();
// _logger.LogInformation($"Total updated: {updateCount}");
// }
// catch (Exception ex)
// {
// _logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
// products = await _shopifyClient.Products.GetPrevious();
// }
// }
// }
// }
//}