Finalize hourly inventory configuration
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class BulkUpdateInventory : IAutomationJob
|
||||
{
|
||||
private readonly FtpService _ftpService;
|
||||
private readonly ILogger<BulkUpdateInventory> _logger;
|
||||
|
||||
public BulkUpdateInventory(IConfiguration configuration, ILogger<BulkUpdateInventory> logger)
|
||||
{
|
||||
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 Full") > -1)
|
||||
.OrderByDescending(f => f.Modified)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (lastUploadedFile == null)
|
||||
{
|
||||
_logger.LogInformation($"No full inventory file available.");
|
||||
return;
|
||||
}
|
||||
|
||||
string file = _ftpService.Download(lastUploadedFile.Filename);
|
||||
|
||||
DataTable dataTable = GetDataTable(file);
|
||||
|
||||
using SqlConnection connection = new SqlConnection("Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-test;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
|
||||
connection.Open();
|
||||
|
||||
using SqlCommand command = new SqlCommand("TRUNCATE TABLE PartAvailability", connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
|
||||
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
||||
{
|
||||
DestinationTableName = "PartAvailability",
|
||||
BulkCopyTimeout = 14400
|
||||
};
|
||||
|
||||
bulk.WriteToServer(dataTable);
|
||||
|
||||
_ftpService.Delete(lastUploadedFile.Filename);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
private DataTable GetDataTable(string filename)
|
||||
{
|
||||
using DataTable dataTable = new DataTable();
|
||||
dataTable.Columns.Add("Store", typeof(int));
|
||||
dataTable.Columns.Add("SKU", typeof(string));
|
||||
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);
|
||||
}
|
||||
|
||||
string sku = columns[1].Trim();
|
||||
if (int.TryParse(columns[0], out int store)
|
||||
&& !string.IsNullOrEmpty(sku)
|
||||
&& int.TryParse(columns[2], out int quantity))
|
||||
{
|
||||
dataTable.Rows.Add(new object[] { store, sku, quantity });
|
||||
}
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class PartialInventoryUpdate : IAutomationJob
|
||||
{
|
||||
private readonly FtpService _ftpService;
|
||||
private readonly ILogger<PartialInventoryUpdate> _logger;
|
||||
|
||||
public PartialInventoryUpdate(IConfiguration configuration, ILogger<PartialInventoryUpdate> logger)
|
||||
{
|
||||
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 Partial") > -1)
|
||||
.OrderByDescending(f => f.Modified)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (lastUploadedFile == null)
|
||||
{
|
||||
_logger.LogInformation($"No partial inventory file available.");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Processing {filename}", lastUploadedFile.Filename);
|
||||
|
||||
string file = _ftpService.Download($"{lastUploadedFile.Filename}");
|
||||
|
||||
using SqlConnection connection = new SqlConnection("Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-test;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
|
||||
connection.Open();
|
||||
|
||||
using StreamReader reader = new StreamReader(file);
|
||||
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))
|
||||
{
|
||||
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));
|
||||
|
||||
await sqlCommand.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
|
||||
_ftpService.Delete(lastUploadedFile.Filename);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user