Merge pull request 'HourlyInventory' (#1) from HourlyInventory into master
Reviewed-on: #1
This commit is contained in:
@@ -51,15 +51,13 @@ namespace PartSource.Api
|
||||
|
||||
services.AddAutoMapper(typeof(PartSourceProfile));
|
||||
|
||||
services.AddTransient<FitmentService>();
|
||||
services.AddTransient<PartService>();
|
||||
services.AddTransient<FitmentService>();
|
||||
services.AddTransient<NexpartService>();
|
||||
services.AddTransient<SecurityService>();
|
||||
services.AddTransient<VehicleService>();
|
||||
services.AddTransient<ShopifyChangelogService>();
|
||||
|
||||
services.AddTransient<FitmentContext>();
|
||||
|
||||
services.AddCors(o => o.AddPolicy("Default", builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"PartSourceDatabase": "Server=tcp:ps-whi.database.windows.net,1433;Initial Catalog=ps-whi-stage;Persist Security Info=False;User ID=ps-whi;Password=9-^*N5dw!6:|.5Q;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
|
||||
"FitmentDatabase": "Server=tcp:ps-automation.eastus2.cloudapp.azure.com,1433;Initial Catalog=WhiFitment;User ID=automation;Password=)6L)XP%m(x-UU#M;Encrypt=True;TrustServerCertificate=True;Connection Timeout=300"
|
||||
//"FitmentDatabase": "Data Source=localhost;Initial Catalog=WhiFitment;Integrated Security=true"
|
||||
"FitmentDatabase": "Server=tcp:ps-automation.eastus2.cloudapp.azure.com,1433;Initial Catalog=WhiFitment;User ID=sa;Password=GZ0`-ekd~[2u;Encrypt=True;TrustServerCertificate=True;Connection Timeout=300"
|
||||
},
|
||||
|
||||
23
PartSource.Automation/Extensions/FileInfoExtensions.cs
Normal file
23
PartSource.Automation/Extensions/FileInfoExtensions.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Extensions
|
||||
{
|
||||
public static class FileInfoExtensions
|
||||
{
|
||||
public static DateTime GetWhiTimestamp(this FileInfo fileInfo)
|
||||
{
|
||||
Match match = Regex.Match(fileInfo.Name, "[0-9]{8}");
|
||||
|
||||
return match.Success && DateTime.TryParseExact(match.Value, "MMddyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime timestamp)
|
||||
? timestamp
|
||||
: DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
100
PartSource.Automation/Jobs/POC/BulkUpdateInventory.cs
Normal file
100
PartSource.Automation/Jobs/POC/BulkUpdateInventory.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
119
PartSource.Automation/Jobs/POC/GetImageUrls.cs
Normal file
119
PartSource.Automation/Jobs/POC/GetImageUrls.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Data.Nexpart;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class GetImageUrls : IAutomationJob
|
||||
{
|
||||
private readonly NexpartService _nexpartService;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly PartService _partService;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
public GetImageUrls(NexpartService nexpartService, PartService partService, FitmentContext fitmentContext, ShopifyClient shopifyClient)
|
||||
{
|
||||
_nexpartService = nexpartService;
|
||||
_fitmentContext = fitmentContext;
|
||||
_partService = partService;
|
||||
_shopifyClient = shopifyClient;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
IList<string> rows = new List<string> {
|
||||
"\"Line Code\", \"Part Number\", \"Image URL(s)\""
|
||||
};
|
||||
|
||||
using StreamReader reader = new StreamReader("C:\\Users\\Tom\\Desktop\\image parts.csv");
|
||||
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 partsourceCode = columns[0].Trim();
|
||||
string partNumber = columns[1].Trim();
|
||||
|
||||
|
||||
IList<DcfMapping> dcfMappings = await _partService.GetDcfMapping(partsourceCode);
|
||||
if (dcfMappings.Count == 0)
|
||||
{
|
||||
Console.WriteLine($"No images for {partsourceCode} {partNumber}");
|
||||
}
|
||||
|
||||
bool hasImage = false;
|
||||
foreach (DcfMapping mapping in dcfMappings)
|
||||
{
|
||||
if (hasImage)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
SmartPageDataSearch dataSearch = new SmartPageDataSearch
|
||||
{
|
||||
Items = new Item[]
|
||||
{
|
||||
new Item
|
||||
{
|
||||
MfrCode = mapping.WhiCode,
|
||||
PartNumber = partNumber
|
||||
}
|
||||
},
|
||||
DataOption = new[] { "ALL" }
|
||||
};
|
||||
|
||||
SmartPageDataSearchResponse response = await _nexpartService.SendRequest<SmartPageDataSearch, SmartPageDataSearchResponse>(dataSearch);
|
||||
|
||||
if (response.ResponseBody.Item?.Length > 0)
|
||||
{
|
||||
List<string> urls = new List<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl))
|
||||
{
|
||||
urls.Add(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl);
|
||||
};
|
||||
|
||||
if (response.ResponseBody.Item[0].AddImgs?.AddImg?.Length > 0)
|
||||
{
|
||||
urls.AddRange(response.ResponseBody.Item[0].AddImgs.AddImg.Select(i => i.AddImgUrl));
|
||||
}
|
||||
|
||||
if (urls.Count > 0)
|
||||
{
|
||||
rows.Add($"\"{partsourceCode}\", \"{partNumber}\", \"{string.Join(";", urls)}\"");
|
||||
hasImage = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasImage)
|
||||
{
|
||||
Console.WriteLine($"No images for {partsourceCode} {partNumber}");
|
||||
}
|
||||
}
|
||||
|
||||
await File.WriteAllLinesAsync($"C:\\users\\Tom\\desktop\\WHI Images {DateTime.Now:yyyyMMdd}.csv", rows);
|
||||
}
|
||||
}
|
||||
}
|
||||
106
PartSource.Automation/Jobs/POC/GetImageUrlsTemp.cs
Normal file
106
PartSource.Automation/Jobs/POC/GetImageUrlsTemp.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Models;
|
||||
using PartSource.Data.Nexpart;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using Ratermania.Shopify;
|
||||
using Ratermania.Shopify.Resources;
|
||||
|
||||
namespace PartSource.Automation.Jobs.POC
|
||||
{
|
||||
public class GetImageUrlsTemp : IAutomationJob
|
||||
{
|
||||
private readonly NexpartService _nexpartService;
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
private readonly PartService _partService;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
private readonly string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
public GetImageUrlsTemp(NexpartService nexpartService, PartService partService, FitmentContext fitmentContext, ShopifyClient shopifyClient)
|
||||
{
|
||||
_nexpartService = nexpartService;
|
||||
_fitmentContext = fitmentContext;
|
||||
_partService = partService;
|
||||
_shopifyClient = shopifyClient;
|
||||
}
|
||||
|
||||
public async Task Run(CancellationToken token, params string[] arguments)
|
||||
{
|
||||
IList<KeyValuePair<string, string>> parts = new List<KeyValuePair<string, string>>();
|
||||
parts.Add(new KeyValuePair<string, string>("DAY", "89310"));
|
||||
parts.Add(new KeyValuePair<string, string>("CNI", "141.40113"));
|
||||
parts.Add(new KeyValuePair<string, string>("PRF", "MU19631"));
|
||||
parts.Add(new KeyValuePair<string, string>("TRK", "SB8100"));
|
||||
parts.Add(new KeyValuePair<string, string>("MON", "906970"));
|
||||
parts.Add(new KeyValuePair<string, string>("FEL", "70804"));
|
||||
parts.Add(new KeyValuePair<string, string>("FEL", "SS71198"));
|
||||
parts.Add(new KeyValuePair<string, string>("CFP", "STS314"));
|
||||
parts.Add(new KeyValuePair<string, string>("NGK", "21517"));
|
||||
parts.Add(new KeyValuePair<string, string>("NGK", "RC-XX89"));
|
||||
parts.Add(new KeyValuePair<string, string>("FRA", "CA176"));
|
||||
|
||||
for (int i = 0; i < chars.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < chars.Length; j++)
|
||||
{
|
||||
for (int k = 0; k < chars.Length; k++)
|
||||
{
|
||||
string actualLineCode = $"{chars[i]}{chars[j]}{chars[k]}";
|
||||
System.Diagnostics.Debug.WriteLine(actualLineCode);
|
||||
|
||||
foreach (KeyValuePair<string, string> part in parts)
|
||||
{
|
||||
|
||||
|
||||
SmartPageDataSearch dataSearch = new SmartPageDataSearch
|
||||
{
|
||||
Items = new Item[]
|
||||
{
|
||||
new Item
|
||||
{
|
||||
MfrCode = actualLineCode,
|
||||
PartNumber = part.Value
|
||||
}
|
||||
},
|
||||
DataOption = new[] { "DIST_LINE", "ALL" }
|
||||
};
|
||||
|
||||
SmartPageDataSearchResponse response = await _nexpartService.SendRequest<SmartPageDataSearch, SmartPageDataSearchResponse>(dataSearch);
|
||||
|
||||
if (response.ResponseBody.Item?.Length > 0)
|
||||
{
|
||||
List<string> urls = new List<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl))
|
||||
{
|
||||
urls.Add(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl);
|
||||
};
|
||||
|
||||
if (response.ResponseBody.Item[0].AddImgs?.AddImg?.Length > 0)
|
||||
{
|
||||
urls.AddRange(response.ResponseBody.Item[0].AddImgs.AddImg.Select(i => i.AddImgUrl));
|
||||
}
|
||||
|
||||
if (urls.Count > 0)
|
||||
{
|
||||
Console.WriteLine($"Image {urls[0]} found for {part.Value}. Expected: {part.Key}, Actual: {actualLineCode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
83
PartSource.Automation/Jobs/POC/PartialInventoryUpdate.cs
Normal file
83
PartSource.Automation/Jobs/POC/PartialInventoryUpdate.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Extensions;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Enums;
|
||||
using PartSource.Automation.Services;
|
||||
@@ -40,7 +41,9 @@ namespace PartSource.Automation.Jobs
|
||||
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"));
|
||||
IEnumerable<FileInfo> files = directoryInfo.GetFiles()
|
||||
.Where(f => f.Name.StartsWith("seo_aces_vehicle_feed"))
|
||||
.OrderByDescending(f => f.GetWhiTimestamp());
|
||||
|
||||
foreach (FileInfo fileInfo in files)
|
||||
{
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace PartSource.Automation.Jobs
|
||||
}
|
||||
|
||||
IList<Fitment> fitments = GetPositionOrderedFitments(importData?.PartNumber, importData?.LineCode);
|
||||
IList<Vehicle> vehicles = _vehicleFitmentService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
IList<Vehicle> vehicles = await _vehicleFitmentService.GetVehiclesForPart(importData?.PartNumber, importData?.LineCode);
|
||||
|
||||
if (fitments.Count == 0 || vehicles.Count == 0)
|
||||
{
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -7,12 +6,10 @@ using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Jobs;
|
||||
using PartSource.Automation.Jobs.POC;
|
||||
using PartSource.Automation.Services;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.AutoMapper;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Services;
|
||||
using Ratermania.Automation.DependencyInjection;
|
||||
using Ratermania.Automation.Logging;
|
||||
using Ratermania.Shopify.DependencyInjection;
|
||||
using System;
|
||||
using System.IO;
|
||||
@@ -22,7 +19,8 @@ namespace PartSource.Automation
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args){
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
using IHost host = CreateHostBuilder().Build();
|
||||
@@ -77,7 +75,7 @@ namespace PartSource.Automation
|
||||
|
||||
.AddAutomation(options =>
|
||||
{
|
||||
options.HasBaseInterval(new TimeSpan(0, 15, 0))
|
||||
options.HasBaseInterval(new TimeSpan(0, 5, 0))
|
||||
.HasMaxFailures(1)
|
||||
//.HasJob<TestJob>(options => options.HasInterval(new TimeSpan(7, 0, 0, 0)));
|
||||
//
|
||||
@@ -95,16 +93,19 @@ namespace PartSource.Automation
|
||||
//.HasJob<StatusCheck>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||
// .StartsAt(DateTime.Parse("2021-04-01 08:00:00"))
|
||||
//)
|
||||
|
||||
.HasJob<UpdateFitment>(options =>
|
||||
options.HasInterval(new TimeSpan(24, 0, 0))
|
||||
// .StartsAt(DateTime.Today.AddMinutes(15))
|
||||
// )
|
||||
|
||||
//.HasJob<UpdatePricing>(options => options.HasInterval(new TimeSpan(24, 0, 0))
|
||||
// .HasDependency<ExecuteSsisPackages>()
|
||||
// // .StartsAt(DateTime.Today.AddHours(28))
|
||||
.HasJob<BulkUpdateInventory>(options =>
|
||||
options.HasInterval(new TimeSpan(1, 0, 0))
|
||||
.StartsAt(DateTime.Today.AddHours(-27))
|
||||
)
|
||||
.HasJob<PartialInventoryUpdate>(options =>
|
||||
options.HasInterval(new TimeSpan(1, 0, 0))
|
||||
.StartsAt(DateTime.Today.AddHours(-27).AddMinutes(30))
|
||||
);
|
||||
|
||||
//.HasJob<PartialInventoryUpdate>(options => options.HasInterval(new TimeSpan(1, 0, 0))
|
||||
//.HasDependency<ExecuteSsisPackages>()
|
||||
// .StartsAt(DateTime.Today)
|
||||
//);
|
||||
//);
|
||||
//.AddApiServer();
|
||||
})
|
||||
@@ -115,6 +116,7 @@ namespace PartSource.Automation
|
||||
.AddSingleton<VehicleService>()
|
||||
.AddSingleton<VehicleFitmentService>()
|
||||
.AddSingleton<NexpartService>()
|
||||
.AddSingleton<PartService>()
|
||||
|
||||
.AddAutoMapper(typeof(PartSourceProfile));
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Ftp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
@@ -18,7 +19,55 @@ namespace PartSource.Automation.Services
|
||||
_ftpConfiguration = ftpConfiguration;
|
||||
}
|
||||
|
||||
public string[] ListFiles(string directory)
|
||||
public IList<FtpFileInfo> ListFilesExtended(string directory = "")
|
||||
{
|
||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{directory}"));
|
||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
||||
|
||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||
using StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
|
||||
IList<FtpFileInfo> files = new List<FtpFileInfo>();
|
||||
string[] fileStrings = reader.ReadToEnd().Split("\r\n");
|
||||
|
||||
foreach (string fileString in fileStrings)
|
||||
{
|
||||
if (string.IsNullOrEmpty(fileString))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string dateString = fileString[..17];
|
||||
string[] sizeAndName = fileString[18..].TrimStart().Split(" ", 2);
|
||||
|
||||
if (sizeAndName[0].ToUpperInvariant().IndexOf("DIR") > -1)
|
||||
{
|
||||
files.Add(new FtpFileInfo
|
||||
{
|
||||
Modified = DateTime.Parse(fileString[..17]),
|
||||
Size = 0,
|
||||
Filename = sizeAndName[1].Trim(),
|
||||
FileType = FtpFileType.Directory
|
||||
});
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
files.Add(new FtpFileInfo
|
||||
{
|
||||
Modified = DateTime.Parse(fileString[..17]),
|
||||
Size = long.Parse(sizeAndName[0]),
|
||||
Filename = sizeAndName[1].Trim(),
|
||||
FileType = FtpFileType.File
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -34,19 +83,46 @@ namespace PartSource.Automation.Services
|
||||
: Array.Empty<string>();
|
||||
}
|
||||
|
||||
public void Download(string filename)
|
||||
public string Download(string filename, string destination = null)
|
||||
{
|
||||
string file = $"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}";
|
||||
|
||||
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);
|
||||
string destinationDirectory = Path.GetDirectoryName(destination);
|
||||
if (!Directory.Exists(destinationDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(destinationDirectory);
|
||||
}
|
||||
|
||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||
using Stream responseStream = response.GetResponseStream();
|
||||
using FileStream fileStream = new FileStream($"{_ftpConfiguration.Destination}\\{filename.Replace("/", "\\")}", FileMode.Create);
|
||||
using FileStream fileStream = new FileStream(destination, FileMode.Create);
|
||||
|
||||
responseStream.CopyTo(fileStream);
|
||||
|
||||
return destination;
|
||||
}
|
||||
|
||||
public void Delete(string filename)
|
||||
{
|
||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri($"{_ftpConfiguration.Url}/{filename}"));
|
||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||
request.Method = WebRequestMethods.Ftp.DeleteFile;
|
||||
|
||||
|
||||
using FtpWebResponse response = (FtpWebResponse)request.GetResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace PartSource.Automation.Services
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "C:\\Program Files (x86)\\Microsoft SQL Server\\130\\DTS\\Binn\\dtexec",
|
||||
FileName = "C:\\Program Files\\Microsoft SQL Server\\150\\DTS\\Binn\\dtexec.exe",
|
||||
Arguments = $"/file \"{_ssisConfiguration.Directory}\\{packageName}\"",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = false,
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Dtos;
|
||||
using PartSource.Data.Models;
|
||||
@@ -95,7 +96,7 @@ namespace PartSource.Automation.Services
|
||||
: new List<int>();
|
||||
}
|
||||
|
||||
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||
public async Task<IList<Vehicle>> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
|
||||
{
|
||||
@@ -104,9 +105,10 @@ namespace PartSource.Automation.Services
|
||||
|
||||
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
|
||||
|
||||
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
||||
.Where(d => d.LineCode == lineCode)
|
||||
.Select(d => d.WhiCode);
|
||||
IList<string> whiCodes = await _fitmentContext.DcfMappings
|
||||
.Where(dcf => dcf.LineCode == lineCode)
|
||||
.Select(dcf => dcf.WhiCode)
|
||||
.ToListAsync();
|
||||
|
||||
IQueryable<Vehicle> vehicles = _fitmentContext.Fitments
|
||||
.Where(f => f.PartNumber == partNumber && whiCodes.Contains(f.LineCode))
|
||||
|
||||
Reference in New Issue
Block a user