87 lines
3.3 KiB
C#

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";
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; }
internal static List<WSFach> GetFachliste(Maschine maschine)
{
int faecher = 0;
if (maschine.TrocknerKaputt) faecher = maschine.Faecher - 1;
else faecher = maschine.Faecher;
DatenbankConnection.GetConnection().Open();
List<WSFach> resultlist = new List<WSFach>();
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);
}
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 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)";
}
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);
int result = command.ExecuteNonQuery();
DatenbankConnection.GetConnection().Close();
return result;
}
}
}