Export
This commit is contained in:
@ -2,6 +2,7 @@ using OI.Metrology.Shared.DataModels;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using OI.Metrology.Shared.Services;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace OI.Metrology.Server.Repository;
|
||||
@ -216,7 +217,7 @@ public class ToolTypesRepository : IToolTypesRepository
|
||||
// The second table has the header data
|
||||
if (ds.Tables[1].Rows.Count != 1)
|
||||
throw new Exception("Error exporting, invalid header data");
|
||||
System.Text.StringBuilder sb = new();
|
||||
StringBuilder sb = new();
|
||||
foreach (object? o in ds.Tables[1].Rows[0].ItemArray)
|
||||
{
|
||||
if ((o is not null) && (!Convert.IsDBNull(o)))
|
||||
@ -244,4 +245,96 @@ public class ToolTypesRepository : IToolTypesRepository
|
||||
return result;
|
||||
}
|
||||
|
||||
Result<DataTable> IToolTypesRepository.GetExportData(IMetrologyRepository metrologyRepository, int toolTypeId, DateTime? datebegin, DateTime? dateend)
|
||||
{
|
||||
Result<DataTable>? r;
|
||||
if (!string.IsNullOrEmpty(_MockRoot))
|
||||
{
|
||||
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), "GetExportDataApi.json"));
|
||||
r = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<DataTable>>(json);
|
||||
if (r is null)
|
||||
throw new NullReferenceException(nameof(r));
|
||||
}
|
||||
else
|
||||
{
|
||||
dateend ??= DateTime.Now;
|
||||
datebegin ??= dateend.Value.AddMonths(-1);
|
||||
ToolType tt = metrologyRepository.GetToolTypeByID(toolTypeId);
|
||||
if (string.IsNullOrEmpty(tt.ExportSPName))
|
||||
throw new NullReferenceException(nameof(tt.ExportSPName));
|
||||
DataTable dataTable = metrologyRepository.ExportData(tt.ExportSPName, datebegin.Value, dateend.Value);
|
||||
r = new()
|
||||
{
|
||||
Results = dataTable,
|
||||
TotalRows = dataTable.Rows.Count,
|
||||
};
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
protected static string GetRowData(DataRow dr)
|
||||
{
|
||||
StringBuilder result = new();
|
||||
for (int i = 0; i < dr.Table.Columns.Count; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
_ = result.Append(',');
|
||||
object v = dr[i];
|
||||
if (v is not null && !Convert.IsDBNull(v))
|
||||
_ = result.Append(FormatForCSV(string.Concat(Convert.ToString(v))));
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
protected static string GetColumnHeaders(DataTable dataTable)
|
||||
{
|
||||
StringBuilder result = new();
|
||||
for (int i = 0; i < dataTable.Columns.Count; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
_ = result.Append(',');
|
||||
_ = result.Append(FormatForCSV(dataTable.Columns[i].ColumnName.TrimEnd('_')));
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
protected static string FormatForCSV(string v)
|
||||
{
|
||||
StringBuilder result = new(v.Length + 2);
|
||||
bool doubleQuoted = false;
|
||||
if (v.StartsWith(' ') || v.EndsWith(' ') || v.Contains(',') || v.Contains('"'))
|
||||
{
|
||||
_ = result.Append('"');
|
||||
doubleQuoted = true;
|
||||
}
|
||||
foreach (char c in v)
|
||||
{
|
||||
_ = c switch
|
||||
{
|
||||
'\r' or '\n' => result.Append(' '),
|
||||
'"' => result.Append("\"\""),
|
||||
_ => result.Append(c),
|
||||
};
|
||||
}
|
||||
if (doubleQuoted)
|
||||
_ = result.Append('"');
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
byte[] IToolTypesRepository.GetCSVExport(IMetrologyRepository metrologyRepository, int toolTypeId, DateTime? datebegin, DateTime? dateend)
|
||||
{
|
||||
byte[] results;
|
||||
Result<DataTable> result;
|
||||
IToolTypesRepository repository = this;
|
||||
result = repository.GetExportData(metrologyRepository, toolTypeId, datebegin, dateend);
|
||||
if (result.Results is null)
|
||||
throw new NullReferenceException(nameof(result.Results));
|
||||
StringBuilder stringBuilder = new();
|
||||
_ = stringBuilder.AppendLine(GetColumnHeaders(result.Results));
|
||||
foreach (DataRow dr in result.Results.Rows)
|
||||
_ = stringBuilder.AppendLine(GetRowData(dr));
|
||||
results = Encoding.UTF8.GetBytes(stringBuilder.ToString());
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user