This is it. Don't get scared now. (Converted to Ratermania.Automation)
This commit is contained in:
@@ -1,200 +1,196 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Newtonsoft.Json;
|
||||
//using PartSource.Automation.Jobs.Interfaces;
|
||||
//using PartSource.Automation.Models;
|
||||
//using PartSource.Automation.Services;
|
||||
//using PartSource.Data;
|
||||
//using PartSource.Data.Models;
|
||||
//using PartSource.Data.Nexpart;
|
||||
//using PartSource.Services;
|
||||
//using PartSource.Services.Integrations;
|
||||
//using System;
|
||||
//using System.Collections.Concurrent;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Data;
|
||||
//using System.Data.SqlClient;
|
||||
//using System.IO;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Text.RegularExpressions;
|
||||
//using System.Threading.Tasks;
|
||||
//using Ratermania.Shopify.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Automation.Models;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Exceptions;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
//namespace PartSource.Automation.Jobs
|
||||
//{
|
||||
// public class UpdateFitment : IAutomationJob
|
||||
// {
|
||||
// private readonly IServiceProvider _serviceProvider;
|
||||
// private readonly ShopifyClient _shopifyClient;
|
||||
// private readonly PartSourceContext _partSourceContext;
|
||||
// private readonly NexpartService _nexpartService;
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class UpdateFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitment> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
|
||||
// private readonly VehicleService _vehicleService;
|
||||
public UpdateFitment(ILogger<UpdateFitment> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
}
|
||||
|
||||
// public UpdateFitment(IServiceProvider serviceProvider, PartSourceContext partSourceContext, ShopifyClient shopifyClient, NexpartService nexpartService, VehicleService vehicleService)
|
||||
// {
|
||||
// _partSourceContext = partSourceContext;
|
||||
// _shopifyClient = shopifyClient;
|
||||
// _vehicleService = vehicleService;
|
||||
// }
|
||||
public async Task Run()
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
// public async Task<AutomationJobResult> Run()
|
||||
// {
|
||||
// IEnumerable<Product> products = await _shopifyClient.Products.Get();
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
}
|
||||
|
||||
// int i = 0;
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
// while (products != null && products.Any())
|
||||
// {
|
||||
// foreach (Product product in products)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// ImportData importData = _partSourceContext.ImportData.FirstOrDefault(i => i.VariantSku == product.Variants[0].Sku && i.UpdatedAt == null);
|
||||
// IList<VehicleData> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
|
||||
// if (vehicles == null || vehicles.Count == 0)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
try
|
||||
{
|
||||
importData = await _partSourceContext.ImportData
|
||||
.Where(i => i.ShopifyId == product.Id && i.UpdatedAt <= DateTime.Now.AddDays(-7))
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
// await DeleteFitmentMetafields(product.Id);
|
||||
if (importData == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// bool isFitment = false;
|
||||
bool isFitment = false;
|
||||
|
||||
// IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
// if (vehicleIdFitment.Count > 0)
|
||||
// {
|
||||
// isFitment = true;
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode, 255);
|
||||
|
||||
// string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
if (vehicles.Count > 250)
|
||||
{
|
||||
vehicles = vehicles.Take(250)
|
||||
.ToList();
|
||||
|
||||
// Metafield vehicleMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "ids",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
_logger.LogInformation($"SKU {importData.VariantSku} fits more than 250 vehicles. Only the first 250 will be used.");
|
||||
}
|
||||
|
||||
// await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
// }
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
// IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
// if (ymmFitment.Count > 0)
|
||||
// {
|
||||
// isFitment = true;
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
// string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
// if (json.Length >= 100000)
|
||||
// {
|
||||
// continue;
|
||||
// }
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length < 100000)
|
||||
{
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
// Metafield ymmMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "fitment",
|
||||
// Key = "seo",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
// await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
// }
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Vehicle ID fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Metafield isFitmentMetafield = new Metafield
|
||||
// {
|
||||
// Namespace = "Flags",
|
||||
// Key = "IsFitment",
|
||||
// Value = isFitment.ToString(),
|
||||
// ValueType = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
// };
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
// await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length < 100000)
|
||||
{
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
// importData.UpdatedAt = DateTime.Now;
|
||||
// importData.UpdateType = "Fitment";
|
||||
// }
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"{product.Id}: {ex.Message}");
|
||||
// }
|
||||
// }
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Year/make/model fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// try
|
||||
// {
|
||||
// i++;
|
||||
// Console.WriteLine(i);
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
// await _partSourceContext.SaveChangesAsync();
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
List<string> tags = new List<string>
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber
|
||||
};
|
||||
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// i++;
|
||||
// Console.WriteLine(i);
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
|
||||
// Console.WriteLine($"Retrying request: {ex.Message}");
|
||||
tags.AddRange(ymmFitment);
|
||||
|
||||
// await _partSourceContext.SaveChangesAsync();
|
||||
if (tags.Count > 250)
|
||||
{
|
||||
tags = tags.Take(250).ToList();
|
||||
}
|
||||
|
||||
// products = await _shopifyClient.Products.GetNext();
|
||||
// }
|
||||
// }
|
||||
product.Tags = string.Join(',', tags);
|
||||
|
||||
// return new AutomationJobResult
|
||||
// {
|
||||
// IsSuccess = true
|
||||
// };
|
||||
// }
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
// private async Task DeleteFitmentMetafields(long shopifyId)
|
||||
// {
|
||||
// IDictionary<string, object> parameters = new Dictionary<string, object>
|
||||
// {
|
||||
// { "metafield[owner_id]", shopifyId},
|
||||
// { "metafield[owner_resource]", "product" },
|
||||
// { "namespace", "fitment" },
|
||||
// };
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
// IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(parameters);
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// foreach (Metafield metafield in metafields)
|
||||
// {
|
||||
// await _shopifyClient.Metafields.Delete(metafield);
|
||||
// }
|
||||
// }
|
||||
try
|
||||
{
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
// public IList<VehicleData> GetVehicles(string partNumber, string lineCode)
|
||||
// {
|
||||
// partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
// //string sql = $"select distinct BaseVehicleId, EngineConfigId from dbo.Fitment where ManufacturerCode in (select WhiCode from DcfMapping where PartSourceCode='{lineCode}') and (partNumber = '{partNumber}' or partNumber = '{partNumber.Replace("-", string.Empty)}')";
|
||||
// string sql = $"with FitmentIds (BaseVehicleId, EngineConfigId) as (select distinct BaseVehicleId, EngineConfigId from dbo.Fitment where LineCode in (select WhiCode from DcfMapping where LineCode='{lineCode}') and PartNumber = '{partNumber}') select v.* from VehicleData v join FitmentIds f on v.BaseVehicleId = f.BaseVehicleId and v.EngineConfigId = f.EngineConfigId;";
|
||||
|
||||
//#pragma warning disable EF1000 // Possible SQL injection vulnerability.
|
||||
// IList<VehicleData> vehicles = _partSourceContext.VehicleData.FromSql(sql).ToList();
|
||||
//#pragma warning restore EF1000 // Possible SQL injection vulnerability.
|
||||
|
||||
// return vehicles;
|
||||
// }
|
||||
// private void Update()
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user