This commit is contained in:
2023-08-23 15:04:54 -04:00
parent d95d947bc2
commit 68c9e01ef1
24 changed files with 655 additions and 388 deletions

View File

@@ -8,6 +8,7 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Models.Configuration;
using PartSource.Automation.Models.Ftp;
using PartSource.Automation.Services;
@@ -18,67 +19,62 @@ namespace PartSource.Automation.Jobs.POC
public class PartialInventoryUpdate : IAutomationJob
{
private readonly FtpService _ftpService;
private readonly ILogger<PartialInventoryUpdate> _logger;
public PartialInventoryUpdate(IConfiguration configuration)
public PartialInventoryUpdate(IConfiguration configuration, ILogger<PartialInventoryUpdate> logger)
{
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:AzureConfiguration").Get<FtpConfiguration>();
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:AutomationConfiguration").Get<FtpConfiguration>();
_ftpService = new FtpService(ftpConfiguration);
_logger = logger;
}
public async Task Run(CancellationToken token, params string[] arguments)
{
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended("")
.Where(f => f.FileType == FtpFileType.File && f.Filename.IndexOf("Availability") > -1)
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended()
.Where(f => f.FileType == FtpFileType.File && f.Modified >= DateTime.Now.AddHours(-24) && f.Filename.IndexOf("Availability Partial") > -1)
.OrderByDescending(f => f.Modified)
.First();
.FirstOrDefault();
string file = _ftpService.Download(lastUploadedFile.Filename, Path.GetTempPath());
if (lastUploadedFile == null)
{
_logger.LogInformation($"No partial inventory file available for the time period {DateTime.Now.AddHours(-24)} - {DateTime.Now}");
return;
}
DataTable dataTable = GetDataTable(file);
string file = _ftpService.Download($"{lastUploadedFile.Filename}", "C:\\Users\\Tom\\Desktop");
using SqlConnection connection = new SqlConnection("Server=tcp:ps-automation-stage.eastus2.cloudapp.azure.com,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=stageuser;Password=]FXepK^cFYS|[H<;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;");
using SqlConnection connection = new SqlConnection("Server=tcp:ps-automation-stage.eastus2.cloudapp.azure.com,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=stageuser;Password=]FXepK^cFYS|[H<;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;");
connection.Open();
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
{
DestinationTableName = $"PartAvailability",
BulkCopyTimeout = 14400
};
using StreamReader reader = new StreamReader(file);
string line = reader.ReadLine(); // Burn the header row
bulk.WriteToServer(dataTable);
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);
}
if (int.TryParse(columns[0], out int store)
&& int.TryParse(columns[1], out int quantity)
&& int.TryParse(columns[2], out int sku))
{
using SqlCommand sqlCommand = new SqlCommand("UPDATE Inventory SET QTY = @qty WHERE SKU = @sku AND Store = @store", connection);
sqlCommand.Parameters.Add(new SqlParameter("qty", quantity));
sqlCommand.Parameters.Add(new SqlParameter("sku", sku));
sqlCommand.Parameters.Add(new SqlParameter("store", store));
await sqlCommand.ExecuteNonQueryAsync();
}
}
return;
}
private DataTable GetDataTable(string filename)
{
using DataTable dataTable = new DataTable();
dataTable.Columns.Add("Store", typeof(int));
dataTable.Columns.Add("SKU", typeof(int));
dataTable.Columns.Add("QTY", typeof(int));
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);
}
if (int.TryParse(columns[0], out int store)
&& int.TryParse(columns[1], out int sku)
&& int.TryParse(columns[2], out int quantity))
{
dataTable.Rows.Add(new object[] { store, sku, quantity });
}
}
return dataTable;
}
}
}