Current state, whatever that means
This commit is contained in:
126
PartSource.Automation/Jobs/ProcessWhiVehicles.cs
Normal file
126
PartSource.Automation/Jobs/ProcessWhiVehicles.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Enums;
|
||||
using PartSource.Automation.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class ProcessWhiVehicles : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<ProcessWhiVehicles> _logger;
|
||||
private readonly WhiSeoService _whiSeoService;
|
||||
private readonly FtpConfiguration _ftpConfiguration;
|
||||
private readonly SeoDataType _seoDataType;
|
||||
|
||||
public ProcessWhiVehicles(IConfiguration configuration, ILogger<ProcessWhiVehicles> logger, WhiSeoService whiSeoService)
|
||||
{
|
||||
_logger = logger;
|
||||
_whiSeoService = whiSeoService;
|
||||
|
||||
_seoDataType = SeoDataType.Vehicle;
|
||||
|
||||
_ftpConfiguration = configuration.GetSection("ftpServers:WhiConfiguration").Get<FtpConfiguration>();
|
||||
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
{
|
||||
_whiSeoService.TruncateVehicleTable();
|
||||
_whiSeoService.GetFiles(_seoDataType);
|
||||
|
||||
string directory = Path.Combine(_ftpConfiguration.Destination, _seoDataType.ToString().ToLowerInvariant());
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
||||
|
||||
IEnumerable<FileInfo> files = directoryInfo.GetFiles().Where(f => f.Name.StartsWith("seo_aces_vehicle_feed"));
|
||||
|
||||
foreach (FileInfo fileInfo in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
string tableName = fileInfo.Name.Substring(0, fileInfo.Name.IndexOf('.'));
|
||||
|
||||
DataTable dataTable = GetDataTable(fileInfo.FullName);
|
||||
|
||||
_whiSeoService.BulkCopyVehicle(dataTable, tableName);
|
||||
_logger.LogInformation($"Copied {fileInfo.Name} to the database.");
|
||||
|
||||
File.Delete(fileInfo.FullName);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to write {fileInfo.Name} to the database - {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
_whiSeoService.CreateVehicleTable();
|
||||
|
||||
_logger.LogInformation($"Created vehicle table.");
|
||||
}
|
||||
|
||||
private DataTable GetDataTable(string filename)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("Year", typeof(int));
|
||||
dataTable.Columns.Add("MakeId", typeof(int));
|
||||
dataTable.Columns.Add("MakeName", typeof(string));
|
||||
dataTable.Columns.Add("ModelId", typeof(int));
|
||||
dataTable.Columns.Add("ModelName", typeof(string));
|
||||
dataTable.Columns.Add("EngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("EngineDescription", typeof(string));
|
||||
dataTable.Columns.Add("BaseVehicleId", typeof(int));
|
||||
dataTable.Columns.Add("VehicleToEngineConfigId", typeof(int));
|
||||
dataTable.Columns.Add("SubmodelId", typeof(int));
|
||||
dataTable.Columns.Add("SubmodelName", 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 makeName = columns[4].Trim();
|
||||
string modelName = columns[6].Trim();
|
||||
string submodelName = columns[34].Trim();
|
||||
string engineDescription = columns[51].Trim();
|
||||
|
||||
if (!string.IsNullOrEmpty(makeName)
|
||||
&& !string.IsNullOrEmpty(modelName)
|
||||
&& !string.IsNullOrEmpty(submodelName)
|
||||
&& !string.IsNullOrEmpty(engineDescription)
|
||||
&& int.TryParse(columns[0], out int baseVehicleId)
|
||||
&& int.TryParse(columns[2], out int year)
|
||||
&& int.TryParse(columns[3], out int makeId)
|
||||
&& int.TryParse(columns[5], out int modelId)
|
||||
&& int.TryParse(columns[33], out int submodelId)
|
||||
&& int.TryParse(columns[35], out int engineConfigId)
|
||||
&& int.TryParse(columns[36], out int vehicleToEngineConfigId))
|
||||
{
|
||||
dataTable.Rows.Add(new object[] { year, makeId, makeName, modelId, modelName, engineConfigId, engineDescription, baseVehicleId, vehicleToEngineConfigId, submodelId, submodelName });
|
||||
}
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user