192 lines
8.1 KiB
C#
192 lines
8.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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 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(CancellationToken token, params string[] arguments)
|
|
{
|
|
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 ((Apps)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,
|
|
Type = "json",
|
|
OwnerResource = "product",
|
|
OwnerId = product.Id
|
|
};
|
|
|
|
;
|
|
System.Diagnostics.Debug.WriteLine(json);
|
|
|
|
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
|
}
|
|
}
|
|
} |