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 ImportNotesToDatabase(IEnumerable data) { List savedData = new List(); 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 ImportDataFile(IEnumerable models) { List savedData = new List(); 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 SetAllAvailableFloors() { var list = new List(); 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()}; 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; } } }