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(); } Result IPinRepository.GetPinnedTable(IMetrologyRepository metrologyRepository, int id, string? bioRad, string? cde) { Result? r; HeaderCommon? cdeHeader = cde is null ? null : JsonSerializer.Deserialize(cde); long cdeId = cdeHeader is null ? (long)IPinRepository.ToolId.CDE : cdeHeader.ToolTypeID; HeaderCommon? bioRadHeader = bioRad is null ? null : JsonSerializer.Deserialize(bioRad); long bioRadId = bioRadHeader is null ? (long)IPinRepository.ToolId.BioRad : bioRadHeader.ToolTypeID; if (cdeId != id && bioRadId != id) r = new() { Results = Array.Empty(), TotalRows = 0 }; else { if (!string.IsNullOrEmpty(_MockRoot)) { string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), "GetPinnedTableApi.json")); r = JsonSerializer.Deserialize>(json); if (r is null) throw new NullReferenceException(nameof(r)); } else { List values; const int points = 9; Pinned headerCommond; List 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; } }