using ivf_tl_Entity.Entity;
using ivf_tl_Entity.Entity.AppSetting;
using ivf_tl_Entity.Entity.Mark;
using ivf_tl_Entity.Enums;
using ivf_tl_Manage.Converts;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ivf_tl_Manage.Win
{
///
/// UserEntityWindow.xaml 的交互逻辑
///
public partial class UserEntityWindow : Window
{
private bool isAdd = true;
private UserManageEntity UserManageEntity { get; set; } = null;
public UserEntityWindow(ObservableCollection roleList)
{
InitializeComponent();
this.Owner = AppData.Instance.MainWindow;
this._role_ComBox.ItemsSource = roleList;
this.Closed += (a, b) =>
{
AppData.Instance.MainWindow.Activate();
};
}
public UserEntityWindow(UserManageEntity markKeyEntity, ObservableCollection roleList)
{
InitializeComponent();
this.Owner = AppData.Instance.MainWindow;
this._role_ComBox.ItemsSource = roleList;
UserManageEntity = markKeyEntity;
if(markKeyEntity.roles != null && markKeyEntity.roles.Any() && roleList.Any())
{
this._role_ComBox.SelectedItem = roleList.FirstOrDefault(x => x.id == markKeyEntity.roles.First().id);
}
isAdd = false;
//this._title_TextBlock.Text = "编辑";
this._title_TextBlock.Text = KeyToStringConvert.GetLanguageStringByKey("0083");
this._name_TextBox.Text = UserManageEntity.username;
this._account_TextBox.Text = UserManageEntity.account;
this._password_TextBox.Text = "********";
this._passSure_TextBox.Text = "********";
this._account_TextBox.IsEnabled = false;
this._password_TextBox.IsEnabled = false;
this._passSure_TextBox.IsEnabled = false;
this.Closed += (a, b) =>
{
AppData.Instance.MainWindow.Activate();
};
}
private void MessageBoxShow(string mess)
{
this.message.Text = mess;
}
private void ExLog(Exception ex, string name)
{
AppData.Instance.LogService.ExceptionLog(ex, $"UserEntityWindow.{name}", LogEnum.RunException);
}
private void ErrorLog(string message, LogEnum logType)
{
AppData.Instance.LogService.TLLog($"UserEntityWindow.{message}", logType);
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
return;
}
private void Ok_Click(object sender, RoutedEventArgs e)
{
try
{
string nameString = this._name_TextBox.Text.Trim();
if (string.IsNullOrEmpty(nameString))
{
//MessageBoxShow($"用户名称不能为空");
MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0135"));
return;
}
string accountString = this._account_TextBox.Text.Trim();
if (string.IsNullOrEmpty(accountString))
{
//MessageBoxShow($"登录账号不能为空");
MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0131"));
return;
}
string wordString = this._password_TextBox.Text.Trim();
if (string.IsNullOrEmpty(wordString))
{
//MessageBoxShow($"登录密码不能为空");
MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0136"));
return;
}
string wordSureString = this._passSure_TextBox.Text.Trim();
if (string.IsNullOrEmpty(wordSureString))
{
//MessageBoxShow($"确认密码不能为空");
MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0137"));
return;
}
if(wordString != wordSureString)
{
//MessageBoxShow($"两次密码不一致");
MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0285"));
return;
}
if(!(this._role_ComBox.SelectedItem is RoleEntity currentRole))
{
//MessageBoxShow($"请选择角色");
MessageBoxShow(KeyToStringConvert.GetLanguageStringByKey("0133"));
return;
}
long roleId = currentRole.id;
var pro = AppData.Instance.GetSettingProvider();
string error = null;
if (isAdd)
{
string body = JsonConvert.SerializeObject(new
{
account = accountString,
password = wordString,
username = nameString,
verifyPassword = wordSureString,
roleIds = new List { roleId },
});
error = pro.AddUserApi(body);
}
else
{
string body = JsonConvert.SerializeObject(new
{
id = UserManageEntity.id,
username= nameString,
roleIds=new List { roleId },
});
error = pro.EditUserApi(body);
}
if (!string.IsNullOrEmpty(error))
{
//MessageBoxShow($"操作失败,{error}");
MessageBoxShow($"{KeyToStringConvert.GetLanguageStringByKey("0286")},{error}");
return;
}
if (!isAdd)
{
UserManageEntity.username = nameString;
UserManageEntity.roles = new List { currentRole };
}
this.DialogResult = true;
return;
}
catch (Exception ex)
{
ExLog(ex, "Ok_Click");
//MessageBoxShow($"操作失败:{ex.Message}");
MessageBoxShow($"{KeyToStringConvert.GetLanguageStringByKey("0291")}:{ex.Message}");
}
}
}
}