242 lines
9.3 KiB
C#

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;
using System.Configuration;
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
public const string COLUMNS = "artikel_id, nummer, bezeichnung, short, nachwaesche, muellwaesche, last_reset, kategorie"; // aktive hinzufügen
private const string TABLE = "kundenverwaltung.artikel";
public Artikel() { }
public Artikel(string row, int nr)
{
//if (nr <= 1) return; //ERSTE ZEILE IGNORIEREN.
string[] zeileData = row.Split(';');
if (zeileData[0] == "60" || zeileData[0] == "20")
{
this.Nummer = Convert.ToInt32(zeileData[1]);
this.Bezeichnung = zeileData[3];
//this.Short = zeileData[4];
}
}
internal static int? GetArtikelID(int artNr)
{
DatenbankConnection.GetConnection().Open();
int? result = 0;
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select artikel_id from {TABLE} where nummer = {artNr}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) result = reader.GetInt32(0);
reader.Close();
DatenbankConnection.GetConnection().Close();
return result;
}
internal static string GetShort(int? artikelID)
{
DatenbankConnection.GetConnection().Open();
string result = null;
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select short from {TABLE} where artikel_id = {artikelID}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) result = reader.IsDBNull(0) ? string.Empty : reader.GetString(0);
reader.Close();
DatenbankConnection.GetConnection().Close();
return result;
}
public static Artikel GetArtikel(int? artikelNr, int? artikelID)
{
DatenbankConnection.GetConnection().Open();
Artikel result = null;
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
if (artikelNr != null) command.CommandText = $"select {COLUMNS} from {TABLE} where nummer = {artikelNr}";
if (artikelID != null) command.CommandText = $"select {COLUMNS} from {TABLE} where artikel_id = {artikelID}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) result = new Artikel(reader);
reader.Close();
DatenbankConnection.GetConnection().Close();
return result;
}
internal static List<Artikel> GetRegArtikelList()
{
DatenbankConnection.GetConnection().Open();
List<Artikel> resultList = new List<Artikel>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select {COLUMNS} from {TABLE} where SUBSTRING(nummer::text, 2, 1) = '9'";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultList.Add(new Artikel(reader));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultList;
}
public static List<Artikel> GetArtikelList(string s)
{
DatenbankConnection.GetConnection().Open();
List<Artikel> resultList = new List<Artikel>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
if (s == "Alle") command.CommandText = $"select {COLUMNS} from {TABLE}";
if (s == "Nachwaesche") command.CommandText = $"select {COLUMNS} from {TABLE} where kategorie > 0 and SUBSTRING(nummer::text, 1, 1) = '6' order by kategorie ASC";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultList.Add(new Artikel(reader));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultList;
}
public static int SaveList(List<Artikel> 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);
}
#region Properties
[OLVIgnore]
public int? ArtikelID { get; set; }
[OLVColumn("ArtNr.", DisplayIndex = 0, TextAlign = HorizontalAlignment.Right, IsEditable = false)]
public int Nummer { get; set; }
[OLVColumn("Bezeichnung", DisplayIndex = 1, IsEditable = false)]
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)]
[OLVIgnore]
public int Nachwaesche { get; set; }
//[OLVColumn("Müll", DisplayIndex = 4, TextAlign = System.Windows.Forms.HorizontalAlignment.Center)]
[OLVIgnore]
public int Muellwaesche { get; set; }
//[OLVColumn("Letzter Reset", DisplayIndex = 5, AspectToStringFormat = "{0:d}", TextAlign = System.Windows.Forms.HorizontalAlignment.Right)]
[OLVIgnore]
public DateTime? LastReset { get; set; }
[OLVColumn("Kategorie", DisplayIndex = 6)]
public ArtikelKategorie Kategorie { get; set; }
private string _reset;
//[OLVColumn("Reset", DisplayIndex = 7)]
[OLVIgnore]
public string Reset
{
get
{
if (Nachwaesche > 0)
return _reset = "Reset";
else
return _reset = string.Empty; // Alternativwert, wenn Bedingung nicht erfüllt
}
set { }
}
#endregion
}
}