State of OMG-LEGION prior to merge
This commit is contained in:
@@ -27,7 +27,7 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
MenuNodesLookup menuNodesLookup = new MenuNodesLookup
|
||||
{
|
||||
MenuId = 1,
|
||||
MenuId = 2,
|
||||
NumberOfLevels = 1
|
||||
};
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
MenuNodesLookup subgroupLookup = new MenuNodesLookup
|
||||
{
|
||||
MenuId = 1,
|
||||
MenuId = 2,
|
||||
NumberOfLevels = 1,
|
||||
ParentMenuNodeId = categoryNode.Id
|
||||
};
|
||||
@@ -52,7 +52,7 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
MenuNodesLookup thirdLookup = new MenuNodesLookup
|
||||
{
|
||||
MenuId = 1,
|
||||
MenuId = 2,
|
||||
NumberOfLevels = 1,
|
||||
ParentMenuNodeId = subgroupNode.Id
|
||||
};
|
||||
@@ -67,7 +67,7 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
}
|
||||
|
||||
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Partsource Menu Items.csv", rows);
|
||||
//await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Partsource Menu Items.csv", rows);
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
191
PartSource.Automation/Jobs/POC/UpdateBulbFitment.cs
Normal file
191
PartSource.Automation/Jobs/POC/UpdateBulbFitment.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Data.Nexpart;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class UpdateBulbFitment : IAutomationJob
|
||||
{
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly NexpartService _nexpartService;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
public UpdateBulbFitment(FitmentContext fitmentContext, NexpartService nexpartService, ShopifyClient shopifyClient)
|
||||
{
|
||||
_fitmentContext = fitmentContext;
|
||||
_nexpartService = nexpartService;
|
||||
_shopifyClient = shopifyClient;
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
await BuildDatabase();
|
||||
await UpdateShopify();
|
||||
}
|
||||
|
||||
public async Task BuildDatabase()
|
||||
{
|
||||
IList<int> baseVehicles = await _fitmentContext.Vehicles
|
||||
.Select(v => v.BaseVehicleId)
|
||||
.Distinct()
|
||||
.OrderBy(i => i)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (int baseVehicleId in baseVehicles)
|
||||
{
|
||||
ApplicationSearch applicationSearch = new ApplicationSearch
|
||||
{
|
||||
VehicleIdentifier = new VehicleIdentifier
|
||||
{
|
||||
BaseVehicleId = baseVehicleId
|
||||
},
|
||||
MfrCode = new[] { "C23", "CBX", "CCH", "CCJ", "CF1", "CHU", "GOO", "GPL", "OEB", "UTY", "TYC", "ILB", "SYL", "SYR", "PLP", "FOU", },
|
||||
PartType = new[] { new PartType { Id = 11696 }, new PartType { Id = 11701 }, new PartType { Id = 13343 }, new PartType { Id = 13661 }, new PartType { Id = 13662 }, new PartType { Id = 13663 }, new PartType { Id = 13675 }, new PartType { Id = 13676 }, new PartType { Id = 13677 }, new PartType { Id = 13678 }, new PartType { Id = 13716 } },
|
||||
Criterion = new[]
|
||||
{
|
||||
new Criterion
|
||||
{
|
||||
Attribute = "REGION",
|
||||
Id = 2
|
||||
}
|
||||
},
|
||||
GroupBy = "PARTTYPE"
|
||||
};
|
||||
|
||||
ApplicationSearchResponse response = await _nexpartService.SendRequest<ApplicationSearch, ApplicationSearchResponse>(applicationSearch);
|
||||
if (response.ResponseBody != null)
|
||||
{
|
||||
foreach (App app in response.ResponseBody.App)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(app.Position))
|
||||
{
|
||||
app.Position = "not provided by WHI";
|
||||
}
|
||||
|
||||
await _fitmentContext.Database.ExecuteSqlRawAsync("INSERT INTO Wiper (BaseVehicleId, LineCode, PartNumber, Position, PartName) VALUES ({0}, {1}, {2}, {3}, {4});",
|
||||
baseVehicleId, app.MfrCode, Regex.Replace(app.Part, "[^a-zA-Z0-9]", string.Empty), app.Position, app.MfrLabel);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Could not save {app.MfrCode}, {app.Part}, {app.Position}, {app.MfrLabel} for {baseVehicleId}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(baseVehicleId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateShopify()
|
||||
{
|
||||
//foreach (string productType in new[] { "CA171-SC223-FL22302_Halogen Lighting - Certified", "CA171-SC223-FL22303_Halogen Lighting - Xtra Vision", "CA171-SC223-FL22304_Halogen Lighting - Silverstar", "CA171-SC223-FL22305_Halogen Lighting - Silverstar Ultra", "CA171-SC223-FL22306_Halogen Lighting - Silverstar Zxe", "CA171-SC223-FL22307_Sealed Beams - OPP", "CA171-SC223-FL22308_Headlight Assemblies", "CA171-SC223-FL22311_Halogen Lighting - Fog Vision", "CA171-SC223-FL22314_Halogen Lighting - Sylvania Standard", "CA171-SC223-FL22315_Forward Lighting - HID", "CA171-SC223-FL22317_Sealed Beams - Silverstar", "CA171-SC223-FL22318_Sealed Beams - Xtra Vision", "CA171-SC223-FL22319_Forward Lighting - LED", "CA171-SC239-FL23910_Minibulbs - Long Life", "CA171-SC239-FL23920_Minibulbs - Silver Star", "CA171-SC239-FL23940_Minibulbs - LED" })
|
||||
//{
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
try
|
||||
{
|
||||
string partNumber = Regex.Replace(product.Title.Split(' ')[0], "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
IList<Wiper> wipers = await _fitmentContext.Wipers
|
||||
.Where(w => w.PartNumber == partNumber)
|
||||
.OrderBy(w => w.Position)
|
||||
.ToListAsync();
|
||||
|
||||
string currentPosition = wipers.FirstOrDefault()?.Position;
|
||||
if (currentPosition == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
List<int> vehicleIds = new List<int>();
|
||||
|
||||
foreach (Wiper wiper in wipers)
|
||||
{
|
||||
if (wiper.Position != currentPosition)
|
||||
{
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
currentPosition = wiper.Position;
|
||||
vehicleIds = new List<int>();
|
||||
}
|
||||
|
||||
IList<int> fitmentVehicleIds = _fitmentContext.Vehicles
|
||||
.Where(v => v.BaseVehicleId == wiper.BaseVehicleId)
|
||||
.Select(v => v.VehicleToEngineConfigId)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
vehicleIds.AddRange(fitmentVehicleIds);
|
||||
}
|
||||
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Could not update {product.Id}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
//[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "It's a Shopify metafield key")]
|
||||
private async Task SavePositionMetafield(Product product, IList<int> vehicleIds, string position)
|
||||
{
|
||||
if (vehicleIds.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIds);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
// TODO: Logging
|
||||
return;
|
||||
}
|
||||
|
||||
string key = position.ToLowerInvariant().Replace(" ", "_");
|
||||
if (key.Length > 20)
|
||||
{
|
||||
key = key.Substring(0, 20);
|
||||
}
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "position",
|
||||
Key = key,
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
;
|
||||
System.Diagnostics.Debug.WriteLine(json);
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
}
|
||||
}
|
||||
182
PartSource.Automation/Jobs/POC/UpdateWiperFitment.cs
Normal file
182
PartSource.Automation/Jobs/POC/UpdateWiperFitment.cs
Normal file
@@ -0,0 +1,182 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Data.Nexpart;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class UpdateWiperFitment : IAutomationJob
|
||||
{
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly NexpartService _nexpartService;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
public UpdateWiperFitment(FitmentContext fitmentContext, NexpartService nexpartService, ShopifyClient shopifyClient)
|
||||
{
|
||||
_fitmentContext = fitmentContext;
|
||||
_nexpartService = nexpartService;
|
||||
_shopifyClient = shopifyClient;
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
// await BuildDatabase();
|
||||
await UpdateShopify();
|
||||
}
|
||||
|
||||
public async Task BuildDatabase()
|
||||
{
|
||||
IList<int> baseVehicles = await _fitmentContext.Vehicles
|
||||
.Select(v => v.BaseVehicleId)
|
||||
.Distinct()
|
||||
.OrderBy(i => i)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (int baseVehicleId in baseVehicles)
|
||||
{
|
||||
ApplicationSearch applicationSearch = new ApplicationSearch
|
||||
{
|
||||
VehicleIdentifier = new VehicleIdentifier
|
||||
{
|
||||
BaseVehicleId = baseVehicleId
|
||||
},
|
||||
MfrCode = new[] { "BOS", "TRI" },
|
||||
PartType = new[] { new PartType { Id = 8852 } },
|
||||
Criterion = new[]
|
||||
{
|
||||
new Criterion
|
||||
{
|
||||
Attribute = "REGION",
|
||||
Id = 2
|
||||
}
|
||||
},
|
||||
GroupBy = "PARTTYPE"
|
||||
};
|
||||
|
||||
ApplicationSearchResponse response = await _nexpartService.SendRequest<ApplicationSearch, ApplicationSearchResponse>(applicationSearch);
|
||||
if (response.ResponseBody != null)
|
||||
{
|
||||
foreach (App app in response.ResponseBody.App)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _fitmentContext.Database.ExecuteSqlRawAsync("INSERT INTO Wiper (BaseVehicleId, LineCode, PartNumber, Position, PartName) VALUES ({0}, {1}, {2}, {3}, {4});",
|
||||
baseVehicleId, app.MfrCode, Regex.Replace(app.Part, "[^a-zA-Z0-9]", string.Empty), app.Position, app.MfrLabel);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Could not save {app.MfrCode}, {app.Part}, {app.Position}, {app.MfrLabel} for {baseVehicleId}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(baseVehicleId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateShopify()
|
||||
{
|
||||
foreach (string productType in new[] { "CA172-SC231-FL23107_(PS) Wipers - TRICO Neoform", "CA172-SC231-FL23109_(PS) Wipers - TRICO Tech/Exact Fit", "CA172-SC231-FL23110_Wiper Accessories", "CA172-SC231-FL23116_(PS) Wipers - Bosch Insight (Hybrid)", "CA172-SC231-FL23117_(PS) Wipers - Bosch Clear Advantage (Beam)" })
|
||||
{
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", productType } });
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"{productType}: Count: {products.Count()}");
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
try
|
||||
{
|
||||
string partNumber = Regex.Replace(product.Title.Split(' ')[0], "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
IList<Wiper> wipers = await _fitmentContext.Wipers
|
||||
.Where(w => w.PartNumber == partNumber)
|
||||
.OrderBy(w => w.Position)
|
||||
.ToListAsync();
|
||||
|
||||
string currentPosition = wipers[0].Position;
|
||||
List<int> vehicleIds = new List<int>();
|
||||
|
||||
foreach (Wiper wiper in wipers)
|
||||
{
|
||||
if (wiper.Position != currentPosition)
|
||||
{
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
currentPosition = wiper.Position;
|
||||
vehicleIds = new List<int>();
|
||||
}
|
||||
|
||||
IList<int> fitmentVehicleIds = _fitmentContext.Vehicles
|
||||
.Where(v => v.BaseVehicleId == wiper.BaseVehicleId)
|
||||
.Select(v => v.VehicleToEngineConfigId)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
vehicleIds.AddRange(fitmentVehicleIds);
|
||||
}
|
||||
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Could not update {product.Id}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "It's a Shopify metafield key")]
|
||||
private async Task SavePositionMetafield(Product product, IList<int> vehicleIds, string position)
|
||||
{
|
||||
if (vehicleIds.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIds);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
// TODO: Logging
|
||||
return;
|
||||
}
|
||||
|
||||
string key = position.ToLowerInvariant().Replace(" ", "_");
|
||||
if (key.Length > 20)
|
||||
{
|
||||
key = key.Substring(0, 20);
|
||||
}
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "position",
|
||||
Key = key,
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
System.Diagnostics.Debug.WriteLine(json);
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,163 +18,163 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class ProcessWhiFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<ProcessWhiFitment> _logger;
|
||||
private readonly WhiSeoService _whiSeoService;
|
||||
private readonly FtpConfiguration _ftpConfiguration;
|
||||
private readonly SeoDataType _seoDataType;
|
||||
public class ProcessWhiFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<ProcessWhiFitment> _logger;
|
||||
private readonly WhiSeoService _whiSeoService;
|
||||
private readonly FtpConfiguration _ftpConfiguration;
|
||||
private readonly SeoDataType _seoDataType;
|
||||
|
||||
private readonly IDictionary<string, string> _noteDictionary;
|
||||
private readonly IDictionary<string, string> _noteDictionary;
|
||||
|
||||
public ProcessWhiFitment(IConfiguration configuration, ILogger<ProcessWhiFitment> logger, WhiSeoService whiSeoService)
|
||||
{
|
||||
_logger = logger;
|
||||
_whiSeoService = whiSeoService;
|
||||
public ProcessWhiFitment(IConfiguration configuration, ILogger<ProcessWhiFitment> logger, WhiSeoService whiSeoService)
|
||||
{
|
||||
_logger = logger;
|
||||
_whiSeoService = whiSeoService;
|
||||
|
||||
_seoDataType = SeoDataType.Fitment;
|
||||
_seoDataType = SeoDataType.Fitment;
|
||||
|
||||
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
|
||||
_noteDictionary = new ConcurrentDictionary<string, string>();
|
||||
}
|
||||
_noteDictionary = new ConcurrentDictionary<string, string>();
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2008:Do not create tasks without passing a TaskScheduler", Justification = "<Pending>")]
|
||||
public async Task Run()
|
||||
{
|
||||
_whiSeoService.TruncateFitmentTables();
|
||||
// _whiSeoService.GetFiles(_seoDataType);
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2008:Do not create tasks without passing a TaskScheduler", Justification = "<Pending>")]
|
||||
public async Task Run()
|
||||
{
|
||||
_whiSeoService.TruncateFitmentTables();
|
||||
// _whiSeoService.GetFiles(_seoDataType);
|
||||
|
||||
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||
|
||||
ConcurrentQueue<IGrouping<string, FileInfo>> fileGroups = new ConcurrentQueue<IGrouping<string, FileInfo>>();
|
||||
ConcurrentQueue<IGrouping<string, FileInfo>> fileGroups = new ConcurrentQueue<IGrouping<string, FileInfo>>();
|
||||
|
||||
foreach (IGrouping<string, FileInfo> fileGroup in directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last()))
|
||||
{
|
||||
fileGroups.Enqueue(fileGroup);
|
||||
}
|
||||
foreach (IGrouping<string, FileInfo> fileGroup in directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last()))
|
||||
{
|
||||
fileGroups.Enqueue(fileGroup);
|
||||
}
|
||||
|
||||
Task[] taskArray = new Task[8];
|
||||
Task[] taskArray = new Task[8];
|
||||
|
||||
for (int i = 0; i < taskArray.Length; i++)
|
||||
{
|
||||
taskArray[i] = Task.Factory.StartNew(() =>
|
||||
{
|
||||
while (fileGroups.TryDequeue(out IGrouping<string, FileInfo> fileGroup))
|
||||
{
|
||||
foreach (FileInfo fileInfo in fileGroup)
|
||||
{
|
||||
try
|
||||
{
|
||||
string filename = Decompress(fileInfo);
|
||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||
for (int i = 0; i < taskArray.Length; i++)
|
||||
{
|
||||
taskArray[i] = Task.Factory.StartNew(() =>
|
||||
{
|
||||
while (fileGroups.TryDequeue(out IGrouping<string, FileInfo> fileGroup))
|
||||
{
|
||||
foreach (FileInfo fileInfo in fileGroup)
|
||||
{
|
||||
try
|
||||
{
|
||||
string filename = Decompress(fileInfo);
|
||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||
|
||||
DataTable dataTable = GetDataTable(filename);
|
||||
DataTable dataTable = GetDataTable(filename);
|
||||
|
||||
_whiSeoService.BulkCopyFitment(dataTable, tableName);
|
||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||
_whiSeoService.BulkCopyFitment(dataTable, tableName);
|
||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||
|
||||
File.Delete(filename);
|
||||
}
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
|
||||
_whiSeoService.CreateFitmentTable(fitmentTable);
|
||||
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
|
||||
_whiSeoService.CreateFitmentTable(fitmentTable);
|
||||
|
||||
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
|
||||
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Task.WaitAll(taskArray);
|
||||
Task.WaitAll(taskArray);
|
||||
|
||||
_whiSeoService.CreateFitmentView();
|
||||
_whiSeoService.CreateFitmentView();
|
||||
|
||||
_whiSeoService.SaveNotes(_noteDictionary);
|
||||
}
|
||||
_whiSeoService.SaveNotes(_noteDictionary);
|
||||
}
|
||||
|
||||
public string Decompress(FileInfo fileInfo)
|
||||
{
|
||||
string decompressedFile = fileInfo.FullName.Remove(fileInfo.FullName.Length - fileInfo.Extension.Length);
|
||||
public string Decompress(FileInfo fileInfo)
|
||||
{
|
||||
string decompressedFile = fileInfo.FullName.Remove(fileInfo.FullName.Length - fileInfo.Extension.Length);
|
||||
|
||||
using FileStream filestream = File.Create(decompressedFile);
|
||||
using GZipStream decompressionStream = new GZipStream(fileInfo.OpenRead(), CompressionMode.Decompress);
|
||||
using FileStream filestream = File.Create(decompressedFile);
|
||||
using GZipStream decompressionStream = new GZipStream(fileInfo.OpenRead(), CompressionMode.Decompress);
|
||||
|
||||
decompressionStream.CopyTo(filestream);
|
||||
decompressionStream.CopyTo(filestream);
|
||||
|
||||
return decompressedFile;
|
||||
}
|
||||
return decompressedFile;
|
||||
}
|
||||
|
||||
private DataTable GetDataTable(string filename)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("LineCode", typeof(string));
|
||||
dataTable.Columns.Add("PartNumber", typeof(string));
|
||||
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("Position", typeof(string));
|
||||
dataTable.Columns.Add("FitmentNoteHash", typeof(string));
|
||||
private DataTable GetDataTable(string filename)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("LineCode", typeof(string));
|
||||
dataTable.Columns.Add("PartNumber", typeof(string));
|
||||
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("Position", typeof(string));
|
||||
dataTable.Columns.Add("FitmentNoteHash", typeof(string));
|
||||
|
||||
using StreamReader reader = new StreamReader(filename);
|
||||
string line = reader.ReadLine(); // Burn the header row
|
||||
using StreamReader reader = new StreamReader(filename);
|
||||
string line = reader.ReadLine(); // Burn the header row
|
||||
|
||||
while (reader.Peek() > 0)
|
||||
{
|
||||
line = reader.ReadLine();
|
||||
while (reader.Peek() > 0)
|
||||
{
|
||||
line = reader.ReadLine();
|
||||
|
||||
string[] columns = line.Split("\",\"");
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
{
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
}
|
||||
string[] columns = line.Split("\",\"");
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
{
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
}
|
||||
|
||||
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
|
||||
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9]", string.Empty).Trim();
|
||||
string position = columns[7].Trim();
|
||||
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
|
||||
string partNumber = Regex.Replace(columns[1], "[^a-zA-Z0-9]", string.Empty).Trim();
|
||||
string position = columns[7].Trim();
|
||||
|
||||
string noteText = columns[4].Trim();
|
||||
string noteTextHash = GetMD5Hash(noteText);
|
||||
string noteText = columns[4].Trim();
|
||||
string noteTextHash = GetMD5Hash(noteText);
|
||||
|
||||
if (!_noteDictionary.ContainsKey(noteTextHash))
|
||||
{
|
||||
_noteDictionary.Add(noteTextHash, noteText);
|
||||
}
|
||||
if (!_noteDictionary.ContainsKey(noteTextHash))
|
||||
{
|
||||
_noteDictionary.Add(noteTextHash, noteText);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(lineCode)
|
||||
&& !string.IsNullOrEmpty(partNumber)
|
||||
&& int.TryParse(columns[5], out int baseVehicleId)
|
||||
&& int.TryParse(columns[6], out int engineConfigId))
|
||||
{
|
||||
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteTextHash });
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(lineCode)
|
||||
&& !string.IsNullOrEmpty(partNumber)
|
||||
&& int.TryParse(columns[5], out int baseVehicleId)
|
||||
&& int.TryParse(columns[6], out int engineConfigId))
|
||||
{
|
||||
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteTextHash });
|
||||
}
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms", Justification = "Not used for security")]
|
||||
private string GetMD5Hash(string input)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms", Justification = "Not used for security")]
|
||||
private string GetMD5Hash(string input)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
|
||||
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
||||
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < hashBytes.Length; i++)
|
||||
{
|
||||
stringBuilder.Append(hashBytes[i].ToString("X2"));
|
||||
}
|
||||
for (int i = 0; i < hashBytes.Length; i++)
|
||||
{
|
||||
stringBuilder.Append(hashBytes[i].ToString("X2"));
|
||||
}
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,109 +18,117 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class ProcessWhiVehicles : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<ProcessWhiVehicles> _logger;
|
||||
private readonly WhiSeoService _whiSeoService;
|
||||
private readonly FtpConfiguration _ftpConfiguration;
|
||||
private readonly SeoDataType _seoDataType;
|
||||
public class ProcessWhiVehicles : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<ProcessWhiVehicles> _logger;
|
||||
private readonly WhiSeoService _whiSeoService;
|
||||
private readonly FtpConfiguration _ftpConfiguration;
|
||||
private readonly SeoDataType _seoDataType;
|
||||
|
||||
public ProcessWhiVehicles(IConfiguration configuration, ILogger<ProcessWhiVehicles> logger, WhiSeoService whiSeoService)
|
||||
{
|
||||
_logger = logger;
|
||||
_whiSeoService = whiSeoService;
|
||||
public ProcessWhiVehicles(IConfiguration configuration, ILogger<ProcessWhiVehicles> logger, WhiSeoService whiSeoService)
|
||||
{
|
||||
_logger = logger;
|
||||
_whiSeoService = whiSeoService;
|
||||
|
||||
_seoDataType = SeoDataType.Vehicle;
|
||||
_seoDataType = SeoDataType.Vehicle;
|
||||
|
||||
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
_whiSeoService.TruncateVehicleTable();
|
||||
_whiSeoService.GetFiles(_seoDataType);
|
||||
public async Task Run()
|
||||
{
|
||||
_whiSeoService.TruncateVehicleTables();
|
||||
_whiSeoService.GetFiles(_seoDataType);
|
||||
|
||||
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||
|
||||
IEnumerable<FileInfo> files = directoryInfo.GetFiles().Where(f => f.Name.StartsWith("seo_aces_vehicle_feed"));
|
||||
IEnumerable<FileInfo> files = directoryInfo.GetFiles().Where(f => f.Name.StartsWith("seo_aces_vehicle_feed"));
|
||||
|
||||
foreach (FileInfo fileInfo in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||
foreach (FileInfo fileInfo in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||
|
||||
DataTable dataTable = GetDataTable(fileInfo.FullName);
|
||||
DataTable dataTable = GetDataTable(fileInfo.FullName);
|
||||
|
||||
_whiSeoService.BulkCopyVehicle(dataTable, tableName);
|
||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||
_whiSeoService.BulkCopyVehicle(dataTable, tableName);
|
||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||
|
||||
File.Delete(fileInfo.FullName);
|
||||
}
|
||||
File.Delete(fileInfo.FullName);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
_whiSeoService.CreateVehicleTable();
|
||||
_whiSeoService.CreateVehicleTable();
|
||||
|
||||
_logger.LogInformation($"Created vehicle table.");
|
||||
}
|
||||
_logger.LogInformation($"Created vehicle table.");
|
||||
}
|
||||
|
||||
private DataTable GetDataTable(string filename)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("Year", typeof(int));
|
||||
dataTable.Columns.Add("MakeId", typeof(int));
|
||||
dataTable.Columns.Add("MakeName", typeof(string));
|
||||
dataTable.Columns.Add("ModelId", typeof(int));
|
||||
dataTable.Columns.Add("ModelName", typeof(string));
|
||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("EngineDescription", typeof(string));
|
||||
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||
dataTable.Columns.Add("VehicleToEngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("SubmodelId", typeof(int));
|
||||
dataTable.Columns.Add("SubmodelName", typeof(string));
|
||||
private DataTable GetDataTable(string filename)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("Year", typeof(int));
|
||||
dataTable.Columns.Add("MakeId", typeof(int));
|
||||
dataTable.Columns.Add("MakeName", typeof(string));
|
||||
dataTable.Columns.Add("ModelId", typeof(int));
|
||||
dataTable.Columns.Add("ModelName", typeof(string));
|
||||
dataTable.Columns.Add("RegionId", typeof(int));
|
||||
dataTable.Columns.Add("RegionName", typeof(string));
|
||||
dataTable.Columns.Add("VehicleTypeId", typeof(int));
|
||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("EngineDescription", typeof(string));
|
||||
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||
dataTable.Columns.Add("VehicleToEngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("SubmodelId", typeof(int));
|
||||
dataTable.Columns.Add("SubmodelName", typeof(string));
|
||||
|
||||
using StreamReader reader = new StreamReader(filename);
|
||||
string line = reader.ReadLine(); // Burn the header row
|
||||
using StreamReader reader = new StreamReader(filename);
|
||||
string line = reader.ReadLine(); // Burn the header row
|
||||
|
||||
while (reader.Peek() > 0)
|
||||
{
|
||||
line = reader.ReadLine();
|
||||
while (reader.Peek() > 0)
|
||||
{
|
||||
line = reader.ReadLine();
|
||||
|
||||
string[] columns = line.Split("\",\"");
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
{
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
}
|
||||
string[] columns = line.Split("\",\"");
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
{
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
}
|
||||
|
||||
string makeName = columns[4].Trim();
|
||||
string modelName = columns[6].Trim();
|
||||
string submodelName = columns[34].Trim();
|
||||
string engineDescription = columns[51].Trim();
|
||||
string makeName = columns[4].Trim();
|
||||
string modelName = columns[6].Trim();
|
||||
string regionName = columns[8].Trim();
|
||||
string submodelName = columns[34].Trim();
|
||||
string engineDescription = columns[51].Trim();
|
||||
|
||||
if (!string.IsNullOrEmpty(makeName)
|
||||
&& !string.IsNullOrEmpty(modelName)
|
||||
&& !string.IsNullOrEmpty(submodelName)
|
||||
&& !string.IsNullOrEmpty(engineDescription)
|
||||
&& int.TryParse(columns[0], out int baseVehicleId)
|
||||
&& int.TryParse(columns[2], out int year)
|
||||
&& int.TryParse(columns[3], out int makeId)
|
||||
&& int.TryParse(columns[5], out int modelId)
|
||||
&& int.TryParse(columns[33], out int submodelId)
|
||||
&& int.TryParse(columns[35], out int engineConfigId)
|
||||
&& int.TryParse(columns[36], out int vehicleToEngineConfigId))
|
||||
{
|
||||
dataTable.Rows.Add(new object[] { year, makeId, makeName, modelId, modelName, engineConfigId, engineDescription, baseVehicleId, vehicleToEngineConfigId, submodelId, submodelName });
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(makeName)
|
||||
&& !string.IsNullOrEmpty(modelName)
|
||||
&& !string.IsNullOrEmpty(regionName)
|
||||
&& !string.IsNullOrEmpty(submodelName)
|
||||
&& !string.IsNullOrEmpty(engineDescription)
|
||||
&& int.TryParse(columns[0], out int baseVehicleId)
|
||||
&& int.TryParse(columns[2], out int year)
|
||||
&& int.TryParse(columns[3], out int makeId)
|
||||
&& int.TryParse(columns[5], out int modelId)
|
||||
&& int.TryParse(columns[7], out int regionId)
|
||||
&& int.TryParse(columns[9], out int vehicleTypeId)
|
||||
&& int.TryParse(columns[33], out int submodelId)
|
||||
&& int.TryParse(columns[35], out int engineConfigId)
|
||||
&& int.TryParse(columns[36], out int vehicleToEngineConfigId)
|
||||
&& new[] { 5, 6, 7 }.Contains(vehicleTypeId))
|
||||
{
|
||||
dataTable.Rows.Add(new object[] { year, makeId, makeName, modelId, modelName, regionId, regionName, vehicleTypeId, engineConfigId, engineDescription, baseVehicleId, vehicleToEngineConfigId, submodelId, submodelName });
|
||||
}
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
/// <summary>
|
||||
/// Ensures syncronization between Shopify IDs and Partsource SKUs
|
||||
/// </summary>
|
||||
public class SyncronizeProducts : IAutomationJob
|
||||
{
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly ILogger<TestJob> _logger;
|
||||
|
||||
public SyncronizeProducts(ILogger<TestJob> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
||||
{
|
||||
_partSourceContext = partSourceContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
IList<ImportData> importData = _partSourceContext.ImportData.FromSql<ImportData>($"SELECT * FROM ImportDataFilters").ToList();
|
||||
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
|
||||
while (products?.Any() == true)
|
||||
{
|
||||
|
||||
foreach (Product product in products)
|
||||
{
|
||||
foreach (Variant variant in product.Variants)
|
||||
{
|
||||
ImportData item = importData.FirstOrDefault(i => i.VariantSku == variant.Sku);
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
_partSourceContext.Database.ExecuteSqlCommand($"UPDATE ImportDataFilters SET ShopifyId = {product.Id} WHERE VariantSku = {variant.Sku}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Did 250");
|
||||
//await _partSourceContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("Failed to save a batch of products");
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,257 +20,227 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class UpdateFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitment> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
public class UpdateFitment : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdateFitment> _logger;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
|
||||
public UpdateFitment(ILogger<UpdateFitment> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
}
|
||||
public UpdateFitment(ILogger<UpdateFitment> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
IList<string> productTypes = new List<string>
|
||||
{
|
||||
"CA108-SC349-FL34907_CV Shafts, New"
|
||||
};
|
||||
public async Task Run()
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
|
||||
foreach (string type in productTypes)
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA108-SC349-FL34907_CV Shafts, New" } });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
int i = 1;
|
||||
|
||||
int i = 1;
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
// Wiper blades are a separate fitment process.
|
||||
if (product.ProductType.Contains("CA172-SC231"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
ImportData importData = null;
|
||||
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
|
||||
//importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
|
||||
//importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.ShopifyId == product.Id);
|
||||
|
||||
//if (importData == null)
|
||||
//{
|
||||
// continue;
|
||||
importData = new ImportData
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
|
||||
PartNumber = product.Title.Split(' ')[0],
|
||||
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
// }
|
||||
//if (importData == null)
|
||||
//{
|
||||
// continue;
|
||||
importData = new ImportData
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
|
||||
PartNumber = product.Title.Split(' ')[0],
|
||||
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
// }
|
||||
|
||||
//importData.PartNumber = product.Title.Split(' ')[0];
|
||||
bool isFitment = false;
|
||||
string bodyHtml = product.BodyHtml[..(product.BodyHtml.IndexOf("</ul>") + "</ul>".Length)];
|
||||
|
||||
bool isFitment = false;
|
||||
string bodyHtml = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
//if (vehicles.Count > 250)
|
||||
//{
|
||||
// vehicles = vehicles.Take(250)
|
||||
// .ToList();
|
||||
if (vehicleIdFitment.Any())
|
||||
{
|
||||
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
|
||||
// _logger.LogInformation($"SKU {importData.VariantSku} fits more than 250 vehicles. Only the first 250 will be used.");
|
||||
//}
|
||||
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
isFitment = true;
|
||||
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
isFitment = true;
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
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
|
||||
};
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Vehicle ID fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
stringBuilder.AppendLine("</table>");
|
||||
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine("</table>");
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||
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
|
||||
};
|
||||
Metafield lineCodeMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_0",
|
||||
Value = importData.LineCode,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
// await _shopifyClient.Metafields.Add(lineCodeMetafield);
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Year/make/model fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Metafield partNumberMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_1",
|
||||
Value = importData.PartNumber,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
//await _shopifyClient.Metafields.Add(partNumberMetafield);
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
List<string> tags = new List<string>();
|
||||
|
||||
Metafield lineCodeMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_0",
|
||||
Value = importData.LineCode,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
|
||||
//await _shopifyClient.Metafields.Add(lineCodeMetafield);
|
||||
tags.AddRange(ymmFitment);
|
||||
|
||||
Metafield partNumberMetafield = new Metafield
|
||||
{
|
||||
Namespace = "google",
|
||||
Key = "custom_label_1",
|
||||
Value = importData.PartNumber,
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
if (tags.Count > 249)
|
||||
{
|
||||
tags = tags.Take(249).ToList();
|
||||
}
|
||||
|
||||
// await _shopifyClient.Metafields.Add(partNumberMetafield);
|
||||
string zzzIsFitment = isFitment
|
||||
? "zzzIsFitment=true"
|
||||
: "zzzIsFitment=false";
|
||||
|
||||
List<string> tags = new List<string>();
|
||||
tags.Add(zzzIsFitment);
|
||||
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
product.Tags = string.Join(',', tags);
|
||||
product.BodyHtml = bodyHtml;
|
||||
|
||||
tags.AddRange(ymmFitment);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
if (tags.Count > 249)
|
||||
{
|
||||
tags = tags.Take(249).ToList();
|
||||
}
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
string zzzIsFitment = isFitment
|
||||
? "zzzIsFitment=true"
|
||||
: "zzzIsFitment=false";
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
tags.Add(zzzIsFitment);
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
|
||||
//product.Tags = string.Join(',', tags);
|
||||
product.BodyHtml = bodyHtml;
|
||||
await _shopifyClient.Products.Update(product);
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(parameters);
|
||||
|
||||
int i = 1;
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
@@ -94,45 +96,51 @@ namespace PartSource.Automation.Jobs
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
|
||||
//IList<string> notes = fitments.Select(f => f.NoteText)
|
||||
IList<string> notes = fitments.Select(f => f.FitmentNoteHash)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
// .Distinct()
|
||||
// .ToList();
|
||||
IList<object> vehicleNotes = new List<object>();
|
||||
|
||||
//IList<object> vehicleNotes = new List<object>();
|
||||
foreach (string noteHash in notes)
|
||||
{
|
||||
FitmentNote fitmentNote = await _fitmentContext.FitmentNotes.FirstOrDefaultAsync(f => f.Hash == noteHash);
|
||||
|
||||
//foreach (string noteText in notes)
|
||||
//{
|
||||
// vehicleIds = fitments.Where(f => f.NoteText == noteText)
|
||||
// .Select(f => new { f.EngineConfigId, f.BaseVehicleId })
|
||||
// .SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
|
||||
// .Select(v => v.VehicleToEngineConfigId)
|
||||
// .ToList();
|
||||
if (fitmentNote == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// vehicleNotes.Add(new { noteText, vehicleIds });
|
||||
//}
|
||||
vehicleIds = fitments.Where(f => f.FitmentNoteHash == noteHash)
|
||||
.Select(f => new { f.EngineConfigId, f.BaseVehicleId })
|
||||
.SelectMany(f => vehicles.Where(v => v.BaseVehicleId == f.BaseVehicleId && v.EngineConfigId == f.EngineConfigId))
|
||||
.Select(v => v.VehicleToEngineConfigId)
|
||||
.ToList();
|
||||
|
||||
//string json = JsonConvert.SerializeObject(vehicleNotes);
|
||||
//if (json.Length >= 100000)
|
||||
//{
|
||||
// continue;
|
||||
//}
|
||||
vehicleNotes.Add(new { fitmentNote.NoteText, vehicleIds });
|
||||
}
|
||||
|
||||
//Metafield vehicleMetafield = new Metafield
|
||||
//{
|
||||
// Namespace = "fitment",
|
||||
// Key = "note_text",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
string json = JsonConvert.SerializeObject(vehicleNotes);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "note_text",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
//importData.UpdatedAt = DateTime.Now;
|
||||
//importData.UpdateType = "Positioning";
|
||||
}
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
|
||||
//importData.UpdatedAt = DateTime.Now;
|
||||
//importData.UpdateType = "Positioning";
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -141,7 +149,8 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
@@ -200,7 +209,7 @@ namespace PartSource.Automation.Jobs
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
System.Diagnostics.Debug.WriteLine(json);
|
||||
//System.Diagnostics.Debug.WriteLine(json);
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace PartSource.Automation.Jobs
|
||||
product.Variants[i].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? DateTime.Now : null;
|
||||
product.PublishedScope = PublishedScope.Global;
|
||||
|
||||
//Metafield metafield = new Metafield
|
||||
|
||||
Reference in New Issue
Block a user