This commit is contained in:
2023-08-23 15:04:54 -04:00
parent d95d947bc2
commit 68c9e01ef1
24 changed files with 655 additions and 388 deletions

View File

@@ -1,159 +1,160 @@
using System;
using System.Collections.Generic;
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;
using PartSource.Data.Nexpart;
namespace PartSource.Services
{
public class FitmentService
{
private readonly FitmentContext _fitmentContext;
private readonly FitmentContext _fitmentContext;
public FitmentService(FitmentContext fitmentContext)
public FitmentService(FitmentContext fitmentContext)
{
_fitmentContext = fitmentContext;
_fitmentContext = fitmentContext;
}
public IList<string> GetYmmFitment(IList<Vehicle> vehicles)
{
if (vehicles.Count == 0)
{
return new string[0];
}
public async Task<VehicleFitmentDto> GetFitmentNotes(string sku, int vehicleId)
{
VehicleFitmentDto vehicleFitment = await _fitmentContext.VehicleFitments
.Where(vf => vf.VehicleToEngineConfigId == vehicleId && vf.Sku == sku)
.Select(vf => new VehicleFitmentDto
{
Sku = vf.Sku,
LineCode = vf.LineCode,
PartNumber = vf.PartNumber,
NoteText = vf.NoteText,
Year = vf.Year,
MakeName = vf.MakeName,
ModelName = vf.ModelName,
BaseVehicleId = vf.BaseVehicleId,
EngineConfigId = vf.EngineConfigId,
VehicleToEngineConfigId = vf.VehicleToEngineConfigId
})
.FirstOrDefaultAsync();
IList<string> fitmentTags = new List<string>();
if (vehicleFitment == null)
{
return null;
}
IList<string> makeModels = vehicles.OrderBy(v => v.MakeName).ThenBy(v => v.ModelName).Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
vehicleFitment.SubmodelNames = await _fitmentContext.VehicleFitments
.Where(vf => vf.BaseVehicleId == vehicleFitment.BaseVehicleId && vf.Sku == sku)
.Select(vf => vf.SubmodelName)
.Distinct()
.ToListAsync();
foreach (string makeModel in makeModels)
{
string make = makeModel.Split(',')[0];
string model = makeModel.Split(',')[1];
return vehicleFitment;
}
List<string> years = vehicles
.Where(v => v.MakeName == make && v.ModelName == model)
.OrderBy(v => v.Year)
.Select(v => v.Year.ToString().Trim())
.Distinct()
.ToList();
public IList<string> GetYmmFitment(IList<Vehicle> vehicles)
{
if (vehicles.Count == 0)
{
return new string[0];
}
string tag = $"{string.Join('-', years)} {make.Trim()} {model.Trim()}";
IList<string> fitmentTags = new List<string>();
System.Diagnostics.Debug.WriteLine(tag);
IList<string> makeModels = vehicles.OrderBy(v => v.MakeName).ThenBy(v => v.ModelName).Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
fitmentTags.Add(tag);
}
foreach (string makeModel in makeModels)
{
string make = makeModel.Split(',')[0];
string model = makeModel.Split(',')[1];
return fitmentTags;
}
List<string> years = vehicles
.Where(v => v.MakeName == make && v.ModelName == model)
.OrderBy(v => v.Year)
.Select(v => v.Year.ToString().Trim())
.Distinct()
.ToList();
public IList<string> GetYmmFitmentRange(IList<Vehicle> vehicles)
{
if (vehicles.Count == 0)
{
return new string[0];
}
string tag = $"{string.Join('-', years)} {make.Trim()} {model.Trim()}";
IList<string> fitmentTags = new List<string>();
System.Diagnostics.Debug.WriteLine(tag);
IList<string> makeModels = vehicles.Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
fitmentTags.Add(tag);
}
foreach (string makeModel in makeModels)
{
string make = makeModel.Split(',')[0];
string model = makeModel.Split(',')[1];
return fitmentTags;
}
int minYear = vehicles
.Where(v => v.MakeName == make && v.ModelName == model)
.Min(v => v.Year);
public IList<string> GetYmmFitmentRange(IList<Vehicle> vehicles)
{
if (vehicles.Count == 0)
{
return new string[0];
}
int maxYear = vehicles
.Where(v => v.MakeName == make && v.ModelName == model)
.Max(v => v.Year);
IList<string> fitmentTags = new List<string>();
string tag = minYear == maxYear
? $"{minYear} {make.Trim()} {model.Trim()}"
: $"{minYear}-{maxYear} {make.Trim()} {model.Trim()}";
IList<string> makeModels = vehicles.Select(v => $"{v.MakeName},{v.ModelName}").Distinct().ToList();
System.Diagnostics.Debug.WriteLine(tag);
foreach (string makeModel in makeModels)
{
string make = makeModel.Split(',')[0];
string model = makeModel.Split(',')[1];
fitmentTags.Add(tag);
}
int minYear = vehicles
.Where(v => v.MakeName == make && v.ModelName == model)
.Min(v => v.Year);
return fitmentTags;
}
int maxYear = vehicles
.Where(v => v.MakeName == make && v.ModelName == model)
.Max(v => v.Year);
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
{
return vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToList();
}
string tag = minYear == maxYear
? $"{minYear} {make.Trim()} {model.Trim()}"
: $"{minYear}-{maxYear} {make.Trim()} {model.Trim()}";
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
{
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
{
return null;
}
System.Diagnostics.Debug.WriteLine(tag);
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
fitmentTags.Add(tag);
}
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
.Where(d => d.LineCode == lineCode)
.Select(d => d.WhiCode);
return fitmentTags;
}
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);
public IList<int> GetVehicleIdFitment(IList<Vehicle> vehicles)
{
return vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToList();
}
if (maxVehicles > 0)
{
vehicles = vehicles.Take(maxVehicles);
}
public IList<Vehicle> GetVehiclesForPart(string partNumber, string lineCode, int maxVehicles = 0)
{
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
{
return null;
}
return vehicles.ToList();
}
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9]", string.Empty);
public IList<VehicleFitmentDto> GetVehicleFitmentForPart(string partNumber, string lineCode, int maxVehicles = 0)
{
if (string.IsNullOrEmpty(partNumber) || string.IsNullOrEmpty(lineCode))
{
return null;
}
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
.Where(d => d.LineCode == lineCode)
.Select(d => d.WhiCode);
partNumber = Regex.Replace(partNumber, "[^a-zA-Z0-9\\-]", string.Empty);
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);
IQueryable<string> whiCodes = _fitmentContext.DcfMappings
.Where(d => d.LineCode == lineCode)
.Select(d => d.WhiCode);
if (maxVehicles > 0)
{
vehicles = vehicles.Take(maxVehicles);
}
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();
}
}
return vehicles.ToList();
}
}
}