oi-metrology/Server/Repositories/PinRepository.cs
2023-02-08 10:26:59 -07:00

93 lines
3.8 KiB
C#

using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using System.Text.Json;
namespace OI.Metrology.Server.Repository;
public class PinRepository : IPinRepository
{
private readonly string _MockRoot;
private readonly Serilog.ILogger _Log;
public PinRepository(string mockRoot)
{
_MockRoot = mockRoot;
_Log = Serilog.Log.ForContext<PinRepository>();
}
Result<Pinned[]> IPinRepository.GetPinnedTable(IMetrologyRepository metrologyRepository, int id, string? bioRad, string? cde)
{
Result<Pinned[]>? r;
HeaderCommon? cdeHeader = cde is null ? null : JsonSerializer.Deserialize<HeaderCommon>(cde);
long cdeId = cdeHeader is null ? (long)IPinRepository.ToolId.CDE : cdeHeader.ToolTypeID;
HeaderCommon? bioRadHeader = bioRad is null ? null : JsonSerializer.Deserialize<HeaderCommon>(bioRad);
long bioRadId = bioRadHeader is null ? (long)IPinRepository.ToolId.BioRad : bioRadHeader.ToolTypeID;
if (cdeId != id && bioRadId != id)
r = new() { Results = Array.Empty<Pinned>(), TotalRows = 0 };
else
{
if (!string.IsNullOrEmpty(_MockRoot))
{
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), "GetPinnedTableApi.json"));
r = JsonSerializer.Deserialize<Result<Pinned[]>>(json);
if (r is null)
throw new NullReferenceException(nameof(r));
}
else
{
List<string> values;
const int points = 9;
Pinned headerCommond;
List<Pinned> results = new();
if (bioRadHeader is not null)
{
const int thickness = 5;
System.Data.DataTable dataTable = metrologyRepository.GetData((int)bioRadHeader.ToolTypeID, bioRadHeader.ID);
if (dataTable.Rows.Count > points && dataTable.Columns.Count > thickness)
{
values = new();
for (int i = 0; i < dataTable.Rows.Count - 1; i++)
{
if (dataTable.Rows[i]?.ItemArray[thickness] is null)
break;
values.Add(string.Concat(dataTable.Rows[i].ItemArray[thickness]));
}
if (values.Count > points)
{
headerCommond = new(bioRadHeader, values);
results.Add(headerCommond);
}
}
}
if (cdeHeader is not null)
{
const int rs = 6;
System.Data.DataTable dataTable = metrologyRepository.GetData((int)cdeHeader.ToolTypeID, cdeHeader.ID);
if (dataTable.Rows.Count > points && dataTable.Columns.Count > rs)
{
values = new();
for (int i = 0; i < dataTable.Rows.Count - 1; i++)
{
if (dataTable.Rows[i]?.ItemArray[rs] is null)
break;
values.Add(string.Concat(dataTable.Rows[i].ItemArray[rs]));
}
if (values.Count > points)
{
headerCommond = new(cdeHeader, values);
results.Add(headerCommond);
}
}
}
r = new()
{
Results = results.ToArray(),
TotalRows = results.Count,
};
}
}
return r;
}
}