using DatenDB; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using ZXing; namespace Deckungsbeitrag.AA_Klassen { public class Tour { // 0 1 2 3 4 public const string COLUMNS = "tour_id, benutzer_id, saison_id, bezeichnung, priority"; public const string TABLE = "kundenverwaltung.tour"; #region Class-Standarts (Properties, Save, reader) public Tour() { } public int? TourID { get; set; } public int? BenutzerID { get; set; } public int? SaisonID { get; set; } public string Bezeichnung { get; set; } public Priority Priority { get; set; } public int? Save() { DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); int? result; if (this.TourID.HasValue & this.TourID != 0) { command.CommandText = $"update {TABLE} set benutzer_id = :p1, saison_id = :p2, bezeichnung = :p3, priority = :p4 WHERE tour_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.tour_seq')"; this.TourID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4)"; } Match match = Regex.Match(this.Bezeichnung ?? "", @"\d+"); int zahl = match.Success ? int.Parse(match.Value) : 0; // oder throw bei Fehler command.Parameters.AddWithValue("p0", this.TourID); command.Parameters.AddWithValue("p1", this.BenutzerID.HasValue ? this.BenutzerID.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p2", this.SaisonID ?? (object)DBNull.Value); command.Parameters.AddWithValue("p3", this.Bezeichnung); command.Parameters.AddWithValue("p4", zahl); result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } } public Tour(NpgsqlDataReader reader) { this.TourID = reader.GetInt32(0); this.BenutzerID = reader.IsDBNull(1) ? (int?)null : reader.GetInt32(1); this.SaisonID = reader.GetInt32(2); this.Bezeichnung = reader.IsDBNull(3) ? string.Empty : reader.GetString(3); this.Priority = reader.IsDBNull(4) ? Priority.None : (Priority)reader.GetInt32(4); } #endregion #region Konstruktors /// /// Sucht und Findet Tour wenn TourID is null, werden BenutzerID und Liefertag benötigt /// /// /// /// /// Tour internal static Tour FindeTour(int? tourID, int? arbeiterID, DateTime? liefertag) { Tour result = null; DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); if (tourID == null) command.CommandText = $"select {COLUMNS} from {TABLE} where benutzer_id = {arbeiterID} and liefertag = '{liefertag}'"; else { if (arbeiterID == null) command.CommandText = $"select {COLUMNS} from {TABLE} where liefertag = {liefertag}"; else command.CommandText = $"select {COLUMNS} from {TABLE} where tour_id = {tourID}"; } NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = new Tour(reader); reader.Close(); } DatenbankConnection.GetConnection().Close(); return result; } internal static Tour GetTour(int? saison, string bez) { Tour result = null; DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where saison_id = {saison} and bezeichnung = '{bez}'"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = new Tour(reader); reader.Close(); } DatenbankConnection.GetConnection().Close(); return result; } internal static List GetSaisonListe(Saison saison) { List resultList = new List(); DatenbankConnection.GetConnection().Open(); using (NpgsqlCommand command = new NpgsqlCommand()) { command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where saison_id = {saison.SaisonId}"; using (var reader = command.ExecuteReader()) { while (reader.Read()) resultList.Add(new Tour(reader)); } } DatenbankConnection.GetConnection().Close(); return resultList; } #endregion } public class TDaten { public string TourName { get; set; } public int FahrerId { get; set; } // pro Tag = string → eigene Kundenliste public Dictionary> KundenProTag { get; set; } = new Dictionary>(); public Dictionary Counter { get; set; } = new Dictionary(); } }