Files
Partsource/PartSource.Automation/Services/WhiSeoService.cs
2025-02-12 18:12:19 -05:00

158 lines
5.6 KiB
C#

#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
using Microsoft.Extensions.Configuration;
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 WhiSeoService(IConfiguration configuration, ILogger<WhiSeoService> logger)
{
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:WhiConfiguration").Get<FtpConfiguration>();
_ftpService = new FtpService(ftpConfiguration);
_connectionString = configuration.GetConnectionString("FitmentDatabase");
_logger = logger;
}
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();
}
public void TruncateVehicleTable()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
command.ExecuteNonQuery();
}
public void TruncateFitmentTables()
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec DropFitmentTables", 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));
foreach (KeyValuePair<string, string> note in notes)
{
dataTable.Rows.Add(new string[] { note.Value, note.Key });
}
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentNote",
BulkCopyTimeout = 14400
};
bulk.WriteToServer(dataTable);
}
public void BulkCopyFitment(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
string sql = string.Empty;
using SqlCommand command = new SqlCommand($"EXEC CreateFitmentTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"FitmentTemp.{tableName}",
BulkCopyTimeout = 14400
};
bulk.WriteToServer(dataTable);
}
public void BulkCopyVehicle(DataTable dataTable, string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
string sql = string.Empty;
using SqlCommand command = new SqlCommand($"EXEC CreateVehicleTempTable @tableName = '{tableName}'", connection);
command.ExecuteNonQuery();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"VehicleTemp.{tableName}",
BulkCopyTimeout = 14400
};
bulk.WriteToServer(dataTable);
}
public void CreateFitmentTable(string tableName)
{
using SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
using SqlCommand command = new SqlCommand($"exec CreateFitmentTable @tableName = '{tableName}'", connection);
command.CommandTimeout = 1800;
command.ExecuteNonQuery();
}
public void CreateFitmentView()
{
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();
}
}
}
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities