182 lines
6.9 KiB
C#
182 lines
6.9 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 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(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[] { "CSD" },
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |