269 lines
9.6 KiB
C#
269 lines
9.6 KiB
C#
using BrightIdeasSoftware;
|
|
using Deckungsbeitrag;
|
|
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace DatenDB
|
|
{
|
|
public enum BenutzerRolle
|
|
{
|
|
Verwaltung = 0,
|
|
Fahrer = 1,
|
|
Admin = 2,
|
|
Waschstrasse = 3,
|
|
Master = 4,
|
|
Expedit = 5,
|
|
Frottee = 6,
|
|
Flach = 7
|
|
}
|
|
|
|
[Flags]
|
|
public enum OnboardingSchritt
|
|
{
|
|
NeuerAuftrag = 1,
|
|
Main = 2,
|
|
Expedit = 4,
|
|
Tourenplanung = 8
|
|
}
|
|
|
|
public class Benutzer
|
|
{
|
|
// 0 1 2 3 4 5 6 7 8 9
|
|
public const string COLUMNS = "benutzer_id, rolle, passwort, vorname, nachname, schein, gueltig_bis, ist_aktiv, benutzer_name, onboarding_status";
|
|
private const string TABLE = "kundenverwaltung.benutzer";
|
|
|
|
public Benutzer()
|
|
{
|
|
}
|
|
public static List<Benutzer> GetList()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
List<Benutzer> resultList = new List<Benutzer>();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select {COLUMNS} from {TABLE}";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) resultList.Add(new Benutzer(reader));
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return resultList;
|
|
|
|
}
|
|
public static List<Benutzer> GetFahrerList()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
List<Benutzer> resultList = new List<Benutzer>();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select {COLUMNS} from {TABLE} where rolle = 'Fahrer' and ist_aktiv is true order by benutzer_id asc";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) resultList.Add(new Benutzer(reader));
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return resultList;
|
|
}
|
|
public static List<Benutzer> GetArbeiterList()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
List<Benutzer> resultList = new List<Benutzer>();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select {COLUMNS} from {TABLE} where ist_aktiv is true and rolle = 'Fahrer' or rolle = 'Verwaltung' or rolle = 'Expedit' order by benutzer_id asc";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) resultList.Add(new Benutzer(reader));
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return resultList;
|
|
}
|
|
|
|
public static Benutzer GetBenutzer(string benutzerName, int? benID)
|
|
{
|
|
Benutzer benutzer = new Benutzer();
|
|
|
|
try
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
if (!string.IsNullOrEmpty(benutzerName)) command.CommandText = $"select {COLUMNS} from {TABLE} where benutzer_name = '{benutzerName}'";
|
|
if (benID != null) command.CommandText = $"select {COLUMNS} from {TABLE} where benutzer_id = {benID}";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) benutzer = new Benutzer(reader);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return benutzer;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Program.AddFehler(null, ex.Message, ex.StackTrace, null);
|
|
throw;
|
|
}
|
|
}
|
|
public async Task UpdateOnBoardingAsync()
|
|
{
|
|
var conn = new NpgsqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
|
|
await conn.OpenAsync();
|
|
|
|
var cmd = new NpgsqlCommand($"UPDATE {TABLE} SET onboarding_status = @status WHERE benutzer_id = @id", conn);
|
|
|
|
cmd.Parameters.AddWithValue("status", OnboardingStatus);
|
|
cmd.Parameters.AddWithValue("id", BenutzerID);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Benutzer ID
|
|
/// </summary>
|
|
|
|
[OLVIgnore]
|
|
public int? BenutzerID { get; set; }
|
|
|
|
/// <summary>
|
|
/// Rolle des Benutzers
|
|
/// </summary>
|
|
[OLVColumn("Rolle", DisplayIndex = 4)]
|
|
public BenutzerRolle Rolle { get; set; } = BenutzerRolle.Waschstrasse;
|
|
|
|
/// <summary>
|
|
/// Passwort wird von OLV ignoriert
|
|
/// </summary>
|
|
[OLVIgnore]
|
|
public string Passwort { get; set; }
|
|
|
|
/// <summary>
|
|
/// Vorname & Nachname des Benutzers
|
|
/// </summary>
|
|
[OLVColumn("Vorname", DisplayIndex = 1)]
|
|
public string Vorname { get; set; }
|
|
[OLVColumn("Name", DisplayIndex = 2)]
|
|
public string Nachname { get; set; }
|
|
|
|
/// <summary>
|
|
/// Führerscheinnummer und Ablaufdatum
|
|
/// </summary>
|
|
[OLVColumn("FührerscheinNr", DisplayIndex = 6, IsVisible = false)]
|
|
public int? Schein { get; set; }
|
|
[OLVColumn("Ablaufdatum", DisplayIndex = 7, IsVisible = false)]
|
|
public DateTime? GueltigBis { get; set; }
|
|
|
|
/// <summary>
|
|
/// Status des Benutzers.
|
|
/// </summary>
|
|
[OLVColumn("Aktiv", DisplayIndex = 5, CheckBoxes = true)]
|
|
public bool Aktiv { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// Benutzername wird automatisch generiert (Vorname + Nachname in Kleinbuchstaben ohne Leerzeichen)
|
|
/// </summary>
|
|
[OLVColumn("Benutzername", DisplayIndex = 3)]
|
|
public string BenutzerName { get; set; }
|
|
|
|
[OLVIgnore]
|
|
public int OnboardingStatus { get; set; }
|
|
#endregion
|
|
|
|
public static Benutzer Get(string benutzerName, string passwort)
|
|
{
|
|
try
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
}
|
|
catch (NpgsqlException)
|
|
{
|
|
throw new LoginException("Datenbank ist nicht verbunden", -4);
|
|
}
|
|
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select {COLUMNS} from {TABLE} where lower(benutzer_name) = :ben";
|
|
command.Parameters.AddWithValue(":ben", benutzerName.ToLower());
|
|
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
Benutzer person = null;
|
|
if (reader.Read()) person = new Benutzer(reader);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
|
|
if (person == null) throw new LoginException("Benutzer nicht gefunden!", -1);
|
|
else if (string.IsNullOrEmpty(person.Passwort)) throw new LoginException("Benutzer gefunden, Passwort bestätigen", -3);
|
|
if (person.Passwort != passwort) throw new LoginException("Passwort ist falsch!", -2);
|
|
|
|
return person;
|
|
} //FEHLERMELDUNG DYNAMISCH GENERIEREN...
|
|
public Benutzer(NpgsqlDataReader reader)
|
|
{
|
|
this.BenutzerID = reader.GetInt32(0);
|
|
this.Rolle = reader.IsDBNull(1) ? BenutzerRolle.Verwaltung : (BenutzerRolle)Enum.Parse(typeof(BenutzerRolle), reader.GetString(1));
|
|
this.Passwort = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
|
|
this.Vorname = reader.IsDBNull(3) ? string.Empty : reader.GetString(3);
|
|
this.Nachname = reader.IsDBNull(4) ? string.Empty : reader.GetString(4);
|
|
this.Schein = reader.IsDBNull(5) ? null : (int?)reader.GetInt32(5);
|
|
this.GueltigBis = reader.IsDBNull(6) ? null : (DateTime?)reader.GetDateTime(6);
|
|
this.Aktiv = reader.IsDBNull(7) ? true : reader.GetBoolean(7);
|
|
this.BenutzerName = reader.IsDBNull(8) ? string.Empty : reader.GetString(8);
|
|
this.OnboardingStatus = reader.GetInt32(9);
|
|
}
|
|
public int Save()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
|
|
if (this.BenutzerID.HasValue & this.BenutzerID != 0)
|
|
{
|
|
command.CommandText = $"update {TABLE} set rolle = :p1, passwort = :p2, vorname = :p3, nachname = :p4, schein = :p5, gueltig_bis = :p6, ist_aktiv = :p7, benutzer_name = :p8, onboarding_status = :p9 where benutzer_id = :p0";
|
|
}
|
|
else
|
|
{
|
|
command.CommandText = "select nextval('kundenverwaltung.benutzer_seq')";
|
|
this.BenutzerID = (int)(long)command.ExecuteScalar();
|
|
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9)";
|
|
}
|
|
|
|
command.Parameters.AddWithValue("p0", this.BenutzerID);
|
|
command.Parameters.AddWithValue("p1", this.Rolle.ToString());
|
|
command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(this.Passwort) ? (object)DBNull.Value : this.Passwort);
|
|
command.Parameters.AddWithValue("p3", string.IsNullOrEmpty(this.Vorname) ? (object)DBNull.Value : this.Vorname);
|
|
command.Parameters.AddWithValue("p4", string.IsNullOrEmpty(this.Nachname) ? (object)DBNull.Value : this.Nachname);
|
|
command.Parameters.AddWithValue("p5", this.Schein.HasValue ? this.Schein.Value : (object)DBNull.Value);
|
|
command.Parameters.AddWithValue("p6", this.Rolle == BenutzerRolle.Fahrer ? this.GueltigBis.Value : (object)DBNull.Value);
|
|
command.Parameters.AddWithValue("p7", this.Aktiv);
|
|
command.Parameters.AddWithValue("p8", string.IsNullOrEmpty(this.BenutzerName) ? (object)DBNull.Value : this.BenutzerName);
|
|
command.Parameters.AddWithValue("p9", this.OnboardingStatus);
|
|
|
|
int result = command.ExecuteNonQuery();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result;
|
|
}
|
|
}
|
|
public static class BenutzerExtensions
|
|
{
|
|
public static int SetOnboardingFlag(this Benutzer user, OnboardingSchritt schritt)
|
|
{
|
|
return user.OnboardingStatus | (int)schritt;
|
|
}
|
|
public static int ClearOnboardingFlag(this Benutzer user, OnboardingSchritt schritt)
|
|
{
|
|
user.OnboardingStatus &= ~(int)schritt;
|
|
return user.OnboardingStatus;
|
|
}
|
|
public static bool IstGesehen(this Benutzer user, OnboardingSchritt schritt)
|
|
{
|
|
return (user.OnboardingStatus & (int)schritt) != 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
//INSTALL-REMINDER: DB anpassen.
|
|
//CHANGES: OnBoarding wird jetzt gespeichert wenn gesehen.
|