Whatever this is

This commit is contained in:
2025-02-12 18:12:19 -05:00
parent aed30707be
commit cc2cbd09e1
20 changed files with 496 additions and 434 deletions

View File

@@ -90,7 +90,9 @@ namespace PartSource.Automation.Services
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
{
return vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray();
return vehicles != null
? vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray()
: new List<int>();
}
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
@@ -100,7 +102,7 @@ namespace PartSource.Automation.Services
return null;
}
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9\\-]", string.Empty);
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
.Where(d => d.LineCode == lineCode)

View File

@@ -5,167 +5,154 @@ using Microsoft.Extensions.Logging;
using PartSource.Automation.Models.Configuration;
using PartSource.Automation.Models.Enums;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
namespace PartSource.Automation.Services
{
public class WhiSeoService
{
private readonly FtpService _ftpService;
private readonly string _connectionString;
private readonly ILogger<WhiSeoService> _logger;
public class WhiSeoService
{
private readonly FtpService _ftpService;
private readonly string _connectionString;
private readonly ILogger<WhiSeoService> _logger;
public WhiSeoService(IConfiguration configuration, ILogger<WhiSeoService> logger)
{
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:WhiConfiguration").Get<FtpConfiguration>();
_ftpService = new FtpService(ftpConfiguration);
public WhiSeoService(IConfiguration configuration, ILogger<WhiSeoService> logger)
{
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:WhiConfiguration").Get<FtpConfiguration>();
_ftpService = new FtpService(ftpConfiguration);
_connectionString = configuration.GetConnectionString("FitmentDatabase");
_connectionString = configuration.GetConnectionString("FitmentDatabase");
_logger = logger;
}
_logger = logger;
}
public void GetFiles(SeoDataType seoDataType)
{
string seoDataTypeString = seoDataType.ToString().ToLowerInvariant();
string[] files = _ftpService.ListFiles(seoDataTypeString);
public void GetFiles(SeoDataType seoDataType)
{
string seoDataTypeString = seoDataType.ToString().ToLowerInvariant();
// WHI changed the transfer protocol to SFTP and then messed with the directory structure.
// Since fitment isn't really all that automated anyway, just download the files manually with an SFTP client.
Console.WriteLine($"Remember to manually download the {seoDataTypeString} files with an SFTP client. Press any key to continue.");
Console.ReadLine();
}
foreach (string file in files)
{
if (file.Contains(".csv"))
{
try
{
_ftpService.Download($"{seoDataTypeString}/{file}");
_logger.LogInformation($"Finished downloading {file}.");
}
public void TruncateVehicleTable()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
_logger.LogWarning($"Failed to download {file}, quitting", ex);
throw;
}
}
}
}
public void TruncateFitmentTables()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
public void TruncateVehicleTable()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec DropFitmentTables", connection);
command.ExecuteNonQuery();
}
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
command.ExecuteNonQuery();
}
public void SaveNotes(IDictionary<string, string> notes)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("NoteText", typeof(string));
dataTable.Columns.Add("Hash", typeof(string));
public void TruncateFitmentTables()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
foreach (KeyValuePair<string, string> note in notes)
{
using SqlCommand command = new SqlCommand($"exec DropFitmentTables", connection);
command.ExecuteNonQuery();
}
dataTable.Rows.Add(new string[] { note.Value, note.Key });
}
public void SaveNotes(IDictionary<string, string> notes)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("NoteText", typeof(string));
dataTable.Columns.Add("Hash", typeof(string));
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
foreach (KeyValuePair<string, string> note in notes)
{
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentNote",
BulkCopyTimeout = 14400
};
dataTable.Rows.Add(new string[] { note.Value, note.Key });
}
bulk.WriteToServer(dataTable);
}
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
public void BulkCopyFitment(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentNote",
BulkCopyTimeout = 14400
};
string sql = string.Empty;
bulk.WriteToServer(dataTable);
}
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
public void BulkCopyFitment(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentTemp.{tableName}",
BulkCopyTimeout = 14400
};
string sql = string.Empty;
bulk.WriteToServer(dataTable);
}
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
public void BulkCopyVehicle(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentTemp.{tableName}",
BulkCopyTimeout = 14400
};
string sql = string.Empty;
bulk.WriteToServer(dataTable);
}
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
public void BulkCopyVehicle(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"VehicleTemp.{tableName}",
BulkCopyTimeout = 14400
};
string sql = string.Empty;
bulk.WriteToServer(dataTable);
}
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
public void CreateFitmentTable(string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"VehicleTemp.{tableName}",
BulkCopyTimeout = 14400
};
using SqlCommand command = new SqlCommand($"exec CreateFitmentTable @tableName = '{tableName}'", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
bulk.WriteToServer(dataTable);
}
public void CreateFitmentView()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
public void CreateFitmentTable(string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
using SqlCommand command = new SqlCommand($"exec CreateFitmentTable @tableName = '{tableName}'", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
command.CommandTimeout = 3600;
command2.ExecuteNonQuery();
}
public void CreateFitmentView()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
public void CreateVehicleTable()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
command.CommandTimeout = 3600;
command2.ExecuteNonQuery();
}
public void CreateVehicleTable()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec CreateVehicleTable", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
}
using SqlCommand command = new SqlCommand($"exec CreateVehicleTable", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
}
}
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities