159 lines
5.8 KiB
C#
159 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using LCU.API.Collections.RepoApi.Configuration;
|
|
using LCU.API.Collections.RepoApi.Services.AutoIms;
|
|
using LCU.Shared.Collections.RepoApiLib.Models;
|
|
using Serilog;
|
|
|
|
namespace LCU.API.Collections.RepoApi.Services
|
|
{
|
|
public class AutoImsService
|
|
{
|
|
private readonly IImsAutoFileDataConverter _imsAutoFileDataConverter;
|
|
private readonly IImsAutoFileNoteConverter _imsAutoFileNoteConverter;
|
|
private readonly IEmailer _emailer;
|
|
private readonly IAutoImsDownloaderService _downloaderService;
|
|
private readonly RepoApiConfiguration _config;
|
|
private readonly IAutoImsImporterService _autoImsImporterService;
|
|
|
|
public AutoImsService(
|
|
IAutoImsDownloaderService downloaderService,
|
|
RepoApiConfiguration config,
|
|
IAutoImsImporterService autoImsImporterService,
|
|
IImsAutoFileDataConverter imsAutoFileDataConverter,
|
|
IImsAutoFileNoteConverter imsAutoFileNoteConverter, IEmailer emailer)
|
|
{
|
|
_downloaderService = downloaderService;
|
|
_config = config;
|
|
_emailer = emailer;
|
|
_autoImsImporterService = autoImsImporterService;
|
|
_imsAutoFileDataConverter = imsAutoFileDataConverter;
|
|
_imsAutoFileNoteConverter = imsAutoFileNoteConverter;
|
|
}
|
|
|
|
public IEnumerable<FileInfo> Download()
|
|
{
|
|
Log.Information("Starting AutoIMS Download Process.");
|
|
var files = new Dictionary<FileInfo, string>();
|
|
|
|
try
|
|
{
|
|
if (_config.DownloadNewFiles)
|
|
{
|
|
Log.Debug("Downloading auction files.");
|
|
files = _downloaderService.DownLoadAuctionFiles();
|
|
}
|
|
|
|
if (_config.CheckForLocalFiles)
|
|
{
|
|
Log.Debug("Checking for local files.");
|
|
files = _downloaderService.CombineWithLocalFiles(files);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,"AutoIms File Downloader Failure");
|
|
}
|
|
|
|
Log.Information("Finished AutoIMS Download Process.");
|
|
return _downloaderService.ConvertToFileList(files);
|
|
}
|
|
|
|
public void ProcessDownloadedFiles(IEnumerable<FileInfo> ascendingOrderOfFiles)
|
|
{
|
|
Log.Debug("Starting AutoIMS Entry Processing.");
|
|
|
|
if (ascendingOrderOfFiles == null)
|
|
{
|
|
throw new Exception("Must pass a valid IEnumerable of FileInfo to ProcessDownloads.");
|
|
}
|
|
|
|
foreach (var file in ascendingOrderOfFiles)
|
|
{
|
|
ProcessSingleFile(file);
|
|
}
|
|
}
|
|
|
|
public void ProcessSingleFile(FileInfo file)
|
|
{
|
|
Log.Debug("Processing Single Auto IMS File. {0}", file.FullName);
|
|
string fileName = file.Name;
|
|
FileStream stream = null;
|
|
try
|
|
{
|
|
stream = File.OpenRead(Path.Combine(_config.LocalDirectoryPath, fileName));
|
|
}
|
|
catch (IOException ioException)
|
|
{
|
|
Log.Error(ioException, "Failed to open AutoIMS file {0}, got exception.", file.FullName);
|
|
return;
|
|
}
|
|
|
|
if (file.Name.ToLower().Contains("notes"))
|
|
{
|
|
ProcessSingleAutoImsNotesFile(file, stream);
|
|
}
|
|
else
|
|
{
|
|
ProcessSingleAutoImsFile(file, stream);
|
|
}
|
|
|
|
if (_config.MoveProcessedFiles)
|
|
{
|
|
Log.Debug("Moving Files for AutoIMS Process.");
|
|
|
|
// create current month folder
|
|
if (!Directory.Exists(
|
|
Path.Combine(_config.LocalDirectoryPath, DateTime.Now.ToString("MMM yyyy"))))
|
|
{
|
|
var newDir = Path.Combine(_config.LocalDirectoryPath, DateTime.Now.ToString("MMM yyyy"));
|
|
Log.Information("Creating folder {0}", newDir);
|
|
Directory.CreateDirectory(newDir);
|
|
}
|
|
|
|
var sourceFileName = Path.Combine(_config.LocalDirectoryPath, fileName);
|
|
var destFileName =
|
|
Path.Combine(_config.LocalDirectoryPath, DateTime.Now.ToString("MMM yyyy"), fileName);
|
|
Log.Information("Moving File {0} to Folder {1}.", sourceFileName, destFileName);
|
|
File.Move(sourceFileName, destFileName);
|
|
}
|
|
}
|
|
|
|
public void ProcessSingleAutoImsNotesFile(FileInfo file, FileStream stream)
|
|
{
|
|
Log.Debug("Processing notes file {0}.", file);
|
|
|
|
try
|
|
{
|
|
IEnumerable<AuctionAssignmentNote> data =
|
|
_imsAutoFileNoteConverter.CsvToImsAutoFileNotes(stream);
|
|
data = _imsAutoFileNoteConverter.CompressIdentifyingRows(data);
|
|
_autoImsImporterService.ImportNotesToDatabase(data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,"AutoIMS Note Import Failure");
|
|
}
|
|
}
|
|
|
|
public void ProcessSingleAutoImsFile(FileInfo file, FileStream stream)
|
|
{
|
|
Log.Debug("Processing data file {0}.", file);
|
|
|
|
try
|
|
{
|
|
IReadOnlyCollection<ImsAutoFileData> models = _imsAutoFileDataConverter.CsvToImsAutoFileData(stream);
|
|
var imported = _autoImsImporterService.ImportDataFile(models);
|
|
var floors = _autoImsImporterService.SetAllAvailableFloors();
|
|
_emailer.SendRefundProcessStartEmails(imported);
|
|
_emailer.SendSummaryEmail(imported, floors);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,"AutoIMS Data Import Failure");
|
|
}
|
|
}
|
|
}
|
|
}
|