Fitment update
This commit is contained in:
159
PartSource.Automation/Services/VehicleFitmentService.cs
Normal file
159
PartSource.Automation/Services/VehicleFitmentService.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using PartSource.Data.Contexts;
|
||||
using PartSource.Data.Dtos;
|
||||
using PartSource.Data.Models;
|
||||
|
||||
namespace PartSource.Automation.Services
|
||||
{
|
||||
public class VehicleFitmentService
|
||||
{
|
||||
private readonly FitmentContext _fitmentContext;
|
||||
|
||||
public VehicleFitmentService(FitmentContext fitmentContext)
|
||||
{
|
||||
_fitmentContext = fitmentContext;
|
||||
}
|
||||
|
||||
public IList<string> GetYmmFitment(IList<Vehicle> vehicles)
|
||||
{
|
||||
if (vehicles.Count == 0)
|
||||
{
|
||||
return new string[0];
|
||||
}
|
||||
|
||||
IList<string> fitmentTags = new List<string>();
|
||||
|
||||
IList<string> makeModels = vehicles.OrderBy(v => v.MakeName).ThenBy(v => v.ModelName).Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
|
||||
|
||||
foreach (string makeModel in makeModels)
|
||||
{
|
||||
string make = makeModel.Split(',')[0];
|
||||
string model = makeModel.Split(',')[1];
|
||||
|
||||
List<string> years = vehicles
|
||||
.Where(v => v.MakeName == make && v.ModelName == model)
|
||||
.OrderBy(v => v.Year)
|
||||
.Select(v => v.Year.ToString().Trim())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
string tag = $"{string.Join('-', years)} {make.Trim()} {model.Trim()}";
|
||||
|
||||
System.Diagnostics.Debug.WriteLine(tag);
|
||||
|
||||
fitmentTags.Add(tag);
|
||||
}
|
||||
|
||||
return fitmentTags;
|
||||
}
|
||||
|
||||
public IList<string> GetYmmFitmentRange(IList<Vehicle> vehicles)
|
||||
{
|
||||
if (vehicles.Count == 0)
|
||||
{
|
||||
return new string[0];
|
||||
}
|
||||
|
||||
IList<string> fitmentTags = new List<string>();
|
||||
|
||||
IList<string> makeModels = vehicles.Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
|
||||
|
||||
foreach (string makeModel in makeModels)
|
||||
{
|
||||
string make = makeModel.Split(',')[0];
|
||||
string model = makeModel.Split(',')[1];
|
||||
|
||||
int minYear = vehicles
|
||||
.Where(v => v.MakeName == make && v.ModelName == model)
|
||||
.Min(v => v.Year);
|
||||
|
||||
int maxYear = vehicles
|
||||
.Where(v => v.MakeName == make && v.ModelName == model)
|
||||
.Max(v => v.Year);
|
||||
|
||||
string tag = minYear == maxYear
|
||||
? $"{minYear} {make.Trim()} {model.Trim()}"
|
||||
: $"{minYear}-{maxYear} {make.Trim()} {model.Trim()}";
|
||||
|
||||
System.Diagnostics.Debug.WriteLine(tag);
|
||||
|
||||
fitmentTags.Add(tag);
|
||||
}
|
||||
|
||||
return fitmentTags;
|
||||
}
|
||||
|
||||
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
|
||||
{
|
||||
return vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray();
|
||||
}
|
||||
|
||||
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9\\-]", string.Empty);
|
||||
|
||||
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
||||
.Where(d => d.LineCode == lineCode)
|
||||
.Select(d => d.WhiCode);
|
||||
|
||||
IQueryable<Vehicle> vehicles = _fitmentContext.Fitments
|
||||
.Where(f => f.PartNumber == partNumber && whiCodes.Contains(f.LineCode))
|
||||
.Join(_fitmentContext.Vehicles,
|
||||
f => new { f.BaseVehicleId, f.EngineConfigId },
|
||||
v => new { v.BaseVehicleId, v.EngineConfigId },
|
||||
(f, v) => v)
|
||||
.Distinct()
|
||||
.OrderByDescending(x => x.Year);
|
||||
|
||||
if (maxVehicles > 0)
|
||||
{
|
||||
vehicles = vehicles.Take(maxVehicles);
|
||||
}
|
||||
|
||||
return vehicles.ToList();
|
||||
}
|
||||
|
||||
public IList<VehicleFitmentDto> GetVehicleFitmentForPart(string partNumber, string lineCode, int maxVehicles = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9\\-]", string.Empty);
|
||||
|
||||
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
|
||||
.Where(d => d.LineCode == lineCode)
|
||||
.Select(d => d.WhiCode);
|
||||
|
||||
IQueryable<VehicleFitmentDto> vehicles = _fitmentContext.Fitments
|
||||
.Where(f => f.PartNumber == partNumber && whiCodes.Contains(f.LineCode))
|
||||
.Join(_fitmentContext.Vehicles,
|
||||
f => new { f.BaseVehicleId, f.EngineConfigId },
|
||||
v => new { v.BaseVehicleId, v.EngineConfigId },
|
||||
(f, v) => new VehicleFitmentDto
|
||||
{
|
||||
Fitment = f,
|
||||
Vehicle = v
|
||||
})
|
||||
.Distinct();
|
||||
|
||||
if (maxVehicles > 0)
|
||||
{
|
||||
vehicles = vehicles.Take(maxVehicles);
|
||||
}
|
||||
|
||||
return vehicles.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using PartSource.Automation.Models.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Text;
|
||||
|
||||
namespace PartSource.Automation.Services
|
||||
{
|
||||
public class WhiSeoService
|
||||
public class WhiSeoService
|
||||
{
|
||||
private readonly FtpService _ftpService;
|
||||
private readonly string _connectionString;
|
||||
@@ -52,12 +53,12 @@ namespace PartSource.Automation.Services
|
||||
}
|
||||
}
|
||||
|
||||
public void TruncateVehicleTables()
|
||||
public void TruncateVehicleTable()
|
||||
{
|
||||
using SqlConnection connection = new SqlConnection(_connectionString);
|
||||
connection.Open();
|
||||
|
||||
using SqlCommand command = new SqlCommand($"exec DropVehicleTables", connection);
|
||||
using SqlCommand command = new SqlCommand($"truncate table dbo.Vehicle", connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
@@ -150,6 +151,10 @@ namespace PartSource.Automation.Services
|
||||
using SqlCommand command = new SqlCommand($"exec CreateFitmentView", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
using SqlCommand command2 = new SqlCommand($"exec CreateFitmentIndexes", connection);
|
||||
command.CommandTimeout = 1800;
|
||||
command2.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void CreateVehicleTable()
|
||||
|
||||
Reference in New Issue
Block a user