Files
2026-04-20 14:47:24 -05:00

311 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using LCU.API.Collections.RepoApi.Configuration;
using LCU.Shared.Collections.RepoApiLib.Models;
using NotVisualBasic.FileIO;
using Serilog;
namespace LCU.API.Collections.RepoApi.Services.AutoIms
{
public class ImsAutoFileDataConverter : IImsAutoFileDataConverter
{
private readonly RepoApiConfiguration _configuration;
public ImsAutoFileDataConverter( RepoApiConfiguration configuration)
{
_configuration = configuration;
}
public IReadOnlyCollection<ImsAutoFileData> CsvToImsAutoFileData(Stream file)
{
List<ImsAutoFileData> listOfObjects = new List<ImsAutoFileData>();
using (CsvTextFieldParser tfp = new CsvTextFieldParser(file))
{
// Get Some Column Names
string[] columns = tfp.ReadFields();
while (!tfp.EndOfData)
{
var fields = tfp.ReadFields();
if (!fields[0].Contains("------") && (fields.Any(x => x.Trim().Length > 0)))
{
var obj = new ImsAutoFileData();
for (var i = 0; i < fields.Length; i++)
{
obj = SetFieldByName(obj, columns[i].Trim(), fields[i]);
}
listOfObjects.Add(obj);
}
}
}
return listOfObjects;
}
public ImsAutoFileData SetFieldByName(ImsAutoFileData input, string fieldName, string value)
{
try
{
switch (fieldName)
{
case "Announced Conditions":
input.AnnouncedConditions = value.Trim();
break;
case "Arbitration Reason":
input.ArbitrationReason = value.Trim();
break;
case "As Is 100K Miles":
input.AsIs100KMiles = (value.Trim() == "Yes");
break;
case "Assignment Accepted":
input.AssignmentAccepted =
!string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Auction Age":
input.AuctionAge = !string.IsNullOrWhiteSpace(value) ? NumbersOnly<int>(value) : (int?) null;
break;
case "Auction Guide Book Price":
input.AuctionGuideBookPrice = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Buyer ID":
input.BuyerId = value.Trim();
break;
case "Buyer Name":
input.BuyerName = value.Trim();
break;
case "Charge Amount":
input.ChargeAmount = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Check Amount":
input.CheckAmount = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Client Age":
input.ClientAge = !string.IsNullOrWhiteSpace(value) ? NumbersOnly<int>(value) : (int?) null;
break;
case "Client Cancelled":
input.ClientCancelled =
!string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Collision Damage":
input.CollisionDamage = value.Trim();
break;
case "Inspected Date":
input.InspectedDate = !string.IsNullOrEmpty(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Date of Loss":
input.DateofLoss = !string.IsNullOrEmpty(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Date Picked Up":
input.DatePickedUp = !string.IsNullOrEmpty(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Date Entered AutoIMS":
input.DateEnteredAutoIMS = !string.IsNullOrEmpty(value)
? DateTime.Parse(value.Substring(0, value.Trim().Length - 4))
: (DateTime?) null;
break;
case "Days Run":
input.DaysRun = !string.IsNullOrWhiteSpace(value) ? NumbersOnly<int>(value) : (int?) null;
break;
case "Days Run/Highest Bid Recorded by Auction":
input.DaysRunHighestBidRecordedbyAuction = value;
break;
case "Days to Delivery":
input.DaystoDelivery =
!string.IsNullOrWhiteSpace(value) ? NumbersOnly<int>(value) : (int?) null;
break;
case "Description":
input.Description = value.Trim();
break;
case "Drivable":
input.IsDrivable = !string.IsNullOrWhiteSpace(value)
? value == "Yes" ? true : false
: (bool?) null;
break;
case "Floor Price (Accepted)":
input.FloorPriceAccepted = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Funds Sent":
input.FundsSent = !string.IsNullOrEmpty(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Gross % Floor":
input.GrossPercentFloor =
!string.IsNullOrWhiteSpace(value) ? NumbersOnly<decimal>(value) : (decimal?) null;
break;
case "Highest Bid":
input.HighestBid = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Mileage":
input.StatedMileage = !string.IsNullOrWhiteSpace(value)
? int.Parse(value.Replace(",", ""))
: (int?) null;
break;
case "Net % Floor":
input.NetPercentFloor = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Other Advance Fees":
input.OtherAdvanceFees = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "OTHER Total":
input.OTHERTotal = value.Trim();
break;
case "OTHER: Advanced Other(72)":
input.OTHERAdvancedOther = value.Trim();
break;
case "OTHER: Advanced Storage(71)":
input.OTHERAdvancedStorage = value.Trim();
break;
case "OTHER: Advanced Tow(70)":
input.OTHERAdvancedTow = value.Trim();
break;
case "OTHER: Miscellaneous(41)":
input.OTHERMiscellaneous = value.Trim();
break;
case "Sale Price":
input.SalePrice = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Scheduled Sale Date":
input.ScheduledSaleDate =
!string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Sold Date":
input.SoldDate = !string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Title #":
input.TitleNumber = value.Trim();
break;
case "Title Received":
input.TitleReceived =
!string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Title Returned":
input.TitleReturned =
!string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Title State":
input.TitleState = value.Trim();
break;
case "Title Status":
input.TitleStatus = value.Trim();
break;
case "Title Status from Auction":
input.TitleStatusfromAuction = value.Trim();
break;
case "Total Charges":
input.TotalCharges = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Vehicle Grade (Approved CR)":
input.ApprovedVehicleGrade = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Operable":
input.IsOperable = !string.IsNullOrWhiteSpace(value)
? value == "Yes" ? true : false
: (bool?) null;
break;
case "Shipment Date":
input.ShipmentDate =
!string.IsNullOrWhiteSpace(value) ? DateTime.Parse(value) : (DateTime?) null;
break;
case "Status":
input.Status = value.Trim();
break;
case "VIN":
input.VIN = value.Trim();
break;
case "Storage Cost":
input.StorageCost = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Storage Rate Per Day":
input.StorageRatePerDay = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Tow Cost":
input.TowCost = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Cn. Acc. #":
input.AuctionCustomerNo = value.Trim();
break;
case "Auction ID":
break;
case "Vehicle Decimal Grade Code":
input.VehicleDecimalGradeCode = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Vehicle Grade":
input.VehicleGrade = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Most Recent National Adjusted MMR Price":
input.MMRNationalAdjustedPrice = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "Most Recent Regional Adjusted MMR Price":
input.MMRRegionalAdjustedPrice = !string.IsNullOrWhiteSpace(value)
? NumbersOnly<decimal>(value)
: (decimal?) null;
break;
case "MMR":
input.MMR = value.Trim();
break;
case "External CR Link":
input.ConditionReportLink = _configuration.ConditionReportUrl + value;
break;
case "Open Text 1":
input.AuctionAssignmentId =
!string.IsNullOrWhiteSpace(value) ? int.Parse(value.Replace(",", "")) : -1;
break;
default:
Log.Error("Got unknown field {0} with value {1} for ImsAutoFileData", fieldName,
value);
break;
}
}
catch (Exception ex)
{
Log.Error(ex, $"Failed to set field {fieldName} with value {value} for ImsAutoFileData");
}
return input;
}
private static T NumbersOnly<T>(string val)
{
var formattedStr = Regex.Replace(val.Trim(), "[^0-9\\.]", "");
if (string.IsNullOrWhiteSpace(formattedStr))
formattedStr = "-1";
return (T) Convert.ChangeType(formattedStr, typeof(T));
}
}
}