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

133 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using LCU.Shared.Collections.RepoApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Models.AutoIms;
using LCU.Shared.Collections.RepoApiLib.Repositories;
using Serilog;
namespace LCU.API.Collections.RepoApi.Services.AutoIms
{
public class AutoImsImporterService : IAutoImsImporterService
{
private readonly IAuctionAssignmentRepository _auctionAssignmentRepository;
private readonly IAutoImsNoteProcessorService _autoImsNoteProcessorService;
private readonly IAutoImsAuctionProcessorService _autoImsAuctionProcessorService;
private readonly IFloorPricingService _floorPricingService;
private readonly IAuctionAssignmentService _auctionAssignmentService;
public AutoImsImporterService(
IAuctionAssignmentRepository auctionAssignmentRepository,
IAutoImsNoteProcessorService autoImsNoteProcessorService,
IAutoImsAuctionProcessorService autoImsAuctionProcessorService, IFloorPricingService floorPricingService,
IAuctionAssignmentService auctionAssignmentService)
{
_autoImsAuctionProcessorService = autoImsAuctionProcessorService;
_floorPricingService = floorPricingService;
_auctionAssignmentService = auctionAssignmentService;
_autoImsNoteProcessorService = autoImsNoteProcessorService;
_auctionAssignmentRepository = auctionAssignmentRepository;
}
/**
* A set of notes comes from AutoIMS and must be inserted (But not updated).
* Duplicates/Existing are checked for based on writer, time and text.
*/
public IReadOnlyCollection<AuctionAssignmentNote> ImportNotesToDatabase(IEnumerable<AuctionAssignmentNote> data)
{
List<AuctionAssignmentNote> savedData = new List<AuctionAssignmentNote>();
foreach (var item in data)
{
try
{
var result = _autoImsNoteProcessorService.ImportSingleNoteRow(item);
if (result != null)
{
savedData.Add(result);
}
}
catch (Exception ex)
{
Log.Error(ex, $"Error processing notes for auction accountNo: {item.AuctionCustomerNo}");
}
}
return savedData;
}
/**
* Each row in the Data file is an ImsAutoAuctionData row used to track the status of
* a Repo at auction and assist in setting the floor.
*/
public IReadOnlyCollection<ImportResultRow> ImportDataFile(IEnumerable<ImsAutoFileData> models)
{
List<ImportResultRow> savedData = new List<ImportResultRow>();
foreach (var item in models)
{
try
{
var result = _autoImsAuctionProcessorService.ImportSingleAutoImsAuction(item);
if (result != null)
{
savedData.Add(result);
}
}
catch (Exception ex)
{
Log.Error(ex, $"Error processing data for auction account with repoId: {item.RepoId}");
}
}
return savedData;
}
public IReadOnlyCollection<ImportResultRow> SetAllAvailableFloors()
{
var list = new List<ImportResultRow>();
var auctionAssignments =
_auctionAssignmentRepository.ListReadyForFloorPriceAsync()
.ConfigureAwait(continueOnCapturedContext: false).GetAwaiter().GetResult();
if (auctionAssignments.Items != null && auctionAssignments.Items.Any())
{
foreach (var auctionAssignment in auctionAssignments.Items)
{
var result = new ImportResultRow() {Messages = new List<string>()};
var floorPriceCalculationResultSet = _floorPricingService.DetermineFloorPrice(auctionAssignment.ID);
if (floorPriceCalculationResultSet?.PrimaryResult != null)
{
result.Messages.Add("Floor Price Results Generated for Account " + auctionAssignment.RepoId);
if (floorPriceCalculationResultSet.PrimaryResult.Success)
{
result.Messages.Add("Floor Price Results SUCCESS for Account " + auctionAssignment.RepoId);
auctionAssignment.FloorPrice = floorPriceCalculationResultSet.PrimaryResult.FloorPrice;
auctionAssignment.FloorPriceValuesUsed =
floorPriceCalculationResultSet.FloorPriceValuesUsed;
var updated = _auctionAssignmentService.UpdateAsync(auctionAssignment).GetAwaiter()
.GetResult();
Log.Information($"Auction Floor Set Id:{updated.ID}, FloorPrice:{updated.FloorPrice}, Skipping.");
result.Messages.Add(
$"Floor price set for {auctionAssignment.RepoId} to {auctionAssignment.FloorPrice}");
result.Success = true;
}
else
{
result.Messages.Add("Floor Price Results FAILURE for Account " + auctionAssignment.RepoId);
result.Messages.Add("Reason: " +
floorPriceCalculationResultSet.PrimaryResult.FailureReason);
}
}
list.Add(result);
}
}
else
{
Log.Information("SetAllAvailableFloors: No floors to set.");
}
return list;
}
}
}