HourlyInventory #1
84
PartSource.Automation/Jobs/POC/PartialInventoryUpdate.cs
Normal file
84
PartSource.Automation/Jobs/POC/PartialInventoryUpdate.cs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
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 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;
|
||||||
|
|
||||||
|
public PartialInventoryUpdate(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:AzureConfiguration").Get<FtpConfiguration>();
|
||||||
|
_ftpService = new FtpService(ftpConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Run(CancellationToken token, params string[] arguments)
|
||||||
|
{
|
||||||
|
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended("")
|
||||||
|
.Where(f => f.FileType == FtpFileType.File && f.Filename.IndexOf("Availability") > -1)
|
||||||
|
.OrderByDescending(f => f.Modified)
|
||||||
|
.First();
|
||||||
|
|
||||||
|
string file = _ftpService.Download(lastUploadedFile.Filename, Path.GetTempPath());
|
||||||
|
|
||||||
|
DataTable dataTable = GetDataTable(file);
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
bulk.WriteToServer(dataTable);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,7 +40,7 @@ namespace PartSource.Automation.Jobs
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA111-SC250-FL25049_Entry Ball Joints" } });
|
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 } });
|
||||||
//products = new List<Product>
|
//products = new List<Product>
|
||||||
//{
|
//{
|
||||||
// await _shopifyClient.Products.GetById(4388919574575)
|
// await _shopifyClient.Products.GetById(4388919574575)
|
||||||
|
|||||||
19
PartSource.Automation/Models/Ftp/FtpFileInfo.cs
Normal file
19
PartSource.Automation/Models/Ftp/FtpFileInfo.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Models.Ftp
|
||||||
|
{
|
||||||
|
public class FtpFileInfo
|
||||||
|
{
|
||||||
|
public string Filename { get; set; }
|
||||||
|
|
||||||
|
public DateTime Modified { get; set; }
|
||||||
|
|
||||||
|
public long Size { get; set; }
|
||||||
|
|
||||||
|
public FtpFileType FileType { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
PartSource.Automation/Models/Ftp/FtpFileType.cs
Normal file
14
PartSource.Automation/Models/Ftp/FtpFileType.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PartSource.Automation.Models.Ftp
|
||||||
|
{
|
||||||
|
public enum FtpFileType
|
||||||
|
{
|
||||||
|
File,
|
||||||
|
Directory
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -100,7 +100,7 @@ namespace PartSource.Automation
|
|||||||
// //.StartsAt(DateTime.Today.AddHours(25))
|
// //.StartsAt(DateTime.Today.AddHours(25))
|
||||||
// )
|
// )
|
||||||
|
|
||||||
.HasJob<UpdateFitment>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
.HasJob<ProcessWhiVehicles>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||||
//.HasDependency<ExecuteSsisPackages>()
|
//.HasDependency<ExecuteSsisPackages>()
|
||||||
// .StartsAt(DateTime.Today.AddHours(28))
|
// .StartsAt(DateTime.Today.AddHours(28))
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using PartSource.Automation.Models.Configuration;
|
using PartSource.Automation.Models.Configuration;
|
||||||
|
using PartSource.Automation.Models.Ftp;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
@@ -9,44 +10,106 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace PartSource.Automation.Services
|
namespace PartSource.Automation.Services
|
||||||
{
|
{
|
||||||
public class FtpService
|
public class FtpService
|
||||||
{
|
{
|
||||||
private readonly FtpConfiguration _ftpConfiguration;
|
private readonly FtpConfiguration _ftpConfiguration;
|
||||||
|
|
||||||
public FtpService(FtpConfiguration ftpConfiguration)
|
public FtpService(FtpConfiguration ftpConfiguration)
|
||||||
{
|
{
|
||||||
_ftpConfiguration = ftpConfiguration;
|
_ftpConfiguration = ftpConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] ListFiles(string directory)
|
public IList<FtpFileInfo> ListFilesExtended(string directory)
|
||||||
{
|
{
|
||||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{directory}"));
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{directory}"));
|
||||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||||
request.Method = WebRequestMethods.Ftp.ListDirectory;
|
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
||||||
|
|
||||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||||
using StreamReader reader = new StreamReader(response.GetResponseStream());
|
using StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||||
|
|
||||||
string files = reader.ReadToEnd();
|
IList<FtpFileInfo> files = new List<FtpFileInfo>();
|
||||||
|
string[] fileStrings = reader.ReadToEnd().Split("\r\n");
|
||||||
|
|
||||||
return files.Length > 0
|
foreach (string fileString in fileStrings)
|
||||||
? files.Split("\r\n")
|
{
|
||||||
: Array.Empty<string>();
|
if (string.IsNullOrEmpty(fileString))
|
||||||
}
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
public void Download(string filename)
|
string dateString = fileString[..17];
|
||||||
{
|
string[] sizeAndName = fileString[18..].TrimStart().Split(" ", 2);
|
||||||
string file = $"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}";
|
|
||||||
|
|
||||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
if (sizeAndName[0].ToUpperInvariant().IndexOf("DIR") > -1)
|
||||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
{
|
||||||
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
files.Add(new FtpFileInfo
|
||||||
|
{
|
||||||
|
Modified = DateTime.Parse(fileString[..17]),
|
||||||
|
Size = 0,
|
||||||
|
Filename = sizeAndName[1].Trim(),
|
||||||
|
FileType = FtpFileType.Directory
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
else
|
||||||
using Stream responseStream = response.GetResponseStream();
|
{
|
||||||
using FileStream fileStream = new FileStream($"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}", FileMode.Create);
|
files.Add(new FtpFileInfo
|
||||||
|
{
|
||||||
|
Modified = DateTime.Parse(fileString[..17]),
|
||||||
|
Size = long.Parse(sizeAndName[0]),
|
||||||
|
Filename = sizeAndName[1].Trim(),
|
||||||
|
FileType = FtpFileType.File
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
responseStream.CopyTo(fileStream);
|
return files;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public string[] ListFiles(string directory)
|
||||||
|
{
|
||||||
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{directory}"));
|
||||||
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||||
|
request.Method = WebRequestMethods.Ftp.ListDirectory;
|
||||||
|
|
||||||
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||||
|
using StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||||
|
|
||||||
|
string files = reader.ReadToEnd();
|
||||||
|
|
||||||
|
return files.Length > 0
|
||||||
|
? files.Split("\r\n")
|
||||||
|
: Array.Empty<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Download(string filename, string destination = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
||||||
|
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||||
|
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(destination))
|
||||||
|
{
|
||||||
|
destination = _ftpConfiguration.Destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Path.DirectorySeparatorChar == '\\')
|
||||||
|
{
|
||||||
|
filename = filename.Replace("/", "\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
destination = Path.Combine(destination, filename);
|
||||||
|
|
||||||
|
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||||
|
using Stream responseStream = response.GetResponseStream();
|
||||||
|
using FileStream fileStream = new FileStream(destination, FileMode.Create);
|
||||||
|
|
||||||
|
responseStream.CopyTo(fileStream);
|
||||||
|
|
||||||
|
return destination;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user