Add support for inventory timestamps
This commit is contained in:
@@ -32,6 +32,7 @@ namespace PartSource.Automation.Jobs
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA2100:Review SQL queries for security vulnerabilities")]
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended()
|
||||
@@ -50,37 +51,67 @@ namespace PartSource.Automation.Jobs
|
||||
string file = _ftpService.Download($"{lastUploadedFile.Filename}");
|
||||
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
connection.Open();
|
||||
|
||||
using StreamReader reader = new StreamReader(file);
|
||||
string line = reader.ReadLine(); // Burn the header row
|
||||
|
||||
IDictionary<string, object> parameters = new Dictionary<string, object>();
|
||||
string command = string.Empty;
|
||||
int i = 0;
|
||||
|
||||
while (reader.Peek() > 0)
|
||||
{
|
||||
line = reader.ReadLine();
|
||||
|
||||
string[] columns = line.Split("|");
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
for (int j = 0; j < columns.Length; j++)
|
||||
{
|
||||
columns[i] = columns[i].Replace("\"", string.Empty);
|
||||
columns[j] = columns[j].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))
|
||||
&& int.TryParse(columns[2], out int quantity)
|
||||
&& DateTime.TryParse(columns[3], out DateTime updated))
|
||||
{
|
||||
using SqlCommand sqlCommand = new SqlCommand("UPDATE PartAvailability 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));
|
||||
command += $"UPDATE PartAvailability SET QTY = @qty_{i}, Updated = @updated_{i} WHERE SKU = @sku_{i} AND Store = @store_{i};";
|
||||
|
||||
await sqlCommand.ExecuteNonQueryAsync();
|
||||
parameters.Add($"qty_{i}", quantity);
|
||||
parameters.Add($"store_{i}", store);
|
||||
parameters.Add($"sku_{i}", sku);
|
||||
parameters.Add($"updated_{i}", updated);
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i == 250)
|
||||
{
|
||||
using SqlCommand nested = new SqlCommand(command, connection);
|
||||
foreach (KeyValuePair<string, object> parameter in parameters)
|
||||
{
|
||||
nested.Parameters.Add(new SqlParameter(parameter.Key, parameter.Value));
|
||||
}
|
||||
|
||||
await nested.ExecuteNonQueryAsync(token);
|
||||
|
||||
parameters.Clear();
|
||||
command = string.Empty;
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
using SqlCommand sqlCommand = new SqlCommand(command, connection);
|
||||
foreach (KeyValuePair<string, object> parameter in parameters)
|
||||
{
|
||||
sqlCommand.Parameters.Add(new SqlParameter(parameter.Key, parameter.Value));
|
||||
}
|
||||
|
||||
await sqlCommand.ExecuteNonQueryAsync(token);
|
||||
|
||||
_ftpService.Delete(lastUploadedFile.Filename);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user