2025-11-30 11:51:28 +01:00

167 lines
6.7 KiB
C#

using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatenDB
{
public enum Liefertag
{
SO = 0,
MO = 1,
DI = 2,
MI = 3,
DO = 4,
FR = 5,
SA = 6,
STV = 7
}
public class Fach
{
private const string COLUMNS = "fach_id, maschine_id, kunde_id, wprogramm_id, gewicht, extra, gewaschen, extrakunde_id, liefertag, auftrag_id, extraauftrag_id, tischsummary, containersummary";
private const string TABLE = "kundenverwaltung.fach";
public static List<Fach> GetVerlauf(int? mID)
{
DatenbankConnection.GetConnection().Open();
List<Fach> resultlist = new List<Fach>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select {COLUMNS} from {TABLE} where maschine_id = {mID}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultlist.Add(new Fach(reader));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultlist;
}
public static List<int> GetTischSummary(int? mID)
{
DatenbankConnection.GetConnection().Open();
List<int> resultlist = new List<int>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select distinct(auftrag_id) from {TABLE} where maschine_id = {mID} and fach_id > (select max(fach_id) from {TABLE} where tischsummary = true) order by auftrag_id asc";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultlist.Add(reader.GetInt32(0));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultlist;
}
public static List<Fach> GetInhalt(int? mID, int fachzahl)
{
DatenbankConnection.GetConnection().Open();
List<Fach> resultlist = new List<Fach>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select {COLUMNS} from {TABLE} where maschine_id = {mID} order by gewaschen desc Limit {fachzahl}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultlist.Add(new Fach(reader));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultlist;
}
public static Fach GetFach(int? ms, int fach)
{
DatenbankConnection.GetConnection().Open();
Fach result = new Fach();
using (NpgsqlCommand command = new NpgsqlCommand())
{
command.Connection = DatenbankConnection.GetConnection();
if (ms == 1) command.CommandText = $"select fach_id, maschine_id, kunde_id, wprogramm_id, gewicht, extra, gewaschen, extrakunde_id, liefertag, auftrag_id, extraauftrag_id from kundenverwaltung.inhalt_ws1 where row_number = {fach}";
if (ms == 2) command.CommandText = $"select fach_id, maschine_id, kunde_id, wprogramm_id, gewicht, extra, gewaschen, extrakunde_id, liefertag, auftrag_id, extraauftrag_id from kundenverwaltung.inhalt_ws2 where row_number = {fach}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) result = new Fach(reader);
reader.Close();
}
DatenbankConnection.GetConnection().Close();
return result;
}
public int? FachID { get; set; }
public int MaschineID { get; set; }
public int KundeID { get; set; }
public int WProgrammID { get; set; }
public double? Gewicht { get; set; }
public DateTime Gewaschen { get; set; }
public int? Extra { get; set; }
public Liefertag Liefertag { get; set; } = Liefertag.MO;
public int? ExtraKundeID { get; set; }
public DateTime? Lieferdatum { get; set; }
public int? AuftragID { get; set; }
public int? ExtraAuftragID { get; set; }
public bool TischSummary { get; set; }
public bool ContainerSummary { get; set; }
public Fach(NpgsqlDataReader reader)
{
this.FachID = reader.GetInt32(0);
this.MaschineID = reader.GetInt32(1);
this.KundeID = reader.GetInt32(2);
this.WProgrammID = reader.GetInt32(3);
this.Gewicht = reader.IsDBNull(4) ? null : (double?)reader.GetDouble(4);
this.Extra = reader.IsDBNull(5) ? null : (int?)reader.GetInt16(5);
this.Gewaschen = reader.GetDateTime(6);
this.ExtraKundeID = reader.IsDBNull(7) ? (int?)null : reader.GetInt32(7);
this.Lieferdatum = reader.IsDBNull(8) ? (DateTime?)null : reader.GetDateTime(8);
this.AuftragID = reader.IsDBNull(9) ? (int?)null : reader.GetInt32(9);
this.ExtraAuftragID = reader.IsDBNull(10) ? (int?)null : reader.GetInt32(10);
if(reader.FieldCount > 11)
{
this.TischSummary = reader.IsDBNull(11) ? false : reader.GetBoolean(11);
this.ContainerSummary = reader.IsDBNull(12) ? false : reader.GetBoolean(12);
}
}
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 maschine_id = :p1, kunde_id = :p2, wprogramm_id = :p3, gewicht = :p4, extra = :p5, gewaschen = :p6, extrakunde_id = :p7, liefertag = :p8, auftrag_id = :p9, extraauftrag_id = :p10, tischsummary = :p11, containersummary = :p12 where fach_id = :p0";
}
else
{
command.CommandText = "select nextval('kundenverwaltung.fach_seq')";
this.FachID = (int)(long)command.ExecuteScalar();
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12)";
}
command.Parameters.AddWithValue("p0", this.FachID);
command.Parameters.AddWithValue("p1", this.MaschineID);
command.Parameters.AddWithValue("p2", this.KundeID);
command.Parameters.AddWithValue("p3", this.WProgrammID);
command.Parameters.AddWithValue("p4", this.Gewicht.HasValue ? this.Gewicht.Value : (object)DBNull.Value);
command.Parameters.AddWithValue("p5", this.Extra.HasValue ? this.Extra.Value : (object)DBNull.Value);
command.Parameters.AddWithValue("p6", this.Gewaschen);
command.Parameters.AddWithValue("p7", this.ExtraKundeID.HasValue ? this.ExtraKundeID : (object)DBNull.Value);
command.Parameters.AddWithValue("p8", this.Lieferdatum.HasValue ? this.Lieferdatum : (object)DBNull.Value);
command.Parameters.AddWithValue("p9", this.AuftragID.HasValue ? this.AuftragID.Value : (object)DBNull.Value);
command.Parameters.AddWithValue("p10", this.ExtraAuftragID.HasValue ? this.ExtraAuftragID.Value : (object)DBNull.Value);
command.Parameters.AddWithValue("p11", this.TischSummary);
command.Parameters.AddWithValue("p12", this.ContainerSummary);
int result = command.ExecuteNonQuery();
DatenbankConnection.GetConnection().Close();
return result;
}
public Fach()
{
}
}
}