using DatenDB; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Deckungsbeitrag.AA_Klassen { public class WSFach { private const string COLUMNS = "fach_id, auftrag_id, maschine_id, gewaschen, extraauftrag_id, gewicht, wprogramm_id, aufgabe_id"; private const string TABLE = "kundenverwaltung.wsfach"; public WSFach() { } public int? FachID { get; set; } public int AuftragID { get; set; } public int MaschineID { get; set; } public DateTime Gewaschen { get; set; } public int? ExtraAuftragID { get; set; } public double Gewicht { get; set; } = 0; public int? WProgrammID { get; set; } public int? AufgabeID { get; set; } internal static List GetFachliste(Maschine maschine) { int faecher = 0; if (maschine.TrocknerKaputt) faecher = maschine.Faecher - 1; else faecher = maschine.Faecher; DatenbankConnection.GetConnection().Open(); List resultlist = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where maschine_id = {maschine.MaschineID} order by fach_id desc limit {faecher}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultlist.Add(new WSFach(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultlist; } public WSFach(NpgsqlDataReader reader) { this.FachID = reader.GetInt32(0); this.AuftragID = reader.GetInt32(1); this.MaschineID = reader.GetInt32(2); this.Gewaschen = reader.GetDateTime(3); this.ExtraAuftragID = reader.IsDBNull(4) ? null : (int?)reader.GetInt32(4); this.Gewicht = reader.GetDouble(5); this.WProgrammID = reader.IsDBNull(6) ? null : (int?)reader.GetInt32(6); this.AufgabeID = reader.IsDBNull(7) ? null : (int?)reader.GetInt32(7); } public int Save() { DatenbankConnection.GetConnection().Open(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (this.FachID.HasValue & this.FachID != 0) { command.CommandText = $"update {TABLE} set auftrag_id = :p1, maschine_id = :p2, gewaschen = :p3, extraauftrag_id = :p4, gewicht = :p5, wprogramm_id = :p6, aufgabe_id = :p7 where fach_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.wsfach_seq')"; this.FachID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7)"; } command.Parameters.AddWithValue("p0", this.FachID); command.Parameters.AddWithValue("p1", this.AuftragID); command.Parameters.AddWithValue("p2", this.MaschineID); command.Parameters.AddWithValue("p3", this.Gewaschen); command.Parameters.AddWithValue("p4", this.ExtraAuftragID.HasValue ? this.ExtraAuftragID.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p5", this.Gewicht); command.Parameters.AddWithValue("p6", this.WProgrammID.HasValue ? this.WProgrammID.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p7", this.AufgabeID.HasValue ? this.AufgabeID.Value : (object)DBNull.Value); int result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } } }