oi-metrology/Server/Repositories/ReactorsRepository.cs

146 lines
3.8 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 ReactorsRepository : IReactorsRepository
{
private readonly AppSettings _AppSettings;
private readonly Dictionary<int, char> _SecondsToAlpha;
public ReactorsRepository(AppSettings appSettings)
{
_AppSettings = appSettings;
_SecondsToAlpha = new();
for (int i = 65; i < 91; i++)
{
if (i is 73 or 79)
continue;
_SecondsToAlpha.Add(i - 65, (char)i);
}
}
Result<int[]> IReactorsRepository.EvenReactors()
{
Result<int[]> results;
int[] collection = new int[] {
20,
22,
24,
26,
28,
30,
32,
34,
36,
38,
40,
42,
44,
46,
48,
50,
52,
54,
56,
58,
60,
62,
64,
66,
68,
70,
72,
74
};
results = new()
{
Results = collection,
TotalRows = collection.Length,
};
return results;
}
Result<int[]> IReactorsRepository.OddReactors()
{
Result<int[]> results;
int[] collection = new int[] {
21,
23,
25,
27,
29,
31,
33,
35,
37,
39,
41,
43,
45,
47,
49,
51,
53,
55,
57,
59,
61,
63,
65,
73,
75,
77,
79
};
results = new()
{
Results = collection,
TotalRows = collection.Length,
};
return results;
}
string? IReactorsRepository.GetKey(WorkMaterialOut workMaterialOut, bool save)
{
if (workMaterialOut is null)
throw new Exception();
if (workMaterialOut.Username is null || workMaterialOut.Username.Length < 2)
throw new ArgumentException(nameof(workMaterialOut.Username));
if (workMaterialOut.RunDataSheet is null || workMaterialOut.RunDataSheet.Length < 2)
throw new ArgumentException(nameof(workMaterialOut.RunDataSheet));
string? result = null;
char c;
string? fileName = null;
DateTime dateTime = DateTime.Now;
Calendar calendar = new CultureInfo("en-US").Calendar;
string json = JsonSerializer.Serialize(workMaterialOut, new JsonSerializerOptions { WriteIndented = true });
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
string directory = Path.Combine(_AppSettings.ApiExportPath, "WorkMaterialOut", "API", weekOfYear, dateTime.ToString("yyyy-MM-dd_HH"));
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
for (int i = 0; i < int.MaxValue; i++)
{
dateTime = dateTime.AddSeconds(i);
if (!_SecondsToAlpha.TryGetValue(dateTime.Second, out c))
continue;
fileName = Path.Combine(directory, $"WMO-{dateTime:mm}{c}.json");
if (!File.Exists(fileName))
{
result = $"{c}{dateTime:mm}";
break;
}
}
if (fileName is null)
throw new Exception();
if (save)
File.WriteAllText(fileName, json);
return result;
}
}