Fitment update
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Automation.Models;
|
||||
using PartSource.Automation.Services;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
@@ -23,14 +24,14 @@ namespace PartSource.Automation.Jobs
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly VehicleService _vehicleService;
|
||||
private readonly VehicleFitmentService _vehicleFitmentService;
|
||||
|
||||
public UpdatePositioning(PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
|
||||
public UpdatePositioning(PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleFitmentService vehicleFitmentService)
|
||||
{
|
||||
_partSourceContext = partSourceContext;
|
||||
_fitmentContext = fitmentContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
_vehicleService = vehicleService;
|
||||
_vehicleFitmentService = vehicleFitmentService;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
@@ -42,8 +43,6 @@ 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)
|
||||
@@ -65,7 +64,7 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
|
||||
IList<Fitment> fitments = GetPositionOrderedFitments(importData?.PartNumber, importData?.LineCode);
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
IList<Vehicle> vehicles = _vehicleFitmentService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
|
||||
if (fitments.Count == 0 || vehicles.Count == 0)
|
||||
{
|
||||
@@ -97,51 +96,45 @@ namespace PartSource.Automation.Jobs
|
||||
await SavePositionMetafield(product, vehicleIds, currentPosition);
|
||||
|
||||
|
||||
IList<string> notes = fitments.Select(f => f.FitmentNoteHash)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
//IList<string> notes = fitments.Select(f => f.NoteText)
|
||||
|
||||
IList<object> vehicleNotes = new List<object>();
|
||||
// .Distinct()
|
||||
// .ToList();
|
||||
|
||||
foreach (string noteHash in notes)
|
||||
{
|
||||
FitmentNote fitmentNote = await _fitmentContext.FitmentNotes.FirstOrDefaultAsync(f => f.Hash == noteHash);
|
||||
//IList<object> vehicleNotes = new List<object>();
|
||||
|
||||
if (fitmentNote == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//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();
|
||||
|
||||
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();
|
||||
// vehicleNotes.Add(new { noteText, vehicleIds });
|
||||
//}
|
||||
|
||||
vehicleNotes.Add(new { fitmentNote.NoteText, vehicleIds });
|
||||
}
|
||||
//string json = JsonConvert.SerializeObject(vehicleNotes);
|
||||
//if (json.Length >= 100000)
|
||||
//{
|
||||
// continue;
|
||||
//}
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleNotes);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//Metafield vehicleMetafield = new Metafield
|
||||
//{
|
||||
// Namespace = "fitment",
|
||||
// Key = "note_text",
|
||||
// Value = json,
|
||||
// ValueType = "json_string",
|
||||
// OwnerResource = "product",
|
||||
// OwnerId = product.Id
|
||||
//};
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "note_text",
|
||||
Value = json,
|
||||
Type = "json",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
//await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
|
||||
//importData.UpdatedAt = DateTime.Now;
|
||||
//importData.UpdateType = "Positioning";
|
||||
}
|
||||
//importData.UpdatedAt = DateTime.Now;
|
||||
//importData.UpdateType = "Positioning";
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -150,8 +143,7 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
{
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
}
|
||||
|
||||
@@ -205,12 +197,12 @@ namespace PartSource.Automation.Jobs
|
||||
Namespace = "position",
|
||||
Key = key,
|
||||
Value = json,
|
||||
Type = "json",
|
||||
Type = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine(json);
|
||||
System.Diagnostics.Debug.WriteLine(json);
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user