150 lines
5.5 KiB
C#
150 lines
5.5 KiB
C#
using OI.Metrology.Server.Models;
|
|
using OI.Metrology.Shared.DataModels;
|
|
using OI.Metrology.Shared.Models.Stateless;
|
|
using System.Globalization;
|
|
using System.Text.Json;
|
|
|
|
namespace OI.Metrology.Server.Repository;
|
|
|
|
public class ExportRepository : IExportRepository
|
|
{
|
|
|
|
private readonly string _MockRoot;
|
|
private readonly Serilog.ILogger _Log;
|
|
private readonly string _RepositoryName;
|
|
private readonly AppSettings _AppSettings;
|
|
private readonly Dictionary<string, Dictionary<long, HeaderCommon>> _RdsToHeaderCommonCollection;
|
|
|
|
public ExportRepository(AppSettings appSettings)
|
|
{
|
|
_AppSettings = appSettings;
|
|
_MockRoot = appSettings.MockRoot;
|
|
_RdsToHeaderCommonCollection = new();
|
|
_RepositoryName = nameof(ExportRepository)[..^10];
|
|
_Log = Serilog.Log.ForContext<ExportRepository>();
|
|
}
|
|
|
|
private static string[] Get()
|
|
{
|
|
DateTime dateTime = DateTime.Now;
|
|
Calendar calendar = new CultureInfo("en-US").Calendar;
|
|
string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
string lastWeekOfYear = calendar.GetWeekOfYear(dateTime.AddDays(7), CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
return new string[] { weekOfYear, lastWeekOfYear };
|
|
}
|
|
|
|
private List<string> GetFiles(HeaderCommon headerCommon, string searchPattern)
|
|
{
|
|
List<string> results = new();
|
|
string directory;
|
|
string[] weeks = Get();
|
|
foreach (string weekYear in weeks)
|
|
{
|
|
directory = Path.Combine(_AppSettings.ApiExportPath, weekYear, $"-{headerCommon.PSN}", $"-{headerCommon.Reactor}", $"-{headerCommon.RDS}", $"-{headerCommon.ID}");
|
|
if (!Directory.Exists(directory))
|
|
continue;
|
|
results.AddRange(Directory.GetFiles(directory, searchPattern, SearchOption.TopDirectoryOnly));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
string IExportRepository.GetExport(HeaderCommon headerCommon)
|
|
{
|
|
string result;
|
|
if (!string.IsNullOrEmpty(_MockRoot))
|
|
result = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetExport)}.txt"));
|
|
else
|
|
{
|
|
List<string> files = GetFiles(headerCommon, "*.txt");
|
|
if (files.Count != 1)
|
|
result = string.Empty;
|
|
else
|
|
result = File.ReadAllText(files.First());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Result<HeaderCommon[]> IExportRepository.GetHeaders(HeaderCommon headerCommon)
|
|
{
|
|
Result<HeaderCommon[]>? result;
|
|
if (!string.IsNullOrEmpty(_MockRoot))
|
|
{
|
|
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetHeaders)}.json"));
|
|
result = JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
|
|
if (result is null)
|
|
throw new NullReferenceException(nameof(result));
|
|
}
|
|
else
|
|
{
|
|
List<HeaderCommon> results = new();
|
|
string json;
|
|
HeaderCommon? hc;
|
|
List<string> files = GetFiles(headerCommon, "*.json");
|
|
foreach (string file in files)
|
|
{
|
|
json = File.ReadAllText(file);
|
|
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
|
|
if (hc is null)
|
|
continue;
|
|
results.Add(hc);
|
|
}
|
|
result = new()
|
|
{
|
|
Results = results.ToArray(),
|
|
TotalRows = results.Count,
|
|
};
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
Result<HeaderCommon[]> IExportRepository.GetLogistics(HeaderCommon headerCommon)
|
|
{
|
|
Result<HeaderCommon[]>? result;
|
|
if (!string.IsNullOrEmpty(_MockRoot))
|
|
{
|
|
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetLogistics)}.json"));
|
|
result = JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
|
|
if (result is null)
|
|
throw new NullReferenceException(nameof(result));
|
|
}
|
|
else
|
|
{
|
|
List<HeaderCommon> results = new();
|
|
string json;
|
|
HeaderCommon? hc;
|
|
List<string> files = GetFiles(headerCommon, "*.json");
|
|
foreach (string file in files)
|
|
{
|
|
json = File.ReadAllText(file);
|
|
hc = JsonSerializer.Deserialize<HeaderCommon>(json);
|
|
if (hc is null)
|
|
continue;
|
|
results.Add(hc);
|
|
}
|
|
result = new()
|
|
{
|
|
Results = results.ToArray(),
|
|
TotalRows = results.Count,
|
|
};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
string IExportRepository.GetProcessDataStandardFormat(HeaderCommon headerCommon)
|
|
{
|
|
string result;
|
|
if (!string.IsNullOrEmpty(_MockRoot))
|
|
result = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetProcessDataStandardFormat)}.pdsf"));
|
|
else
|
|
{
|
|
List<string> files = GetFiles(headerCommon, "*.pdsf");
|
|
if (files.Count != 1)
|
|
result = string.Empty;
|
|
else
|
|
result = File.ReadAllText(files.First());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} |