Host from Windows
This commit is contained in:
		| @ -2,7 +2,9 @@ using OI.Metrology.Server.Models; | ||||
| using OI.Metrology.Shared.DataModels; | ||||
| using OI.Metrology.Shared.Models; | ||||
| using OI.Metrology.Shared.Models.Stateless; | ||||
| using System.Data; | ||||
| using System.Globalization; | ||||
| using System.Text; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace OI.Metrology.Server.Repository; | ||||
| @ -160,4 +162,86 @@ public class ExportRepository : IExportRepository | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     Result<DataTable> IExportRepository.GetExportData(IMetrologyRepository metrologyRepository, int toolTypeId, string? datebegin, string? dateend) | ||||
|     { | ||||
|         Result<DataTable>? r; | ||||
|         DateTime dateEnd = dateend is null ? DateTime.Now : DateTime.Parse(dateend); | ||||
|         DateTime dateBegin = datebegin is null ? dateEnd.AddMonths(-1) : DateTime.Parse(datebegin); | ||||
|         ToolType tt = metrologyRepository.GetToolTypeByID(toolTypeId); | ||||
|         if (string.IsNullOrEmpty(tt.ExportSPName)) | ||||
|             throw new NullReferenceException(nameof(tt.ExportSPName)); | ||||
|         DataTable dataTable = metrologyRepository.ExportData(tt.ExportSPName, dateBegin, dateEnd); | ||||
|         r = new() | ||||
|         { | ||||
|             Results = dataTable, | ||||
|             TotalRows = dataTable.Rows.Count, | ||||
|         }; | ||||
|         return r; | ||||
|     } | ||||
|  | ||||
|     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(); | ||||
|     } | ||||
|  | ||||
|     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 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(); | ||||
|     } | ||||
|  | ||||
|     string IExportRepository.GetCSVExport(IMetrologyRepository metrologyRepository, int toolTypeId, string? datebegin, string? dateend) | ||||
|     { | ||||
|         string results; | ||||
|         Result<DataTable> result; | ||||
|         IExportRepository 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 = stringBuilder.ToString(); | ||||
|         return results; | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user