Merge from master
This commit is contained in:
@@ -17,7 +17,7 @@ namespace PartSource.Automation.Jobs
|
||||
private readonly ILogger<ExecuteSsisPackages> _logger;
|
||||
|
||||
// TODO: set from config
|
||||
private readonly string[] _ssisPackages = { "Parts Availability" };
|
||||
private readonly string[] _ssisPackages = {"Parts Availability" };
|
||||
|
||||
public ExecuteSsisPackages(EmailService emailService, IConfiguration configuration, SsisService ssisService, ILogger<ExecuteSsisPackages> logger)
|
||||
{
|
||||
@@ -36,7 +36,7 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
try
|
||||
{
|
||||
_ftpService.Download($"{package}.txt");
|
||||
// _ftpService.Download($"{package}.txt");
|
||||
_ssisService.Execute($"{package}.dtsx");
|
||||
|
||||
_logger.LogInformation($"Execution of SSIS package {package} completed successfully.");
|
||||
|
||||
95
PartSource.Automation/Jobs/POC/ImageList.cs
Normal file
95
PartSource.Automation/Jobs/POC/ImageList.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
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 GetImageUrls : IAutomationJob
|
||||
{
|
||||
private readonly NexpartService _nexpartService;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
|
||||
public GetImageUrls(NexpartService nexpartService, PartSourceContext partSourceContext, FitmentContext fitmentContext)
|
||||
{
|
||||
_nexpartService = nexpartService;
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
IList<string> rows = new List<string> {
|
||||
"\"Line Code\", \"Part Number\", \"Image URL(s)\""
|
||||
};
|
||||
|
||||
IList<Data.Models.Part> parts = await _fitmentContext.Parts.ToListAsync();
|
||||
string oldLineCode = string.Empty;
|
||||
IList<DcfMapping> mappings = new List<DcfMapping>();
|
||||
|
||||
foreach (Data.Models.Part part in parts)
|
||||
{
|
||||
if (part.LineCode != oldLineCode)
|
||||
{
|
||||
mappings = await _fitmentContext.DcfMappings.Where(d => d.LineCode == part.LineCode).ToListAsync();
|
||||
}
|
||||
;
|
||||
|
||||
foreach (DcfMapping mapping in mappings)
|
||||
{
|
||||
SmartPageDataSearch dataSearch = new SmartPageDataSearch
|
||||
{
|
||||
Items = new Item[]
|
||||
{
|
||||
new Item
|
||||
{
|
||||
MfrCode = mapping.WhiCode,
|
||||
PartNumber = part.PartNumber
|
||||
}
|
||||
},
|
||||
DataOption = new[] { "ALL" }
|
||||
};
|
||||
|
||||
SmartPageDataSearchResponse response = await _nexpartService.SendRequest<SmartPageDataSearch, SmartPageDataSearchResponse>(dataSearch);
|
||||
|
||||
if (response.ResponseBody.Item?.Length > 0)
|
||||
{
|
||||
List<string> urls = new List<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl))
|
||||
{
|
||||
urls.Add(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl);
|
||||
};
|
||||
|
||||
if (response.ResponseBody.Item[0].AddImgs?.AddImg?.Length > 0)
|
||||
{
|
||||
urls.AddRange(response.ResponseBody.Item[0].AddImgs.AddImg.Select(i => i.AddImgUrl));
|
||||
}
|
||||
|
||||
if (urls.Count > 0)
|
||||
{
|
||||
rows.Add($"\"{part.LineCode}\", \"{part.PartNumber}\", \"{string.Join(";", urls)}\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\WHI Images.csv", rows);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ namespace PartSource.Automation.Jobs.POC
|
||||
ApplicationSearchResponse response = await _nexpartService.SendRequest<ApplicationSearch, ApplicationSearchResponse>(applicationSearch);
|
||||
if (response.ResponseBody != null)
|
||||
{
|
||||
foreach (App app in response.ResponseBody.App)
|
||||
foreach (App app in ((Apps)response.ResponseBody).App)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace PartSource.Automation.Jobs.POC
|
||||
ApplicationSearchResponse response = await _nexpartService.SendRequest<ApplicationSearch, ApplicationSearchResponse>(applicationSearch);
|
||||
if (response.ResponseBody != null)
|
||||
{
|
||||
foreach (App app in response.ResponseBody.App)
|
||||
foreach (App app in ((Apps)response.ResponseBody).App)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
89
PartSource.Automation/Jobs/PartsSync.cs
Normal file
89
PartSource.Automation/Jobs/PartsSync.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Models.Jobs;
|
||||
using PartSource.Automation.Services;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using Ratermania.Shopify.Resources.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class PartsSync : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdatePricing> _logger;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
public PartsSync(ILogger<UpdatePricing> logger, FitmentContext fitmentContext, ShopifyClient shopifyClient)
|
||||
{
|
||||
_logger = logger;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
|
||||
foreach (Product product in products)
|
||||
{
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
Part part = new Part
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0")?.Value ?? string.Empty,
|
||||
PartNumber = metafields.FirstOrDefault(m => m.Key == "custom_label_1")?.Value ?? string.Empty,
|
||||
Sku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
|
||||
// part.PartNumber = part.PartNumber.Replace("-", string.Empty);
|
||||
|
||||
if (
|
||||
string.IsNullOrEmpty(part.LineCode)
|
||||
|| string.IsNullOrEmpty(part.PartNumber)
|
||||
|| int.TryParse(part.LineCode, out _)) //If the line code is numeric, it cannot have fitment data associated with it.
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Part? existing = await _fitmentContext.Parts.FirstOrDefaultAsync(p => p.Sku == part.Sku);
|
||||
if (existing == null)
|
||||
{
|
||||
await _fitmentContext.Parts.AddAsync(part);
|
||||
await _fitmentContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogInformation(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogInformation(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,24 +56,25 @@ namespace PartSource.Automation.Jobs
|
||||
fileGroups.Enqueue(fileGroup);
|
||||
}
|
||||
|
||||
Task[] taskArray = new Task[8];
|
||||
|
||||
Task[] taskArray = new Task[18];
|
||||
for (int i = 0; i < taskArray.Length; i++)
|
||||
{
|
||||
taskArray[i] = Task.Factory.StartNew(() =>
|
||||
{
|
||||
while (fileGroups.TryDequeue(out IGrouping<string, FileInfo> fileGroup))
|
||||
{
|
||||
string tableName = string.Empty;
|
||||
|
||||
foreach (FileInfo fileInfo in fileGroup)
|
||||
{
|
||||
try
|
||||
{
|
||||
string filename = Decompress(fileInfo);
|
||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||
DataTable dataTable = GetDataTable(filename, out tableName);
|
||||
|
||||
DataTable dataTable = GetDataTable(filename);
|
||||
string tempTable = $"Fitment_{Guid.NewGuid():N}_{tableName}";
|
||||
|
||||
_whiSeoService.BulkCopyFitment(dataTable, tableName);
|
||||
_whiSeoService.BulkCopyFitment(dataTable, tempTable);
|
||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||
|
||||
File.Delete(filename);
|
||||
@@ -85,19 +86,18 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
}
|
||||
|
||||
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
|
||||
_whiSeoService.CreateFitmentTable(fitmentTable);
|
||||
_whiSeoService.CreateFitmentTable(tableName);
|
||||
|
||||
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
|
||||
_logger.LogInformation($"Created fitment table for part group {tableName}.");
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Task.WaitAll(taskArray);
|
||||
|
||||
_whiSeoService.SaveNotes(_noteDictionary);
|
||||
//_whiSeoService.CreateFitmentView();
|
||||
|
||||
_whiSeoService.CreateFitmentView();
|
||||
}
|
||||
|
||||
public string Decompress(FileInfo fileInfo)
|
||||
@@ -112,8 +112,10 @@ namespace PartSource.Automation.Jobs
|
||||
return decompressedFile;
|
||||
}
|
||||
|
||||
private DataTable GetDataTable(string filename)
|
||||
private DataTable GetDataTable(string filename, out string lineCode)
|
||||
{
|
||||
lineCode = string.Empty;
|
||||
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("LineCode", typeof(string));
|
||||
dataTable.Columns.Add("PartNumber", typeof(string));
|
||||
@@ -121,8 +123,9 @@ namespace PartSource.Automation.Jobs
|
||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("Position", typeof(string));
|
||||
dataTable.Columns.Add("FitmentNoteHash", typeof(string));
|
||||
dataTable.Columns.Add("PartTerminologyId", typeof(int));
|
||||
|
||||
using StreamReader reader = new StreamReader(filename);
|
||||
using StreamReader reader = new StreamReader(filename);
|
||||
string line = reader.ReadLine(); // Burn the header row
|
||||
|
||||
while (reader.Peek() > 0)
|
||||
@@ -135,7 +138,7 @@ namespace PartSource.Automation.Jobs
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
}
|
||||
|
||||
string lineCode = Regex.Replace(columns[0], "[^a-zA-Z0-9]", string.Empty).Trim();
|
||||
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();
|
||||
|
||||
@@ -149,10 +152,11 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
if (!string.IsNullOrEmpty(lineCode)
|
||||
&& !string.IsNullOrEmpty(partNumber)
|
||||
&& int.TryParse(columns[2], out int partTerminologyId)
|
||||
&& 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 });
|
||||
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteTextHash, partTerminologyId });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Extensions;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Enums;
|
||||
using PartSource.Automation.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class ProcessWhiVehicles : IAutomationJob
|
||||
public class ProcessWhiVehicles : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<ProcessWhiVehicles> _logger;
|
||||
private readonly WhiSeoService _whiSeoService;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Automation.Services;
|
||||
@@ -13,6 +14,7 @@ using PartSource.Data.Models;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
using System.Web;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
@@ -36,216 +38,148 @@ namespace PartSource.Automation.Jobs
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
|
||||
IEnumerable<Product> products = null;
|
||||
IList<string> productTypes = await _fitmentContext.ProductTypes
|
||||
.Where(p => p.Active)
|
||||
.Select(p => HttpUtility.UrlEncode(p.Name))
|
||||
.ToListAsync();
|
||||
|
||||
try
|
||||
foreach (string productType in productTypes)
|
||||
{
|
||||
//products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||
products = new List<Product>
|
||||
{
|
||||
await _shopifyClient.Products.GetById(7285013446703)
|
||||
};
|
||||
}
|
||||
_logger.LogInformation("Processing {productType}", HttpUtility.UrlDecode(productType));
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
importData = new ImportData
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0")?.Value ?? string.Empty,
|
||||
PartNumber = metafields.FirstOrDefault(m => m.Key == "custom_label_1")?.Value ?? string.Empty,
|
||||
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
|
||||
bool isFitment = false;
|
||||
string bodyHtml = string.IsNullOrEmpty(product.BodyHtml)
|
||||
? "<ul></ul>"
|
||||
: product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
|
||||
|
||||
IList<Vehicle> vehicles = await _vehicleFitmentService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<int> vehicleIdFitment = _vehicleFitmentService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
if (vehicleIdFitment.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string vehicleIdString = string.Join(',', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
|
||||
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length < 100000)
|
||||
{
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
Type = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Vehicle ID fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleFitmentService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
|
||||
catch
|
||||
{
|
||||
// This is still a POC at this point. Oh well...
|
||||
}
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine("</table>");
|
||||
|
||||
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
|
||||
|
||||
json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length < 100000)
|
||||
{
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
Type = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Year/make/model 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(),
|
||||
Type = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
//Metafield lineCodeMetafield = new Metafield
|
||||
//{
|
||||
// Namespace = "google",
|
||||
// Key = "custom_label_0",
|
||||
// Value = importData.LineCode,
|
||||
// Type = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
|
||||
//await _shopifyClient.Metafields.Add(lineCodeMetafield);
|
||||
|
||||
//Metafield partNumberMetafield = new Metafield
|
||||
//{
|
||||
// Namespace = "google",
|
||||
// Key = "custom_label_1",
|
||||
// Value = importData.PartNumber,
|
||||
// Type = "string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
|
||||
//await _shopifyClient.Metafields.Add(partNumberMetafield);
|
||||
|
||||
List<string> tags = new List<string>();
|
||||
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
|
||||
tags.AddRange(ymmFitment);
|
||||
|
||||
if (tags.Count > 249)
|
||||
{
|
||||
tags = tags.Take(249).ToList();
|
||||
}
|
||||
|
||||
string zzzIsFitment = isFitment
|
||||
? "zzzIsFitment=true"
|
||||
: "zzzIsFitment=false";
|
||||
|
||||
tags.Add(zzzIsFitment);
|
||||
|
||||
product.Tags = string.Join(',', tags);
|
||||
product.BodyHtml = bodyHtml;
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
|
||||
}
|
||||
IEnumerable<Product> products = null;
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
|
||||
_partSourceContext.SaveChanges();
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
i++;
|
||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", productType } });
|
||||
//products = new List<Product>
|
||||
//{
|
||||
// await _shopifyClient.Products.GetById(7458071052335)
|
||||
//};
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
_logger.LogError("Failed to get products from Shopify", ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
{
|
||||
foreach (Product product in products)
|
||||
{
|
||||
ImportData importData = null;
|
||||
bool isFitment = false;
|
||||
|
||||
try
|
||||
{
|
||||
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
|
||||
importData = new ImportData
|
||||
{
|
||||
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0")?.Value ?? string.Empty,
|
||||
PartNumber = metafields.FirstOrDefault(m => m.Key == "custom_label_1")?.Value ?? string.Empty,
|
||||
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
|
||||
};
|
||||
|
||||
importData.PartNumber = importData.PartNumber.Replace("-", string.Empty);
|
||||
|
||||
//If the line code is numeric, it cannot have fitment data associated with it.
|
||||
if (int.TryParse(importData.LineCode, out _))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract Partsource bullet points if present.
|
||||
string bodyHtml = string.IsNullOrEmpty(product.BodyHtml)
|
||||
? string.Empty
|
||||
: product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleFitmentService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<int> vehicleIdFitment = _vehicleFitmentService.GetVehicleIdFitment(vehicles);
|
||||
|
||||
if (!vehicleIdFitment.Any())
|
||||
{
|
||||
Console.WriteLine($"No fitment data for {importData.LineCode} {importData.PartNumber}");
|
||||
continue;
|
||||
}
|
||||
|
||||
string vehicleIdString = string.Join(',', vehicleIdFitment.Select(j => $"v{j}"));
|
||||
|
||||
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
|
||||
isFitment = true;
|
||||
|
||||
IList<string> ymmFitment = _vehicleFitmentService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
|
||||
|
||||
foreach (string fitment in ymmFitment)
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] parts = fitment.Split(' ', 2);
|
||||
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "YMM fitment for {fitment} was in an invalid format", fitment);
|
||||
}
|
||||
}
|
||||
|
||||
stringBuilder.AppendLine("</table>");
|
||||
|
||||
bodyHtml += $"<div id=\"seoData\">{stringBuilder}</div>";
|
||||
}
|
||||
|
||||
List<string> tags = new List<string>();
|
||||
|
||||
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(j).Take(25).Select(j => $"v{j}")));
|
||||
}
|
||||
|
||||
tags.AddRange(ymmFitment);
|
||||
|
||||
if (tags.Count > 249)
|
||||
{
|
||||
tags = tags.Take(249).ToList();
|
||||
}
|
||||
|
||||
string zzzIsFitment = isFitment
|
||||
? "zzzIsFitment=true"
|
||||
: "zzzIsFitment=false";
|
||||
|
||||
tags.Add(zzzIsFitment);
|
||||
|
||||
product.Tags = string.Join(',', tags);
|
||||
product.BodyHtml = bodyHtml;
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
importData.IsFitment = isFitment;
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
|
||||
}
|
||||
|
||||
}
|
||||
try
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,5 +16,7 @@ namespace PartSource.Automation.Models.Configuration
|
||||
public string Username { get; set; }
|
||||
|
||||
public string Password { get; set; }
|
||||
|
||||
public int Port { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,9 @@ namespace PartSource.Automation.Services
|
||||
|
||||
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
|
||||
{
|
||||
return vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray();
|
||||
return vehicles != null
|
||||
? vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray()
|
||||
: new List<int>();
|
||||
}
|
||||
|
||||
public async Task<IList<Vehicle>> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||
|
||||
@@ -5,167 +5,154 @@ using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Enums;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Services
|
||||
{
|
||||
public class WhiSeoService
|
||||
{
|
||||
private readonly FtpService _ftpService;
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<WhiSeoService> _logger;
|
||||
public class WhiSeoService
|
||||
{
|
||||
private readonly FtpService _ftpService;
|
||||
private readonly string _connectionString;
|
||||
private readonly ILogger<WhiSeoService> _logger;
|
||||
|
||||
public WhiSeoService(IConfiguration configuration, ILogger<WhiSeoService> logger)
|
||||
{
|
||||
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
_ftpService = new FtpService(ftpConfiguration);
|
||||
public WhiSeoService(IConfiguration configuration, ILogger<WhiSeoService> logger)
|
||||
{
|
||||
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
_ftpService = new FtpService(ftpConfiguration);
|
||||
|
||||
_connectionString = configuration.GetConnectionString("FitmentDatabase");
|
||||
_connectionString = configuration.GetConnectionString("FitmentDatabase");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void GetFiles(SeoDataType seoDataType)
|
||||
{
|
||||
string seoDataTypeString = seoDataType.ToString().ToLowerInvariant();
|
||||
string[] files = _ftpService.ListFiles(seoDataTypeString);
|
||||
public void GetFiles(SeoDataType seoDataType)
|
||||
{
|
||||
string seoDataTypeString = seoDataType.ToString().ToLowerInvariant();
|
||||
|
||||
// WHI changed the transfer protocol to SFTP and then messed with the directory structure.
|
||||
// Since fitment isn't really all that automated anyway, just download the files manually with an SFTP client.
|
||||
Console.WriteLine($"Remember to manually download the {seoDataTypeString} files with an SFTP client. Press any key to continue.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (file.Contains(".csv"))
|
||||
{
|
||||
try
|
||||
{
|
||||
_ftpService.Download($"{seoDataTypeString}/{file}");
|
||||
_logger.LogInformation($"Finished downloading {file}.");
|
||||
}
|
||||
public void TruncateVehicleTable()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Failed to download {file}, quitting", ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void TruncateFitmentTables()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
public void TruncateVehicleTable()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
using SqlCommand command = new SqlCommand($"exec DropFitmentTables", connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
public void SaveNotes(IDictionary<string, string> notes)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("NoteText", typeof(string));
|
||||
dataTable.Columns.Add("Hash", typeof(string));
|
||||
|
||||
public void TruncateFitmentTables()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
foreach (KeyValuePair<string, string> note in notes)
|
||||
{
|
||||
|
||||
using SqlCommand command = new SqlCommand($"exec DropFitmentTables", connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
dataTable.Rows.Add(new string[] { note.Value, note.Key });
|
||||
}
|
||||
|
||||
public void SaveNotes(IDictionary<string, string> notes)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("NoteText", typeof(string));
|
||||
dataTable.Columns.Add("Hash", typeof(string));
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
foreach (KeyValuePair<string, string> note in notes)
|
||||
{
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"FitmentNote",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
|
||||
dataTable.Rows.Add(new string[] { note.Value, note.Key });
|
||||
}
|
||||
bulk.WriteToServer(dataTable);
|
||||
}
|
||||
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
public void BulkCopyFitment(DataTable dataTable, string tableName)
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"FitmentNote",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
string sql = string.Empty;
|
||||
|
||||
bulk.WriteToServer(dataTable);
|
||||
}
|
||||
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
public void BulkCopyFitment(DataTable dataTable, string tableName)
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"FitmentTemp.{tableName}",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
|
||||
string sql = string.Empty;
|
||||
bulk.WriteToServer(dataTable);
|
||||
}
|
||||
|
||||
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
|
||||
command.ExecuteNonQuery();
|
||||
public void BulkCopyVehicle(DataTable dataTable, string tableName)
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"FitmentTemp.{tableName}",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
string sql = string.Empty;
|
||||
|
||||
bulk.WriteToServer(dataTable);
|
||||
}
|
||||
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
public void BulkCopyVehicle(DataTable dataTable, string tableName)
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"VehicleTemp.{tableName}",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
|
||||
string sql = string.Empty;
|
||||
bulk.WriteToServer(dataTable);
|
||||
}
|
||||
|
||||
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
|
||||
command.ExecuteNonQuery();
|
||||
public void CreateFitmentTable(string tableName)
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = $"VehicleTemp.{tableName}",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
using SqlCommand command = new SqlCommand($"exec CreateFitmentTable @tableName = '{tableName}'", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
bulk.WriteToServer(dataTable);
|
||||
}
|
||||
public void CreateFitmentView()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
public void CreateFitmentTable(string tableName)
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
using SqlCommand command = new SqlCommand($"exec CreateFitmentTable @tableName = '{tableName}'", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
|
||||
command.CommandTimeout = 3600;
|
||||
command2.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void CreateFitmentView()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
public void CreateVehicleTable()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command2.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void CreateVehicleTable()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlCommand command = new SqlCommand($"exec CreateVehicleTable", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
using SqlCommand command = new SqlCommand($"exec CreateVehicleTable", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities
|
||||
@@ -1,8 +1,7 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
//"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true;TrustServerCertificate=True",
|
||||
//"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;User ID=stageuser;Password=FXepK^cFYS|[H<;Encrypt=True;TrustServerCertificate=True;Connection Timeout=300",
|
||||
"FitmentDatabase": "Data Source=localhost;User ID=stageuser;Password=FXepK^cFYS|[H<;Connect Timeout=30;Encrypt=True;Trust Server Certificate=True;Application Intent=ReadWrite;Multi Subnet Failover=False",
|
||||
"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true;TrustServerCertificate=true",
|
||||
//"FitmentDatabase": "Server=tcp:ps-automation.eastus2.cloudapp.azure.com,1433;Initial Catalog=WhiFitment;User ID=sa;Password=GZ0`-ekd~[2u;Encrypt=True;TrustServerCertificate=True;Connection Timeout=300",
|
||||
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
|
||||
},
|
||||
"emailConfiguration": {
|
||||
@@ -16,19 +15,15 @@
|
||||
"Username": "ps-ftp\\$ps-ftp",
|
||||
"Password": "ycvXptffBxqkBXW4vuRYqn4Zi1soCvnvMMolTe5HNSeAlcl3bAyJYtNhG579",
|
||||
"Url": "ftp://waws-prod-yq1-007.ftp.azurewebsites.windows.net/site/wwwroot",
|
||||
"Destination": "C:\\Partsource.Automation\\Downloads"
|
||||
},
|
||||
"AutomationConfiguration": {
|
||||
"Username": "stageuser",
|
||||
"Password": "FXepK^cFYS|[H<",
|
||||
"Url": "ftp://ps-automation-stage.eastus2.cloudapp.azure.com",
|
||||
"Destination": "C:\\Partsource.Automation\\Downloads\\Stage"
|
||||
"Destination": "C:\\Partsource.Automation\\Downloads",
|
||||
"Port": 21
|
||||
},
|
||||
"WhiConfiguration": {
|
||||
"Username": "ctc_seo",
|
||||
"Password": "be34hz64e4",
|
||||
"Password": "YD3gtaQ5kPdtNKs",
|
||||
"Url": "ftp://ftp.whisolutions.com",
|
||||
"Destination": "C:\\Partsource.Automation\\Downloads\\WHI"
|
||||
"Destination": "C:\\Partsource.Automation\\Downloads\\WHI",
|
||||
"Port": 3001
|
||||
}
|
||||
},
|
||||
"ssisConfiguration": {
|
||||
@@ -43,7 +38,7 @@
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
"Microsoft.Hosting.Lifetime": "Information",
|
||||
// "Microsoft.EntityFrameworkCore.Database.Command": "Information"
|
||||
},
|
||||
"EventLog": {
|
||||
|
||||
Reference in New Issue
Block a user