using DatenDB;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
namespace Deckungsbeitrag.AA_Klassen
{
public class Tour
{
// 0 1 2 3 4 5 6 7 8 9 10 11
public const string COLUMNS = "tour_id, benutzer_id, kundenanzahl, liefertag, start, ende, container_dirt, container_clean, saison_id, tourenliste, listegedruckt, gedruckt_von";
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 int? Kundenanzahl { get; set; }
public DateTime Liefertag { get; set; }
public DateTime? Start { get; set; }
public DateTime? Ende { get; set; }
public int ContainerDirt { get; set; }
public int ContainerClean { get; set; }
public byte[] Tourenliste { get; set; }
public DateTime? ListeGedruckt { get; set; }
public int? GedrucktVon { get; set; }
public int? Save()
{
int? result = 0;
DatenbankConnection.GetConnection().Open();
using (NpgsqlCommand command = new NpgsqlCommand())
{
command.Connection = DatenbankConnection.GetConnection();
if (this.TourID.HasValue & this.TourID != 0)
{
result = this.TourID;
command.CommandText = $"update {TABLE} set benutzer_id = :p1, kundenanzahl = :p2, liefertag = :p3, start = :p4, ende = :p5, container_dirt = :p6, container_clean = :p7, saison_id = :p8, tourenliste = :p9, listegedruckt = :p10, gedruckt_von = :p11 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, :p5, :p6, :p7, :p8, :p9, :p10, :p11)";
}
command.Parameters.AddWithValue("p0", this.TourID);
command.Parameters.AddWithValue("p1", this.BenutzerID);
command.Parameters.AddWithValue("p2", this.Kundenanzahl ?? 0);
command.Parameters.AddWithValue("p3", this.Liefertag);
command.Parameters.AddWithValue("p4", this.Start ?? (object)DBNull.Value);
command.Parameters.AddWithValue("p5", this.Ende ?? (object)DBNull.Value);
command.Parameters.AddWithValue("p6", this.ContainerDirt);
command.Parameters.AddWithValue("p7", this.ContainerClean);
command.Parameters.AddWithValue("p8", this.SaisonID ?? (object)DBNull.Value);
command.Parameters.AddWithValue("p9", this.Tourenliste ?? (object)DBNull.Value);
command.Parameters.AddWithValue("p10", this.ListeGedruckt.HasValue ? (DateTime?)this.ListeGedruckt.Value : (DateTime?)DateTime.Now);
command.Parameters.AddWithValue("p11", this.GedrucktVon ?? (object)DBNull.Value);
result = command.ExecuteNonQuery();
DatenbankConnection.GetConnection().Close();
return result;
}
}
public Tour(NpgsqlDataReader reader)
{
this.TourID = reader.GetInt32(0);
this.BenutzerID = reader.GetInt32(1);
this.Kundenanzahl = reader.GetInt32(2);
this.Liefertag = reader.GetDateTime(3);
this.Start = reader.IsDBNull(4) ? (DateTime?)null : reader.GetDateTime(4);
this.Ende = reader.IsDBNull(5) ? (DateTime?)null : reader.GetDateTime(5);
this.ContainerDirt = reader.IsDBNull(6) ? 0 : reader.GetInt32(6);
this.ContainerClean = reader.IsDBNull(7) ? 0 : reader.GetInt32(7);
this.SaisonID = reader.GetInt32(8);
this.Tourenliste = reader.IsDBNull(9) ? null : (byte[])reader[9];
this.ListeGedruckt = reader.IsDBNull(10) ? (DateTime?)null : reader.GetDateTime(10);
this.GedrucktVon = reader.IsDBNull(11) ? (int?)null : reader.GetInt32(11);
}
#endregion
#region Konstruktors
///
/// Sucht und Findet Tour wenn tTourID 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();
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? tourID)
{
Tour result = null;
DatenbankConnection.GetConnection().Open();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
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;
}
#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();
}
}
//INSTALL-REMINDER: Tabelle Tour in Datenbank einfügen
//CHANGES: Class Tour wurde erstellt. Speichern, Laden und Bearbeiten von Touren sollte möglich sein.
//DONE: Wenn Fahrer neuen Auftrag erstellt, wird Tour (BenutzerID & Liefertag) gesucht und gespeichert oder upgedatet. Dabei werden die Containerzahlen schmutzig addiert und die Kundenanzahl um 1 erhöht.
//TODO: Wenn Auftrag abgeschlossen wird muss die Containerzahl sauber addiert werden. Kundenanzahl und Containeranzahl schmutzig darf nicht erhöht werden.
//TODO: Eingabemöglichkeit von Tour Start und Ende muss erledigt werden.
//TODO: !!ÜBERLEGE!! Tour Start und Ende automatisch erfassen.
//TODO: Kundenanzahl bei Tourenliste erstellen vergleichen und anpassen.
//TODO: !!ÜBERLEGE!! DB-Query für Kundenanzahl und Containeranzahl Abgleich. (Aufträge haben TourID hinterlegt. So sollte alles vergleichbar sein.)