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; private readonly Dictionary> _RdsToHeaderCommonCollection; public PinRepository(string mockRoot) { _MockRoot = mockRoot; _RdsToHeaderCommonCollection = new(); _Log = Serilog.Log.ForContext(); } void IPinRepository.SetPinnedTable(HeaderCommon headerCommon) { Dictionary? toolIdToHeader; if (!string.IsNullOrEmpty(headerCommon.RDS)) { if (!_RdsToHeaderCommonCollection.TryGetValue(headerCommon.RDS, out toolIdToHeader)) { _RdsToHeaderCommonCollection.Add(headerCommon.RDS, new()); if (!_RdsToHeaderCommonCollection.TryGetValue(headerCommon.RDS, out toolIdToHeader)) throw new Exception(); } if (toolIdToHeader.ContainsKey(headerCommon.ToolTypeID)) toolIdToHeader[headerCommon.ToolTypeID] = headerCommon; else toolIdToHeader.Add(headerCommon.ToolTypeID, headerCommon); } } Result IPinRepository.GetPinnedTable(IMetrologyRepository metrologyRepository, int id, string? rds, 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 (!string.IsNullOrEmpty(rds) && (cdeHeader is null || bioRadHeader is null)) { Dictionary? toolIdToHeader; if (!_RdsToHeaderCommonCollection.TryGetValue(rds, out toolIdToHeader)) { _RdsToHeaderCommonCollection.Add(rds, new()); if (!_RdsToHeaderCommonCollection.TryGetValue(rds, out toolIdToHeader)) throw new Exception(); } if (cdeHeader is null && toolIdToHeader.ContainsKey(cdeId)) cdeHeader = toolIdToHeader[cdeId]; if (bioRadHeader is null && toolIdToHeader.ContainsKey(bioRadId)) bioRadHeader = toolIdToHeader[bioRadId]; } 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; } }