129 lines
3.3 KiB
C#
129 lines
3.3 KiB
C#
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 != null
|
|
? vehicles.Select(v => v.VehicleToEngineConfigId).Distinct().ToArray()
|
|
: new List<int>();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|