Files
BeWoPlaner/BeWo/ownChat/ChatServiceCustomer.xaml.cs
2026-07-14 14:40:39 +02:00

467 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using BeWo.Core;
using BeWo.Core.Service;
using BeWo.PasswortSecurity;
using BeWo.ViewModel;
using BeWo.ServiceProxy;
using BS.Shared.Extensions;
using BS.Shared;
using ChatController.ChatKlassen;
using Microsoft.Win32;
using Newtonsoft.Json;
using CheckBox = System.Windows.Controls.CheckBox;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
using Panel = System.Windows.Controls.Panel;
using SaveFileDialog = System.Windows.Forms.SaveFileDialog;
using BeWo.View;
namespace BeWo.ownChat
{
public partial class ChatServiceCustomer
{
private CustomerVM _ViewModel;
// TableID tableid;
private long? customerOid;
private long? personOid;
PasswortSicherheit pwS = new PasswortSicherheit();
public ChatServiceCustomer(TableID pObjectTid, long? oid,long? cusUserOid)
{
InitializeComponent();
customerOid = oid;
personOid = cusUserOid;
// tableid = pObjectTid;
this.Loaded += ChatServiceCustomer_Loaded;
}
private void ChatServiceCustomer_Loaded(object sender, RoutedEventArgs e)
{
LaodInfostoView();
}
public CustomerVM PublicViewModel { get { return _ViewModel; } }
public override bool IsDirty
{
get
{
return ViewModel != null && ViewModel.IsDirty;
}
}
private CustomerVM ViewModel
{
get { return _ViewModel; }
set
{
DataContext = value;
_ViewModel = value;
}
}
#region DoSaveCheck Nicht aktiviert
//TODO Speicher Button Aktivieren
/* private bool DoSaveCheck()
{
Cache.GetInstance().GetActiveSupportConceptCostbearer(scList =>
VMFactory.CreateServiceRecordVMAsync(
BeWoApp.LoggedOnUser.Employee.EmployeeOid,
vm => this.Dispatch(
delegate
{
ChatServiceCustomer newView = new ChatServiceCustomer(tableid, customerOid);
if (newView.CommandBindings.Count == 0)
{
newView.CommandBindings.Add(
new CommandBinding(
ApplicationCommands.Close,
(s, e) =>
{
if (!newView.DoSaveCheck())
{
return;
}
}));
newView.CommandBindings.Add(
new CommandBinding(
ApplicationCommands.Save,
(s, e) =>
{
bool save = true;
if (!save)
{
return;
}
newView.SaveData();
}));
}
})));
}
*/
#endregion
private void LaodInfostoView()
{
var getCustomerAppCode = ServiceFacade.DoOperationsServiceSync(s => s.GetIsChatActiveCustomerAPPCodeDC(customerOid.Value, 0));
int x ;
if (getCustomerAppCode.Count > 0)
{
x = getCustomerAppCode[getCustomerAppCode.Count - 1].IsChatAktive;
GeneratetTexfield.Text = getCustomerAppCode[getCustomerAppCode.Count - 1].CustomerCode;
}
CodeButtonsOnOFF();
if (getCustomerAppCode.Count > 0)
{
TXTBenutzerName.Text = getCustomerAppCode[getCustomerAppCode.Count - 1].Benutzername;
TXTPasswort.Text = getCustomerAppCode[getCustomerAppCode.Count - 1].Passwort;
pwS.ErmittleUndZeigePasswortStaerke(TXTPasswort.Text, LblPwStaerkenAnzeige);
}
}
private void CodeButtonsOnOFF()
{
if (!GeneratetTexfield.Text.Equals(""))
{
//lblGenerateCode.Content = "Chat-Code erneuern";
RemoveButton.Visibility = Visibility.Visible;
CustomerHandoutButton.Visibility = Visibility.Visible;
// CustomerBenutzerKontoGroupBox.Visibility = Visibility.Visible;
ButtonGenerateCode.ToolTip = "Chat-Code erneuern";
var image = new System.Windows.Controls.Image
{
Source = new BitmapImage(
new Uri(
"pack://application:,,,/Ressources/Icons/SymbolRefresh32.png"))
};
ImageGenerateCode.Source = image.Source;
}
else
{
//lblGenerateCode.Content = "Chat-Code erneuern";
RemoveButton.Visibility = Visibility.Collapsed;
CustomerHandoutButton.Visibility = Visibility.Collapsed;
//CustomerBenutzerKontoGroupBox.Visibility = Visibility.Collapsed;
TXTBenutzerName.Visibility = Visibility.Collapsed;
Benutzer.Visibility = Visibility.Collapsed;
passwd.Visibility = Visibility.Collapsed;
TXTPasswort.Visibility = Visibility.Collapsed;
LblPwStaerkenAnzeige.Visibility = Visibility.Collapsed;
Speichernbutton.Visibility =Visibility.Collapsed;
ButtonGenerateCode.ToolTip = "Chat-Code anfordern";
var image = new System.Windows.Controls.Image
{
Source = new BitmapImage(
new Uri(
"pack://application:,,,/Ressources/Icons/DocumentWithAdd2.png"))
};
ImageGenerateCode.Source = image.Source;
}
}
#region Code Generieren / Erneuern / Löschen
private void GenerateCodeClick(object sender, RoutedEventArgs e)
{
if (!GeneratetTexfield.Text.Equals("") && BeWoApp.Tenant != null && BeWoApp.Mandator.Apikey != null)
{
if (MessageBox.Show("Wollen Sie wirklich den Chat-Code ändern?\nAlle Daten auf dem Mobilgerät werden gelöscht.", "Chat-Code ändern", MessageBoxButton.YesNo,
MessageBoxImage.Question) == MessageBoxResult.Yes)
{
StartGenerateCode();
}
}
else if (BeWoApp.Tenant != null && BeWoApp.Mandator.Apikey != null)
{
StartGenerateCode();
// BeWoUtils.ownChatSynchronisationVeranlassen();
}
else
{
MessageBox.Show("Sie besitzen derzeit keinen API-Key. Bitte wenden sie sich an den Support.", "Info",
MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
private void StartGenerateCode()
{
StartWaiting();
List<string> parameter = new List<string>();
parameter.Add(customerOid.Value.ToString());
parameter.Add(BeWoApp.Tenant);
parameter.Add(BeWoApp.Mandator.Apikey);
Thread newThread = new Thread(GenerateCode);
newThread.IsBackground = true;
newThread.Start(parameter);
}
private void GenerateCode(object o)
{
List<string> para = o as List<string>;
var result =
ServiceFacade.DoOperationsServiceSync(s => s.CreateNewCustomerAPPCodeDC(Convert.ToInt64(para[0]),para[1], para[2]));
EndWaiting();
this.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
(Action)delegate
{
if (result != null)
{
if (result.Result == 0)
{
if (GeneratetTexfield.Text.IsNullOrEmpty())
{
BeWoUtils.OwnChatSynchronisationVeranlassen();
}
GeneratetTexfield.Text = result.CustomerCode;
CodeButtonsOnOFF();
}
else
{
OwnChatResults(result.Result);
}
}
});
}
public void OwnChatResults(int result)
{
switch (result)
{
case 3:
MessageBox.Show("Chat Server konnte nicht ermittelt werden.", "Chat-Code erstellen", MessageBoxButton.OK, MessageBoxImage.Exclamation);
break;
case 4:
MessageBox.Show("ChatCode Datensatz konnte nicht gefunden werden.", "Fehler", MessageBoxButton.OK);
break;
case 11:
MessageBox.Show("Kundennummer unbekannt.", "Chat-Code erstellen", MessageBoxButton.OK, MessageBoxImage.Exclamation);
break;
case 12:
MessageBox.Show("Der Api-Key ist falsch.", "Chat-Code erstellen", MessageBoxButton.OK, MessageBoxImage.Exclamation);
break;
case 32:
MessageBox.Show("Der vorhandene ChatCode konnte nicht deaktiviert werden.", "Chat-Code erstellen", MessageBoxButton.OK, MessageBoxImage.Exclamation);
break;
default:
MessageBox.Show("Beim Erstellen eines neuen Chat-Codes ist ein Fehler aufgetreten.", "Chat-Code erstellen", MessageBoxButton.OK, MessageBoxImage.Exclamation);
break;
}
}
#region Remove
private void RemoveCode_OnClick(object sender, RoutedEventArgs e)
{
if (!GeneratetTexfield.Text.Equals(""))
{
if (MessageBox.Show("Wollen Sie den Chat-Code wirklich löschen?\nAlle Daten werden gelöscht.", "Achtung", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
StartWaiting();
List<string> parameter = new List<string>();
parameter.Add(BeWoApp.Mandator.Apikey);
parameter.Add(BeWoApp.Tenant);
parameter.Add(customerOid.Value.ToString());
Thread newThread = new Thread(DeletGeneratedCode);
newThread.IsBackground = true;
newThread.Start(parameter);
}
}
else
{
MessageBox.Show("Sie müssen vorher einen Chat-Code generieren, bevor sie ihn löschen können", "Info",
MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void DeletGeneratedCode(object o)
{
List<string> para = o as List<string>;
var result = ServiceFacade.DoOperationsServiceSync(r => r.DeletCustomerChatCode(para[0], para[1], Convert.ToInt64(para[2])));
EndWaiting();
this.Dispatcher.BeginInvoke(
DispatcherPriority.Background,
(Action)delegate
{
if (result.Result == 0)
{
GeneratetTexfield.Text = "";
CodeButtonsOnOFF();
BeWoUtils.OwnChatSynchronisationVeranlassen();
}
else
{
//MessageBox.Show("Löschen des Chat-Codes fehlgeschlagen.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Exclamation);
OwnChatResults(result.Result);//ändern
}
});
}
#endregion
#endregion
#region Klient Ownchat Anmelde-Daten Handler
//speicher button auf do savecheck auslagern
private void SpeicherButton_OnClick(object sender, RoutedEventArgs e)
{
var pwStaerke = BeWoUtils.CheckPasswordSecurity(TXTPasswort.Text);
if (BeWoApp.AppSettings.IsPasswordSecurityActiv && (pwStaerke == PasswortSecurityStrength.SehrSchwach || pwStaerke == PasswortSecurityStrength.Schwach))
{
string failure = "Das Passwort ist zu schwach! Das Passwort sollte mindestens 8 Zeichen lang sein und Sonderzeichen, Ziffern oder Großbuchstaben enthalten.";
MessageBox.Show(failure, "Erstellen des Passworts fehlgeschlagen", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
StartWaiting();
var isBenutzerNameVorhanden = ServiceFacade.DoCustomerServiceSync(r => r.PruefeObBenutzerKontoSchonVorhanden(customerOid.Value, TXTBenutzerName.Text));
if (!isBenutzerNameVorhanden)
{
ServiceFacade.DoCustomerServiceSync(
s =>
s.SetBenutzerPasswortChatAppCode(customerOid.Value, TXTBenutzerName.Text,
TXTPasswort.Text));
EndWaiting();
BeWoUtils.OwnChatSynchronisationVeranlassen();
MessageBox.Show("Benutzername und Passwort wurden erfolgreich gespeichert.", "Speichern", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
EndWaiting();
MessageBox.Show("Dieser Benutzername ist bereits vergeben.", "Speichern", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
private void TXTPasswort_OnTextChanged(object sender, TextChangedEventArgs e)
{
LblPwStaerkenAnzeige = pwS.ErmittleUndZeigePasswortStaerke(TXTPasswort.Text, LblPwStaerkenAnzeige);
}
#endregion
#region pdf // auf den server auslagern und als byte[] zurück geben und hier verarbeiten
private void ZeigeHandoutPDF_OnClick(object sender, RoutedEventArgs e)
{
String tenant = BeWoApp.Tenant;
#if DEBUG
tenant = "5473568546";
#endif
OwnChatHelper.OpenOwnChatPdf(tenant, BeWoApp.Mandator.Apikey, TXTBenutzerName.Text, TXTPasswort.Text, GeneratetTexfield.Text);
}
#endregion
#region Waitlayer
private WaitLayer2 _waitLayer = null;
public void StartWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)StartWaitingImmediately);
}
public void StartWaitingImmediately()
{
if (_waitLayer == null)
{
_waitLayer = new WaitLayer2();
//Grid.SetColumnSpan(_waitLayer, 3);
//Grid.SetRowSpan(_waitLayer, 3);
ChatServiceCustomerGrid.Children.Add(_waitLayer);
Panel.SetZIndex(_waitLayer, int.MaxValue);
_waitLayer.RefreshChatUI();
}
}
public void EndWaiting()
{
Dispatcher.BeginInvoke(
DispatcherPriority.Background,
(Action)delegate
{
if (_waitLayer != null)
{
ChatServiceCustomerGrid.Children.Remove(_waitLayer);
_waitLayer = null;
}
});
}
#endregion
}
}