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> _RdsToHeaderCommonCollection; public ExportRepository(AppSettings appSettings) { _AppSettings = appSettings; _MockRoot = appSettings.MockRoot; _RdsToHeaderCommonCollection = new(); _RepositoryName = nameof(ExportRepository)[..^10]; _Log = Serilog.Log.ForContext(); } private static string[] Get() { DateTime dateTime = DateTime.Now; DateTime lastWeekDateTime = dateTime.AddDays(-7); Calendar calendar = new CultureInfo("en-US").Calendar; string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; string lastWeekOfYear = $"{lastWeekDateTime:yyyy}_Week_{calendar.GetWeekOfYear(lastWeekDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; return new string[] { weekOfYear, lastWeekOfYear }; } private List GetFiles(HeaderCommon headerCommon, string searchPattern) { List 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 files = GetFiles(headerCommon, "*.txt"); if (files.Count != 1) result = string.Empty; else result = File.ReadAllText(files.First()); } return result; } Result IExportRepository.GetHeaders(HeaderCommon headerCommon) { Result? result; if (!string.IsNullOrEmpty(_MockRoot)) { string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetHeaders)}.json")); result = JsonSerializer.Deserialize>(json); if (result is null) throw new NullReferenceException(nameof(result)); } else { List results = new(); string json; HeaderCommon? hc; List files = GetFiles(headerCommon, "*.json"); foreach (string file in files) { json = File.ReadAllText(file); hc = JsonSerializer.Deserialize(json); if (hc is null) continue; results.Add(hc); } result = new() { Results = results.ToArray(), TotalRows = results.Count, }; } return result; } Result IExportRepository.GetLogistics(HeaderCommon headerCommon) { Result? result; if (!string.IsNullOrEmpty(_MockRoot)) { string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IExportRepository.GetLogistics)}.json")); result = JsonSerializer.Deserialize>(json); if (result is null) throw new NullReferenceException(nameof(result)); } else { List results = new(); string json; HeaderCommon? hc; List files = GetFiles(headerCommon, "*.json"); foreach (string file in files) { json = File.ReadAllText(file); hc = JsonSerializer.Deserialize(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 files = GetFiles(headerCommon, "*.pdsf"); if (files.Count != 1) result = string.Empty; else result = File.ReadAllText(files.First()); } return result; } }