Stuff and things

This commit is contained in:
2021-06-29 19:00:13 -04:00
parent 0ff42f148a
commit 962ad3383f
22 changed files with 320 additions and 226 deletions

View File

@@ -42,96 +42,38 @@ namespace PartSource.Automation.Jobs
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
ConcurrentQueue<FileInfo> files = new ConcurrentQueue<FileInfo>(directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).OrderBy(f => f.Length));
IEnumerable<IGrouping<string, FileInfo>> fileGroups = directoryInfo.GetFiles().Where(f => f.Name.EndsWith("csv.gz")).GroupBy(x => x.Name.Split('_').Last());
while (files.Count > 0)
foreach (IGrouping<string, FileInfo> fileGroup in fileGroups)
{
Parallel.For(0, 4, index =>
foreach (FileInfo fileInfo in fileGroup)
{
if (!files.TryDequeue(out FileInfo fileInfo))
{
return;
}
string filename = Decompress(fileInfo);
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("LineCode", typeof(string));
dataTable.Columns.Add("PartNumber", typeof(string));
dataTable.Columns.Add("BaseVehicleId", typeof(int));
dataTable.Columns.Add("EngineConfigId", typeof(int));
dataTable.Columns.Add("Position", typeof(string));
dataTable.Columns.Add("NoteText", typeof(string));
using StreamReader reader = new StreamReader(filename);
string line = reader.ReadLine(); // Burn the header row
try
{
int skippedLines = 0;
while (reader.Peek() > 0)
{
line = reader.ReadLine();
string[] columns = line.Split("\",\"");
if (columns.Length != 8)
{
skippedLines++;
continue;
}
for (int i = 0; i < columns.Length; i++)
{
columns[i] = columns[i].Replace("\"", string.Empty);
}
string 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();
string noteText = columns[4].Trim();
if (!string.IsNullOrEmpty(lineCode)
&& !string.IsNullOrEmpty(partNumber)
&& int.TryParse(columns[5], out int baseVehicleId)
&& int.TryParse(columns[6], out int engineConfigId))
{
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId, position, noteText });
}
}
string filename = Decompress(fileInfo);
DataTable dataTable = GetDataTable(filename);
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
_whiSeoService.BulkCopy(_seoDataType, dataTable, tableName);
if (skippedLines == 0)
{
_logger.LogInformation($"Copied {filename} to the database.");
}
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
else
{
_logger.LogWarning($"Copied {filename} to the database with warnings. {skippedLines} lines contained errors and could not be processed.");
}
File.Delete(fileInfo.FullName);
}
catch (Exception ex)
{
_logger.LogError($"Failed to copy {filename} to the database.", ex);
}
try
{
reader.Close();
File.Delete(filename);
}
catch (Exception ex) { }
});
}
catch (Exception ex)
{
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
}
}
string fitmentTable = fileGroup.Key.Substring(0, fileGroup.Key.IndexOf('.'));
_whiSeoService.CreateFitmentTable(fitmentTable);
_logger.LogInformation($"Created fitment table for part group {fitmentTable}.");
}
_whiSeoService.CreateFitmentView();
}
@@ -146,5 +88,45 @@ namespace PartSource.Automation.Jobs
return decompressedFile;
}
private DataTable GetDataTable(string filename)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("LineCode", typeof(string));
dataTable.Columns.Add("PartNumber", typeof(string));
dataTable.Columns.Add("BaseVehicleId", typeof(int));
dataTable.Columns.Add("EngineConfigId", typeof(int));
//dataTable.Columns.Add("Position", typeof(string));
//dataTable.Columns.Add("NoteText", typeof(string));
using StreamReader reader = new StreamReader(filename);
string line = reader.ReadLine(); // Burn the header row
while (reader.Peek() > 0)
{
line = reader.ReadLine();
string[] columns = line.Split("\",\"");
for (int i = 0; i < columns.Length; i++)
{
columns[i] = columns[i].Replace("\"", string.Empty);
}
string 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();
string noteText = columns[4].Trim();
if (!string.IsNullOrEmpty(lineCode)
&& !string.IsNullOrEmpty(partNumber)
&& int.TryParse(columns[5], out int baseVehicleId)
&& int.TryParse(columns[6], out int engineConfigId))
{
dataTable.Rows.Add(new object[] { lineCode, partNumber, baseVehicleId, engineConfigId }); //, position, noteText });
}
}
return dataTable;
}
}
}

View File

@@ -1,17 +1,14 @@
using Ratermania.Automation.Interfaces;
using PartSource.Automation.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PartSource.Data;
using PartSource.Services;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Resources;
using PartSource.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{

View File

@@ -74,7 +74,7 @@ namespace PartSource.Automation.Jobs
bool isFitment = false;
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode, 255);
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
//if (vehicles.Count > 250)
//{
@@ -166,9 +166,9 @@ namespace PartSource.Automation.Jobs
tags.AddRange(ymmFitment);
if (tags.Count > 250)
if (tags.Count > 249)
{
tags = tags.Take(250).ToList();
tags = tags.Take(249).ToList();
}
string zzzIsFitment = isFitment
@@ -188,7 +188,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku}", ex);
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
}
}

View File

@@ -1,9 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Ratermania.Automation.Interfaces;
using PartSource.Automation.Models;
using PartSource.Data;
using Microsoft.Extensions.Logging;
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;
@@ -11,8 +11,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Services;
namespace PartSource.Automation.Jobs
{
@@ -46,7 +44,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
throw;
}
@@ -56,35 +54,45 @@ namespace PartSource.Automation.Jobs
{
if (product.Variants.Length > 0)
{
Variant variant = product.Variants[0];
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
bool hasUpdate = false;
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
for (int i = 0; i < product.Variants.Length; i++)
{
continue;
Variant variant = product.Variants[i];
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
if (partPrice == null || !partPrice.Your_Price.HasValue || !partPrice.Compare_Price.HasValue)
{
continue;
}
if (product.Variants[i].Price.ToString("G29") != partPrice.Your_Price.Value.ToString("G29") || product.Variants[i].CompareAtPrice.ToString("G29") != partPrice.Compare_Price.Value.ToString("G29"))
{
product.Variants[i].Price = partPrice.Your_Price.Value;
product.Variants[i].CompareAtPrice = partPrice.Compare_Price.Value;
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
product.PublishedScope = PublishedScope.Global;
//Metafield metafield = new Metafield
//{
// Namespace = "Pricing",
// Key = "CorePrice",
// Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
// ValueType = "string",
// OwnerResource = "product",
// OwnerId = product.Id
//};
hasUpdate = true;
}
}
if (product.Variants[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
if (hasUpdate)
{
product.Variants[0].Price = partPrice.Your_Price.Value;
product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
product.PublishedScope = PublishedScope.Global;
Metafield metafield = new Metafield
{
Namespace = "Pricing",
Key = "CorePrice",
Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
ValueType = "string",
OwnerResource = "product",
OwnerId = product.Id
};
try
{
await _shopifyClient.Metafields.Add(metafield);
//await _shopifyClient.Metafields.Add(metafield);
await _shopifyClient.Products.Update(product);
updateCount++;
@@ -92,7 +100,7 @@ namespace PartSource.Automation.Jobs
catch (Exception ex)
{
_logger.LogWarning(ex, $"Failed to update pricing for SKU {variant.Sku}");
_logger.LogWarning(ex, $"Failed to update pricing for product ID {product.Id}");
}
}
}
@@ -100,7 +108,7 @@ namespace PartSource.Automation.Jobs
try
{
products = await _shopifyClient.Products.GetNext();
products = await _shopifyClient.Products.GetNext();
_logger.LogInformation($"Total updated: {updateCount}");
}