using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BrightIdeasSoftware; using System.Runtime.CompilerServices; using System.Windows.Forms; namespace DatenDB { public enum ArtikelKategorie { Unbekannt = 0, Frottee = 1, Grossteile = 2, Kleinteile = 3, Spannleintuch = 4 } public class Artikel { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public const string COLUMNS = "artikel_id, nummer, bezeichnung, short, nachwaesche, muellwaesche, last_reset, kategorie"; private const string TABLE = "kundenverwaltung.artikel"; public Artikel() { } public Artikel(string row, int nr) { if (nr == 0) return; //ERSTE ZEILE IGNORIEREN. string[] zeileData = row.Split(';'); this.Nummer = Convert.ToInt32(zeileData[1]); this.Bezeichnung = zeileData[3]; this.Short = zeileData[4]; } public static Artikel GetArtikel(int? artikelNr) { DatenbankConnection.GetConnection().Open(); Artikel result = new Artikel(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where nummer = {artikelNr}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) result = new Artikel(reader); reader.Close(); DatenbankConnection.GetConnection().Close(); return result; } public static List GetArtikelList() { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Artikel(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static int SaveList(List artlist) { DatenbankConnection.GetConnection().Open(); int result = 0; foreach (Artikel art in artlist) { NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (art.ArtikelID.HasValue & art.ArtikelID != 0) { command.CommandText = $"update {TABLE} set nummer = :p1, bezeichnung = :p2, short = :p3, nachwaesche = :p4, muellwaesche = :p5, last_reset = :p6, kategorie = :p7 where artikel_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.artikel_seq')"; art.ArtikelID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7)"; } command.Parameters.AddWithValue("p0", art.ArtikelID); command.Parameters.AddWithValue("p1", art.Nummer); command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(art.Bezeichnung) ? (object)DBNull.Value : art.Bezeichnung); command.Parameters.AddWithValue("p3", string.IsNullOrEmpty(art.Short) ? (object)DBNull.Value : art.Short); command.Parameters.AddWithValue("p4", art.Nachwaesche); command.Parameters.AddWithValue("p5", art.Muellwaesche); command.Parameters.AddWithValue("p6", art.LastReset.HasValue ? art.LastReset.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p7", (int)art.Kategorie); command.ExecuteNonQuery(); result++; } //result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } public int Save() { DatenbankConnection.GetConnection().Open(); int result = 0; NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (this.ArtikelID.HasValue & this.ArtikelID != 0) { command.CommandText = $"update {TABLE} set nummer = :p1, bezeichnung = :p2, short = :p3, nachwaesche = :p4, muellwaesche = :p5, last_reset = :p6, kategorie = :p7 where artikel_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.artikel_seq')"; this.ArtikelID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7)"; } command.Parameters.AddWithValue("p0", this.ArtikelID); command.Parameters.AddWithValue("p1", this.Nummer); command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(this.Bezeichnung) ? (object)DBNull.Value : this.Bezeichnung); command.Parameters.AddWithValue("p3", string.IsNullOrEmpty(this.Short) ? (object)DBNull.Value : this.Short); command.Parameters.AddWithValue("p4", this.Nachwaesche); command.Parameters.AddWithValue("p5", this.Muellwaesche); command.Parameters.AddWithValue("p6", this.LastReset.HasValue ? this.LastReset.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p7", (int)this.Kategorie); result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } public Artikel(NpgsqlDataReader reader) { this.ArtikelID = reader.GetInt32(0); this.Nummer = reader.GetInt32(1); this.Bezeichnung = reader.IsDBNull(2) ? string.Empty : reader.GetString(2); this.Short = reader.IsDBNull(3) ? string.Empty : reader.GetString(3); this.Nachwaesche = reader.GetInt32(4); this.Muellwaesche = reader.GetInt32(5); this.LastReset = reader.IsDBNull(6) ? null : (DateTime?)reader.GetDateTime(6); this.Kategorie = (ArtikelKategorie)reader.GetInt16(7); } [OLVIgnore] public int? ArtikelID { get; set; } [OLVColumn("ArtNr.", DisplayIndex = 0, TextAlign = System.Windows.Forms.HorizontalAlignment.Right)] public int Nummer { get; set; } [OLVColumn("Bezeichnung", DisplayIndex = 1)] public string Bezeichnung { get; set; } [OLVColumn("Abkürzung", DisplayIndex = 2)] public string Short { get; set; } [OLVColumn("Nachwäsche", DisplayIndex = 3, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public int Nachwaesche { get; set; } [OLVColumn("Müll", DisplayIndex = 4, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)] public int Muellwaesche { get; set; } [OLVColumn("Letzter Reset", DisplayIndex = 5, AspectToStringFormat = "{0:d}", TextAlign = System.Windows.Forms.HorizontalAlignment.Right)] public DateTime? LastReset { get; set; } [OLVColumn("Kategorie", DisplayIndex = 6)] public ArtikelKategorie Kategorie { get; set; } private string _reset; [OLVColumn("Reset", DisplayIndex = 7)] public string Reset { get { if (Nachwaesche > 0) return _reset = "Reset"; else return _reset = string.Empty; // Alternativwert, wenn Bedingung nicht erfüllt } set { } } } }