Files
SewerGenerator/WWTech_KanalSchnittstelle/Importer/CSVImporter.cs

99 lines
3.1 KiB
C#

using SewerStammGen.Shared.Domain;
using SewerStammGen.Shared.Enum;
using Shared.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WWTech_KanalSchnittstelle.Importer
{
internal enum EKennung
{
DECKEL = 1,
SOHLE = 2
}
public class CSVImporter : IImport
{
private string[]? input;
private readonly Projekt projekt;
public CSVImporter(int projektID)
{
projekt = new Projekt() { Id = projektID };
}
private decimal parseKoordinate(string input)
{
decimal result = 0m;
input = string.Format("{0:0.000}", input).Replace('.', ',');
if(!decimal.TryParse(input, out result))
{
throw new Exception("Konnte koordinate nicht parsen");
}
return result;
}
public List<Schacht> LoadSchaechte(string filename, EEntwaeserung entwaeserung)
{
List<Schacht> result = new List<Schacht>();
if(!File.Exists(filename))
{
throw new FileNotFoundException(filename);
}
input = File.ReadAllLines(filename);
int zeile = -1;
foreach (string line in input)
{
zeile++;
if (zeile == 0) continue;
string[] parsed = line.Split(new char[] { ';' });
string objektbezeichnung = parsed[0];
string objektname = objektbezeichnung.Substring(0, objektbezeichnung.Length - 2);
EKennung kennung = (EKennung)Convert.ToInt32(objektbezeichnung.Substring(objektbezeichnung.Length - 2, 2));
bool neueSchacht = false;
Schacht? schacht = result.FindLast(x => x.Objektbezeichnung.Equals(objektname) && x.Projekt.Id.Equals(projekt.Id));
if (schacht == null)
{
schacht = new Schacht();
neueSchacht = true;
}
if(kennung == EKennung.DECKEL)
{
schacht.DeckelRechtsWert = parseKoordinate(parsed[1]);
schacht.DeckelHochWert = parseKoordinate(parsed[2]);
schacht.DeckelHoehe = parseKoordinate(parsed[3]);
}
if(kennung == EKennung.SOHLE)
{
schacht.SohlRechtsWert = parseKoordinate(parsed[1]);
schacht.SohlHochWert = parseKoordinate(parsed[2]);
schacht.SohlHoehe = parseKoordinate(parsed[3]);
}
schacht.Projekt = projekt;
schacht.Entwaesserung = entwaeserung;
schacht.Vermesser = "Marwede";
schacht.AufnahmeDatum = DateTime.Now.ToShortDateString();
if (neueSchacht)
{
schacht.Objektbezeichnung = objektname;
result.Add(schacht);
}
}
return result;
}
}
}