.
This commit is contained in:
@@ -7,7 +7,6 @@ using PartSource.Data;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Data.Nexpart;
|
||||
using PartSource.Services;
|
||||
using PartSource.Services.Integrations;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
@@ -18,7 +17,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Ratermania.Shopify.Entities;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
@@ -40,110 +40,128 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
public async Task<AutomationJobResult> Run()
|
||||
{
|
||||
IList<ImportData> parts = _partSourceContext.ImportData.Where(i => new[] { "Raybestos Brakes", "MotoMaster", "Pro Series OE Plus Brake Pads" }.Contains(i.Vendor)).ToList();
|
||||
IList<ImportData> parts = _partSourceContext.ImportData
|
||||
.Where(p => p.UpdatedAt < DateTime.Now.AddDays(-3))
|
||||
.Take(50)
|
||||
.ToList();
|
||||
|
||||
foreach (ImportData importData in parts)
|
||||
while (parts != null && parts.Count > 0)
|
||||
{
|
||||
try
|
||||
foreach (ImportData importData in parts)
|
||||
{
|
||||
Product product = await _shopifyClient.Products.GetById((long)importData.ShopifyId);
|
||||
if (product == null)
|
||||
try
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
bool isFitment = false;
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length >= 100000)
|
||||
Product product = await _shopifyClient.Products.GetById(importData.ShopifyId.GetValueOrDefault());
|
||||
if (product == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
|
||||
bool isFitment = false;
|
||||
|
||||
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
|
||||
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
|
||||
if (vehicleIdFitment.Count > 0)
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
isFitment = true;
|
||||
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
string json = JsonConvert.SerializeObject(vehicleIdFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
isFitment = true;
|
||||
Metafield vehicleMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "ids",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
await _shopifyClient.Metafields.Add(vehicleMetafield);
|
||||
}
|
||||
|
||||
Metafield ymmMetafield = new Metafield
|
||||
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
|
||||
if (ymmFitment.Count > 0)
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
isFitment = true;
|
||||
|
||||
string json = JsonConvert.SerializeObject(ymmFitment);
|
||||
if (json.Length >= 100000)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Metafield ymmMetafield = new Metafield
|
||||
{
|
||||
Namespace = "fitment",
|
||||
Key = "seo",
|
||||
Value = json,
|
||||
ValueType = "json_string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(ymmMetafield);
|
||||
}
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
Metafield isFitmentMetafield = new Metafield
|
||||
{
|
||||
Namespace = "Flags",
|
||||
Key = "IsFitment",
|
||||
Value = isFitment.ToString(),
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
await _shopifyClient.Metafields.Add(isFitmentMetafield);
|
||||
|
||||
List<string> tags = new List<string>
|
||||
List<string> tags = new List<string>
|
||||
{
|
||||
importData.LineCode,
|
||||
importData.PartNumber
|
||||
};
|
||||
|
||||
for (int i = 0; i < vehicleIdFitment.Count; i += 25)
|
||||
{
|
||||
tags.Add(string.Join('-', vehicleIdFitment.Skip(i).Take(25).Select(i => $"v{i}")));
|
||||
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 > 250)
|
||||
{
|
||||
tags = tags.Take(250).ToList();
|
||||
}
|
||||
|
||||
product.Tags = string.Join(',', tags);
|
||||
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
importData.UpdatedAt = DateTime.Now;
|
||||
importData.UpdateType = "Fitment";
|
||||
}
|
||||
|
||||
tags.AddRange(ymmFitment);
|
||||
|
||||
if (tags.Count > 250)
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
tags = tags.Take(250).ToList();
|
||||
if (!ex.Message.Contains("response content", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
Console.WriteLine($"{importData.VariantSku}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
product.Tags = string.Join(',', tags);
|
||||
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"{importData.VariantSku}: {ex.Message}");
|
||||
}
|
||||
await _partSourceContext.SaveChangesAsync();
|
||||
|
||||
parts = _partSourceContext.ImportData
|
||||
.Where(p => p.UpdatedAt < DateTime.Now.AddDays(-3))
|
||||
.Take(50)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user